summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc/malloc.c
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2008-10-16 21:16:46 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2008-10-16 21:16:46 +0000
commit0be050c8b6579abd76f4a405a1964532d4e71bf1 (patch)
tree646250b525d2fc2116b54455cd3e1146ebf85e7f /libc/stdlib/malloc/malloc.c
parent9674f4133f323e0d312a0e8f5ff3886c590f459e (diff)
This should fix malloc with debug and without threads. (Chase N Douglas)
This should have been in r23660. Untested.
Diffstat (limited to 'libc/stdlib/malloc/malloc.c')
-rw-r--r--libc/stdlib/malloc/malloc.c26
1 files changed, 21 insertions, 5 deletions
diff --git a/libc/stdlib/malloc/malloc.c b/libc/stdlib/malloc/malloc.c
index 0caf012a2..d58d62d2d 100644
--- a/libc/stdlib/malloc/malloc.c
+++ b/libc/stdlib/malloc/malloc.c
@@ -27,7 +27,9 @@ libc_hidden_proto(sbrk)
programs can do a little mallocing without mmaping in more space. */
HEAP_DECLARE_STATIC_FREE_AREA (initial_fa, 256);
struct heap_free_area *__malloc_heap = HEAP_INIT_WITH_FA (initial_fa);
+#ifdef HEAP_USE_LOCKING
malloc_mutex_t __malloc_heap_lock = PTHREAD_MUTEX_INITIALIZER;
+#endif
#if defined(MALLOC_USE_LOCKING) && defined(MALLOC_USE_SBRK)
/* A lock protecting our use of sbrk. */
@@ -45,12 +47,18 @@ struct malloc_mmb *__malloc_mmapped_blocks = 0;
annoying ways. */
HEAP_DECLARE_STATIC_FREE_AREA (initial_mmb_fa, 48); /* enough for 3 mmbs */
struct heap_free_area *__malloc_mmb_heap = HEAP_INIT_WITH_FA (initial_mmb_fa);
+#ifdef HEAP_USE_LOCKING
malloc_mutex_t __malloc_mmb_heap_lock = PTHREAD_MUTEX_INITIALIZER;
+#endif
#endif /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
static void *
+#ifdef HEAP_USE_LOCKING
malloc_from_heap (size_t size, struct heap_free_area *heap, malloc_mutex_t *heap_lock)
+#else
+malloc_from_heap (size_t size, struct heap_free_area *heap)
+#endif
{
void *mem;
@@ -59,12 +67,12 @@ malloc_from_heap (size_t size, struct heap_free_area *heap, malloc_mutex_t *heap
/* Include extra space to record the size of the allocated block. */
size += MALLOC_HEADER_SIZE;
- __pthread_mutex_lock (heap_lock);
+ __heap_do_lock (heap_lock);
/* First try to get memory that's already in our heap. */
mem = __heap_alloc (heap, &size);
- __pthread_mutex_unlock (heap_lock);
+ __heap_do_unlock (heap_lock);
if (unlikely (! mem))
/* We couldn't allocate from the heap, so grab some more
@@ -128,7 +136,7 @@ malloc_from_heap (size_t size, struct heap_free_area *heap, malloc_mutex_t *heap
(long)block, (long)block + block_size, block_size);
/* Get back the heap lock. */
- __pthread_mutex_lock (heap_lock);
+ __heap_do_lock (heap_lock);
/* Put BLOCK into the heap. */
__heap_free (heap, block, block_size);
@@ -138,7 +146,7 @@ malloc_from_heap (size_t size, struct heap_free_area *heap, malloc_mutex_t *heap
/* Try again to allocate. */
mem = __heap_alloc (heap, &size);
- __pthread_mutex_unlock (heap_lock);
+ __heap_do_unlock (heap_lock);
#if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
/* Insert a record of BLOCK in sorted order into the
@@ -150,7 +158,11 @@ malloc_from_heap (size_t size, struct heap_free_area *heap, malloc_mutex_t *heap
if (block < mmb->mem)
break;
+#ifdef HEAP_USE_LOCKING
new_mmb = malloc_from_heap (sizeof *new_mmb, __malloc_mmb_heap, &__malloc_mmb_heap_lock);
+#else
+ new_mmb = malloc_from_heap (sizeof *new_mmb, __malloc_mmb_heap);
+#endif
new_mmb->next = mmb;
new_mmb->mem = block;
new_mmb->size = block_size;
@@ -193,7 +205,7 @@ malloc (size_t size)
__malloc_debug_init ();
}
if (__malloc_check)
- __heap_check (&__malloc_heap, "malloc");
+ __heap_check (__malloc_heap, "malloc");
#endif
#ifdef __MALLOC_GLIBC_COMPAT__
@@ -209,7 +221,11 @@ malloc (size_t size)
if (unlikely(((unsigned long)size > (unsigned long)(MALLOC_HEADER_SIZE*-2))))
goto oom;
+#ifdef HEAP_USE_LOCKING
mem = malloc_from_heap (size, __malloc_heap, &__malloc_heap_lock);
+#else
+ mem = malloc_from_heap (size, __malloc_heap);
+#endif
if (unlikely (!mem))
{
oom: