1 /* Malloc implementation for multiple threads without lock contention.
2 Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2009
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
;
82 static size_t narenas
;
83 static mstate free_list
;
87 static int stat_n_heaps
;
88 #define THREAD_STAT(x) x
90 #define THREAD_STAT(x) do ; while(0)
93 /* Mapped memory in non-main arenas (reliable only for NO_THREADS). */
94 static unsigned long arena_mem
;
96 /* Already initialized? */
97 int __malloc_initialized
= -1;
99 /**************************************************************************/
103 /* arena_get() acquires an arena and locks the corresponding mutex.
104 First, try the one last locked successfully by this thread. (This
105 is the common case and handled with a macro for speed.) Then, loop
106 once over the circularly linked list of arenas. If no arena is
107 readily available, create a new one. In this latter case, `size'
108 is just a hint as to how much memory will be required immediately
111 #define arena_get(ptr, size) do { \
113 arena_lock(ptr, size); \
116 #define arena_lookup(ptr) do { \
117 Void_t *vptr = NULL; \
118 ptr = (mstate)tsd_getspecific(arena_key, vptr); \
122 #define arena_lock(ptr, size) do { \
124 (void)mutex_lock(&ptr->mutex); \
126 ptr = arena_get2(ptr, (size)); \
129 #define arena_lock(ptr, size) do { \
130 if(ptr && !mutex_trylock(&ptr->mutex)) { \
131 THREAD_STAT(++(ptr->stat_lock_direct)); \
133 ptr = arena_get2(ptr, (size)); \
137 /* find the heap and corresponding arena for a given ptr */
139 #define heap_for_ptr(ptr) \
140 ((heap_info *)((unsigned long)(ptr) & ~(HEAP_MAX_SIZE-1)))
141 #define arena_for_chunk(ptr) \
142 (chunk_non_main_arena(ptr) ? heap_for_ptr(ptr)->ar_ptr : &main_arena)
144 #else /* !USE_ARENAS */
146 /* There is only one arena, main_arena. */
149 #define arena_get(ar_ptr, sz) do { \
150 ar_ptr = &main_arena; \
151 if(!mutex_trylock(&ar_ptr->mutex)) \
152 ++(ar_ptr->stat_lock_direct); \
154 (void)mutex_lock(&ar_ptr->mutex); \
155 ++(ar_ptr->stat_lock_wait); \
159 #define arena_get(ar_ptr, sz) do { \
160 ar_ptr = &main_arena; \
161 (void)mutex_lock(&ar_ptr->mutex); \
164 #define arena_for_chunk(ptr) (&main_arena)
166 #endif /* USE_ARENAS */
168 /**************************************************************************/
172 /* atfork support. */
174 static __malloc_ptr_t (*save_malloc_hook
) (size_t __size
,
175 __const __malloc_ptr_t
);
176 # if !defined _LIBC || (defined SHARED && !USE___THREAD)
177 static __malloc_ptr_t (*save_memalign_hook
) (size_t __align
, size_t __size
,
178 __const __malloc_ptr_t
);
180 static void (*save_free_hook
) (__malloc_ptr_t __ptr
,
181 __const __malloc_ptr_t
);
182 static Void_t
* save_arena
;
188 /* Magic value for the thread-specific arena pointer when
189 malloc_atfork() is in use. */
191 #define ATFORK_ARENA_PTR ((Void_t*)-1)
193 /* The following hooks are used while the `atfork' handling mechanism
197 malloc_atfork(size_t sz
, const Void_t
*caller
)
202 tsd_getspecific(arena_key
, vptr
);
203 if(vptr
== ATFORK_ARENA_PTR
) {
204 /* We are the only thread that may allocate at all. */
205 if(save_malloc_hook
!= malloc_check
) {
206 return _int_malloc(&main_arena
, sz
);
210 victim
= _int_malloc(&main_arena
, sz
+1);
211 return mem2mem_check(victim
, sz
);
214 /* Suspend the thread until the `atfork' handlers have completed.
215 By that time, the hooks will have been reset as well, so that
216 mALLOc() can be used again. */
217 (void)mutex_lock(&list_lock
);
218 (void)mutex_unlock(&list_lock
);
219 return public_mALLOc(sz
);
224 free_atfork(Void_t
* mem
, const Void_t
*caller
)
228 mchunkptr p
; /* chunk corresponding to mem */
230 if (mem
== 0) /* free(0) has no effect */
233 p
= mem2chunk(mem
); /* do not bother to replicate free_check here */
236 if (chunk_is_mmapped(p
)) /* release mmapped memory. */
243 #ifdef ATOMIC_FASTBINS
244 ar_ptr
= arena_for_chunk(p
);
245 tsd_getspecific(arena_key
, vptr
);
246 _int_free(ar_ptr
, p
, vptr
== ATFORK_ARENA_PTR
);
248 ar_ptr
= arena_for_chunk(p
);
249 tsd_getspecific(arena_key
, vptr
);
250 if(vptr
!= ATFORK_ARENA_PTR
)
251 (void)mutex_lock(&ar_ptr
->mutex
);
252 _int_free(ar_ptr
, p
);
253 if(vptr
!= ATFORK_ARENA_PTR
)
254 (void)mutex_unlock(&ar_ptr
->mutex
);
259 /* Counter for number of times the list is locked by the same thread. */
260 static unsigned int atfork_recursive_cntr
;
262 /* The following two functions are registered via thread_atfork() to
263 make sure that the mutexes remain in a consistent state in the
264 fork()ed version of a thread. Also adapt the malloc and free hooks
265 temporarily, because the `atfork' handler mechanism may use
266 malloc/free internally (e.g. in LinuxThreads). */
269 ptmalloc_lock_all (void)
273 if(__malloc_initialized
< 1)
275 if (mutex_trylock(&list_lock
))
278 tsd_getspecific(arena_key
, my_arena
);
279 if (my_arena
== ATFORK_ARENA_PTR
)
280 /* This is the same thread which already locks the global list.
281 Just bump the counter. */
284 /* This thread has to wait its turn. */
285 (void)mutex_lock(&list_lock
);
287 for(ar_ptr
= &main_arena
;;) {
288 (void)mutex_lock(&ar_ptr
->mutex
);
289 ar_ptr
= ar_ptr
->next
;
290 if(ar_ptr
== &main_arena
) break;
292 save_malloc_hook
= __malloc_hook
;
293 save_free_hook
= __free_hook
;
294 __malloc_hook
= malloc_atfork
;
295 __free_hook
= free_atfork
;
296 /* Only the current thread may perform malloc/free calls now. */
297 tsd_getspecific(arena_key
, save_arena
);
298 tsd_setspecific(arena_key
, ATFORK_ARENA_PTR
);
300 ++atfork_recursive_cntr
;
304 ptmalloc_unlock_all (void)
308 if(__malloc_initialized
< 1)
310 if (--atfork_recursive_cntr
!= 0)
312 tsd_setspecific(arena_key
, save_arena
);
313 __malloc_hook
= save_malloc_hook
;
314 __free_hook
= save_free_hook
;
315 for(ar_ptr
= &main_arena
;;) {
316 (void)mutex_unlock(&ar_ptr
->mutex
);
317 ar_ptr
= ar_ptr
->next
;
318 if(ar_ptr
== &main_arena
) break;
320 (void)mutex_unlock(&list_lock
);
325 /* In NPTL, unlocking a mutex in the child process after a
326 fork() is currently unsafe, whereas re-initializing it is safe and
327 does not leak resources. Therefore, a special atfork handler is
328 installed for the child. */
331 ptmalloc_unlock_all2 (void)
335 if(__malloc_initialized
< 1)
337 #if defined _LIBC || defined MALLOC_HOOKS
338 tsd_setspecific(arena_key
, save_arena
);
339 __malloc_hook
= save_malloc_hook
;
340 __free_hook
= save_free_hook
;
345 for(ar_ptr
= &main_arena
;;) {
346 mutex_init(&ar_ptr
->mutex
);
348 if (ar_ptr
!= save_arena
) {
349 ar_ptr
->next_free
= free_list
;
353 ar_ptr
= ar_ptr
->next
;
354 if(ar_ptr
== &main_arena
) break;
356 mutex_init(&list_lock
);
357 atfork_recursive_cntr
= 0;
362 #define ptmalloc_unlock_all2 ptmalloc_unlock_all
366 #endif /* !defined NO_THREADS */
368 /* Initialization routine. */
371 extern char **_environ
;
375 next_env_entry (char ***position
)
377 char **current
= *position
;
380 while (*current
!= NULL
)
382 if (__builtin_expect ((*current
)[0] == 'M', 0)
383 && (*current
)[1] == 'A'
384 && (*current
)[2] == 'L'
385 && (*current
)[3] == 'L'
386 && (*current
)[4] == 'O'
387 && (*current
)[5] == 'C'
388 && (*current
)[6] == '_')
390 result
= &(*current
)[7];
392 /* Save current position for next visit. */
393 *position
= ++current
;
405 /* Set up basic state so that _int_malloc et al can work. */
407 ptmalloc_init_minimal (void)
409 #if DEFAULT_TOP_PAD != 0
410 mp_
.top_pad
= DEFAULT_TOP_PAD
;
412 mp_
.n_mmaps_max
= DEFAULT_MMAP_MAX
;
413 mp_
.mmap_threshold
= DEFAULT_MMAP_THRESHOLD
;
414 mp_
.trim_threshold
= DEFAULT_TRIM_THRESHOLD
;
415 mp_
.pagesize
= malloc_getpagesize
;
417 # define NARENAS_FROM_NCORES(n) ((n) * (sizeof(long) == 4 ? 2 : 8))
418 mp_
.arena_test
= NARENAS_FROM_NCORES (1);
427 __failing_morecore (ptrdiff_t d
)
429 return (void *) MORECORE_FAILURE
;
432 extern struct dl_open_hook
*_dl_open_hook
;
433 libc_hidden_proto (_dl_open_hook
);
436 # if defined SHARED && !USE___THREAD
437 /* This is called by __pthread_initialize_minimal when it needs to use
438 malloc to set up the TLS state. We cannot do the full work of
439 ptmalloc_init (below) until __pthread_initialize_minimal has finished,
440 so it has to switch to using the special startup-time hooks while doing
441 those allocations. */
443 __libc_malloc_pthread_startup (bool first_time
)
447 ptmalloc_init_minimal ();
448 save_malloc_hook
= __malloc_hook
;
449 save_memalign_hook
= __memalign_hook
;
450 save_free_hook
= __free_hook
;
451 __malloc_hook
= malloc_starter
;
452 __memalign_hook
= memalign_starter
;
453 __free_hook
= free_starter
;
457 __malloc_hook
= save_malloc_hook
;
458 __memalign_hook
= save_memalign_hook
;
459 __free_hook
= save_free_hook
;
475 if(__malloc_initialized
>= 0) return;
476 __malloc_initialized
= 0;
479 # if defined SHARED && !USE___THREAD
480 /* ptmalloc_init_minimal may already have been called via
481 __libc_malloc_pthread_startup, above. */
482 if (mp_
.pagesize
== 0)
485 ptmalloc_init_minimal();
489 /* We know __pthread_initialize_minimal has already been called,
490 and that is enough. */
494 /* With some threads implementations, creating thread-specific data
495 or initializing a mutex may call malloc() itself. Provide a
496 simple starter version (realloc() won't work). */
497 save_malloc_hook
= __malloc_hook
;
498 save_memalign_hook
= __memalign_hook
;
499 save_free_hook
= __free_hook
;
500 __malloc_hook
= malloc_starter
;
501 __memalign_hook
= memalign_starter
;
502 __free_hook
= free_starter
;
504 /* Initialize the pthreads interface. */
505 if (__pthread_initialize
!= NULL
)
506 __pthread_initialize();
507 # endif /* !defined _LIBC */
508 # endif /* !defined NO_STARTER */
509 #endif /* !defined NO_THREADS */
510 mutex_init(&main_arena
.mutex
);
511 main_arena
.next
= &main_arena
;
513 #if defined _LIBC && defined SHARED
514 /* In case this libc copy is in a non-default namespace, never use brk.
515 Likewise if dlopened from statically linked program. */
519 if (_dl_open_hook
!= NULL
520 || (_dl_addr (ptmalloc_init
, &di
, &l
, NULL
) != 0
521 && l
->l_ns
!= LM_ID_BASE
))
522 __morecore
= __failing_morecore
;
525 mutex_init(&list_lock
);
526 tsd_key_create(&arena_key
, NULL
);
527 tsd_setspecific(arena_key
, (Void_t
*)&main_arena
);
528 thread_atfork(ptmalloc_lock_all
, ptmalloc_unlock_all
, ptmalloc_unlock_all2
);
531 __malloc_hook
= save_malloc_hook
;
532 __memalign_hook
= save_memalign_hook
;
533 __free_hook
= save_free_hook
;
539 secure
= __libc_enable_secure
;
541 if (__builtin_expect (_environ
!= NULL
, 1))
543 char **runp
= _environ
;
546 while (__builtin_expect ((envline
= next_env_entry (&runp
)) != NULL
,
549 size_t len
= strcspn (envline
, "=");
551 if (envline
[len
] != '=')
552 /* This is a "MALLOC_" variable at the end of the string
553 without a '=' character. Ignore it since otherwise we
554 will access invalid memory below. */
560 if (memcmp (envline
, "CHECK_", 6) == 0)
566 if (memcmp (envline
, "TOP_PAD_", 8) == 0)
567 mALLOPt(M_TOP_PAD
, atoi(&envline
[9]));
568 else if (memcmp (envline
, "PERTURB_", 8) == 0)
569 mALLOPt(M_PERTURB
, atoi(&envline
[9]));
575 if (memcmp (envline
, "MMAP_MAX_", 9) == 0)
576 mALLOPt(M_MMAP_MAX
, atoi(&envline
[10]));
578 else if (memcmp (envline
, "ARENA_MAX", 9) == 0)
579 mALLOPt(M_ARENA_MAX
, atoi(&envline
[10]));
587 if (memcmp (envline
, "ARENA_TEST", 10) == 0)
588 mALLOPt(M_ARENA_TEST
, atoi(&envline
[11]));
595 if (memcmp (envline
, "TRIM_THRESHOLD_", 15) == 0)
596 mALLOPt(M_TRIM_THRESHOLD
, atoi(&envline
[16]));
597 else if (memcmp (envline
, "MMAP_THRESHOLD_", 15) == 0)
598 mALLOPt(M_MMAP_THRESHOLD
, atoi(&envline
[16]));
609 if((s
= getenv("MALLOC_TRIM_THRESHOLD_")))
610 mALLOPt(M_TRIM_THRESHOLD
, atoi(s
));
611 if((s
= getenv("MALLOC_TOP_PAD_")))
612 mALLOPt(M_TOP_PAD
, atoi(s
));
613 if((s
= getenv("MALLOC_PERTURB_")))
614 mALLOPt(M_PERTURB
, atoi(s
));
615 if((s
= getenv("MALLOC_MMAP_THRESHOLD_")))
616 mALLOPt(M_MMAP_THRESHOLD
, atoi(s
));
617 if((s
= getenv("MALLOC_MMAP_MAX_")))
618 mALLOPt(M_MMAP_MAX
, atoi(s
));
620 s
= getenv("MALLOC_CHECK_");
623 mALLOPt(M_CHECK_ACTION
, (int)(s
[0] - '0'));
624 if (check_action
!= 0)
625 __malloc_check_init();
627 void (*hook
) (void) = force_reg (__malloc_initialize_hook
);
630 __malloc_initialized
= 1;
633 /* There are platforms (e.g. Hurd) with a link-time hook mechanism. */
634 #ifdef thread_atfork_static
635 thread_atfork_static(ptmalloc_lock_all
, ptmalloc_unlock_all
, \
636 ptmalloc_unlock_all2
)
641 /* Managing heaps and arenas (for concurrent threads) */
647 /* Print the complete contents of a single heap to stderr. */
651 dump_heap(heap_info
*heap
)
653 dump_heap(heap
) heap_info
*heap
;
659 fprintf(stderr
, "Heap %p, size %10lx:\n", heap
, (long)heap
->size
);
660 ptr
= (heap
->ar_ptr
!= (mstate
)(heap
+1)) ?
661 (char*)(heap
+ 1) : (char*)(heap
+ 1) + sizeof(struct malloc_state
);
662 p
= (mchunkptr
)(((unsigned long)ptr
+ MALLOC_ALIGN_MASK
) &
665 fprintf(stderr
, "chunk %p size %10lx", p
, (long)p
->size
);
666 if(p
== top(heap
->ar_ptr
)) {
667 fprintf(stderr
, " (top)\n");
669 } else if(p
->size
== (0|PREV_INUSE
)) {
670 fprintf(stderr
, " (fence)\n");
673 fprintf(stderr
, "\n");
678 #endif /* MALLOC_DEBUG > 1 */
680 /* If consecutive mmap (0, HEAP_MAX_SIZE << 1, ...) calls return decreasing
681 addresses as opposed to increasing, new_heap would badly fragment the
682 address space. In that case remember the second HEAP_MAX_SIZE part
683 aligned to HEAP_MAX_SIZE from last mmap (0, HEAP_MAX_SIZE << 1, ...)
684 call (if it is already aligned) and try to reuse it next time. We need
685 no locking for it, as kernel ensures the atomicity for us - worst case
686 we'll call mmap (addr, HEAP_MAX_SIZE, ...) for some value of addr in
687 multiple threads, but only one will succeed. */
688 static char *aligned_heap_area
;
690 /* Create a new heap. size is automatically rounded up to a multiple
696 new_heap(size_t size
, size_t top_pad
)
698 new_heap(size
, top_pad
) size_t size
, top_pad
;
701 size_t page_mask
= malloc_getpagesize
- 1;
706 if(size
+top_pad
< HEAP_MIN_SIZE
)
707 size
= HEAP_MIN_SIZE
;
708 else if(size
+top_pad
<= HEAP_MAX_SIZE
)
710 else if(size
> HEAP_MAX_SIZE
)
713 size
= HEAP_MAX_SIZE
;
714 size
= (size
+ page_mask
) & ~page_mask
;
716 /* A memory region aligned to a multiple of HEAP_MAX_SIZE is needed.
717 No swap space needs to be reserved for the following large
718 mapping (on Linux, this is the case for all non-writable mappings
721 if(aligned_heap_area
) {
722 p2
= (char *)MMAP(aligned_heap_area
, HEAP_MAX_SIZE
, PROT_NONE
,
723 MAP_PRIVATE
|MAP_NORESERVE
);
724 aligned_heap_area
= NULL
;
725 if (p2
!= MAP_FAILED
&& ((unsigned long)p2
& (HEAP_MAX_SIZE
-1))) {
726 munmap(p2
, HEAP_MAX_SIZE
);
730 if(p2
== MAP_FAILED
) {
731 p1
= (char *)MMAP(0, HEAP_MAX_SIZE
<<1, PROT_NONE
,
732 MAP_PRIVATE
|MAP_NORESERVE
);
733 if(p1
!= MAP_FAILED
) {
734 p2
= (char *)(((unsigned long)p1
+ (HEAP_MAX_SIZE
-1))
735 & ~(HEAP_MAX_SIZE
-1));
740 aligned_heap_area
= p2
+ HEAP_MAX_SIZE
;
741 munmap(p2
+ HEAP_MAX_SIZE
, HEAP_MAX_SIZE
- ul
);
743 /* Try to take the chance that an allocation of only HEAP_MAX_SIZE
744 is already aligned. */
745 p2
= (char *)MMAP(0, HEAP_MAX_SIZE
, PROT_NONE
, MAP_PRIVATE
|MAP_NORESERVE
);
748 if((unsigned long)p2
& (HEAP_MAX_SIZE
-1)) {
749 munmap(p2
, HEAP_MAX_SIZE
);
754 if(mprotect(p2
, size
, PROT_READ
|PROT_WRITE
) != 0) {
755 munmap(p2
, HEAP_MAX_SIZE
);
760 h
->mprotect_size
= size
;
761 THREAD_STAT(stat_n_heaps
++);
765 /* Grow a heap. size is automatically rounded up to a
766 multiple of the page size. */
770 grow_heap(heap_info
*h
, long diff
)
772 grow_heap(h
, diff
) heap_info
*h
; long diff
;
775 size_t page_mask
= malloc_getpagesize
- 1;
778 diff
= (diff
+ page_mask
) & ~page_mask
;
779 new_size
= (long)h
->size
+ diff
;
780 if((unsigned long) new_size
> (unsigned long) HEAP_MAX_SIZE
)
782 if((unsigned long) new_size
> h
->mprotect_size
) {
783 if (mprotect((char *)h
+ h
->mprotect_size
,
784 (unsigned long) new_size
- h
->mprotect_size
,
785 PROT_READ
|PROT_WRITE
) != 0)
787 h
->mprotect_size
= new_size
;
798 shrink_heap(heap_info
*h
, long diff
)
800 shrink_heap(h
, diff
) heap_info
*h
; long diff
;
805 new_size
= (long)h
->size
- diff
;
806 if(new_size
< (long)sizeof(*h
))
808 /* Try to re-map the extra heap space freshly to save memory, and
809 make it inaccessible. */
811 if (__builtin_expect (__libc_enable_secure
, 0))
816 if((char *)MMAP((char *)h
+ new_size
, diff
, PROT_NONE
,
817 MAP_PRIVATE
|MAP_FIXED
) == (char *) MAP_FAILED
)
819 h
->mprotect_size
= new_size
;
823 madvise ((char *)h
+ new_size
, diff
, MADV_DONTNEED
);
825 /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/
833 #define delete_heap(heap) \
835 if ((char *)(heap) + HEAP_MAX_SIZE == aligned_heap_area) \
836 aligned_heap_area = NULL; \
837 munmap((char*)(heap), HEAP_MAX_SIZE); \
843 heap_trim(heap_info
*heap
, size_t pad
)
845 heap_trim(heap
, pad
) heap_info
*heap
; size_t pad
;
848 mstate ar_ptr
= heap
->ar_ptr
;
849 unsigned long pagesz
= mp_
.pagesize
;
850 mchunkptr top_chunk
= top(ar_ptr
), p
, bck
, fwd
;
851 heap_info
*prev_heap
;
852 long new_size
, top_size
, extra
;
854 /* Can this heap go away completely? */
855 while(top_chunk
== chunk_at_offset(heap
, sizeof(*heap
))) {
856 prev_heap
= heap
->prev
;
857 p
= chunk_at_offset(prev_heap
, prev_heap
->size
- (MINSIZE
-2*SIZE_SZ
));
858 assert(p
->size
== (0|PREV_INUSE
)); /* must be fencepost */
860 new_size
= chunksize(p
) + (MINSIZE
-2*SIZE_SZ
);
861 assert(new_size
>0 && new_size
<(long)(2*MINSIZE
));
863 new_size
+= p
->prev_size
;
864 assert(new_size
>0 && new_size
<HEAP_MAX_SIZE
);
865 if(new_size
+ (HEAP_MAX_SIZE
- prev_heap
->size
) < pad
+ MINSIZE
+ pagesz
)
867 ar_ptr
->system_mem
-= heap
->size
;
868 arena_mem
-= heap
->size
;
871 if(!prev_inuse(p
)) { /* consolidate backward */
875 assert(((unsigned long)((char*)p
+ new_size
) & (pagesz
-1)) == 0);
876 assert( ((char*)p
+ new_size
) == ((char*)heap
+ heap
->size
) );
877 top(ar_ptr
) = top_chunk
= p
;
878 set_head(top_chunk
, new_size
| PREV_INUSE
);
879 /*check_chunk(ar_ptr, top_chunk);*/
881 top_size
= chunksize(top_chunk
);
882 extra
= ((top_size
- pad
- MINSIZE
+ (pagesz
-1))/pagesz
- 1) * pagesz
;
883 if(extra
< (long)pagesz
)
886 if(shrink_heap(heap
, extra
) != 0)
888 ar_ptr
->system_mem
-= extra
;
891 /* Success. Adjust top accordingly. */
892 set_head(top_chunk
, (top_size
- extra
) | PREV_INUSE
);
893 /*check_chunk(ar_ptr, top_chunk);*/
897 /* Create a new arena with initial size "size". */
900 _int_new_arena(size_t size
)
905 unsigned long misalign
;
907 h
= new_heap(size
+ (sizeof(*h
) + sizeof(*a
) + MALLOC_ALIGNMENT
),
910 /* Maybe size is too large to fit in a single heap. So, just try
911 to create a minimally-sized arena and let _int_malloc() attempt
912 to deal with the large request via mmap_chunk(). */
913 h
= new_heap(sizeof(*h
) + sizeof(*a
) + MALLOC_ALIGNMENT
, mp_
.top_pad
);
917 a
= h
->ar_ptr
= (mstate
)(h
+1);
918 malloc_init_state(a
);
920 a
->system_mem
= a
->max_system_mem
= h
->size
;
921 arena_mem
+= h
->size
;
923 if((unsigned long)(mp_
.mmapped_mem
+ arena_mem
+ main_arena
.system_mem
) >
925 mp_
.max_total_mem
= mp_
.mmapped_mem
+ arena_mem
+ main_arena
.system_mem
;
928 /* Set up the top chunk, with proper alignment. */
929 ptr
= (char *)(a
+ 1);
930 misalign
= (unsigned long)chunk2mem(ptr
) & MALLOC_ALIGN_MASK
;
932 ptr
+= MALLOC_ALIGNMENT
- misalign
;
933 top(a
) = (mchunkptr
)ptr
;
934 set_head(top(a
), (((char*)h
+ h
->size
) - ptr
) | PREV_INUSE
);
936 tsd_setspecific(arena_key
, (Void_t
*)a
);
937 mutex_init(&a
->mutex
);
938 (void)mutex_lock(&a
->mutex
);
941 (void)mutex_lock(&list_lock
);
944 /* Add the new arena to the global list. */
945 a
->next
= main_arena
.next
;
946 atomic_write_barrier ();
952 (void)mutex_unlock(&list_lock
);
955 THREAD_STAT(++(a
->stat_lock_loop
));
965 mstate result
= free_list
;
968 (void)mutex_lock(&list_lock
);
971 free_list
= result
->next_free
;
972 (void)mutex_unlock(&list_lock
);
976 (void)mutex_lock(&result
->mutex
);
977 tsd_setspecific(arena_key
, (Void_t
*)result
);
978 THREAD_STAT(++(result
->stat_lock_loop
));
989 if (narenas
<= mp_
.arena_test
)
992 static int narenas_limit
;
993 if (narenas_limit
== 0)
995 if (mp_
.arena_max
!= 0)
996 narenas_limit
= mp_
.arena_max
;
999 int n
= __get_nprocs ();
1002 narenas_limit
= NARENAS_FROM_NCORES (n
);
1004 /* We have no information about the system. Assume two
1006 narenas_limit
= NARENAS_FROM_NCORES (2);
1010 if (narenas
< narenas_limit
)
1014 static mstate next_to_use
;
1015 if (next_to_use
== NULL
)
1016 next_to_use
= &main_arena
;
1018 result
= next_to_use
;
1021 if (!mutex_trylock(&result
->mutex
))
1024 result
= result
->next
;
1026 while (result
!= next_to_use
);
1028 /* No arena available. Wait for the next in line. */
1029 (void)mutex_lock(&result
->mutex
);
1032 tsd_setspecific(arena_key
, (Void_t
*)result
);
1033 THREAD_STAT(++(result
->stat_lock_loop
));
1034 next_to_use
= result
->next
;
1043 arena_get2(mstate a_tsd
, size_t size
)
1045 arena_get2(a_tsd
, size
) mstate a_tsd
; size_t size
;
1051 if ((a
= get_free_list ()) == NULL
1052 && (a
= reused_arena ()) == NULL
)
1053 /* Nothing immediately available, so generate a new arena. */
1054 a
= _int_new_arena(size
);
1057 a
= a_tsd
= &main_arena
;
1061 /* This can only happen while initializing the new arena. */
1062 (void)mutex_lock(&main_arena
.mutex
);
1063 THREAD_STAT(++(main_arena
.stat_lock_wait
));
1068 /* Check the global, circularly linked list for available arenas. */
1069 bool retried
= false;
1072 if(!mutex_trylock(&a
->mutex
)) {
1074 (void)mutex_unlock(&list_lock
);
1075 THREAD_STAT(++(a
->stat_lock_loop
));
1076 tsd_setspecific(arena_key
, (Void_t
*)a
);
1080 } while(a
!= a_tsd
);
1082 /* If not even the list_lock can be obtained, try again. This can
1083 happen during `atfork', or for example on systems where thread
1084 creation makes it temporarily impossible to obtain _any_
1086 if(!retried
&& mutex_trylock(&list_lock
)) {
1087 /* We will block to not run in a busy loop. */
1088 (void)mutex_lock(&list_lock
);
1090 /* Since we blocked there might be an arena available now. */
1096 /* Nothing immediately available, so generate a new arena. */
1097 a
= _int_new_arena(size
);
1098 (void)mutex_unlock(&list_lock
);
1105 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
1106 arena_thread_freeres (void)
1108 Void_t
*vptr
= NULL
;
1109 mstate a
= tsd_getspecific(arena_key
, vptr
);
1110 tsd_setspecific(arena_key
, NULL
);
1114 (void)mutex_lock(&list_lock
);
1115 a
->next_free
= free_list
;
1117 (void)mutex_unlock(&list_lock
);
1120 text_set_element (__libc_thread_subfreeres
, arena_thread_freeres
);
1123 #endif /* USE_ARENAS */