From fa8d436c87f156d18208df3819fecee9fc1dbd9e Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Tue, 29 Jan 2002 07:54:51 +0000 Subject: Update. 2002-01-18 Wolfram Gloger * malloc/malloc.c: Rewrite, adapted from Doug Lea's malloc-2.7.0.c. * malloc/malloc.h: Likewise. * malloc/arena.c: New file. * malloc/hooks.c: New file. * malloc/tst-mallocstate.c: New file. * malloc/Makefile: Add new testcase tst-mallocstate. Add arena.c and hooks.c to distribute. Fix commented CPPFLAGS. 2002-01-28 Ulrich Drepper * stdlib/msort.c: Remove last patch. The optimization violates the same rule which qsort.c had problems with. 2002-01-27 Paul Eggert * stdlib/qsort.c (_quicksort): Do not apply the comparison function to a pivot element that lies outside the array to be sorted, as ISO C99 requires that the comparison function be called only with addresses of array elements [PR libc/2880]. --- malloc/Makefile | 12 +- malloc/arena.c | 746 +++++ malloc/hooks.c | 631 ++++ malloc/malloc.c | 8084 ++++++++++++++++++++++++---------------------- malloc/malloc.h | 53 +- malloc/tst-mallocstate.c | 82 + 6 files changed, 5665 insertions(+), 3943 deletions(-) create mode 100644 malloc/arena.c create mode 100644 malloc/hooks.c create mode 100644 malloc/tst-mallocstate.c (limited to 'malloc') diff --git a/malloc/Makefile b/malloc/Makefile index 3b5d991971..226ed99fc3 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc. +# Copyright (C) 1991-1999, 2000, 2001, 2002 Free Software Foundation, Inc. # This file is part of the GNU C Library. # The GNU C Library is free software; you can redistribute it and/or @@ -25,11 +25,12 @@ all: dist-headers := malloc.h headers := $(dist-headers) obstack.h mcheck.h -tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack +tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \ + tst-mallocstate test-srcs = tst-mtrace distribute = thread-m.h mtrace.pl mcheck-init.c stackinfo.h memusage.h \ - memusage.sh memusagestat.c tst-mtrace.sh + memusage.sh memusagestat.c tst-mtrace.sh arena.c hooks.c # Things which get pasted together into gmalloc.c. gmalloc-routines := malloc morecore @@ -109,7 +110,7 @@ endif endif # Uncomment this for test releases. For public releases it is too expensive. -#CPPFLAGS-malloc.o += -DMALLOC_DEBUG +#CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1 $(objpfx)mtrace: mtrace.pl rm -f $@.new @@ -126,3 +127,6 @@ $(objpfx)memusage: memusage.sh # The implementation uses `dlsym' $(objpfx)libmemusage.so: $(common-objpfx)dlfcn/libdl.so + +# Extra dependencies +$(foreach o,$(all-object-suffixes),$(objpfx)malloc$(o)): arena.c hooks.c diff --git a/malloc/arena.c b/malloc/arena.c new file mode 100644 index 0000000000..8324b68689 --- /dev/null +++ b/malloc/arena.c @@ -0,0 +1,746 @@ +/* Malloc implementation for multiple threads without lock contention. + Copyright (C) 2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Wolfram Gloger , 2001. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +/* $Id$ */ + +/* Compile-time constants. */ + +#define HEAP_MIN_SIZE (32*1024) +#ifndef HEAP_MAX_SIZE +#define HEAP_MAX_SIZE (1024*1024) /* must be a power of two */ +#endif + +/* HEAP_MIN_SIZE and HEAP_MAX_SIZE limit the size of mmap()ed heaps + that are dynamically created for multi-threaded programs. The + maximum size must be a power of two, for fast determination of + which heap belongs to a chunk. It should be much larger than the + mmap threshold, so that requests with a size just below that + threshold can be fulfilled without creating too many heaps. */ + + +#ifndef THREAD_STATS +#define THREAD_STATS 0 +#endif + +/* If THREAD_STATS is non-zero, some statistics on mutex locking are + computed. */ + +/***************************************************************************/ + +#define top(ar_ptr) ((ar_ptr)->top) + +/* A heap is a single contiguous memory region holding (coalesceable) + malloc_chunks. It is allocated with mmap() and always starts at an + address aligned to HEAP_MAX_SIZE. Not used unless compiling with + USE_ARENAS. */ + +typedef struct _heap_info { + mstate ar_ptr; /* Arena for this heap. */ + struct _heap_info *prev; /* Previous heap. */ + size_t size; /* Current size in bytes. */ + size_t pad; /* Make sure the following data is properly aligned. */ +} heap_info; + +/* Thread specific data */ + +static tsd_key_t arena_key; +static mutex_t list_lock; + +#if THREAD_STATS +static int stat_n_heaps; +#define THREAD_STAT(x) x +#else +#define THREAD_STAT(x) do ; while(0) +#endif + +/* Mapped memory in non-main arenas (reliable only for NO_THREADS). */ +static unsigned long arena_mem; + +/**************************************************************************/ + +#if USE_ARENAS + +/* arena_get() acquires an arena and locks the corresponding mutex. + First, try the one last locked successfully by this thread. (This + is the common case and handled with a macro for speed.) Then, loop + once over the circularly linked list of arenas. If no arena is + readily available, create a new one. In this latter case, `size' + is just a hint as to how much memory will be required immediately + in the new arena. */ + +#define arena_get(ptr, size) do { \ + Void_t *vptr = NULL; \ + ptr = (mstate)tsd_getspecific(arena_key, vptr); \ + if(ptr && !mutex_trylock(&ptr->mutex)) { \ + THREAD_STAT(++(ptr->stat_lock_direct)); \ + } else \ + ptr = arena_get2(ptr, (size)); \ +} while(0) + +/* find the heap and corresponding arena for a given ptr */ + +#define heap_for_ptr(ptr) \ + ((heap_info *)((unsigned long)(ptr) & ~(HEAP_MAX_SIZE-1))) +#define arena_for_chunk(ptr) \ + (chunk_non_main_arena(ptr) ? heap_for_ptr(ptr)->ar_ptr : &main_arena) + +#else /* !USE_ARENAS */ + +/* There is only one arena, main_arena. */ + +#if THREAD_STATS +#define arena_get(ar_ptr, sz) do { \ + ar_ptr = &main_arena; \ + if(!mutex_trylock(&ar_ptr->mutex)) \ + ++(ar_ptr->stat_lock_direct); \ + else { \ + (void)mutex_lock(&ar_ptr->mutex); \ + ++(ar_ptr->stat_lock_wait); \ + } \ +} while(0) +#else +#define arena_get(ar_ptr, sz) do { \ + ar_ptr = &main_arena; \ + (void)mutex_lock(&ar_ptr->mutex); \ +} while(0) +#endif +#define arena_for_chunk(ptr) (&main_arena) + +#endif /* USE_ARENAS */ + +/**************************************************************************/ + +#ifndef NO_THREADS + +/* atfork support. */ + +static __malloc_ptr_t (*save_malloc_hook) __MALLOC_P ((size_t __size, + __const __malloc_ptr_t)); +static void (*save_free_hook) __MALLOC_P ((__malloc_ptr_t __ptr, + __const __malloc_ptr_t)); +static Void_t* save_arena; + +/* Magic value for the thread-specific arena pointer when + malloc_atfork() is in use. */ + +#define ATFORK_ARENA_PTR ((Void_t*)-1) + +/* The following hooks are used while the `atfork' handling mechanism + is active. */ + +static Void_t* +malloc_atfork(size_t sz, const Void_t *caller) +{ + Void_t *vptr = NULL; + Void_t *victim; + + tsd_getspecific(arena_key, vptr); + if(vptr == ATFORK_ARENA_PTR) { + /* We are the only thread that may allocate at all. */ + if(save_malloc_hook != malloc_check) { + return _int_malloc(&main_arena, sz); + } else { + if(top_check()<0) + return 0; + victim = _int_malloc(&main_arena, sz+1); + return mem2mem_check(victim, sz); + } + } else { + /* Suspend the thread until the `atfork' handlers have completed. + By that time, the hooks will have been reset as well, so that + mALLOc() can be used again. */ + (void)mutex_lock(&list_lock); + (void)mutex_unlock(&list_lock); + return public_mALLOc(sz); + } +} + +static void +free_atfork(Void_t* mem, const Void_t *caller) +{ + Void_t *vptr = NULL; + mstate ar_ptr; + mchunkptr p; /* chunk corresponding to mem */ + + if (mem == 0) /* free(0) has no effect */ + return; + + p = mem2chunk(mem); /* do not bother to replicate free_check here */ + +#if HAVE_MMAP + if (chunk_is_mmapped(p)) /* release mmapped memory. */ + { + munmap_chunk(p); + return; + } +#endif + + ar_ptr = arena_for_chunk(p); + tsd_getspecific(arena_key, vptr); + if(vptr != ATFORK_ARENA_PTR) + (void)mutex_lock(&ar_ptr->mutex); + _int_free(ar_ptr, mem); + if(vptr != ATFORK_ARENA_PTR) + (void)mutex_unlock(&ar_ptr->mutex); +} + +/* The following two functions are registered via thread_atfork() to + make sure that the mutexes remain in a consistent state in the + fork()ed version of a thread. Also adapt the malloc and free hooks + temporarily, because the `atfork' handler mechanism may use + malloc/free internally (e.g. in LinuxThreads). */ + +static void +ptmalloc_lock_all __MALLOC_P((void)) +{ + mstate ar_ptr; + + (void)mutex_lock(&list_lock); + for(ar_ptr = &main_arena;;) { + (void)mutex_lock(&ar_ptr->mutex); + ar_ptr = ar_ptr->next; + if(ar_ptr == &main_arena) break; + } + save_malloc_hook = __malloc_hook; + save_free_hook = __free_hook; + __malloc_hook = malloc_atfork; + __free_hook = free_atfork; + /* Only the current thread may perform malloc/free calls now. */ + tsd_getspecific(arena_key, save_arena); + tsd_setspecific(arena_key, ATFORK_ARENA_PTR); +} + +static void +ptmalloc_unlock_all __MALLOC_P((void)) +{ + mstate ar_ptr; + + tsd_setspecific(arena_key, save_arena); + __malloc_hook = save_malloc_hook; + __free_hook = save_free_hook; + for(ar_ptr = &main_arena;;) { + (void)mutex_unlock(&ar_ptr->mutex); + ar_ptr = ar_ptr->next; + if(ar_ptr == &main_arena) break; + } + (void)mutex_unlock(&list_lock); +} + +#ifdef __linux__ + +/* In LinuxThreads, unlocking a mutex in the child process after a + fork() is currently unsafe, whereas re-initializing it is safe and + does not leak resources. Therefore, a special atfork handler is + installed for the child. */ + +static void +ptmalloc_unlock_all2 __MALLOC_P((void)) +{ + mstate ar_ptr; + +#if defined _LIBC || defined MALLOC_HOOKS + tsd_setspecific(arena_key, save_arena); + __malloc_hook = save_malloc_hook; + __free_hook = save_free_hook; +#endif + for(ar_ptr = &main_arena;;) { + (void)mutex_init(&ar_ptr->mutex); + ar_ptr = ar_ptr->next; + if(ar_ptr == &main_arena) break; + } + (void)mutex_init(&list_lock); +} + +#else + +#define ptmalloc_unlock_all2 ptmalloc_unlock_all + +#endif + +#endif /* !defined NO_THREADS */ + +/* Already initialized? */ +int __malloc_initialized = -1; + +/* Initialization routine. */ +#ifdef _LIBC +#include +extern char **_environ; + +static char * +internal_function +next_env_entry (char ***position) +{ + char **current = *position; + char *result = NULL; + + while (*current != NULL) + { + if (__builtin_expect ((*current)[0] == 'M', 0) + && (*current)[1] == 'A' + && (*current)[2] == 'L' + && (*current)[3] == 'L' + && (*current)[4] == 'O' + && (*current)[5] == 'C' + && (*current)[6] == '_') + { + result = &(*current)[7]; + + /* Save current position for next visit. */ + *position = ++current; + + break; + } + + ++current; + } + + return result; +} +#endif /* _LIBC */ + +static void +ptmalloc_init __MALLOC_P((void)) +{ +#if __STD_C + const char* s; +#else + char* s; +#endif + int secure = 0; + + if(__malloc_initialized >= 0) return; + __malloc_initialized = 0; + + mp_.top_pad = DEFAULT_TOP_PAD; + mp_.n_mmaps_max = DEFAULT_MMAP_MAX; + mp_.mmap_threshold = DEFAULT_MMAP_THRESHOLD; + mp_.trim_threshold = DEFAULT_TRIM_THRESHOLD; + mp_.pagesize = malloc_getpagesize; + +#ifndef NO_THREADS + /* With some threads implementations, creating thread-specific data + or initializing a mutex may call malloc() itself. Provide a + simple starter version (realloc() won't work). */ + save_malloc_hook = __malloc_hook; + save_free_hook = __free_hook; + __malloc_hook = malloc_starter; + __free_hook = free_starter; +#ifdef _LIBC + /* Initialize the pthreads interface. */ + if (__pthread_initialize != NULL) + __pthread_initialize(); +#endif +#endif /* !defined NO_THREADS */ + mutex_init(&main_arena.mutex); + main_arena.next = &main_arena; + + mutex_init(&list_lock); + tsd_key_create(&arena_key, NULL); + tsd_setspecific(arena_key, (Void_t *)&main_arena); + thread_atfork(ptmalloc_lock_all, ptmalloc_unlock_all, ptmalloc_unlock_all2); +#ifndef NO_THREADS + __malloc_hook = save_malloc_hook; + __free_hook = save_free_hook; +#endif +#ifdef _LIBC + secure = __libc_enable_secure; + s = NULL; + { + char **runp = _environ; + char *envline; + + while (__builtin_expect ((envline = next_env_entry (&runp)) != NULL, + 0)) + { + size_t len = strcspn (envline, "="); + + if (envline[len] != '=') + /* This is a "MALLOC_" variable at the end of the string + without a '=' character. Ignore it since otherwise we + will access invalid memory below. */ + continue; + + switch (len) + { + case 6: + if (memcmp (envline, "CHECK_", 6) == 0) + s = &envline[7]; + break; + case 8: + if (! secure && memcmp (envline, "TOP_PAD_", 8) == 0) + mALLOPt(M_TOP_PAD, atoi(&envline[9])); + break; + case 9: + if (! secure && memcmp (envline, "MMAP_MAX_", 9) == 0) + mALLOPt(M_MMAP_MAX, atoi(&envline[10])); + break; + case 15: + if (! secure) + { + if (memcmp (envline, "TRIM_THRESHOLD_", 15) == 0) + mALLOPt(M_TRIM_THRESHOLD, atoi(&envline[16])); + else if (memcmp (envline, "MMAP_THRESHOLD_", 15) == 0) + mALLOPt(M_MMAP_THRESHOLD, atoi(&envline[16])); + } + break; + default: + break; + } + } + } +#else + if (! secure) + { + if((s = getenv("MALLOC_TRIM_THRESHOLD_"))) + mALLOPt(M_TRIM_THRESHOLD, atoi(s)); + if((s = getenv("MALLOC_TOP_PAD_"))) + mALLOPt(M_TOP_PAD, atoi(s)); + if((s = getenv("MALLOC_MMAP_THRESHOLD_"))) + mALLOPt(M_MMAP_THRESHOLD, atoi(s)); + if((s = getenv("MALLOC_MMAP_MAX_"))) + mALLOPt(M_MMAP_MAX, atoi(s)); + } + s = getenv("MALLOC_CHECK_"); +#endif + if(s) { + if(s[0]) mALLOPt(M_CHECK_ACTION, (int)(s[0] - '0')); + __malloc_check_init(); + } + if(__malloc_initialize_hook != NULL) + (*__malloc_initialize_hook)(); + __malloc_initialized = 1; +} + +/* There are platforms (e.g. Hurd) with a link-time hook mechanism. */ +#ifdef thread_atfork_static +thread_atfork_static(ptmalloc_lock_all, ptmalloc_unlock_all, \ + ptmalloc_unlock_all2) +#endif + + + +/* Managing heaps and arenas (for concurrent threads) */ + +#if USE_ARENAS + +#if MALLOC_DEBUG > 1 + +/* Print the complete contents of a single heap to stderr. */ + +static void +#if __STD_C +dump_heap(heap_info *heap) +#else +dump_heap(heap) heap_info *heap; +#endif +{ + char *ptr; + mchunkptr p; + + fprintf(stderr, "Heap %p, size %10lx:\n", heap, (long)heap->size); + ptr = (heap->ar_ptr != (mstate)(heap+1)) ? + (char*)(heap + 1) : (char*)(heap + 1) + sizeof(struct malloc_state); + p = (mchunkptr)(((unsigned long)ptr + MALLOC_ALIGN_MASK) & + ~MALLOC_ALIGN_MASK); + for(;;) { + fprintf(stderr, "chunk %p size %10lx", p, (long)p->size); + if(p == top(heap->ar_ptr)) { + fprintf(stderr, " (top)\n"); + break; + } else if(p->size == (0|PREV_INUSE)) { + fprintf(stderr, " (fence)\n"); + break; + } + fprintf(stderr, "\n"); + p = next_chunk(p); + } +} + +#endif /* MALLOC_DEBUG > 1 */ + +/* Create a new heap. size is automatically rounded up to a multiple + of the page size. */ + +static heap_info * +internal_function +#if __STD_C +new_heap(size_t size, size_t top_pad) +#else +new_heap(size, top_pad) size_t size, top_pad; +#endif +{ + size_t page_mask = malloc_getpagesize - 1; + char *p1, *p2; + unsigned long ul; + heap_info *h; + + if(size+top_pad < HEAP_MIN_SIZE) + size = HEAP_MIN_SIZE; + else if(size+top_pad <= HEAP_MAX_SIZE) + size += top_pad; + else if(size > HEAP_MAX_SIZE) + return 0; + else + size = HEAP_MAX_SIZE; + size = (size + page_mask) & ~page_mask; + + /* A memory region aligned to a multiple of HEAP_MAX_SIZE is needed. + No swap space needs to be reserved for the following large + mapping (on Linux, this is the case for all non-writable mappings + anyway). */ + p1 = (char *)MMAP(0, HEAP_MAX_SIZE<<1, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE); + if(p1 != MAP_FAILED) { + p2 = (char *)(((unsigned long)p1 + (HEAP_MAX_SIZE-1)) & ~(HEAP_MAX_SIZE-1)); + ul = p2 - p1; + munmap(p1, ul); + munmap(p2 + HEAP_MAX_SIZE, HEAP_MAX_SIZE - ul); + } else { + /* Try to take the chance that an allocation of only HEAP_MAX_SIZE + is already aligned. */ + p2 = (char *)MMAP(0, HEAP_MAX_SIZE, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE); + if(p2 == MAP_FAILED) + return 0; + if((unsigned long)p2 & (HEAP_MAX_SIZE-1)) { + munmap(p2, HEAP_MAX_SIZE); + return 0; + } + } + if(mprotect(p2, size, PROT_READ|PROT_WRITE) != 0) { + munmap(p2, HEAP_MAX_SIZE); + return 0; + } + h = (heap_info *)p2; + h->size = size; + THREAD_STAT(stat_n_heaps++); + return h; +} + +/* Grow or shrink a heap. size is automatically rounded up to a + multiple of the page size if it is positive. */ + +static int +#if __STD_C +grow_heap(heap_info *h, long diff) +#else +grow_heap(h, diff) heap_info *h; long diff; +#endif +{ + size_t page_mask = malloc_getpagesize - 1; + long new_size; + + if(diff >= 0) { + diff = (diff + page_mask) & ~page_mask; + new_size = (long)h->size + diff; + if(new_size > HEAP_MAX_SIZE) + return -1; + if(mprotect((char *)h + h->size, diff, PROT_READ|PROT_WRITE) != 0) + return -2; + } else { + new_size = (long)h->size + diff; + if(new_size < (long)sizeof(*h)) + return -1; + /* Try to re-map the extra heap space freshly to save memory, and + make it inaccessible. */ + if((char *)MMAP((char *)h + new_size, -diff, PROT_NONE, + MAP_PRIVATE|MAP_FIXED) == (char *) MAP_FAILED) + return -2; + /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/ + } + h->size = new_size; + return 0; +} + +/* Delete a heap. */ + +#define delete_heap(heap) munmap((char*)(heap), HEAP_MAX_SIZE) + +static int +internal_function +#if __STD_C +heap_trim(heap_info *heap, size_t pad) +#else +heap_trim(heap, pad) heap_info *heap; size_t pad; +#endif +{ + mstate ar_ptr = heap->ar_ptr; + unsigned long pagesz = mp_.pagesize; + mchunkptr top_chunk = top(ar_ptr), p, bck, fwd; + heap_info *prev_heap; + long new_size, top_size, extra; + + /* Can this heap go away completely? */ + while(top_chunk == chunk_at_offset(heap, sizeof(*heap))) { + prev_heap = heap->prev; + p = chunk_at_offset(prev_heap, prev_heap->size - (MINSIZE-2*SIZE_SZ)); + assert(p->size == (0|PREV_INUSE)); /* must be fencepost */ + p = prev_chunk(p); + new_size = chunksize(p) + (MINSIZE-2*SIZE_SZ); + assert(new_size>0 && new_size<(long)(2*MINSIZE)); + if(!prev_inuse(p)) + new_size += p->prev_size; + assert(new_size>0 && new_sizesize) < pad + MINSIZE + pagesz) + break; + ar_ptr->system_mem -= heap->size; + arena_mem -= heap->size; + delete_heap(heap); + heap = prev_heap; + if(!prev_inuse(p)) { /* consolidate backward */ + p = prev_chunk(p); + unlink(p, bck, fwd); + } + assert(((unsigned long)((char*)p + new_size) & (pagesz-1)) == 0); + assert( ((char*)p + new_size) == ((char*)heap + heap->size) ); + top(ar_ptr) = top_chunk = p; + set_head(top_chunk, new_size | PREV_INUSE); + /*check_chunk(ar_ptr, top_chunk);*/ + } + top_size = chunksize(top_chunk); + extra = ((top_size - pad - MINSIZE + (pagesz-1))/pagesz - 1) * pagesz; + if(extra < (long)pagesz) + return 0; + /* Try to shrink. */ + if(grow_heap(heap, -extra) != 0) + return 0; + ar_ptr->system_mem -= extra; + arena_mem -= extra; + + /* Success. Adjust top accordingly. */ + set_head(top_chunk, (top_size - extra) | PREV_INUSE); + /*check_chunk(ar_ptr, top_chunk);*/ + return 1; +} + +static mstate +internal_function +#if __STD_C +arena_get2(mstate a_tsd, size_t size) +#else +arena_get2(a_tsd, size) mstate a_tsd; size_t size; +#endif +{ + mstate a; + int err; + + if(!a_tsd) + a = a_tsd = &main_arena; + else { + a = a_tsd->next; + if(!a) { + /* This can only happen while initializing the new arena. */ + (void)mutex_lock(&main_arena.mutex); + THREAD_STAT(++(main_arena.stat_lock_wait)); + return &main_arena; + } + } + + /* Check the global, circularly linked list for available arenas. */ + repeat: + do { + if(!mutex_trylock(&a->mutex)) { + THREAD_STAT(++(a->stat_lock_loop)); + tsd_setspecific(arena_key, (Void_t *)a); + return a; + } + a = a->next; + } while(a != a_tsd); + + /* If not even the list_lock can be obtained, try again. This can + happen during `atfork', or for example on systems where thread + creation makes it temporarily impossible to obtain _any_ + locks. */ + if(mutex_trylock(&list_lock)) { + a = a_tsd; + goto repeat; + } + (void)mutex_unlock(&list_lock); + + /* Nothing immediately available, so generate a new arena. */ + a = _int_new_arena(size); + if(!a) + return 0; + + tsd_setspecific(arena_key, (Void_t *)a); + mutex_init(&a->mutex); + err = mutex_lock(&a->mutex); /* remember result */ + + /* Add the new arena to the global list. */ + (void)mutex_lock(&list_lock); + a->next = main_arena.next; + main_arena.next = a; + (void)mutex_unlock(&list_lock); + + if(err) /* locking failed; keep arena for further attempts later */ + return 0; + + THREAD_STAT(++(a->stat_lock_loop)); + return a; +} + +/* Create a new arena with initial size "size". */ + +mstate +_int_new_arena(size_t size) +{ + mstate a; + heap_info *h; + char *ptr; + unsigned long misalign; + + h = new_heap(size + (sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT), + mp_.top_pad); + if(!h) { + /* Maybe size is too large to fit in a single heap. So, just try + to create a minimally-sized arena and let _int_malloc() attempt + to deal with the large request via mmap_chunk(). */ + h = new_heap(sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT, mp_.top_pad); + if(!h) + return 0; + } + a = h->ar_ptr = (mstate)(h+1); + malloc_init_state(a); + /*a->next = NULL;*/ + a->system_mem = a->max_system_mem = h->size; + arena_mem += h->size; +#ifdef NO_THREADS + if((unsigned long)(mp_.mmapped_mem + arena_mem + main_arena.system_mem) > + mp_.max_total_mem) + mp_.max_total_mem = mp_.mmapped_mem + arena_mem + main_arena.system_mem; +#endif + + /* Set up the top chunk, with proper alignment. */ + ptr = (char *)(a + 1); + misalign = (unsigned long)chunk2mem(ptr) & MALLOC_ALIGN_MASK; + if (misalign > 0) + ptr += MALLOC_ALIGNMENT - misalign; + top(a) = (mchunkptr)ptr; + set_head(top(a), (((char*)h + h->size) - ptr) | PREV_INUSE); + + return a; +} + +#endif /* USE_ARENAS */ + +/* + * Local variables: + * c-basic-offset: 2 + * End: + */ diff --git a/malloc/hooks.c b/malloc/hooks.c new file mode 100644 index 0000000000..28629eff80 --- /dev/null +++ b/malloc/hooks.c @@ -0,0 +1,631 @@ +/* Malloc implementation for multiple threads without lock contention. + Copyright (C) 2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Wolfram Gloger , 2001. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +/* $Id$ */ + +#ifndef weak_variable +#define weak_variable /**/ +#endif + +#ifndef DEFAULT_CHECK_ACTION +#define DEFAULT_CHECK_ACTION 1 +#endif + +/* What to do if the standard debugging hooks are in place and a + corrupt pointer is detected: do nothing (0), print an error message + (1), or call abort() (2). */ + +/* Hooks for debugging versions. The initial hooks just call the + initialization routine, then do the normal work. */ + +static Void_t* +#if __STD_C +malloc_hook_ini(size_t sz, const __malloc_ptr_t caller) +#else +malloc_hook_ini(sz, caller) + size_t sz; const __malloc_ptr_t caller; +#endif +{ + __malloc_hook = NULL; + ptmalloc_init(); + return public_mALLOc(sz); +} + +static Void_t* +#if __STD_C +realloc_hook_ini(Void_t* ptr, size_t sz, const __malloc_ptr_t caller) +#else +realloc_hook_ini(ptr, sz, caller) + Void_t* ptr; size_t sz; const __malloc_ptr_t caller; +#endif +{ + __malloc_hook = NULL; + __realloc_hook = NULL; + ptmalloc_init(); + return public_rEALLOc(ptr, sz); +} + +static Void_t* +#if __STD_C +memalign_hook_ini(size_t alignment, size_t sz, const __malloc_ptr_t caller) +#else +memalign_hook_ini(alignment, sz, caller) + size_t alignment; size_t sz; const __malloc_ptr_t caller; +#endif +{ + __memalign_hook = NULL; + ptmalloc_init(); + return public_mEMALIGn(alignment, sz); +} + +void weak_variable (*__malloc_initialize_hook) __MALLOC_P ((void)) = NULL; +void weak_variable (*__free_hook) __MALLOC_P ((__malloc_ptr_t __ptr, + const __malloc_ptr_t)) = NULL; +__malloc_ptr_t weak_variable (*__malloc_hook) + __MALLOC_P ((size_t __size, const __malloc_ptr_t)) = malloc_hook_ini; +__malloc_ptr_t weak_variable (*__realloc_hook) + __MALLOC_P ((__malloc_ptr_t __ptr, size_t __size, const __malloc_ptr_t)) + = realloc_hook_ini; +__malloc_ptr_t weak_variable (*__memalign_hook) + __MALLOC_P ((size_t __alignment, size_t __size, const __malloc_ptr_t)) + = memalign_hook_ini; +void weak_variable (*__after_morecore_hook) __MALLOC_P ((void)) = NULL; + + +static int check_action = DEFAULT_CHECK_ACTION; + +/* Whether we are using malloc checking. */ +static int using_malloc_checking; + +/* A flag that is set by malloc_set_state, to signal that malloc checking + must not be enabled on the request from the user (via the MALLOC_CHECK_ + environment variable). It is reset by __malloc_check_init to tell + malloc_set_state that the user has requested malloc checking. + + The purpose of this flag is to make sure that malloc checking is not + enabled when the heap to be restored was constructed without malloc + checking, and thus does not contain the required magic bytes. + Otherwise the heap would be corrupted by calls to free and realloc. If + it turns out that the heap was created with malloc checking and the + user has requested it malloc_set_state just calls __malloc_check_init + again to enable it. On the other hand, reusing such a heap without + further malloc checking is safe. */ +static int disallow_malloc_check; + +/* Activate a standard set of debugging hooks. */ +void +__malloc_check_init() +{ + if (disallow_malloc_check) { + disallow_malloc_check = 0; + return; + } + using_malloc_checking = 1; + __malloc_hook = malloc_check; + __free_hook = free_check; + __realloc_hook = realloc_check; + __memalign_hook = memalign_check; + if(check_action & 1) + fprintf(stderr, "malloc: using debugging hooks\n"); +} + +/* A simple, standard set of debugging hooks. Overhead is `only' one + byte per chunk; still this will catch most cases of double frees or + overruns. The goal here is to avoid obscure crashes due to invalid + usage, unlike in the MALLOC_DEBUG code. */ + +#define MAGICBYTE(p) ( ( ((size_t)p >> 3) ^ ((size_t)p >> 11)) & 0xFF ) + +/* Instrument a chunk with overrun detector byte(s) and convert it + into a user pointer with requested size sz. */ + +static Void_t* +internal_function +#if __STD_C +mem2mem_check(Void_t *ptr, size_t sz) +#else +mem2mem_check(ptr, sz) Void_t *ptr; size_t sz; +#endif +{ + mchunkptr p; + unsigned char* m_ptr = (unsigned char*)BOUNDED_N(ptr, sz); + size_t i; + + if (!ptr) + return ptr; + p = mem2chunk(ptr); + for(i = chunksize(p) - (chunk_is_mmapped(p) ? 2*SIZE_SZ+1 : SIZE_SZ+1); + i > sz; + i -= 0xFF) { + if(i-sz < 0x100) { + m_ptr[i] = (unsigned char)(i-sz); + break; + } + m_ptr[i] = 0xFF; + } + m_ptr[sz] = MAGICBYTE(p); + return (Void_t*)m_ptr; +} + +/* Convert a pointer to be free()d or realloc()ed to a valid chunk + pointer. If the provided pointer is not valid, return NULL. */ + +static mchunkptr +internal_function +#if __STD_C +mem2chunk_check(Void_t* mem) +#else +mem2chunk_check(mem) Void_t* mem; +#endif +{ + mchunkptr p; + INTERNAL_SIZE_T sz, c; + unsigned char magic; + + p = mem2chunk(mem); + if(!aligned_OK(p)) return NULL; + if( (char*)p>=mp_.sbrk_base && + (char*)p<(mp_.sbrk_base+main_arena.system_mem) ) { + /* Must be a chunk in conventional heap memory. */ + if(chunk_is_mmapped(p) || + ( (sz = chunksize(p)), + ((char*)p + sz)>=(mp_.sbrk_base+main_arena.system_mem) ) || + szprev_size&MALLOC_ALIGN_MASK || + (long)prev_chunk(p)<(long)mp_.sbrk_base || + next_chunk(prev_chunk(p))!=p) )) + return NULL; + magic = MAGICBYTE(p); + for(sz += SIZE_SZ-1; (c = ((unsigned char*)p)[sz]) != magic; sz -= c) { + if(c<=0 || sz<(c+2*SIZE_SZ)) return NULL; + } + ((unsigned char*)p)[sz] ^= 0xFF; + } else { + unsigned long offset, page_mask = malloc_getpagesize-1; + + /* mmap()ed chunks have MALLOC_ALIGNMENT or higher power-of-two + alignment relative to the beginning of a page. Check this + first. */ + offset = (unsigned long)mem & page_mask; + if((offset!=MALLOC_ALIGNMENT && offset!=0 && offset!=0x10 && + offset!=0x20 && offset!=0x40 && offset!=0x80 && offset!=0x100 && + offset!=0x200 && offset!=0x400 && offset!=0x800 && offset!=0x1000 && + offset<0x2000) || + !chunk_is_mmapped(p) || (p->size & PREV_INUSE) || + ( (((unsigned long)p - p->prev_size) & page_mask) != 0 ) || + ( (sz = chunksize(p)), ((p->prev_size + sz) & page_mask) != 0 ) ) + return NULL; + magic = MAGICBYTE(p); + for(sz -= 1; (c = ((unsigned char*)p)[sz]) != magic; sz -= c) { + if(c<=0 || sz<(c+2*SIZE_SZ)) return NULL; + } + ((unsigned char*)p)[sz] ^= 0xFF; + } + return p; +} + +/* Check for corruption of the top chunk, and try to recover if + necessary. */ + +static int +internal_function +#if __STD_C +top_check(void) +#else +top_check() +#endif +{ + mchunkptr t = top(&main_arena); + char* brk, * new_brk; + INTERNAL_SIZE_T front_misalign, sbrk_size; + unsigned long pagesz = malloc_getpagesize; + + if((char*)t + chunksize(t) == mp_.sbrk_base + main_arena.system_mem || + t == initial_top(&main_arena)) return 0; + + if(check_action & 1) + fprintf(stderr, "malloc: top chunk is corrupt\n"); + if(check_action & 2) + abort(); + + /* Try to set up a new top chunk. */ + brk = MORECORE(0); + front_misalign = (unsigned long)chunk2mem(brk) & MALLOC_ALIGN_MASK; + if (front_misalign > 0) + front_misalign = MALLOC_ALIGNMENT - front_misalign; + sbrk_size = front_misalign + mp_.top_pad + MINSIZE; + sbrk_size += pagesz - ((unsigned long)(brk + sbrk_size) & (pagesz - 1)); + new_brk = (char*)(MORECORE (sbrk_size)); + if (new_brk == (char*)(MORECORE_FAILURE)) return -1; + /* Call the `morecore' hook if necessary. */ + if (__after_morecore_hook) + (*__after_morecore_hook) (); + main_arena.system_mem = (new_brk - mp_.sbrk_base) + sbrk_size; + + top(&main_arena) = (mchunkptr)(brk + front_misalign); + set_head(top(&main_arena), (sbrk_size - front_misalign) | PREV_INUSE); + + return 0; +} + +static Void_t* +#if __STD_C +malloc_check(size_t sz, const Void_t *caller) +#else +malloc_check(sz, caller) size_t sz; const Void_t *caller; +#endif +{ + Void_t *victim; + + (void)mutex_lock(&main_arena.mutex); + victim = (top_check() >= 0) ? _int_malloc(&main_arena, sz+1) : NULL; + (void)mutex_unlock(&main_arena.mutex); + return mem2mem_check(victim, sz); +} + +static void +#if __STD_C +free_check(Void_t* mem, const Void_t *caller) +#else +free_check(mem, caller) Void_t* mem; const Void_t *caller; +#endif +{ + mchunkptr p; + + if(!mem) return; + (void)mutex_lock(&main_arena.mutex); + p = mem2chunk_check(mem); + if(!p) { + (void)mutex_unlock(&main_arena.mutex); + if(check_action & 1) + fprintf(stderr, "free(): invalid pointer %p!\n", mem); + if(check_action & 2) + abort(); + return; + } +#if HAVE_MMAP + if (chunk_is_mmapped(p)) { + (void)mutex_unlock(&main_arena.mutex); + munmap_chunk(p); + return; + } +#endif +#if 0 /* Erase freed memory. */ + memset(mem, 0, chunksize(p) - (SIZE_SZ+1)); +#endif + _int_free(&main_arena, mem); + (void)mutex_unlock(&main_arena.mutex); +} + +static Void_t* +#if __STD_C +realloc_check(Void_t* oldmem, size_t bytes, const Void_t *caller) +#else +realloc_check(oldmem, bytes, caller) + Void_t* oldmem; size_t bytes; const Void_t *caller; +#endif +{ + mchunkptr oldp, newp = 0; + INTERNAL_SIZE_T nb, oldsize; + Void_t* newmem = 0; + + if (oldmem == 0) return malloc_check(bytes, NULL); + (void)mutex_lock(&main_arena.mutex); + oldp = mem2chunk_check(oldmem); + (void)mutex_unlock(&main_arena.mutex); + if(!oldp) { + if(check_action & 1) + fprintf(stderr, "realloc(): invalid pointer %p!\n", oldmem); + if(check_action & 2) + abort(); + return malloc_check(bytes, NULL); + } + oldsize = chunksize(oldp); + + checked_request2size(bytes+1, nb); + (void)mutex_lock(&main_arena.mutex); + +#if HAVE_MMAP + if (chunk_is_mmapped(oldp)) { +#if HAVE_MREMAP + newp = mremap_chunk(oldp, nb); + if(!newp) { +#endif + /* Note the extra SIZE_SZ overhead. */ + if(oldsize - SIZE_SZ >= nb) + newmem = oldmem; /* do nothing */ + else { + /* Must alloc, copy, free. */ + if (top_check() >= 0) + newmem = _int_malloc(&main_arena, bytes+1); + if (newmem) { + MALLOC_COPY(BOUNDED_N(newmem, bytes+1), oldmem, oldsize - 2*SIZE_SZ); + munmap_chunk(oldp); + } + } +#if HAVE_MREMAP + } +#endif + } else { +#endif /* HAVE_MMAP */ + if (top_check() >= 0) + newmem = _int_realloc(&main_arena, oldmem, bytes+1); +#if 0 /* Erase freed memory. */ + if(newmem) + newp = mem2chunk(newmem); + nb = chunksize(newp); + if(oldp=chunk_at_offset(newp, nb)) { + memset((char*)oldmem + 2*sizeof(mbinptr), 0, + oldsize - (2*sizeof(mbinptr)+2*SIZE_SZ+1)); + } else if(nb > oldsize+SIZE_SZ) { + memset((char*)BOUNDED_N(chunk2mem(newp), bytes) + oldsize, + 0, nb - (oldsize+SIZE_SZ)); + } +#endif +#if HAVE_MMAP + } +#endif + (void)mutex_unlock(&main_arena.mutex); + + return mem2mem_check(newmem, bytes); +} + +static Void_t* +#if __STD_C +memalign_check(size_t alignment, size_t bytes, const Void_t *caller) +#else +memalign_check(alignment, bytes, caller) + size_t alignment; size_t bytes; const Void_t *caller; +#endif +{ + INTERNAL_SIZE_T nb; + Void_t* mem; + + if (alignment <= MALLOC_ALIGNMENT) return malloc_check(bytes, NULL); + if (alignment < MINSIZE) alignment = MINSIZE; + + checked_request2size(bytes+1, nb); + (void)mutex_lock(&main_arena.mutex); + mem = (top_check() >= 0) ? _int_memalign(&main_arena, alignment, bytes+1) : + NULL; + (void)mutex_unlock(&main_arena.mutex); + return mem2mem_check(mem, bytes); +} + +#ifndef NO_THREADS + +/* The following hooks are used when the global initialization in + ptmalloc_init() hasn't completed yet. */ + +static Void_t* +#if __STD_C +malloc_starter(size_t sz, const Void_t *caller) +#else +malloc_starter(sz, caller) size_t sz; const Void_t *caller; +#endif +{ + Void_t* victim; + + victim = _int_malloc(&main_arena, sz); + + return victim ? BOUNDED_N(victim, sz) : 0; +} + +static void +#if __STD_C +free_starter(Void_t* mem, const Void_t *caller) +#else +free_starter(mem, caller) Void_t* mem; const Void_t *caller; +#endif +{ + mchunkptr p; + + if(!mem) return; + p = mem2chunk(mem); +#if HAVE_MMAP + if (chunk_is_mmapped(p)) { + munmap_chunk(p); + return; + } +#endif + _int_free(&main_arena, mem); +} + +#endif /* NO_THREADS */ + + +/* Get/set state: malloc_get_state() records the current state of all + malloc variables (_except_ for the actual heap contents and `hook' + function pointers) in a system dependent, opaque data structure. + This data structure is dynamically allocated and can be free()d + after use. malloc_set_state() restores the state of all malloc + variables to the previously obtained state. This is especially + useful when using this malloc as part of a shared library, and when + the heap contents are saved/restored via some other method. The + primary example for this is GNU Emacs with its `dumping' procedure. + `Hook' function pointers are never saved or restored by these + functions, with two exceptions: If malloc checking was in use when + malloc_get_state() was called, then malloc_set_state() calls + __malloc_check_init() if possible; if malloc checking was not in + use in the recorded state but the user requested malloc checking, + then the hooks are reset to 0. */ + +#define MALLOC_STATE_MAGIC 0x444c4541l +#define MALLOC_STATE_VERSION (0*0x100l + 2l) /* major*0x100 + minor */ + +struct malloc_save_state { + long magic; + long version; + mbinptr av[NBINS * 2 + 2]; + char* sbrk_base; + int sbrked_mem_bytes; + unsigned long trim_threshold; + unsigned long top_pad; + unsigned int n_mmaps_max; + unsigned long mmap_threshold; + int check_action; + unsigned long max_sbrked_mem; + unsigned long max_total_mem; + unsigned int n_mmaps; + unsigned int max_n_mmaps; + unsigned long mmapped_mem; + unsigned long max_mmapped_mem; + int using_malloc_checking; +}; + +Void_t* +public_gET_STATe(void) +{ + struct malloc_save_state* ms; + int i; + mbinptr b; + + ms = (struct malloc_save_state*)public_mALLOc(sizeof(*ms)); + if (!ms) + return 0; + (void)mutex_lock(&main_arena.mutex); + malloc_consolidate(&main_arena); + ms->magic = MALLOC_STATE_MAGIC; + ms->version = MALLOC_STATE_VERSION; + ms->av[0] = 0; + ms->av[1] = 0; /* used to be binblocks, now no longer used */ + ms->av[2] = top(&main_arena); + ms->av[3] = 0; /* used to be undefined */ + for(i=1; iav[2*i+2] = ms->av[2*i+3] = 0; /* empty bin */ + else { + ms->av[2*i+2] = first(b); + ms->av[2*i+3] = last(b); + } + } + ms->sbrk_base = mp_.sbrk_base; + ms->sbrked_mem_bytes = main_arena.system_mem; + ms->trim_threshold = mp_.trim_threshold; + ms->top_pad = mp_.top_pad; + ms->n_mmaps_max = mp_.n_mmaps_max; + ms->mmap_threshold = mp_.mmap_threshold; + ms->check_action = check_action; + ms->max_sbrked_mem = main_arena.max_system_mem; +#ifdef NO_THREADS + ms->max_total_mem = max_total_mem; +#else + ms->max_total_mem = 0; +#endif + ms->n_mmaps = mp_.n_mmaps; + ms->max_n_mmaps = mp_.max_n_mmaps; + ms->mmapped_mem = mp_.mmapped_mem; + ms->max_mmapped_mem = mp_.max_mmapped_mem; + ms->using_malloc_checking = using_malloc_checking; + (void)mutex_unlock(&main_arena.mutex); + return (Void_t*)ms; +} + +int +public_sET_STATe(Void_t* msptr) +{ + struct malloc_save_state* ms = (struct malloc_save_state*)msptr; + int i; + mbinptr b; + + disallow_malloc_check = 1; + ptmalloc_init(); + if(ms->magic != MALLOC_STATE_MAGIC) return -1; + /* Must fail if the major version is too high. */ + if((ms->version & ~0xffl) > (MALLOC_STATE_VERSION & ~0xffl)) return -2; + (void)mutex_lock(&main_arena.mutex); + /* There are no fastchunks. */ + clear_fastchunks(&main_arena); + set_max_fast(&main_arena, DEFAULT_MXFAST); + for (i=0; iav[2]; + main_arena.last_remainder = 0; + for(i=1; iav[2*i+2] == 0) { + assert(ms->av[2*i+3] == 0); + first(b) = last(b) = b; + } else { + if(iav[2*i+2]))==i && + largebin_index(chunksize(ms->av[2*i+3]))==i)) { + first(b) = ms->av[2*i+2]; + last(b) = ms->av[2*i+3]; + /* Make sure the links to the bins within the heap are correct. */ + first(b)->bk = b; + last(b)->fd = b; + /* Set bit in binblocks. */ + mark_bin(&main_arena, i); + } else { + /* Oops, index computation from chunksize must have changed. + Link the whole list into unsorted_chunks. */ + first(b) = last(b) = b; + b = unsorted_chunks(&main_arena); + ms->av[2*i+2]->bk = b; + ms->av[2*i+3]->fd = b->fd; + b->fd->bk = ms->av[2*i+3]; + b->fd = ms->av[2*i+2]; + } + } + } + mp_.sbrk_base = ms->sbrk_base; + main_arena.system_mem = ms->sbrked_mem_bytes; + mp_.trim_threshold = ms->trim_threshold; + mp_.top_pad = ms->top_pad; + mp_.n_mmaps_max = ms->n_mmaps_max; + mp_.mmap_threshold = ms->mmap_threshold; + check_action = ms->check_action; + main_arena.max_system_mem = ms->max_sbrked_mem; +#ifdef NO_THREADS + mp_.max_total_mem = ms->max_total_mem; +#endif + mp_.n_mmaps = ms->n_mmaps; + mp_.max_n_mmaps = ms->max_n_mmaps; + mp_.mmapped_mem = ms->mmapped_mem; + mp_.max_mmapped_mem = ms->max_mmapped_mem; + /* add version-dependent code here */ + if (ms->version >= 1) { + /* Check whether it is safe to enable malloc checking, or whether + it is necessary to disable it. */ + if (ms->using_malloc_checking && !using_malloc_checking && + !disallow_malloc_check) + __malloc_check_init (); + else if (!ms->using_malloc_checking && using_malloc_checking) { + __malloc_hook = 0; + __free_hook = 0; + __realloc_hook = 0; + __memalign_hook = 0; + using_malloc_checking = 0; + } + } + check_malloc_state(&main_arena); + + (void)mutex_unlock(&main_arena.mutex); + return 0; +} + +/* + * Local variables: + * c-basic-offset: 2 + * End: + */ diff --git a/malloc/malloc.c b/malloc/malloc.c index 8279ddaf22..e663f84707 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1,32 +1,47 @@ /* Malloc implementation for multiple threads without lock contention. Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc. This file is part of the GNU C Library. - Contributed by Wolfram Gloger - and Doug Lea , 1996. + Contributed by Wolfram Gloger + and Doug Lea , 2001. The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + Library General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ -/* $Id$ +/* + This is a version (aka ptmalloc2) of malloc/free/realloc written by + Doug Lea and adapted to multiple threads/arenas by Wolfram Gloger. + +* Version ptmalloc2-20011215 + $Id$ + based on: + VERSION 2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee) - This work is mainly derived from malloc-2.6.4 by Doug Lea - , which is available from: + Note: There may be an updated version of this malloc obtainable at + http://www.malloc.de/malloc/ptmalloc2.tar.gz + Check before installing! - ftp://g.oswego.edu/pub/misc/malloc.c +* Quickstart - Most of the original comments are reproduced in the code below. + In order to compile this implementation, a Makefile is provided with + the ptmalloc2 distribution, which has pre-defined targets for some + popular systems (e.g. "make posix" for Posix threads). All that is + typically required with regard to compiler flags is the selection of + the thread package via defining one out of USE_PTHREADS, USE_THR or + USE_SPROC. Check the thread-m.h file for what effects this has. + Many/most systems will additionally require USE_TSD_DATA_HACK to be + defined, so this is the default for "make posix". * Why use this malloc? @@ -34,85 +49,62 @@ most tunable malloc ever written. However it is among the fastest while also being among the most space-conserving, portable and tunable. Consistent balance across these factors results in a good general-purpose - allocator. For a high-level description, see - http://g.oswego.edu/dl/html/malloc.html - - On many systems, the standard malloc implementation is by itself not - thread-safe, and therefore wrapped with a single global lock around - all malloc-related functions. In some applications, especially with - multiple available processors, this can lead to contention problems - and bad performance. This malloc version was designed with the goal - to avoid waiting for locks as much as possible. Statistics indicate - that this goal is achieved in many cases. - -* Synopsis of public routines - - (Much fuller descriptions are contained in the program documentation below.) - - ptmalloc_init(); - Initialize global configuration. When compiled for multiple threads, - this function must be called once before any other function in the - package. It is not required otherwise. It is called automatically - in the Linux/GNU C libray or when compiling with MALLOC_HOOKS. - malloc(size_t n); - Return a pointer to a newly allocated chunk of at least n bytes, or null - if no space is available. - free(Void_t* p); - Release the chunk of memory pointed to by p, or no effect if p is null. - realloc(Void_t* p, size_t n); - Return a pointer to a chunk of size n that contains the same data - as does chunk p up to the minimum of (n, p's size) bytes, or null - if no space is available. The returned pointer may or may not be - the same as p. If p is null, equivalent to malloc. Unless the - #define REALLOC_ZERO_BYTES_FREES below is set, realloc with a - size argument of zero (re)allocates a minimum-sized chunk. - memalign(size_t alignment, size_t n); - Return a pointer to a newly allocated chunk of n bytes, aligned - in accord with the alignment argument, which must be a power of - two. - valloc(size_t n); - Equivalent to memalign(pagesize, n), where pagesize is the page - size of the system (or as near to this as can be figured out from - all the includes/defines below.) - pvalloc(size_t n); - Equivalent to valloc(minimum-page-that-holds(n)), that is, - round up n to nearest pagesize. - calloc(size_t unit, size_t quantity); - Returns a pointer to quantity * unit bytes, with all locations - set to zero. - cfree(Void_t* p); - Equivalent to free(p). - malloc_trim(size_t pad); - Release all but pad bytes of freed top-most memory back - to the system. Return 1 if successful, else 0. - malloc_usable_size(Void_t* p); - Report the number usable allocated bytes associated with allocated - chunk p. This may or may not report more bytes than were requested, - due to alignment and minimum size constraints. - malloc_stats(); - Prints brief summary statistics on stderr. - mallinfo() - Returns (by copy) a struct containing various summary statistics. - mallopt(int parameter_number, int parameter_value) - Changes one of the tunable parameters described below. Returns - 1 if successful in changing the parameter, else 0. + allocator for malloc-intensive programs. + + The main properties of the algorithms are: + * For large (>= 512 bytes) requests, it is a pure best-fit allocator, + with ties normally decided via FIFO (i.e. least recently used). + * For small (<= 64 bytes by default) requests, it is a caching + allocator, that maintains pools of quickly recycled chunks. + * In between, and for combinations of large and small requests, it does + the best it can trying to meet both goals at once. + * For very large requests (>= 128KB by default), it relies on system + memory mapping facilities, if supported. + + For a longer but slightly out of date high-level description, see + http://gee.cs.oswego.edu/dl/html/malloc.html + + You may already by default be using a C library containing a malloc + that is based on some version of this malloc (for example in + linux). You might still want to use the one in this file in order to + customize settings or to avoid overheads associated with library + versions. + +* Contents, described in more detail in "description of public routines" below. + + Standard (ANSI/SVID/...) functions: + malloc(size_t n); + calloc(size_t n_elements, size_t element_size); + free(Void_t* p); + realloc(Void_t* p, size_t n); + memalign(size_t alignment, size_t n); + valloc(size_t n); + mallinfo() + mallopt(int parameter_number, int parameter_value) + + Additional functions: + independent_calloc(size_t n_elements, size_t size, Void_t* chunks[]); + independent_comalloc(size_t n_elements, size_t sizes[], Void_t* chunks[]); + pvalloc(size_t n); + cfree(Void_t* p); + malloc_trim(size_t pad); + malloc_usable_size(Void_t* p); + malloc_stats(); * Vital statistics: - Alignment: 8-byte - 8 byte alignment is currently hardwired into the design. This - seems to suffice for all current machines and C compilers. - - Assumed pointer representation: 4 or 8 bytes - Code for 8-byte pointers is untested by me but has worked - reliably by Wolfram Gloger, who contributed most of the - changes supporting this. - - Assumed size_t representation: 4 or 8 bytes + Supported pointer representation: 4 or 8 bytes + Supported size_t representation: 4 or 8 bytes Note that size_t is allowed to be 4 bytes even if pointers are 8. + You can adjust this by defining INTERNAL_SIZE_T + + Alignment: 2 * sizeof(size_t) (default) + (i.e., 8 byte alignment with 4byte size_t). This suffices for + nearly all current machines and C compilers. However, you can + define MALLOC_ALIGNMENT to be wider than this if necessary. - Minimum overhead per allocated chunk: 4 or 8 bytes - Each malloced chunk has a hidden overhead of 4 bytes holding size + Minimum overhead per allocated chunk: 4 or 8 bytes + Each malloced chunk has a hidden word of overhead holding size and status information. Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead) @@ -120,182 +112,136 @@ When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte ptrs but 4 byte size) or 24 (for 8/8) additional bytes are - needed; 4 (8) for a trailing size field - and 8 (16) bytes for free list pointers. Thus, the minimum - allocatable size is 16/24/32 bytes. + needed; 4 (8) for a trailing size field and 8 (16) bytes for + free list pointers. Thus, the minimum allocatable size is + 16/24/32 bytes. Even a request for zero bytes (i.e., malloc(0)) returns a pointer to something of the minimum allocatable size. - Maximum allocated size: 4-byte size_t: 2^31 - 8 bytes - 8-byte size_t: 2^63 - 16 bytes + The maximum overhead wastage (i.e., number of extra bytes + allocated than were requested in malloc) is less than or equal + to the minimum size, except for requests >= mmap_threshold that + are serviced via mmap(), where the worst case wastage is 2 * + sizeof(size_t) bytes plus the remainder from a system page (the + minimal mmap unit); typically 4096 or 8192 bytes. - It is assumed that (possibly signed) size_t bit values suffice to + Maximum allocated size: 4-byte size_t: 2^32 minus about two pages + 8-byte size_t: 2^64 minus about two pages + + It is assumed that (possibly signed) size_t values suffice to represent chunk sizes. `Possibly signed' is due to the fact that `size_t' may be defined on a system as either a signed or - an unsigned type. To be conservative, values that would appear - as negative numbers are avoided. - Requests for sizes with a negative sign bit will return a - minimum-sized chunk. - - Maximum overhead wastage per allocated chunk: normally 15 bytes - - Alignment demands, plus the minimum allocatable size restriction - make the normal worst-case wastage 15 bytes (i.e., up to 15 - more bytes will be allocated than were requested in malloc), with - two exceptions: - 1. Because requests for zero bytes allocate non-zero space, - the worst case wastage for a request of zero bytes is 24 bytes. - 2. For requests >= mmap_threshold that are serviced via - mmap(), the worst case wastage is 8 bytes plus the remainder - from a system page (the minimal mmap unit); typically 4096 bytes. - -* Limitations - - Here are some features that are NOT currently supported - - * No automated mechanism for fully checking that all accesses - to malloced memory stay within their bounds. - * No support for compaction. + an unsigned type. The ISO C standard says that it must be + unsigned, but a few systems are known not to adhere to this. + Additionally, even when size_t is unsigned, sbrk (which is by + default used to obtain memory from system) accepts signed + arguments, and may not be able to handle size_t-wide arguments + with negative sign bit. Generally, values that would + appear as negative after accounting for overhead and alignment + are supported only via mmap(), which does not have this + limitation. + + Requests for sizes outside the allowed range will perform an optional + failure action and then return null. (Requests may also + also fail because a system is out of memory.) + + Thread-safety: thread-safe unless NO_THREADS is defined + + Compliance: I believe it is compliant with the 1997 Single Unix Specification + (See http://www.opennc.org). Also SVID/XPG, ANSI C, and probably + others as well. * Synopsis of compile-time options: People have reported using previous versions of this malloc on all versions of Unix, sometimes by tweaking some of the defines below. It has been tested most extensively on Solaris and - Linux. People have also reported adapting this malloc for use in - stand-alone embedded systems. - - The implementation is in straight, hand-tuned ANSI C. Among other - consequences, it uses a lot of macros. Because of this, to be at - all usable, this code should be compiled using an optimizing compiler - (for example gcc -O2) that can simplify expressions and control - paths. - - __STD_C (default: derived from C compiler defines) - Nonzero if using ANSI-standard C compiler, a C++ compiler, or - a C compiler sufficiently close to ANSI to get away with it. - MALLOC_DEBUG (default: NOT defined) - Define to enable debugging. Adds fairly extensive assertion-based - checking to help track down memory errors, but noticeably slows down - execution. - MALLOC_HOOKS (default: NOT defined) - Define to enable support run-time replacement of the allocation - functions through user-defined `hooks'. - REALLOC_ZERO_BYTES_FREES (default: defined) - Define this if you think that realloc(p, 0) should be equivalent - to free(p). (The C standard requires this behaviour, therefore - it is the default.) Otherwise, since malloc returns a unique - pointer for malloc(0), so does realloc(p, 0). - HAVE_MEMCPY (default: defined) - Define if you are not otherwise using ANSI STD C, but still - have memcpy and memset in your C library and want to use them. - Otherwise, simple internal versions are supplied. - USE_MEMCPY (default: 1 if HAVE_MEMCPY is defined, 0 otherwise) - Define as 1 if you want the C library versions of memset and - memcpy called in realloc and calloc (otherwise macro versions are used). - At least on some platforms, the simple macro versions usually - outperform libc versions. - HAVE_MMAP (default: defined as 1) - Define to non-zero to optionally make malloc() use mmap() to - allocate very large blocks. - HAVE_MREMAP (default: defined as 0 unless Linux libc set) - Define to non-zero to optionally make realloc() use mremap() to - reallocate very large blocks. - USE_ARENAS (default: the same as HAVE_MMAP) - Enable support for multiple arenas, allocated using mmap(). - malloc_getpagesize (default: derived from system #includes) - Either a constant or routine call returning the system page size. - HAVE_USR_INCLUDE_MALLOC_H (default: NOT defined) - Optionally define if you are on a system with a /usr/include/malloc.h - that declares struct mallinfo. It is not at all necessary to - define this even if you do, but will ensure consistency. - INTERNAL_SIZE_T (default: size_t) - Define to a 32-bit type (probably `unsigned int') if you are on a - 64-bit machine, yet do not want or need to allow malloc requests of - greater than 2^31 to be handled. This saves space, especially for - very small chunks. - _LIBC (default: NOT defined) - Defined only when compiled as part of the Linux libc/glibc. - Also note that there is some odd internal name-mangling via defines - (for example, internally, `malloc' is named `mALLOc') needed - when compiling in this case. These look funny but don't otherwise - affect anything. - LACKS_UNISTD_H (default: undefined) - Define this if your system does not have a . - MORECORE (default: sbrk) - The name of the routine to call to obtain more memory from the system. - MORECORE_FAILURE (default: -1) - The value returned upon failure of MORECORE. - MORECORE_CLEARS (default 1) - The degree to which the routine mapped to MORECORE zeroes out - memory: never (0), only for newly allocated space (1) or always - (2). The distinction between (1) and (2) is necessary because on - some systems, if the application first decrements and then - increments the break value, the contents of the reallocated space - are unspecified. - DEFAULT_TRIM_THRESHOLD - DEFAULT_TOP_PAD - DEFAULT_MMAP_THRESHOLD - DEFAULT_MMAP_MAX - Default values of tunable parameters (described in detail below) - controlling interaction with host system routines (sbrk, mmap, etc). - These values may also be changed dynamically via mallopt(). The - preset defaults are those that give best performance for typical - programs/systems. - DEFAULT_CHECK_ACTION - When the standard debugging hooks are in place, and a pointer is - detected as corrupt, do nothing (0), print an error message (1), - or call abort() (2). - - -*/ + Linux. It is also reported to work on WIN32 platforms. + People also report using it in stand-alone embedded systems. + + The implementation is in straight, hand-tuned ANSI C. It is not + at all modular. (Sorry!) It uses a lot of macros. To be at all + usable, this code should be compiled using an optimizing compiler + (for example gcc -O3) that can simplify expressions and control + paths. (FAQ: some macros import variables as arguments rather than + declare locals because people reported that some debuggers + otherwise get confused.) + + OPTION DEFAULT VALUE + + Compilation Environment options: + + __STD_C derived from C compiler defines + WIN32 NOT defined + HAVE_MEMCPY defined + USE_MEMCPY 1 if HAVE_MEMCPY is defined + HAVE_MMAP defined as 1 + MMAP_CLEARS 1 + HAVE_MREMAP 0 unless linux defined + USE_ARENAS the same as HAVE_MMAP + malloc_getpagesize derived from system #includes, or 4096 if not + HAVE_USR_INCLUDE_MALLOC_H NOT defined + LACKS_UNISTD_H NOT defined unless WIN32 + LACKS_SYS_PARAM_H NOT defined unless WIN32 + LACKS_SYS_MMAN_H NOT defined unless WIN32 + + Changing default word sizes: + + INTERNAL_SIZE_T size_t + MALLOC_ALIGNMENT 2 * sizeof(INTERNAL_SIZE_T) + + Configuration and functionality options: + + USE_DL_PREFIX NOT defined + USE_PUBLIC_MALLOC_WRAPPERS NOT defined + USE_MALLOC_LOCK NOT defined + MALLOC_DEBUG NOT defined + REALLOC_ZERO_BYTES_FREES 1 + MALLOC_FAILURE_ACTION errno = ENOMEM, if __STD_C defined, else no-op + TRIM_FASTBINS 0 + + Options for customizing MORECORE: + + MORECORE sbrk + MORECORE_FAILURE -1 + MORECORE_CONTIGUOUS 1 + MORECORE_CANNOT_TRIM NOT defined + MORECORE_CLEARS 1 + MMAP_AS_MORECORE_SIZE (1024 * 1024) + + Tuning options that are also dynamically changeable via mallopt: + + DEFAULT_MXFAST 64 + DEFAULT_TRIM_THRESHOLD 128 * 1024 + DEFAULT_TOP_PAD 0 + DEFAULT_MMAP_THRESHOLD 128 * 1024 + DEFAULT_MMAP_MAX 65536 + + There are several other #defined constants and macros that you + probably don't want to touch unless you are extending or adapting malloc. */ /* - -* Compile-time options for multiple threads: - - USE_PTHREADS, USE_THR, USE_SPROC - Define one of these as 1 to select the thread interface: - POSIX threads, Solaris threads or SGI sproc's, respectively. - If none of these is defined as non-zero, you get a `normal' - malloc implementation which is not thread-safe. Support for - multiple threads requires HAVE_MMAP=1. As an exception, when - compiling for GNU libc, i.e. when _LIBC is defined, then none of - the USE_... symbols have to be defined. - - HEAP_MIN_SIZE - HEAP_MAX_SIZE - When thread support is enabled, additional `heap's a