--- libc/stdlib/malloc/free.c.orig	2010-01-23 11:56:59.000000000 -0500
+++ libc/stdlib/malloc/free.c	2010-01-23 11:59:48.000000000 -0500
@@ -179,14 +179,14 @@
 	      /* Start searching again from the end of this block.  */
 	      start = mmb_end;
 
+	      /* Release the descriptor block we used.  */
+	      free_to_heap (mmb, &__malloc_mmb_heap, &__malloc_mmb_heap_lock);
+
 	      /* We have to unlock the heap before we recurse to free the mmb
 		 descriptor, because we might be unmapping from the mmb
 		 heap.  */
               __heap_unlock (heap_lock);
 
-	      /* Release the descriptor block we used.  */
-	      free_to_heap (mmb, &__malloc_mmb_heap, &__malloc_mmb_heap_lock);
-
 	      /* Do the actual munmap.  */
 	      munmap ((void *)mmb_start, mmb_end - mmb_start);
 
--- libc/stdlib/malloc/malloc.c.orig	2010-01-23 11:56:46.000000000 -0500
+++ libc/stdlib/malloc/malloc.c	2010-01-23 12:07:45.000000000 -0500
@@ -28,7 +28,7 @@
 HEAP_DECLARE_STATIC_FREE_AREA (initial_fa, 256);
 struct heap_free_area *__malloc_heap = HEAP_INIT_WITH_FA (initial_fa);
 #ifdef HEAP_USE_LOCKING
-pthread_mutex_t __malloc_heap_lock = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t __malloc_heap_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
 #endif
 
 #if defined(MALLOC_USE_LOCKING) && defined(MALLOC_USE_SBRK)
@@ -151,11 +151,11 @@
 	  /* Try again to allocate.  */
 	  mem = __heap_alloc (heap, &size);
 
-	  __heap_unlock (heap_lock);
-
 #if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
 	  /* Insert a record of BLOCK in sorted order into the
 	     __malloc_mmapped_blocks list.  */
+	     
+	  new_mmb = malloc_from_heap (sizeof *new_mmb,&__malloc_mmb_heap, &__malloc_mmb_heap_lock);
 
 	  for (prev_mmb = 0, mmb = __malloc_mmapped_blocks;
 	       mmb;
@@ -163,7 +163,6 @@
 	    if (block < mmb->mem)
 	      break;
 
-	  new_mmb = malloc_from_heap (sizeof *new_mmb, &__malloc_mmb_heap, &__malloc_mmb_heap_lock);
 	  new_mmb->next = mmb;
 	  new_mmb->mem = block;
 	  new_mmb->size = block_size;
@@ -177,6 +176,7 @@
 			    (unsigned)new_mmb,
 			    (unsigned)new_mmb->mem, block_size);
 #endif /* !MALLOC_USE_SBRK && __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
+	  __heap_unlock (heap_lock);
 	}
     }
 
