=== modified file 'uspace/lib/c/generic/malloc.c'
--- uspace/lib/c/generic/malloc.c	2011-09-22 18:27:21 +0000
+++ uspace/lib/c/generic/malloc.c	2011-11-15 09:21:39 +0000
@@ -683,7 +683,27 @@
 		return NULL;
 	
 	size_t falign = lcm(align, BASE_ALIGN);
-	size_t real_size = GROSS_SIZE(ALIGN_UP(size, falign));
+
+	/*
+	 * This is minimal block size where we can allocate in.
+	 * The alignment is checked in malloc_area().
+	 */
+	size_t real_size = GROSS_SIZE(size);
+	/*
+	 * When growing the heap, we need to allocate at least this much
+	 * space.
+	 * Generally, to allocate x bytes aligned at y, we need at least
+	 * space x + y bytes.
+	 * Here we add some extra space (through ALIGN_UP(..., BASE_ALIGN))
+	 * just to be on the safe side.
+	 * Notice that we need to take care of all structures as growing
+	 * the heap might bring a new AS area.
+	 */
+	size_t grow_size = ALIGN_UP(sizeof(heap_area_t), BASE_ALIGN)
+	    + ALIGN_UP(sizeof(heap_block_head_t), BASE_ALIGN)
+	    + falign
+	    + ALIGN_UP(size, BASE_ALIGN)
+	    + ALIGN_UP(sizeof(heap_block_foot_t), BASE_ALIGN);
 	
 	bool retry = false;
 	heap_block_head_t *split;
@@ -716,7 +736,7 @@
 	
 	if (!retry) {
 		/* Try to grow the heap space */
-		if (heap_grow(real_size)) {
+		if (heap_grow(grow_size)) {
 			retry = true;
 			goto loop;
 		}

