Index: arch/ia32/src/mm/frame.c
===================================================================
--- arch/ia32/src/mm/frame.c	(revision 4acac8439ff99d7ea9865f2b81bc777c057b18c1)
+++ arch/ia32/src/mm/frame.c	(revision b87f41860c88213bb9dc599f59adedde6615550b)
@@ -58,6 +58,6 @@
 			if (e820table[i].type == MEMMAP_MEMORY_AVAILABLE) {
 				zone_create_in_region(e820table[i].base_address, e820table[i].size & ~(FRAME_SIZE-1));
-				if (last_frame < ALIGN(e820table[i].base_address + e820table[i].size, FRAME_SIZE))
-					last_frame = ALIGN(e820table[i].base_address + e820table[i].size, FRAME_SIZE);
+				if (last_frame < ALIGN_UP(e820table[i].base_address + e820table[i].size, FRAME_SIZE))
+					last_frame = ALIGN_UP(e820table[i].base_address + e820table[i].size, FRAME_SIZE);
 			}			
 		}
Index: arch/ia64/include/context.h
===================================================================
--- arch/ia64/include/context.h	(revision 4acac8439ff99d7ea9865f2b81bc777c057b18c1)
+++ arch/ia64/include/context.h	(revision b87f41860c88213bb9dc599f59adedde6615550b)
@@ -41,5 +41,5 @@
  * One item is put onto the stack to support get_stack_base().
  */
-#define SP_DELTA	(0+ALIGN(STACK_ITEM_SIZE, STACK_ALIGNMENT))
+#define SP_DELTA	(0+ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT))
 
 #define PFM_MASK	(~0x3fffffffff)
@@ -51,7 +51,7 @@
 #define context_set(c, _pc, stack, size) 								\
 	(c)->pc = (__address) _pc;									\
-	(c)->bsp = ((__address) stack) + ALIGN(sizeof(the_t), REGISTER_STACK_ALIGNMENT);		\
+	(c)->bsp = ((__address) stack) + ALIGN_UP(sizeof(the_t), REGISTER_STACK_ALIGNMENT);		\
 	(c)->ar_pfs &= PFM_MASK; 									\
-	(c)->sp = ((__address) stack) + ALIGN((size), STACK_ALIGNMENT) - SP_DELTA;
+	(c)->sp = ((__address) stack) + ALIGN_UP((size), STACK_ALIGNMENT) - SP_DELTA;
 
 /*
Index: arch/sparc64/include/context.h
===================================================================
--- arch/sparc64/include/context.h	(revision 4acac8439ff99d7ea9865f2b81bc777c057b18c1)
+++ arch/sparc64/include/context.h	(revision b87f41860c88213bb9dc599f59adedde6615550b)
@@ -31,5 +31,5 @@
 
 #ifndef __sparc64_STACK_H__
-#include <arch/stack.h>
+# include <arch/stack.h>
 #endif
 
@@ -50,5 +50,5 @@
 #define context_set(c, _pc, stack, size)								\
         (c)->pc = ((__address) _pc) - 8;								\
-        (c)->sp = ((__address) stack) + ALIGN((size), STACK_ALIGNMENT) - (STACK_BIAS + SP_DELTA);	\
+        (c)->sp = ((__address) stack) + ALIGN_UP((size), STACK_ALIGNMENT) - (STACK_BIAS + SP_DELTA);	\
 	(c)->fp = -STACK_BIAS
 	
Index: generic/include/align.h
===================================================================
--- generic/include/align.h	(revision 4acac8439ff99d7ea9865f2b81bc777c057b18c1)
+++ generic/include/align.h	(revision b87f41860c88213bb9dc599f59adedde6615550b)
@@ -30,5 +30,17 @@
 #define __ALIGN_H__
 
-#define ALIGN(s, a)	((s) % (a) ? (((s) / (a)) + 1) * (a) : (s))
+/** Align to the nearest higher address.
+ *
+ * @param s Address or size to be aligned.
+ * @param a Size of alignment.
+ */
+#define ALIGN_UP(s, a)		((s) % (a) ? (((s) / (a)) + 1) * (a) : (s))
+
+/** Align to the nearest lower address.
+ *
+ * @param s Address or size to be aligned.
+ * @param a Size of alignment.
+ */
+#define ALIGN_DOWN(s, a)	((s) & ~((a)-1))
 
 #endif
