write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
-/* V2.6.4-pt3 Thu Feb 20 1997
+/* $Id$
This work is mainly derived from malloc-2.6.4 by Doug Lea
<dl@cs.oswego.edu>, which is available from:
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)
# endif
#else
# include <sys/types.h>
+# if defined _LIBC || defined MALLOC_HOOKS
+extern char* getenv();
+# endif
#endif
#ifndef _LIBC
#endif
/*
- Define HAVE_MMAP to optionally make malloc() use mmap() to
- allocate very large blocks. These will be returned to the
- operating system immediately after a free().
+ Define HAVE_MMAP to optionally make malloc() use mmap() to allocate
+ very large blocks. These will be returned to the operating system
+ immediately after a free(). HAVE_MMAP is also a prerequisite to
+ support multiple `arenas' (see USE_ARENAS below).
*/
#ifndef HAVE_MMAP
#define HAVE_MREMAP defined(__linux__) && !defined(__arm__)
#endif
+/* Define USE_ARENAS to enable support for multiple `arenas'. These
+ are allocated using mmap(), are necessary for threads and
+ occasionally useful to overcome address space limitations affecting
+ sbrk(). */
+
+#ifndef USE_ARENAS
+#define USE_ARENAS HAVE_MMAP
+#endif
+
#if HAVE_MMAP
#include <unistd.h>
#ifdef __cplusplus
-}; /* end of extern "C" */
+} /* end of extern "C" */
#endif
#if !defined(NO_THREADS) && !HAVE_MMAP
"Can't have threads support without mmap"
#endif
+#if USE_ARENAS && !HAVE_MMAP
+"Can't have multiple arenas without mmap"
+#endif
/*
/* 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 for
- multiple threads. */
+ address aligned to HEAP_MAX_SIZE. Not used unless compiling with
+ USE_ARENAS. */
typedef struct _heap_info {
arena *ar_ptr; /* Arena for this heap. */
static mchunkptr chunk_align(arena *ar_ptr, INTERNAL_SIZE_T nb,
size_t alignment) internal_function;
static int main_trim(size_t pad) internal_function;
-#ifndef NO_THREADS
+#if USE_ARENAS
static int heap_trim(heap_info *heap, size_t pad) internal_function;
#endif
#if defined _LIBC || defined MALLOC_HOOKS
static mchunkptr chunk_realloc();
static mchunkptr chunk_align();
static int main_trim();
-#ifndef NO_THREADS
+#if USE_ARENAS
static int heap_trim();
#endif
#if defined _LIBC || defined MALLOC_HOOKS
/* Thread specific data */
-#ifndef NO_THREADS
static tsd_key_t arena_key;
static mutex_t list_lock = MUTEX_INITIALIZER;
-#endif
#if THREAD_STATS
static int stat_n_heaps = 0;
static unsigned long mmapped_mem = 0;
static unsigned long max_mmapped_mem = 0;
+/* Mapped memory in non-main arenas (reliable only for NO_THREADS). */
+static unsigned long arena_mem = 0;
+
\f
#ifndef _LIBC
#endif
{
#if defined _LIBC || defined MALLOC_HOOKS
+# if __STD_C
const char* s;
+# else
+ char* s;
+# endif
#endif
if(__malloc_initialized >= 0) return;
if (__pthread_initialize != NULL)
__pthread_initialize();
#endif
+#endif /* !defined NO_THREADS */
mutex_init(&main_arena.mutex);
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_init_all);
-#endif /* !defined NO_THREADS */
#if defined _LIBC || defined MALLOC_HOOKS
if((s = __secure_getenv("MALLOC_TRIM_THRESHOLD_")))
mALLOPt(M_TRIM_THRESHOLD, atoi(s));
#if defined __GNUC__ && __GNUC__ >= 2
/* This function is only called from one place, inline it. */
-inline
+__inline__
#endif
static mchunkptr
internal_function
if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
max_mmapped_mem = mmapped_mem;
#ifdef NO_THREADS
- if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
- max_total_mem = mmapped_mem + sbrked_mem;
+ if ((unsigned long)(mmapped_mem + arena_mem + sbrked_mem) > max_total_mem)
+ max_total_mem = mmapped_mem + arena_mem + sbrked_mem;
#endif
return p;
}
if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
max_mmapped_mem = mmapped_mem;
#ifdef NO_THREADS
- if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
- max_total_mem = mmapped_mem + sbrked_mem;
+ if ((unsigned long)(mmapped_mem + arena_mem + sbrked_mem) > max_total_mem)
+ max_total_mem = mmapped_mem + arena_mem + sbrked_mem;
#endif
return p;
}
/* Managing heaps and arenas (for concurrent threads) */
-#ifndef NO_THREADS
+#if USE_ARENAS
/* Create a new heap. size is automatically rounded up to a multiple
of the page size. */
/* 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)
+ MAP_PRIVATE|MAP_FIXED) == (char *) MAP_FAILED)
return -2;
}
h->size = new_size;
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. */
+ 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; \
/* Nothing immediately available, so generate a new arena. */
h = new_heap(size + (sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT));
- if(!h)
- return 0;
+ if(!h) {
+ /* Maybe size is too large to fit in a single heap. So, just try
+ to create a minimally-sized arena and let chunk_alloc() attempt
+ to deal with the large request via mmap_chunk(). */
+ h = new_heap(sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT);
+ if(!h)
+ return 0;
+ }
a = h->ar_ptr = (arena *)(h+1);
for(i=0; i<NAV; i++)
init_bin(a, i);
a->next = NULL;
a->size = h->size;
+ arena_mem += h->size;
+#ifdef NO_THREADS
+ if((unsigned long)(mmapped_mem + arena_mem + sbrked_mem) > max_total_mem)
+ max_total_mem = mmapped_mem + arena_mem + sbrked_mem;
+#endif
tsd_setspecific(arena_key, (Void_t *)a);
mutex_init(&a->mutex);
i = mutex_lock(&a->mutex); /* remember result */
(((mchunkptr)(ptr) < top(&main_arena) && (char *)(ptr) >= sbrk_base) ? \
&main_arena : heap_for_ptr(ptr)->ar_ptr)
-#else /* defined(NO_THREADS) */
+#else /* !USE_ARENAS */
-/* Without concurrent threads, there is only one arena. */
+/* There is only one arena, main_arena. */
#define arena_get(ptr, sz) (ptr = &main_arena)
#define arena_for_ptr(ptr) (&main_arena)
-#endif /* !defined(NO_THREADS) */
+#endif /* USE_ARENAS */
\f
/* No checkable chunk is mmapped */
assert(!chunk_is_mmapped(p));
-#ifndef NO_THREADS
+#if USE_ARENAS
if(ar_ptr != &main_arena) {
heap_info *heap = heap_for_ptr(p);
assert(heap->ar_ptr == ar_ptr);
#if defined __GNUC__ && __GNUC__ >= 2
/* This function is called only from one place, inline it. */
-inline
+__inline__
#endif
static void
internal_function
INTERNAL_SIZE_T old_top_size = chunksize(old_top);
INTERNAL_SIZE_T top_size; /* new size of top chunk */
-#ifndef NO_THREADS
+#if USE_ARENAS
if(ar_ptr == &main_arena) {
#endif
if ((unsigned long)sbrked_mem > (unsigned long)max_sbrked_mem)
max_sbrked_mem = sbrked_mem;
#ifdef NO_THREADS
- if ((unsigned long)(mmapped_mem + sbrked_mem) >
- (unsigned long)max_total_mem)
- max_total_mem = mmapped_mem + sbrked_mem;
+ if ((unsigned long)(mmapped_mem + arena_mem + sbrked_mem) > max_total_mem)
+ max_total_mem = mmapped_mem + arena_mem + sbrked_mem;
#endif
-#ifndef NO_THREADS
+#if USE_ARENAS
} else { /* ar_ptr != &main_arena */
heap_info *old_heap, *heap;
size_t old_heap_size;
old_heap_size = old_heap->size;
if(grow_heap(old_heap, MINSIZE + nb - old_top_size) == 0) {
ar_ptr->size += old_heap->size - old_heap_size;
+ arena_mem += old_heap->size - old_heap_size;
+#ifdef NO_THREADS
+ if(mmapped_mem + arena_mem + sbrked_mem > max_total_mem)
+ max_total_mem = mmapped_mem + arena_mem + sbrked_mem;
+#endif
top_size = ((char *)old_heap + old_heap->size) - (char *)old_top;
set_head(old_top, top_size | PREV_INUSE);
return;
heap->ar_ptr = ar_ptr;
heap->prev = old_heap;
ar_ptr->size += heap->size;
+ arena_mem += heap->size;
+#ifdef NO_THREADS
+ if((unsigned long)(mmapped_mem + arena_mem + sbrked_mem) > max_total_mem)
+ max_total_mem = mmapped_mem + arena_mem + sbrked_mem;
+#endif
/* Set up the new top, so we can safely use chunk_free() below. */
top(ar_ptr) = chunk_at_offset(heap, sizeof(*heap));
top_size = heap->size - sizeof(*heap);
set_head(top(ar_ptr), top_size | PREV_INUSE);
}
-#endif /* !defined(NO_THREADS) */
+#endif /* USE_ARENAS */
/* We always land on a page boundary */
assert(((unsigned long)((char*)top(ar_ptr) + top_size) & (pagesz-1)) == 0);
if(!ar_ptr)
return 0;
victim = chunk_alloc(ar_ptr, nb);
- (void)mutex_unlock(&ar_ptr->mutex);
if(!victim) {
/* Maybe the failure is due to running out of mmapped areas. */
if(ar_ptr != &main_arena) {
+ (void)mutex_unlock(&ar_ptr->mutex);
(void)mutex_lock(&main_arena.mutex);
victim = chunk_alloc(&main_arena, nb);
(void)mutex_unlock(&main_arena.mutex);
+ } else {
+#if USE_ARENAS
+ /* ... or sbrk() has failed and there is still a chance to mmap() */
+ ar_ptr = arena_get2(ar_ptr->next ? ar_ptr : 0, nb);
+ (void)mutex_unlock(&main_arena.mutex);
+ if(ar_ptr) {
+ victim = chunk_alloc(ar_ptr, nb);
+ (void)mutex_unlock(&ar_ptr->mutex);
+ }
+#endif
}
if(!victim) return 0;
- }
+ } else
+ (void)mutex_unlock(&ar_ptr->mutex);
return chunk2mem(victim);
}
if (!(hd & PREV_INUSE)) /* consolidate backward */
{
prevsz = p->prev_size;
- p = chunk_at_offset(p, -prevsz);
+ p = chunk_at_offset(p, -(long)prevsz);
sz += prevsz;
unlink(p, bck, fwd);
}
set_head(p, sz | PREV_INUSE);
top(ar_ptr) = p;
-#ifndef NO_THREADS
+#if USE_ARENAS
if(ar_ptr == &main_arena) {
#endif
if ((unsigned long)(sz) >= (unsigned long)trim_threshold)
main_trim(top_pad);
-#ifndef NO_THREADS
+#if USE_ARENAS
} else {
heap_info *heap = heap_for_ptr(p);
if (!(hd & PREV_INUSE)) /* consolidate backward */
{
prevsz = p->prev_size;
- p = chunk_at_offset(p, -prevsz);
+ p = chunk_at_offset(p, -(long)prevsz);
sz += prevsz;
if (p->fd == last_remainder(ar_ptr)) /* keep as last_remainder */
if (!islr)
frontlink(ar_ptr, p, sz, idx, bck, fwd);
-#ifndef NO_THREADS
+#if USE_ARENAS
/* Check whether the heap containing top can go away now. */
if(next->size < MINSIZE &&
(unsigned long)sz > trim_threshold &&
(void)mutex_lock(&main_arena.mutex);
newp = chunk_alloc(&main_arena, nb);
(void)mutex_unlock(&main_arena.mutex);
+ } else {
+#if USE_ARENAS
+ /* ... or sbrk() has failed and there is still a chance to mmap() */
+ arena* ar_ptr2 = arena_get2(ar_ptr->next ? ar_ptr : 0, nb);
+ if(ar_ptr2) {
+ newp = chunk_alloc(ar_ptr2, nb);
+ (void)mutex_unlock(&ar_ptr2->mutex);
+ }
+#endif
}
if (newp == 0) /* propagate failure */
return 0;
(void)mutex_lock(&main_arena.mutex);
p = chunk_align(&main_arena, nb, alignment);
(void)mutex_unlock(&main_arena.mutex);
+ } else {
+#if USE_ARENAS
+ /* ... or sbrk() has failed and there is still a chance to mmap() */
+ ar_ptr = arena_get2(ar_ptr->next ? ar_ptr : 0, nb);
+ if(ar_ptr) {
+ p = chunk_align(ar_ptr, nb, alignment);
+ (void)mutex_unlock(&ar_ptr->mutex);
+ }
+#endif
}
if(!p) return 0;
}
if (p == 0)
return 0; /* propagate failure */
- m = chunk2mem(p);
+ m = (char*)chunk2mem(p);
if ((((unsigned long)(m)) % alignment) == 0) /* aligned */
{
this is always possible.
*/
- brk = (char*)mem2chunk(((unsigned long)(m + alignment - 1)) & -alignment);
+ brk = (char*)mem2chunk(((unsigned long)(m + alignment - 1)) &
+ -(long)alignment);
if ((long)(brk - (char*)(p)) < (long)MINSIZE) brk += alignment;
newp = (mchunkptr)brk;
oldtopsize = chunksize(top(ar_ptr));
#if MORECORE_CLEARS < 2
/* Only newly allocated memory is guaranteed to be cleared. */
- if (oldtopsize < sbrk_base + max_sbrked_mem - (char *)oldtop)
+ if (ar_ptr == &main_arena &&
+ oldtopsize < sbrk_base + max_sbrked_mem - (char *)oldtop)
oldtopsize = (sbrk_base + max_sbrked_mem - (char *)oldtop);
#endif
#endif
(void)mutex_lock(&main_arena.mutex);
p = chunk_alloc(&main_arena, sz);
(void)mutex_unlock(&main_arena.mutex);
+ } else {
+#if USE_ARENAS
+ /* ... or sbrk() has failed and there is still a chance to mmap() */
+ (void)mutex_lock(&main_arena.mutex);
+ ar_ptr = arena_get2(ar_ptr->next ? ar_ptr : 0, sz);
+ (void)mutex_unlock(&main_arena.mutex);
+ if(ar_ptr) {
+ p = chunk_alloc(ar_ptr, sz);
+ (void)mutex_unlock(&ar_ptr->mutex);
+ }
+#endif
}
if (p == 0) return 0;
}
void cfree(mem) Void_t *mem;
#endif
{
- free(mem);
+ fREe(mem);
}
#endif
return 1;
}
-#ifndef NO_THREADS
+#if USE_ARENAS
static int
internal_function
if(new_size + (HEAP_MAX_SIZE - prev_heap->size) < pad + MINSIZE + pagesz)
break;
ar_ptr->size -= heap->size;
+ arena_mem -= heap->size;
delete_heap(heap);
heap = prev_heap;
if(!prev_inuse(p)) { /* consolidate backward */
if(grow_heap(heap, -extra) != 0)
return 0;
ar_ptr->size -= extra;
+ arena_mem -= extra;
/* Success. Adjust top accordingly. */
set_head(top_chunk, (top_size - extra) | PREV_INUSE);
return 1;
}
-#endif
+#endif /* USE_ARENAS */
\f
(void)mutex_unlock(&ar_ptr->mutex);
}
-#if !defined(NO_THREADS) && MALLOC_DEBUG > 1
+#if USE_ARENAS && MALLOC_DEBUG > 1
/* Print the complete contents of a single heap to stderr. */
stat_lock_loop += ar_ptr->stat_lock_loop;
stat_lock_wait += ar_ptr->stat_lock_wait;
#endif
-#if !defined(NO_THREADS) && MALLOC_DEBUG > 1
+#if USE_ARENAS && MALLOC_DEBUG > 1
if(ar_ptr != &main_arena) {
heap_info *heap;
(void)mutex_lock(&ar_ptr->mutex);
case M_TOP_PAD:
top_pad = value; return 1;
case M_MMAP_THRESHOLD:
-#ifndef NO_THREADS
+#if USE_ARENAS
/* Forbid setting the threshold too high. */
if((unsigned long)value > HEAP_MAX_SIZE/2) return 0;
#endif
unsigned int max_n_mmaps;
unsigned long mmapped_mem;
unsigned long max_mmapped_mem;
- int using_malloc_checking;
+ int using_malloc_checking;
};
Void_t*
/* 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)
+ !disallow_malloc_check)
__malloc_check_init ();
else if (!ms->using_malloc_checking && using_malloc_checking) {
__malloc_hook = 0;
oldp = mem2chunk_check(oldmem);
if(!oldp) {
(void)mutex_unlock(&main_arena.mutex);
- if (check_action & 1)
+ if(check_action & 1)
fprintf(stderr, "realloc(): invalid pointer %p!\n", oldmem);
- if (check_action & 2)
+ if(check_action & 2)
abort();
return malloc_check(bytes, NULL);
}