Update.
[glibc.git] / malloc / arena.c
blob88efbf3ca74a9b19067ce920c9db3226d5e737cc
1 /* Malloc implementation for multiple threads without lock contention.
2 Copyright (C) 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Wolfram Gloger <wg@malloc.de>, 2001.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* $Id$ */
23 /* Compile-time constants. */
25 #define HEAP_MIN_SIZE (32*1024)
26 #ifndef HEAP_MAX_SIZE
27 #define HEAP_MAX_SIZE (1024*1024) /* must be a power of two */
28 #endif
30 /* HEAP_MIN_SIZE and HEAP_MAX_SIZE limit the size of mmap()ed heaps
31 that are dynamically created for multi-threaded programs. The
32 maximum size must be a power of two, for fast determination of
33 which heap belongs to a chunk. It should be much larger than the
34 mmap threshold, so that requests with a size just below that
35 threshold can be fulfilled without creating too many heaps. */
38 #ifndef THREAD_STATS
39 #define THREAD_STATS 0
40 #endif
42 /* If THREAD_STATS is non-zero, some statistics on mutex locking are
43 computed. */
45 /***************************************************************************/
47 #define top(ar_ptr) ((ar_ptr)->top)
49 /* A heap is a single contiguous memory region holding (coalesceable)
50 malloc_chunks. It is allocated with mmap() and always starts at an
51 address aligned to HEAP_MAX_SIZE. Not used unless compiling with
52 USE_ARENAS. */
54 typedef struct _heap_info {
55 mstate ar_ptr; /* Arena for this heap. */
56 struct _heap_info *prev; /* Previous heap. */
57 size_t size; /* Current size in bytes. */
58 size_t pad; /* Make sure the following data is properly aligned. */
59 } heap_info;
61 /* Thread specific data */
63 static tsd_key_t arena_key;
64 static mutex_t list_lock;
66 #if THREAD_STATS
67 static int stat_n_heaps;
68 #define THREAD_STAT(x) x
69 #else
70 #define THREAD_STAT(x) do ; while(0)
71 #endif
73 /* Mapped memory in non-main arenas (reliable only for NO_THREADS). */
74 static unsigned long arena_mem;
76 /**************************************************************************/
78 #if USE_ARENAS
80 /* arena_get() acquires an arena and locks the corresponding mutex.
81 First, try the one last locked successfully by this thread. (This
82 is the common case and handled with a macro for speed.) Then, loop
83 once over the circularly linked list of arenas. If no arena is
84 readily available, create a new one. In this latter case, `size'
85 is just a hint as to how much memory will be required immediately
86 in the new arena. */
88 #define arena_get(ptr, size) do { \
89 Void_t *vptr = NULL; \
90 ptr = (mstate)tsd_getspecific(arena_key, vptr); \
91 if(ptr && !mutex_trylock(&ptr->mutex)) { \
92 THREAD_STAT(++(ptr->stat_lock_direct)); \
93 } else \
94 ptr = arena_get2(ptr, (size)); \
95 } while(0)
97 /* find the heap and corresponding arena for a given ptr */
99 #define heap_for_ptr(ptr) \
100 ((heap_info *)((unsigned long)(ptr) & ~(HEAP_MAX_SIZE-1)))
101 #define arena_for_chunk(ptr) \
102 (chunk_non_main_arena(ptr) ? heap_for_ptr(ptr)->ar_ptr : &main_arena)
104 #else /* !USE_ARENAS */
106 /* There is only one arena, main_arena. */
108 #if THREAD_STATS
109 #define arena_get(ar_ptr, sz) do { \
110 ar_ptr = &main_arena; \
111 if(!mutex_trylock(&ar_ptr->mutex)) \
112 ++(ar_ptr->stat_lock_direct); \
113 else { \
114 (void)mutex_lock(&ar_ptr->mutex); \
115 ++(ar_ptr->stat_lock_wait); \
117 } while(0)
118 #else
119 #define arena_get(ar_ptr, sz) do { \
120 ar_ptr = &main_arena; \
121 (void)mutex_lock(&ar_ptr->mutex); \
122 } while(0)
123 #endif
124 #define arena_for_chunk(ptr) (&main_arena)
126 #endif /* USE_ARENAS */
128 /**************************************************************************/
130 #ifndef NO_THREADS
132 /* atfork support. */
134 static __malloc_ptr_t (*save_malloc_hook) __MALLOC_P ((size_t __size,
135 __const __malloc_ptr_t));
136 static void (*save_free_hook) __MALLOC_P ((__malloc_ptr_t __ptr,
137 __const __malloc_ptr_t));
138 static Void_t* save_arena;
140 /* Magic value for the thread-specific arena pointer when
141 malloc_atfork() is in use. */
143 #define ATFORK_ARENA_PTR ((Void_t*)-1)
145 /* The following hooks are used while the `atfork' handling mechanism
146 is active. */
148 static Void_t*
149 malloc_atfork(size_t sz, const Void_t *caller)
151 Void_t *vptr = NULL;
152 Void_t *victim;
154 tsd_getspecific(arena_key, vptr);
155 if(vptr == ATFORK_ARENA_PTR) {
156 /* We are the only thread that may allocate at all. */
157 if(save_malloc_hook != malloc_check) {
158 return _int_malloc(&main_arena, sz);
159 } else {
160 if(top_check()<0)
161 return 0;
162 victim = _int_malloc(&main_arena, sz+1);
163 return mem2mem_check(victim, sz);
165 } else {
166 /* Suspend the thread until the `atfork' handlers have completed.
167 By that time, the hooks will have been reset as well, so that
168 mALLOc() can be used again. */
169 (void)mutex_lock(&list_lock);
170 (void)mutex_unlock(&list_lock);
171 return public_mALLOc(sz);
175 static void
176 free_atfork(Void_t* mem, const Void_t *caller)
178 Void_t *vptr = NULL;
179 mstate ar_ptr;
180 mchunkptr p; /* chunk corresponding to mem */
182 if (mem == 0) /* free(0) has no effect */
183 return;
185 p = mem2chunk(mem); /* do not bother to replicate free_check here */
187 #if HAVE_MMAP
188 if (chunk_is_mmapped(p)) /* release mmapped memory. */
190 munmap_chunk(p);
191 return;
193 #endif
195 ar_ptr = arena_for_chunk(p);
196 tsd_getspecific(arena_key, vptr);
197 if(vptr != ATFORK_ARENA_PTR)
198 (void)mutex_lock(&ar_ptr->mutex);
199 _int_free(ar_ptr, mem);
200 if(vptr != ATFORK_ARENA_PTR)
201 (void)mutex_unlock(&ar_ptr->mutex);
204 /* The following two functions are registered via thread_atfork() to
205 make sure that the mutexes remain in a consistent state in the
206 fork()ed version of a thread. Also adapt the malloc and free hooks
207 temporarily, because the `atfork' handler mechanism may use
208 malloc/free internally (e.g. in LinuxThreads). */
210 static void
211 ptmalloc_lock_all __MALLOC_P((void))
213 mstate ar_ptr;
215 (void)mutex_lock(&list_lock);
216 for(ar_ptr = &main_arena;;) {
217 (void)mutex_lock(&ar_ptr->mutex);
218 ar_ptr = ar_ptr->next;
219 if(ar_ptr == &main_arena) break;
221 save_malloc_hook = __malloc_hook;
222 save_free_hook = __free_hook;
223 __malloc_hook = malloc_atfork;
224 __free_hook = free_atfork;
225 /* Only the current thread may perform malloc/free calls now. */
226 tsd_getspecific(arena_key, save_arena);
227 tsd_setspecific(arena_key, ATFORK_ARENA_PTR);
230 static void
231 ptmalloc_unlock_all __MALLOC_P((void))
233 mstate ar_ptr;
235 tsd_setspecific(arena_key, save_arena);
236 __malloc_hook = save_malloc_hook;
237 __free_hook = save_free_hook;
238 for(ar_ptr = &main_arena;;) {
239 (void)mutex_unlock(&ar_ptr->mutex);
240 ar_ptr = ar_ptr->next;
241 if(ar_ptr == &main_arena) break;
243 (void)mutex_unlock(&list_lock);
246 #ifdef __linux__
248 /* In LinuxThreads, unlocking a mutex in the child process after a
249 fork() is currently unsafe, whereas re-initializing it is safe and
250 does not leak resources. Therefore, a special atfork handler is
251 installed for the child. */
253 static void
254 ptmalloc_unlock_all2 __MALLOC_P((void))
256 mstate ar_ptr;
258 #if defined _LIBC || defined MALLOC_HOOKS
259 tsd_setspecific(arena_key, save_arena);
260 __malloc_hook = save_malloc_hook;
261 __free_hook = save_free_hook;
262 #endif
263 for(ar_ptr = &main_arena;;) {
264 (void)mutex_init(&ar_ptr->mutex);
265 ar_ptr = ar_ptr->next;
266 if(ar_ptr == &main_arena) break;
268 (void)mutex_init(&list_lock);
271 #else
273 #define ptmalloc_unlock_all2 ptmalloc_unlock_all
275 #endif
277 #endif /* !defined NO_THREADS */
279 /* Already initialized? */
280 int __malloc_initialized = -1;
282 /* Initialization routine. */
283 #ifdef _LIBC
284 #include <string.h>
285 extern char **_environ;
287 static char *
288 internal_function
289 next_env_entry (char ***position)
291 char **current = *position;
292 char *result = NULL;
294 while (*current != NULL)
296 if (__builtin_expect ((*current)[0] == 'M', 0)
297 && (*current)[1] == 'A'
298 && (*current)[2] == 'L'
299 && (*current)[3] == 'L'
300 && (*current)[4] == 'O'
301 && (*current)[5] == 'C'
302 && (*current)[6] == '_')
304 result = &(*current)[7];
306 /* Save current position for next visit. */
307 *position = ++current;
309 break;
312 ++current;
315 return result;
317 #endif /* _LIBC */
319 static void
320 ptmalloc_init __MALLOC_P((void))
322 #if __STD_C
323 const char* s;
324 #else
325 char* s;
326 #endif
327 int secure = 0;
329 if(__malloc_initialized >= 0) return;
330 __malloc_initialized = 0;
332 mp_.top_pad = DEFAULT_TOP_PAD;
333 mp_.n_mmaps_max = DEFAULT_MMAP_MAX;
334 mp_.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
335 mp_.trim_threshold = DEFAULT_TRIM_THRESHOLD;
336 mp_.pagesize = malloc_getpagesize;
338 #ifndef NO_THREADS
339 /* With some threads implementations, creating thread-specific data
340 or initializing a mutex may call malloc() itself. Provide a
341 simple starter version (realloc() won't work). */
342 save_malloc_hook = __malloc_hook;
343 save_free_hook = __free_hook;
344 __malloc_hook = malloc_starter;
345 __free_hook = free_starter;
346 #ifdef _LIBC
347 /* Initialize the pthreads interface. */
348 if (__pthread_initialize != NULL)
349 __pthread_initialize();
350 #endif
351 #endif /* !defined NO_THREADS */
352 mutex_init(&main_arena.mutex);
353 main_arena.next = &main_arena;
355 mutex_init(&list_lock);
356 tsd_key_create(&arena_key, NULL);
357 tsd_setspecific(arena_key, (Void_t *)&main_arena);
358 thread_atfork(ptmalloc_lock_all, ptmalloc_unlock_all, ptmalloc_unlock_all2);
359 #ifndef NO_THREADS
360 __malloc_hook = save_malloc_hook;
361 __free_hook = save_free_hook;
362 #endif
363 #ifdef _LIBC
364 secure = __libc_enable_secure;
365 s = NULL;
367 char **runp = _environ;
368 char *envline;
370 while (__builtin_expect ((envline = next_env_entry (&runp)) != NULL,
373 size_t len = strcspn (envline, "=");
375 if (envline[len] != '=')
376 /* This is a "MALLOC_" variable at the end of the string
377 without a '=' character. Ignore it since otherwise we
378 will access invalid memory below. */
379 continue;
381 switch (len)
383 case 6:
384 if (memcmp (envline, "CHECK_", 6) == 0)
385 s = &envline[7];
386 break;
387 case 8:
388 if (! secure && memcmp (envline, "TOP_PAD_", 8) == 0)
389 mALLOPt(M_TOP_PAD, atoi(&envline[9]));
390 break;
391 case 9:
392 if (! secure && memcmp (envline, "MMAP_MAX_", 9) == 0)
393 mALLOPt(M_MMAP_MAX, atoi(&envline[10]));
394 break;
395 case 15:
396 if (! secure)
398 if (memcmp (envline, "TRIM_THRESHOLD_", 15) == 0)
399 mALLOPt(M_TRIM_THRESHOLD, atoi(&envline[16]));
400 else if (memcmp (envline, "MMAP_THRESHOLD_", 15) == 0)
401 mALLOPt(M_MMAP_THRESHOLD, atoi(&envline[16]));
403 break;
404 default:
405 break;
409 #else
410 if (! secure)
412 if((s = getenv("MALLOC_TRIM_THRESHOLD_")))
413 mALLOPt(M_TRIM_THRESHOLD, atoi(s));
414 if((s = getenv("MALLOC_TOP_PAD_")))
415 mALLOPt(M_TOP_PAD, atoi(s));
416 if((s = getenv("MALLOC_MMAP_THRESHOLD_")))
417 mALLOPt(M_MMAP_THRESHOLD, atoi(s));
418 if((s = getenv("MALLOC_MMAP_MAX_")))
419 mALLOPt(M_MMAP_MAX, atoi(s));
421 s = getenv("MALLOC_CHECK_");
422 #endif
423 if(s) {
424 if(s[0]) mALLOPt(M_CHECK_ACTION, (int)(s[0] - '0'));
425 __malloc_check_init();
427 if(__malloc_initialize_hook != NULL)
428 (*__malloc_initialize_hook)();
429 __malloc_initialized = 1;
432 /* There are platforms (e.g. Hurd) with a link-time hook mechanism. */
433 #ifdef thread_atfork_static
434 thread_atfork_static(ptmalloc_lock_all, ptmalloc_unlock_all, \
435 ptmalloc_unlock_all2)
436 #endif
440 /* Managing heaps and arenas (for concurrent threads) */
442 #if USE_ARENAS
444 #if MALLOC_DEBUG > 1
446 /* Print the complete contents of a single heap to stderr. */
448 static void
449 #if __STD_C
450 dump_heap(heap_info *heap)
451 #else
452 dump_heap(heap) heap_info *heap;
453 #endif
455 char *ptr;
456 mchunkptr p;
458 fprintf(stderr, "Heap %p, size %10lx:\n", heap, (long)heap->size);
459 ptr = (heap->ar_ptr != (mstate)(heap+1)) ?
460 (char*)(heap + 1) : (char*)(heap + 1) + sizeof(struct malloc_state);
461 p = (mchunkptr)(((unsigned long)ptr + MALLOC_ALIGN_MASK) &
462 ~MALLOC_ALIGN_MASK);
463 for(;;) {
464 fprintf(stderr, "chunk %p size %10lx", p, (long)p->size);
465 if(p == top(heap->ar_ptr)) {
466 fprintf(stderr, " (top)\n");
467 break;
468 } else if(p->size == (0|PREV_INUSE)) {
469 fprintf(stderr, " (fence)\n");
470 break;
472 fprintf(stderr, "\n");
473 p = next_chunk(p);
477 #endif /* MALLOC_DEBUG > 1 */
479 /* Create a new heap. size is automatically rounded up to a multiple
480 of the page size. */
482 static heap_info *
483 internal_function
484 #if __STD_C
485 new_heap(size_t size, size_t top_pad)
486 #else
487 new_heap(size, top_pad) size_t size, top_pad;
488 #endif
490 size_t page_mask = malloc_getpagesize - 1;
491 char *p1, *p2;
492 unsigned long ul;
493 heap_info *h;
495 if(size+top_pad < HEAP_MIN_SIZE)
496 size = HEAP_MIN_SIZE;
497 else if(size+top_pad <= HEAP_MAX_SIZE)
498 size += top_pad;
499 else if(size > HEAP_MAX_SIZE)
500 return 0;
501 else
502 size = HEAP_MAX_SIZE;
503 size = (size + page_mask) & ~page_mask;
505 /* A memory region aligned to a multiple of HEAP_MAX_SIZE is needed.
506 No swap space needs to be reserved for the following large
507 mapping (on Linux, this is the case for all non-writable mappings
508 anyway). */
509 p1 = (char *)MMAP(0, HEAP_MAX_SIZE<<1, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE);
510 if(p1 != MAP_FAILED) {
511 p2 = (char *)(((unsigned long)p1 + (HEAP_MAX_SIZE-1)) & ~(HEAP_MAX_SIZE-1));
512 ul = p2 - p1;
513 munmap(p1, ul);
514 munmap(p2 + HEAP_MAX_SIZE, HEAP_MAX_SIZE - ul);
515 } else {
516 /* Try to take the chance that an allocation of only HEAP_MAX_SIZE
517 is already aligned. */
518 p2 = (char *)MMAP(0, HEAP_MAX_SIZE, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE);
519 if(p2 == MAP_FAILED)
520 return 0;
521 if((unsigned long)p2 & (HEAP_MAX_SIZE-1)) {
522 munmap(p2, HEAP_MAX_SIZE);
523 return 0;
526 if(mprotect(p2, size, PROT_READ|PROT_WRITE) != 0) {
527 munmap(p2, HEAP_MAX_SIZE);
528 return 0;
530 h = (heap_info *)p2;
531 h->size = size;
532 THREAD_STAT(stat_n_heaps++);
533 return h;
536 /* Grow or shrink a heap. size is automatically rounded up to a
537 multiple of the page size if it is positive. */
539 static int
540 #if __STD_C
541 grow_heap(heap_info *h, long diff)
542 #else
543 grow_heap(h, diff) heap_info *h; long diff;
544 #endif
546 size_t page_mask = malloc_getpagesize - 1;
547 long new_size;
549 if(diff >= 0) {
550 diff = (diff + page_mask) & ~page_mask;
551 new_size = (long)h->size + diff;
552 if(new_size > HEAP_MAX_SIZE)
553 return -1;
554 if(mprotect((char *)h + h->size, diff, PROT_READ|PROT_WRITE) != 0)
555 return -2;
556 } else {
557 new_size = (long)h->size + diff;
558 if(new_size < (long)sizeof(*h))
559 return -1;
560 /* Try to re-map the extra heap space freshly to save memory, and
561 make it inaccessible. */
562 if((char *)MMAP((char *)h + new_size, -diff, PROT_NONE,
563 MAP_PRIVATE|MAP_FIXED) == (char *) MAP_FAILED)
564 return -2;
565 /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/
567 h->size = new_size;
568 return 0;
571 /* Delete a heap. */
573 #define delete_heap(heap) munmap((char*)(heap), HEAP_MAX_SIZE)
575 static int
576 internal_function
577 #if __STD_C
578 heap_trim(heap_info *heap, size_t pad)
579 #else
580 heap_trim(heap, pad) heap_info *heap; size_t pad;
581 #endif
583 mstate ar_ptr = heap->ar_ptr;
584 unsigned long pagesz = mp_.pagesize;
585 mchunkptr top_chunk = top(ar_ptr), p, bck, fwd;
586 heap_info *prev_heap;
587 long new_size, top_size, extra;
589 /* Can this heap go away completely? */
590 while(top_chunk == chunk_at_offset(heap, sizeof(*heap))) {
591 prev_heap = heap->prev;
592 p = chunk_at_offset(prev_heap, prev_heap->size - (MINSIZE-2*SIZE_SZ));
593 assert(p->size == (0|PREV_INUSE)); /* must be fencepost */
594 p = prev_chunk(p);
595 new_size = chunksize(p) + (MINSIZE-2*SIZE_SZ);
596 assert(new_size>0 && new_size<(long)(2*MINSIZE));
597 if(!prev_inuse(p))
598 new_size += p->prev_size;
599 assert(new_size>0 && new_size<HEAP_MAX_SIZE);
600 if(new_size + (HEAP_MAX_SIZE - prev_heap->size) < pad + MINSIZE + pagesz)
601 break;
602 ar_ptr->system_mem -= heap->size;
603 arena_mem -= heap->size;
604 delete_heap(heap);
605 heap = prev_heap;
606 if(!prev_inuse(p)) { /* consolidate backward */
607 p = prev_chunk(p);
608 unlink(p, bck, fwd);
610 assert(((unsigned long)((char*)p + new_size) & (pagesz-1)) == 0);
611 assert( ((char*)p + new_size) == ((char*)heap + heap->size) );
612 top(ar_ptr) = top_chunk = p;
613 set_head(top_chunk, new_size | PREV_INUSE);
614 /*check_chunk(ar_ptr, top_chunk);*/
616 top_size = chunksize(top_chunk);
617 extra = ((top_size - pad - MINSIZE + (pagesz-1))/pagesz - 1) * pagesz;
618 if(extra < (long)pagesz)
619 return 0;
620 /* Try to shrink. */
621 if(grow_heap(heap, -extra) != 0)
622 return 0;
623 ar_ptr->system_mem -= extra;
624 arena_mem -= extra;
626 /* Success. Adjust top accordingly. */
627 set_head(top_chunk, (top_size - extra) | PREV_INUSE);
628 /*check_chunk(ar_ptr, top_chunk);*/
629 return 1;
632 static mstate
633 internal_function
634 #if __STD_C
635 arena_get2(mstate a_tsd, size_t size)
636 #else
637 arena_get2(a_tsd, size) mstate a_tsd; size_t size;
638 #endif
640 mstate a;
641 int err;
643 if(!a_tsd)
644 a = a_tsd = &main_arena;
645 else {
646 a = a_tsd->next;
647 if(!a) {
648 /* This can only happen while initializing the new arena. */
649 (void)mutex_lock(&main_arena.mutex);
650 THREAD_STAT(++(main_arena.stat_lock_wait));
651 return &main_arena;
655 /* Check the global, circularly linked list for available arenas. */
656 repeat:
657 do {
658 if(!mutex_trylock(&a->mutex)) {
659 THREAD_STAT(++(a->stat_lock_loop));
660 tsd_setspecific(arena_key, (Void_t *)a);
661 return a;
663 a = a->next;
664 } while(a != a_tsd);
666 /* If not even the list_lock can be obtained, try again. This can
667 happen during `atfork', or for example on systems where thread
668 creation makes it temporarily impossible to obtain _any_
669 locks. */
670 if(mutex_trylock(&list_lock)) {
671 a = a_tsd;
672 goto repeat;
674 (void)mutex_unlock(&list_lock);
676 /* Nothing immediately available, so generate a new arena. */
677 a = _int_new_arena(size);
678 if(!a)
679 return 0;
681 tsd_setspecific(arena_key, (Void_t *)a);
682 mutex_init(&a->mutex);
683 err = mutex_lock(&a->mutex); /* remember result */
685 /* Add the new arena to the global list. */
686 (void)mutex_lock(&list_lock);
687 a->next = main_arena.next;
688 main_arena.next = a;
689 (void)mutex_unlock(&list_lock);
691 if(err) /* locking failed; keep arena for further attempts later */
692 return 0;
694 THREAD_STAT(++(a->stat_lock_loop));
695 return a;
698 /* Create a new arena with initial size "size". */
700 mstate
701 _int_new_arena(size_t size)
703 mstate a;
704 heap_info *h;
705 char *ptr;
706 unsigned long misalign;
708 h = new_heap(size + (sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT),
709 mp_.top_pad);
710 if(!h) {
711 /* Maybe size is too large to fit in a single heap. So, just try
712 to create a minimally-sized arena and let _int_malloc() attempt
713 to deal with the large request via mmap_chunk(). */
714 h = new_heap(sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT, mp_.top_pad);
715 if(!h)
716 return 0;
718 a = h->ar_ptr = (mstate)(h+1);
719 malloc_init_state(a);
720 /*a->next = NULL;*/
721 a->system_mem = a->max_system_mem = h->size;
722 arena_mem += h->size;
723 #ifdef NO_THREADS
724 if((unsigned long)(mp_.mmapped_mem + arena_mem + main_arena.system_mem) >
725 mp_.max_total_mem)
726 mp_.max_total_mem = mp_.mmapped_mem + arena_mem + main_arena.system_mem;
727 #endif
729 /* Set up the top chunk, with proper alignment. */
730 ptr = (char *)(a + 1);
731 misalign = (unsigned long)chunk2mem(ptr) & MALLOC_ALIGN_MASK;
732 if (misalign > 0)
733 ptr += MALLOC_ALIGNMENT - misalign;
734 top(a) = (mchunkptr)ptr;
735 set_head(top(a), (((char*)h + h->size) - ptr) | PREV_INUSE);
737 return a;
740 #endif /* USE_ARENAS */
743 * Local variables:
744 * c-basic-offset: 2
745 * End: