1 /* Malloc implementation for multiple threads without lock contention.
2 Copyright (C) 2001,2002,2003,2004,2005,2006,2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Wolfram Gloger <wg@malloc.de>, 2001.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 /* Compile-time constants. */
26 #define HEAP_MIN_SIZE (32*1024)
28 # ifdef DEFAULT_MMAP_THRESHOLD_MAX
29 # define HEAP_MAX_SIZE (2 * DEFAULT_MMAP_THRESHOLD_MAX)
31 # define HEAP_MAX_SIZE (1024*1024) /* must be a power of two */
35 /* HEAP_MIN_SIZE and HEAP_MAX_SIZE limit the size of mmap()ed heaps
36 that are dynamically created for multi-threaded programs. The
37 maximum size must be a power of two, for fast determination of
38 which heap belongs to a chunk. It should be much larger than the
39 mmap threshold, so that requests with a size just below that
40 threshold can be fulfilled without creating too many heaps. */
44 #define THREAD_STATS 0
47 /* If THREAD_STATS is non-zero, some statistics on mutex locking are
50 /***************************************************************************/
52 #define top(ar_ptr) ((ar_ptr)->top)
54 /* A heap is a single contiguous memory region holding (coalesceable)
55 malloc_chunks. It is allocated with mmap() and always starts at an
56 address aligned to HEAP_MAX_SIZE. Not used unless compiling with
59 typedef struct _heap_info
{
60 mstate ar_ptr
; /* Arena for this heap. */
61 struct _heap_info
*prev
; /* Previous heap. */
62 size_t size
; /* Current size in bytes. */
63 size_t mprotect_size
; /* Size in bytes that has been mprotected
64 PROT_READ|PROT_WRITE. */
65 /* Make sure the following data is properly aligned, particularly
66 that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of
68 char pad
[-6 * SIZE_SZ
& MALLOC_ALIGN_MASK
];
71 /* Get a compile-time error if the heap_info padding is not correct
72 to make alignment work as expected in sYSMALLOc. */
73 extern int sanity_check_heap_info_alignment
[(sizeof (heap_info
)
74 + 2 * SIZE_SZ
) % MALLOC_ALIGNMENT
77 /* Thread specific data */
79 static tsd_key_t arena_key
;
80 static mutex_t list_lock
;
83 static int stat_n_heaps
;
84 #define THREAD_STAT(x) x
86 #define THREAD_STAT(x) do ; while(0)
89 /* Mapped memory in non-main arenas (reliable only for NO_THREADS). */
90 static unsigned long arena_mem
;
92 /* Already initialized? */
93 int __malloc_initialized
= -1;
95 /**************************************************************************/
99 /* arena_get() acquires an arena and locks the corresponding mutex.
100 First, try the one last locked successfully by this thread. (This
101 is the common case and handled with a macro for speed.) Then, loop
102 once over the circularly linked list of arenas. If no arena is
103 readily available, create a new one. In this latter case, `size'
104 is just a hint as to how much memory will be required immediately
107 #define arena_get(ptr, size) do { \
108 Void_t *vptr = NULL; \
109 ptr = (mstate)tsd_getspecific(arena_key, vptr); \
110 if(ptr && !mutex_trylock(&ptr->mutex)) { \
111 THREAD_STAT(++(ptr->stat_lock_direct)); \
113 ptr = arena_get2(ptr, (size)); \
116 /* find the heap and corresponding arena for a given ptr */
118 #define heap_for_ptr(ptr) \
119 ((heap_info *)((unsigned long)(ptr) & ~(HEAP_MAX_SIZE-1)))
120 #define arena_for_chunk(ptr) \
121 (chunk_non_main_arena(ptr) ? heap_for_ptr(ptr)->ar_ptr : &main_arena)
123 #else /* !USE_ARENAS */
125 /* There is only one arena, main_arena. */
128 #define arena_get(ar_ptr, sz) do { \
129 ar_ptr = &main_arena; \
130 if(!mutex_trylock(&ar_ptr->mutex)) \
131 ++(ar_ptr->stat_lock_direct); \
133 (void)mutex_lock(&ar_ptr->mutex); \
134 ++(ar_ptr->stat_lock_wait); \
138 #define arena_get(ar_ptr, sz) do { \
139 ar_ptr = &main_arena; \
140 (void)mutex_lock(&ar_ptr->mutex); \
143 #define arena_for_chunk(ptr) (&main_arena)
145 #endif /* USE_ARENAS */
147 /**************************************************************************/
151 /* atfork support. */
153 static __malloc_ptr_t (*save_malloc_hook
) (size_t __size
,
154 __const __malloc_ptr_t
);
155 # if !defined _LIBC || (defined SHARED && !USE___THREAD)
156 static __malloc_ptr_t (*save_memalign_hook
) (size_t __align
, size_t __size
,
157 __const __malloc_ptr_t
);
159 static void (*save_free_hook
) (__malloc_ptr_t __ptr
,
160 __const __malloc_ptr_t
);
161 static Void_t
* save_arena
;
163 /* Magic value for the thread-specific arena pointer when
164 malloc_atfork() is in use. */
166 #define ATFORK_ARENA_PTR ((Void_t*)-1)
168 /* The following hooks are used while the `atfork' handling mechanism
172 malloc_atfork(size_t sz
, const Void_t
*caller
)
177 tsd_getspecific(arena_key
, vptr
);
178 if(vptr
== ATFORK_ARENA_PTR
) {
179 /* We are the only thread that may allocate at all. */
180 if(save_malloc_hook
!= malloc_check
) {
181 return _int_malloc(&main_arena
, sz
);
185 victim
= _int_malloc(&main_arena
, sz
+1);
186 return mem2mem_check(victim
, sz
);
189 /* Suspend the thread until the `atfork' handlers have completed.
190 By that time, the hooks will have been reset as well, so that
191 mALLOc() can be used again. */
192 (void)mutex_lock(&list_lock
);
193 (void)mutex_unlock(&list_lock
);
194 return public_mALLOc(sz
);
199 free_atfork(Void_t
* mem
, const Void_t
*caller
)
203 mchunkptr p
; /* chunk corresponding to mem */
205 if (mem
== 0) /* free(0) has no effect */
208 p
= mem2chunk(mem
); /* do not bother to replicate free_check here */
211 if (chunk_is_mmapped(p
)) /* release mmapped memory. */
218 ar_ptr
= arena_for_chunk(p
);
219 tsd_getspecific(arena_key
, vptr
);
220 if(vptr
!= ATFORK_ARENA_PTR
)
221 (void)mutex_lock(&ar_ptr
->mutex
);
222 _int_free(ar_ptr
, mem
);
223 if(vptr
!= ATFORK_ARENA_PTR
)
224 (void)mutex_unlock(&ar_ptr
->mutex
);
228 /* Counter for number of times the list is locked by the same thread. */
229 static unsigned int atfork_recursive_cntr
;
231 /* The following two functions are registered via thread_atfork() to
232 make sure that the mutexes remain in a consistent state in the
233 fork()ed version of a thread. Also adapt the malloc and free hooks
234 temporarily, because the `atfork' handler mechanism may use
235 malloc/free internally (e.g. in LinuxThreads). */
238 ptmalloc_lock_all (void)
242 if(__malloc_initialized
< 1)
244 if (mutex_trylock(&list_lock
))
247 tsd_getspecific(arena_key
, my_arena
);
248 if (my_arena
== ATFORK_ARENA_PTR
)
249 /* This is the same thread which already locks the global list.
250 Just bump the counter. */
253 /* This thread has to wait its turn. */
254 (void)mutex_lock(&list_lock
);
256 for(ar_ptr
= &main_arena
;;) {
257 (void)mutex_lock(&ar_ptr
->mutex
);
258 ar_ptr
= ar_ptr
->next
;
259 if(ar_ptr
== &main_arena
) break;
261 save_malloc_hook
= __malloc_hook
;
262 save_free_hook
= __free_hook
;
263 __malloc_hook
= malloc_atfork
;
264 __free_hook
= free_atfork
;
265 /* Only the current thread may perform malloc/free calls now. */
266 tsd_getspecific(arena_key
, save_arena
);
267 tsd_setspecific(arena_key
, ATFORK_ARENA_PTR
);
269 ++atfork_recursive_cntr
;
273 ptmalloc_unlock_all (void)
277 if(__malloc_initialized
< 1)
279 if (--atfork_recursive_cntr
!= 0)
281 tsd_setspecific(arena_key
, save_arena
);
282 __malloc_hook
= save_malloc_hook
;
283 __free_hook
= save_free_hook
;
284 for(ar_ptr
= &main_arena
;;) {
285 (void)mutex_unlock(&ar_ptr
->mutex
);
286 ar_ptr
= ar_ptr
->next
;
287 if(ar_ptr
== &main_arena
) break;
289 (void)mutex_unlock(&list_lock
);
294 /* In NPTL, unlocking a mutex in the child process after a
295 fork() is currently unsafe, whereas re-initializing it is safe and
296 does not leak resources. Therefore, a special atfork handler is
297 installed for the child. */
300 ptmalloc_unlock_all2 (void)
304 if(__malloc_initialized
< 1)
306 #if defined _LIBC || defined MALLOC_HOOKS
307 tsd_setspecific(arena_key
, save_arena
);
308 __malloc_hook
= save_malloc_hook
;
309 __free_hook
= save_free_hook
;
311 for(ar_ptr
= &main_arena
;;) {
312 mutex_init(&ar_ptr
->mutex
);
313 ar_ptr
= ar_ptr
->next
;
314 if(ar_ptr
== &main_arena
) break;
316 mutex_init(&list_lock
);
317 atfork_recursive_cntr
= 0;
322 #define ptmalloc_unlock_all2 ptmalloc_unlock_all
326 #endif /* !defined NO_THREADS */
328 /* Initialization routine. */
331 extern char **_environ
;
335 next_env_entry (char ***position
)
337 char **current
= *position
;
340 while (*current
!= NULL
)
342 if (__builtin_expect ((*current
)[0] == 'M', 0)
343 && (*current
)[1] == 'A'
344 && (*current
)[2] == 'L'
345 && (*current
)[3] == 'L'
346 && (*current
)[4] == 'O'
347 && (*current
)[5] == 'C'
348 && (*current
)[6] == '_')
350 result
= &(*current
)[7];
352 /* Save current position for next visit. */
353 *position
= ++current
;
365 /* Set up basic state so that _int_malloc et al can work. */
367 ptmalloc_init_minimal (void)
369 #if DEFAULT_TOP_PAD != 0
370 mp_
.top_pad
= DEFAULT_TOP_PAD
;
372 mp_
.n_mmaps_max
= DEFAULT_MMAP_MAX
;
373 mp_
.mmap_threshold
= DEFAULT_MMAP_THRESHOLD
;
374 mp_
.trim_threshold
= DEFAULT_TRIM_THRESHOLD
;
375 mp_
.pagesize
= malloc_getpagesize
;
382 __failing_morecore (ptrdiff_t d
)
384 return (void *) MORECORE_FAILURE
;
387 extern struct dl_open_hook
*_dl_open_hook
;
388 libc_hidden_proto (_dl_open_hook
);
391 # if defined SHARED && !USE___THREAD
392 /* This is called by __pthread_initialize_minimal when it needs to use
393 malloc to set up the TLS state. We cannot do the full work of
394 ptmalloc_init (below) until __pthread_initialize_minimal has finished,
395 so it has to switch to using the special startup-time hooks while doing
396 those allocations. */
398 __libc_malloc_pthread_startup (bool first_time
)
402 ptmalloc_init_minimal ();
403 save_malloc_hook
= __malloc_hook
;
404 save_memalign_hook
= __memalign_hook
;
405 save_free_hook
= __free_hook
;
406 __malloc_hook
= malloc_starter
;
407 __memalign_hook
= memalign_starter
;
408 __free_hook
= free_starter
;
412 __malloc_hook
= save_malloc_hook
;
413 __memalign_hook
= save_memalign_hook
;
414 __free_hook
= save_free_hook
;
430 if(__malloc_initialized
>= 0) return;
431 __malloc_initialized
= 0;
434 # if defined SHARED && !USE___THREAD
435 /* ptmalloc_init_minimal may already have been called via
436 __libc_malloc_pthread_startup, above. */
437 if (mp_
.pagesize
== 0)
440 ptmalloc_init_minimal();
444 /* We know __pthread_initialize_minimal has already been called,
445 and that is enough. */
449 /* With some threads implementations, creating thread-specific data
450 or initializing a mutex may call malloc() itself. Provide a
451 simple starter version (realloc() won't work). */
452 save_malloc_hook
= __malloc_hook
;
453 save_memalign_hook
= __memalign_hook
;
454 save_free_hook
= __free_hook
;
455 __malloc_hook
= malloc_starter
;
456 __memalign_hook
= memalign_starter
;
457 __free_hook
= free_starter
;
459 /* Initialize the pthreads interface. */
460 if (__pthread_initialize
!= NULL
)
461 __pthread_initialize();
462 # endif /* !defined _LIBC */
463 # endif /* !defined NO_STARTER */
464 #endif /* !defined NO_THREADS */
465 mutex_init(&main_arena
.mutex
);
466 main_arena
.next
= &main_arena
;
468 #if defined _LIBC && defined SHARED
469 /* In case this libc copy is in a non-default namespace, never use brk.
470 Likewise if dlopened from statically linked program. */
474 if (_dl_open_hook
!= NULL
475 || (_dl_addr (ptmalloc_init
, &di
, &l
, NULL
) != 0
476 && l
->l_ns
!= LM_ID_BASE
))
477 __morecore
= __failing_morecore
;
480 mutex_init(&list_lock
);
481 tsd_key_create(&arena_key
, NULL
);
482 tsd_setspecific(arena_key
, (Void_t
*)&main_arena
);
483 thread_atfork(ptmalloc_lock_all
, ptmalloc_unlock_all
, ptmalloc_unlock_all2
);
486 __malloc_hook
= save_malloc_hook
;
487 __memalign_hook
= save_memalign_hook
;
488 __free_hook
= save_free_hook
;
494 secure
= __libc_enable_secure
;
496 if (__builtin_expect (_environ
!= NULL
, 1))
498 char **runp
= _environ
;
501 while (__builtin_expect ((envline
= next_env_entry (&runp
)) != NULL
,
504 size_t len
= strcspn (envline
, "=");
506 if (envline
[len
] != '=')
507 /* This is a "MALLOC_" variable at the end of the string
508 without a '=' character. Ignore it since otherwise we
509 will access invalid memory below. */
515 if (memcmp (envline
, "CHECK_", 6) == 0)
521 if (memcmp (envline
, "TOP_PAD_", 8) == 0)
522 mALLOPt(M_TOP_PAD
, atoi(&envline
[9]));
523 else if (memcmp (envline
, "PERTURB_", 8) == 0)
524 mALLOPt(M_PERTURB
, atoi(&envline
[9]));
528 if (! secure
&& memcmp (envline
, "MMAP_MAX_", 9) == 0)
529 mALLOPt(M_MMAP_MAX
, atoi(&envline
[10]));
534 if (memcmp (envline
, "TRIM_THRESHOLD_", 15) == 0)
535 mALLOPt(M_TRIM_THRESHOLD
, atoi(&envline
[16]));
536 else if (memcmp (envline
, "MMAP_THRESHOLD_", 15) == 0)
537 mALLOPt(M_MMAP_THRESHOLD
, atoi(&envline
[16]));
548 if((s
= getenv("MALLOC_TRIM_THRESHOLD_")))
549 mALLOPt(M_TRIM_THRESHOLD
, atoi(s
));
550 if((s
= getenv("MALLOC_TOP_PAD_")))
551 mALLOPt(M_TOP_PAD
, atoi(s
));
552 if((s
= getenv("MALLOC_PERTURB_")))
553 mALLOPt(M_PERTURB
, atoi(s
));
554 if((s
= getenv("MALLOC_MMAP_THRESHOLD_")))
555 mALLOPt(M_MMAP_THRESHOLD
, atoi(s
));
556 if((s
= getenv("MALLOC_MMAP_MAX_")))
557 mALLOPt(M_MMAP_MAX
, atoi(s
));
559 s
= getenv("MALLOC_CHECK_");
562 mALLOPt(M_CHECK_ACTION
, (int)(s
[0] - '0'));
563 if (check_action
!= 0)
564 __malloc_check_init();
566 if(__malloc_initialize_hook
!= NULL
)
567 (*__malloc_initialize_hook
)();
568 __malloc_initialized
= 1;
571 /* There are platforms (e.g. Hurd) with a link-time hook mechanism. */
572 #ifdef thread_atfork_static
573 thread_atfork_static(ptmalloc_lock_all
, ptmalloc_unlock_all
, \
574 ptmalloc_unlock_all2
)
579 /* Managing heaps and arenas (for concurrent threads) */
585 /* Print the complete contents of a single heap to stderr. */
589 dump_heap(heap_info
*heap
)
591 dump_heap(heap
) heap_info
*heap
;
597 fprintf(stderr
, "Heap %p, size %10lx:\n", heap
, (long)heap
->size
);
598 ptr
= (heap
->ar_ptr
!= (mstate
)(heap
+1)) ?
599 (char*)(heap
+ 1) : (char*)(heap
+ 1) + sizeof(struct malloc_state
);
600 p
= (mchunkptr
)(((unsigned long)ptr
+ MALLOC_ALIGN_MASK
) &
603 fprintf(stderr
, "chunk %p size %10lx", p
, (long)p
->size
);
604 if(p
== top(heap
->ar_ptr
)) {
605 fprintf(stderr
, " (top)\n");
607 } else if(p
->size
== (0|PREV_INUSE
)) {
608 fprintf(stderr
, " (fence)\n");
611 fprintf(stderr
, "\n");
616 #endif /* MALLOC_DEBUG > 1 */
618 /* If consecutive mmap (0, HEAP_MAX_SIZE << 1, ...) calls return decreasing
619 addresses as opposed to increasing, new_heap would badly fragment the
620 address space. In that case remember the second HEAP_MAX_SIZE part
621 aligned to HEAP_MAX_SIZE from last mmap (0, HEAP_MAX_SIZE << 1, ...)
622 call (if it is already aligned) and try to reuse it next time. We need
623 no locking for it, as kernel ensures the atomicity for us - worst case
624 we'll call mmap (addr, HEAP_MAX_SIZE, ...) for some value of addr in
625 multiple threads, but only one will succeed. */
626 static char *aligned_heap_area
;
628 /* Create a new heap. size is automatically rounded up to a multiple
634 new_heap(size_t size
, size_t top_pad
)
636 new_heap(size
, top_pad
) size_t size
, top_pad
;
639 size_t page_mask
= malloc_getpagesize
- 1;
644 if(size
+top_pad
< HEAP_MIN_SIZE
)
645 size
= HEAP_MIN_SIZE
;
646 else if(size
+top_pad
<= HEAP_MAX_SIZE
)
648 else if(size
> HEAP_MAX_SIZE
)
651 size
= HEAP_MAX_SIZE
;
652 size
= (size
+ page_mask
) & ~page_mask
;
654 /* A memory region aligned to a multiple of HEAP_MAX_SIZE is needed.
655 No swap space needs to be reserved for the following large
656 mapping (on Linux, this is the case for all non-writable mappings
659 if(aligned_heap_area
) {
660 p2
= (char *)MMAP(aligned_heap_area
, HEAP_MAX_SIZE
, PROT_NONE
,
661 MAP_PRIVATE
|MAP_NORESERVE
);
662 aligned_heap_area
= NULL
;
663 if (p2
!= MAP_FAILED
&& ((unsigned long)p2
& (HEAP_MAX_SIZE
-1))) {
664 munmap(p2
, HEAP_MAX_SIZE
);
668 if(p2
== MAP_FAILED
) {
669 p1
= (char *)MMAP(0, HEAP_MAX_SIZE
<<1, PROT_NONE
,
670 MAP_PRIVATE
|MAP_NORESERVE
);
671 if(p1
!= MAP_FAILED
) {
672 p2
= (char *)(((unsigned long)p1
+ (HEAP_MAX_SIZE
-1))
673 & ~(HEAP_MAX_SIZE
-1));
678 aligned_heap_area
= p2
+ HEAP_MAX_SIZE
;
679 munmap(p2
+ HEAP_MAX_SIZE
, HEAP_MAX_SIZE
- ul
);
681 /* Try to take the chance that an allocation of only HEAP_MAX_SIZE
682 is already aligned. */
683 p2
= (char *)MMAP(0, HEAP_MAX_SIZE
, PROT_NONE
, MAP_PRIVATE
|MAP_NORESERVE
);
686 if((unsigned long)p2
& (HEAP_MAX_SIZE
-1)) {
687 munmap(p2
, HEAP_MAX_SIZE
);
692 if(mprotect(p2
, size
, PROT_READ
|PROT_WRITE
) != 0) {
693 munmap(p2
, HEAP_MAX_SIZE
);
698 h
->mprotect_size
= size
;
699 THREAD_STAT(stat_n_heaps
++);
703 /* Grow or shrink a heap. size is automatically rounded up to a
704 multiple of the page size if it is positive. */
708 grow_heap(heap_info
*h
, long diff
)
710 grow_heap(h
, diff
) heap_info
*h
; long diff
;
713 size_t page_mask
= malloc_getpagesize
- 1;
717 diff
= (diff
+ page_mask
) & ~page_mask
;
718 new_size
= (long)h
->size
+ diff
;
719 if((unsigned long) new_size
> (unsigned long) HEAP_MAX_SIZE
)
721 if((unsigned long) new_size
> h
->mprotect_size
) {
722 if (mprotect((char *)h
+ h
->mprotect_size
,
723 (unsigned long) new_size
- h
->mprotect_size
,
724 PROT_READ
|PROT_WRITE
) != 0)
726 h
->mprotect_size
= new_size
;
729 new_size
= (long)h
->size
+ diff
;
730 if(new_size
< (long)sizeof(*h
))
732 /* Try to re-map the extra heap space freshly to save memory, and
733 make it inaccessible. */
735 if (__builtin_expect (__libc_enable_secure
, 0))
740 if((char *)MMAP((char *)h
+ new_size
, -diff
, PROT_NONE
,
741 MAP_PRIVATE
|MAP_FIXED
) == (char *) MAP_FAILED
)
743 h
->mprotect_size
= new_size
;
747 madvise ((char *)h
+ new_size
, -diff
, MADV_DONTNEED
);
749 /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/
757 #define delete_heap(heap) \
759 if ((char *)(heap) + HEAP_MAX_SIZE == aligned_heap_area) \
760 aligned_heap_area = NULL; \
761 munmap((char*)(heap), HEAP_MAX_SIZE); \
767 heap_trim(heap_info
*heap
, size_t pad
)
769 heap_trim(heap
, pad
) heap_info
*heap
; size_t pad
;
772 mstate ar_ptr
= heap
->ar_ptr
;
773 unsigned long pagesz
= mp_
.pagesize
;
774 mchunkptr top_chunk
= top(ar_ptr
), p
, bck
, fwd
;
775 heap_info
*prev_heap
;
776 long new_size
, top_size
, extra
;
778 /* Can this heap go away completely? */
779 while(top_chunk
== chunk_at_offset(heap
, sizeof(*heap
))) {
780 prev_heap
= heap
->prev
;
781 p
= chunk_at_offset(prev_heap
, prev_heap
->size
- (MINSIZE
-2*SIZE_SZ
));
782 assert(p
->size
== (0|PREV_INUSE
)); /* must be fencepost */
784 new_size
= chunksize(p
) + (MINSIZE
-2*SIZE_SZ
);
785 assert(new_size
>0 && new_size
<(long)(2*MINSIZE
));
787 new_size
+= p
->prev_size
;
788 assert(new_size
>0 && new_size
<HEAP_MAX_SIZE
);
789 if(new_size
+ (HEAP_MAX_SIZE
- prev_heap
->size
) < pad
+ MINSIZE
+ pagesz
)
791 ar_ptr
->system_mem
-= heap
->size
;
792 arena_mem
-= heap
->size
;
795 if(!prev_inuse(p
)) { /* consolidate backward */
799 assert(((unsigned long)((char*)p
+ new_size
) & (pagesz
-1)) == 0);
800 assert( ((char*)p
+ new_size
) == ((char*)heap
+ heap
->size
) );
801 top(ar_ptr
) = top_chunk
= p
;
802 set_head(top_chunk
, new_size
| PREV_INUSE
);
803 /*check_chunk(ar_ptr, top_chunk);*/
805 top_size
= chunksize(top_chunk
);
806 extra
= ((top_size
- pad
- MINSIZE
+ (pagesz
-1))/pagesz
- 1) * pagesz
;
807 if(extra
< (long)pagesz
)
810 if(grow_heap(heap
, -extra
) != 0)
812 ar_ptr
->system_mem
-= extra
;
815 /* Success. Adjust top accordingly. */
816 set_head(top_chunk
, (top_size
- extra
) | PREV_INUSE
);
817 /*check_chunk(ar_ptr, top_chunk);*/
821 /* Create a new arena with initial size "size". */
824 _int_new_arena(size_t size
)
829 unsigned long misalign
;
831 h
= new_heap(size
+ (sizeof(*h
) + sizeof(*a
) + MALLOC_ALIGNMENT
),
834 /* Maybe size is too large to fit in a single heap. So, just try
835 to create a minimally-sized arena and let _int_malloc() attempt
836 to deal with the large request via mmap_chunk(). */
837 h
= new_heap(sizeof(*h
) + sizeof(*a
) + MALLOC_ALIGNMENT
, mp_
.top_pad
);
841 a
= h
->ar_ptr
= (mstate
)(h
+1);
842 malloc_init_state(a
);
844 a
->system_mem
= a
->max_system_mem
= h
->size
;
845 arena_mem
+= h
->size
;
847 if((unsigned long)(mp_
.mmapped_mem
+ arena_mem
+ main_arena
.system_mem
) >
849 mp_
.max_total_mem
= mp_
.mmapped_mem
+ arena_mem
+ main_arena
.system_mem
;
852 /* Set up the top chunk, with proper alignment. */
853 ptr
= (char *)(a
+ 1);
854 misalign
= (unsigned long)chunk2mem(ptr
) & MALLOC_ALIGN_MASK
;
856 ptr
+= MALLOC_ALIGNMENT
- misalign
;
857 top(a
) = (mchunkptr
)ptr
;
858 set_head(top(a
), (((char*)h
+ h
->size
) - ptr
) | PREV_INUSE
);
866 arena_get2(mstate a_tsd
, size_t size
)
868 arena_get2(a_tsd
, size
) mstate a_tsd
; size_t size
;
874 a
= a_tsd
= &main_arena
;
878 /* This can only happen while initializing the new arena. */
879 (void)mutex_lock(&main_arena
.mutex
);
880 THREAD_STAT(++(main_arena
.stat_lock_wait
));
885 /* Check the global, circularly linked list for available arenas. */
886 bool retried
= false;
889 if(!mutex_trylock(&a
->mutex
)) {
891 (void)mutex_unlock(&list_lock
);
892 THREAD_STAT(++(a
->stat_lock_loop
));
893 tsd_setspecific(arena_key
, (Void_t
*)a
);
899 /* If not even the list_lock can be obtained, try again. This can
900 happen during `atfork', or for example on systems where thread
901 creation makes it temporarily impossible to obtain _any_
903 if(!retried
&& mutex_trylock(&list_lock
)) {
904 /* We will block to not run in a busy loop. */
905 (void)mutex_lock(&list_lock
);
907 /* Since we blocked there might be an arena available now. */
913 /* Nothing immediately available, so generate a new arena. */
914 a
= _int_new_arena(size
);
917 tsd_setspecific(arena_key
, (Void_t
*)a
);
918 mutex_init(&a
->mutex
);
919 mutex_lock(&a
->mutex
); /* remember result */
921 /* Add the new arena to the global list. */
922 a
->next
= main_arena
.next
;
923 atomic_write_barrier ();
926 THREAD_STAT(++(a
->stat_lock_loop
));
928 (void)mutex_unlock(&list_lock
);
933 #endif /* USE_ARENAS */