From 6494060312de389feb65ad32bb411fcc64e821b7 Mon Sep 17 00:00:00 2001 From: Khem Raj Date: Sat, 11 Oct 2008 08:52:58 +0000 Subject: Fix bug 4994 hangs on read(). I have tested the patch extensibly on ARM/LT.old. Thank you Chase Douglas for reporting it and for the patch. --- libc/stdlib/malloc/heap.h | 56 ++++++++++++++--------------------------------- 1 file changed, 16 insertions(+), 40 deletions(-) (limited to 'libc/stdlib/malloc/heap.h') diff --git a/libc/stdlib/malloc/heap.h b/libc/stdlib/malloc/heap.h index 6505cd223..8b05cded1 100644 --- a/libc/stdlib/malloc/heap.h +++ b/libc/stdlib/malloc/heap.h @@ -29,32 +29,12 @@ #define HEAP_GRANULARITY (__alignof__ (HEAP_GRANULARITY_TYPE)) -/* A heap is a collection of memory blocks, from which smaller blocks - of memory can be allocated. */ -struct heap -{ - /* A list of memory in the heap available for allocation. */ - struct heap_free_area *free_areas; - -#ifdef HEAP_USE_LOCKING - /* A lock that can be used by callers to control access to the heap. - The heap code _does not_ use this lock, it's merely here for the - convenience of users! */ - pthread_mutex_t lock; -#endif -}; - /* The HEAP_INIT macro can be used as a static initializer for a heap variable. The HEAP_INIT_WITH_FA variant is used to initialize a heap with an initial static free-area; its argument FA should be declared using HEAP_DECLARE_STATIC_FREE_AREA. */ -#ifdef HEAP_USE_LOCKING -# define HEAP_INIT { 0, PTHREAD_MUTEX_INITIALIZER } -# define HEAP_INIT_WITH_FA(fa) { &fa._fa, PTHREAD_MUTEX_INITIALIZER } -#else -# define HEAP_INIT { 0 } -# define HEAP_INIT_WITH_FA(fa) { &fa._fa } -#endif +# define HEAP_INIT 0 +# define HEAP_INIT_WITH_FA(fa) &fa._fa /* A free-list area `header'. These are actually stored at the _ends_ of free areas (to make allocating from the beginning of the area simpler), @@ -129,27 +109,23 @@ extern int __heap_debug; #endif /* Output a text representation of HEAP to stderr, labelling it with STR. */ -extern void __heap_dump (struct heap *heap, const char *str); +extern void __heap_dump (struct heap_free_area *heap, const char *str); /* Do some consistency checks on HEAP. If they fail, output an error message to stderr, and exit. STR is printed with the failure message. */ -extern void __heap_check (struct heap *heap, const char *str); - - -#define __heap_lock(heap) __pthread_mutex_lock (&(heap)->lock) -#define __heap_unlock(heap) __pthread_mutex_unlock (&(heap)->lock) +extern void __heap_check (struct heap_free_area *heap, const char *str); /* Delete the free-area FA from HEAP. */ static __inline__ void -__heap_delete (struct heap *heap, struct heap_free_area *fa) +__heap_delete (struct heap_free_area *heap, struct heap_free_area *fa) { if (fa->next) fa->next->prev = fa->prev; if (fa->prev) fa->prev->next = fa->next; else - heap->free_areas = fa->next; + heap = fa->next; } @@ -157,7 +133,7 @@ __heap_delete (struct heap *heap, struct heap_free_area *fa) HEAP. PREV and NEXT may be 0; if PREV is 0, FA is installed as the first free-area. */ static __inline__ void -__heap_link_free_area (struct heap *heap, struct heap_free_area *fa, +__heap_link_free_area (struct heap_free_area *heap, struct heap_free_area *fa, struct heap_free_area *prev, struct heap_free_area *next) { @@ -167,7 +143,7 @@ __heap_link_free_area (struct heap *heap, struct heap_free_area *fa, if (prev) prev->next = fa; else - heap->free_areas = fa; + heap = fa; if (next) next->prev = fa; } @@ -176,14 +152,14 @@ __heap_link_free_area (struct heap *heap, struct heap_free_area *fa, PREV may be 0, in which case FA is installed as the first free-area (but FA may not be 0). */ static __inline__ void -__heap_link_free_area_after (struct heap *heap, +__heap_link_free_area_after (struct heap_free_area *heap, struct heap_free_area *fa, struct heap_free_area *prev) { if (prev) prev->next = fa; else - heap->free_areas = fa; + heap = fa; fa->prev = prev; } @@ -192,7 +168,7 @@ __heap_link_free_area_after (struct heap *heap, PREV and NEXT may be 0; if PREV is 0, MEM is installed as the first free-area. */ static __inline__ struct heap_free_area * -__heap_add_free_area (struct heap *heap, void *mem, size_t size, +__heap_add_free_area (struct heap_free_area *heap, void *mem, size_t size, struct heap_free_area *prev, struct heap_free_area *next) { @@ -210,7 +186,7 @@ __heap_add_free_area (struct heap *heap, void *mem, size_t size, /* Allocate SIZE bytes from the front of the free-area FA in HEAP, and return the amount actually allocated (which may be more than SIZE). */ static __inline__ size_t -__heap_free_area_alloc (struct heap *heap, +__heap_free_area_alloc (struct heap_free_area *heap, struct heap_free_area *fa, size_t size) { size_t fa_size = fa->size; @@ -234,16 +210,16 @@ __heap_free_area_alloc (struct heap *heap, /* Allocate and return a block at least *SIZE bytes long from HEAP. *SIZE is adjusted to reflect the actual amount allocated (which may be greater than requested). */ -extern void *__heap_alloc (struct heap *heap, size_t *size); +extern void *__heap_alloc (struct heap_free_area *heap, size_t *size); /* Allocate SIZE bytes at address MEM in HEAP. Return the actual size allocated, or 0 if we failed. */ -extern size_t __heap_alloc_at (struct heap *heap, void *mem, size_t size); +extern size_t __heap_alloc_at (struct heap_free_area *heap, void *mem, size_t size); /* Return the memory area MEM of size SIZE to HEAP. Returns the heap free area into which the memory was placed. */ -extern struct heap_free_area *__heap_free (struct heap *heap, +extern struct heap_free_area *__heap_free (struct heap_free_area *heap, void *mem, size_t size); /* Return true if HEAP contains absolutely no memory. */ -#define __heap_is_empty(heap) (! (heap)->free_areas) +#define __heap_is_empty(heap) (! (heap)) -- cgit v1.2.3