Index: generic/include/mm/frame.h
===================================================================
--- generic/include/mm/frame.h	(revision 4acac8439ff99d7ea9865f2b81bc777c057b18c1)
+++ generic/include/mm/frame.h	(revision b87f41860c88213bb9dc599f59adedde6615550b)
@@ -46,5 +46,7 @@
 #define FRAME_INDEX(zone, frame)		((index_t)((frame) - (zone)->frames))
 #define FRAME_INDEX_VALID(zone, index)		(((index) >= 0) && ((index) < ((zone)->free_count + (zone)->busy_count)))
-#define IS_BUDDY_LEFT_BLOCK(zone, frame)	((FRAME_INDEX((zone), (frame)) & ~(((__native) -1)<<((frame)->buddy_order + 1))) == 0)
+#define IS_BUDDY_ORDER_OK(index, order)		((~(((__native) -1) << (order)) & (index)) == 0)
+#define IS_BUDDY_LEFT_BLOCK(zone, frame)	(((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0)
+#define IS_BUDDY_RIGHT_BLOCK(zone, frame)	(((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1)
 
 #define ZONE_BLACKLIST_SIZE	3
Index: generic/src/main/main.c
===================================================================
--- generic/src/main/main.c	(revision 4acac8439ff99d7ea9865f2b81bc777c057b18c1)
+++ generic/src/main/main.c	(revision b87f41860c88213bb9dc599f59adedde6615550b)
@@ -142,5 +142,5 @@
 
 	heap_size = CONFIG_HEAP_SIZE + (config.memory_size/FRAME_SIZE)*sizeof(frame_t);
-	kernel_size = ALIGN(hardcoded_ktext_size + hardcoded_kdata_size + heap_size, PAGE_SIZE);
+	kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size + heap_size, PAGE_SIZE);
 	heap_delta = kernel_size - (hardcoded_ktext_size + hardcoded_kdata_size + heap_size);
 	
Index: generic/src/mm/frame.c
===================================================================
--- generic/src/mm/frame.c	(revision 4acac8439ff99d7ea9865f2b81bc777c057b18c1)
+++ generic/src/mm/frame.c	(revision b87f41860c88213bb9dc599f59adedde6615550b)
@@ -1,4 +1,5 @@
 /*
- * Copyright (C) 2001-2004 Jakub Jermar
+ * Copyright (C) 2001-2005 Jakub Jermar
+ * Copyright (C) 2005 Sergey Bondari
  * All rights reserved.
  *
@@ -226,9 +227,9 @@
 
 	/* Force base to the nearest lower address frame boundary. */
-	base &= ~(FRAME_SIZE - 1);
+	base = ALIGN_DOWN(base, FRAME_SIZE);
 	/* Align size to frame boundary. */
-	size = ALIGN(size, FRAME_SIZE);
-
-	ASSERT(zone_blacklist_count <= ZONE_BLACKLIST_SIZE);
+	size = ALIGN_UP(size, FRAME_SIZE);
+
+	ASSERT(index < ZONE_BLACKLIST_SIZE);
 	zone_blacklist[index].base = base;
 	zone_blacklist[index].size = size;
@@ -285,5 +286,5 @@
 
 	if (!z) {
-		panic("Cannot allocate zone (%dB).\n", size);
+		panic("Cannot allocate zone (base=%P, size=%d).\n", base, size);
 	}
 	
@@ -394,5 +395,5 @@
 	frame_t * frame;
 	zone_t * zone;
-	count_t index;
+	index_t index;
 	bool is_left, is_right;
 
@@ -400,10 +401,11 @@
 	zone = (zone_t *) b->data;
 	
+	ASSERT(IS_BUDDY_ORDER_OK(FRAME_INDEX(zone, frame), frame->buddy_order));
+	
 	is_left = IS_BUDDY_LEFT_BLOCK(zone, frame);
-	is_right = !is_left;
-	
-	/*
-	 * test left buddy
-	 */
+	is_right = IS_BUDDY_RIGHT_BLOCK(zone, frame);
+	
+	ASSERT(is_left ^ is_right);
+	
 	if (is_left) {
 		index = (FRAME_INDEX(zone, frame)) + (1 << frame->buddy_order);
@@ -431,6 +433,8 @@
 link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
 	frame_t * frame_l, * frame_r;
+
 	frame_l = list_get_instance(block, frame_t, buddy_link);
 	frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
+	
 	return &frame_r->buddy_link;
 }
@@ -446,6 +450,8 @@
 link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, link_t * block_2) {
 	frame_t * frame1, * frame2;
+	
 	frame1 = list_get_instance(block_1, frame_t, buddy_link);
 	frame2 = list_get_instance(block_2, frame_t, buddy_link);
+	
 	return frame1 < frame2 ? block_1 : block_2;
 }
