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. */
23 /* Compile-time constants. */
25 #define HEAP_MIN_SIZE (32*1024)
27 #define HEAP_MAX_SIZE (1024*1024) /* must be a power of two */
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. */
39 #define THREAD_STATS 0
42 /* If THREAD_STATS is non-zero, some statistics on mutex locking are
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
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. */
61 /* Thread specific data */
63 static tsd_key_t arena_key
;
64 static mutex_t list_lock
;
67 static int stat_n_heaps
;
68 #define THREAD_STAT(x) x
70 #define THREAD_STAT(x) do ; while(0)
73 /* Mapped memory in non-main arenas (reliable only for NO_THREADS). */
74 static unsigned long arena_mem
;
76 /**************************************************************************/
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
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)); \
94 ptr = arena_get2(ptr, (size)); \
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. */
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); \
114 (void)mutex_lock(&ar_ptr->mutex); \
115 ++(ar_ptr->stat_lock_wait); \
119 #define arena_get(ar_ptr, sz) do { \
120 ar_ptr = &main_arena; \
121 (void)mutex_lock(&ar_ptr->mutex); \
124 #define arena_for_chunk(ptr) (&main_arena)
126 #endif /* USE_ARENAS */
128 /**************************************************************************/
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
149 malloc_atfork(size_t sz
, const Void_t
*caller
)
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
);
162 victim
= _int_malloc(&main_arena
, sz
+1);
163 return mem2mem_check(victim
, sz
);
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
);
176 free_atfork(Void_t
* mem
, const Void_t
*caller
)
180 mchunkptr p
; /* chunk corresponding to mem */
182 if (mem
== 0) /* free(0) has no effect */
185 p
= mem2chunk(mem
); /* do not bother to replicate free_check here */
188 if (chunk_is_mmapped(p
)) /* release mmapped memory. */
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). */
211 ptmalloc_lock_all
__MALLOC_P((void))
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
);
231 ptmalloc_unlock_all
__MALLOC_P((void))
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
);
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. */
254 ptmalloc_unlock_all2
__MALLOC_P((void))
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
;
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
);
273 #define ptmalloc_unlock_all2 ptmalloc_unlock_all
277 #endif /* !defined NO_THREADS */
279 /* Already initialized? */
280 int __malloc_initialized
= -1;
282 /* Initialization routine. */
285 extern char **_environ
;
289 next_env_entry (char ***position
)
291 char **current
= *position
;
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
;
320 ptmalloc_init
__MALLOC_P((void))
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
;
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
;
347 /* Initialize the pthreads interface. */
348 if (__pthread_initialize
!= NULL
)
349 __pthread_initialize();
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
);
360 __malloc_hook
= save_malloc_hook
;
361 __free_hook
= save_free_hook
;
364 secure
= __libc_enable_secure
;
367 char **runp
= _environ
;
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. */
384 if (memcmp (envline
, "CHECK_", 6) == 0)
388 if (! secure
&& memcmp (envline
, "TOP_PAD_", 8) == 0)
389 mALLOPt(M_TOP_PAD
, atoi(&envline
[9]));
392 if (! secure
&& memcmp (envline
, "MMAP_MAX_", 9) == 0)
393 mALLOPt(M_MMAP_MAX
, atoi(&envline
[10]));
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]));
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_");
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
)
440 /* Managing heaps and arenas (for concurrent threads) */
446 /* Print the complete contents of a single heap to stderr. */
450 dump_heap(heap_info
*heap
)
452 dump_heap(heap
) heap_info
*heap
;
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
) &
464 fprintf(stderr
, "chunk %p size %10lx", p
, (long)p
->size
);
465 if(p
== top(heap
->ar_ptr
)) {
466 fprintf(stderr
, " (top)\n");
468 } else if(p
->size
== (0|PREV_INUSE
)) {
469 fprintf(stderr
, " (fence)\n");
472 fprintf(stderr
, "\n");
477 #endif /* MALLOC_DEBUG > 1 */
479 /* Create a new heap. size is automatically rounded up to a multiple
485 new_heap(size_t size
, size_t top_pad
)
487 new_heap(size
, top_pad
) size_t size
, top_pad
;
490 size_t page_mask
= malloc_getpagesize
- 1;
495 if(size
+top_pad
< HEAP_MIN_SIZE
)
496 size
= HEAP_MIN_SIZE
;
497 else if(size
+top_pad
<= HEAP_MAX_SIZE
)
499 else if(size
> HEAP_MAX_SIZE
)
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
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));
514 munmap(p2
+ HEAP_MAX_SIZE
, HEAP_MAX_SIZE
- ul
);
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
);
521 if((unsigned long)p2
& (HEAP_MAX_SIZE
-1)) {
522 munmap(p2
, HEAP_MAX_SIZE
);
526 if(mprotect(p2
, size
, PROT_READ
|PROT_WRITE
) != 0) {
527 munmap(p2
, HEAP_MAX_SIZE
);
532 THREAD_STAT(stat_n_heaps
++);
536 /* Grow or shrink a heap. size is automatically rounded up to a
537 multiple of the page size if it is positive. */
541 grow_heap(heap_info
*h
, long diff
)
543 grow_heap(h
, diff
) heap_info
*h
; long diff
;
546 size_t page_mask
= malloc_getpagesize
- 1;
550 diff
= (diff
+ page_mask
) & ~page_mask
;
551 new_size
= (long)h
->size
+ diff
;
552 if(new_size
> HEAP_MAX_SIZE
)
554 if(mprotect((char *)h
+ h
->size
, diff
, PROT_READ
|PROT_WRITE
) != 0)
557 new_size
= (long)h
->size
+ diff
;
558 if(new_size
< (long)sizeof(*h
))
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
)
565 /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/
573 #define delete_heap(heap) munmap((char*)(heap), HEAP_MAX_SIZE)
578 heap_trim(heap_info
*heap
, size_t pad
)
580 heap_trim(heap
, pad
) heap_info
*heap
; size_t pad
;
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 */
595 new_size
= chunksize(p
) + (MINSIZE
-2*SIZE_SZ
);
596 assert(new_size
>0 && new_size
<(long)(2*MINSIZE
));
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
)
602 ar_ptr
->system_mem
-= heap
->size
;
603 arena_mem
-= heap
->size
;
606 if(!prev_inuse(p
)) { /* consolidate backward */
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
)
621 if(grow_heap(heap
, -extra
) != 0)
623 ar_ptr
->system_mem
-= extra
;
626 /* Success. Adjust top accordingly. */
627 set_head(top_chunk
, (top_size
- extra
) | PREV_INUSE
);
628 /*check_chunk(ar_ptr, top_chunk);*/
635 arena_get2(mstate a_tsd
, size_t size
)
637 arena_get2(a_tsd
, size
) mstate a_tsd
; size_t size
;
644 a
= a_tsd
= &main_arena
;
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
));
655 /* Check the global, circularly linked list for available arenas. */
658 if(!mutex_trylock(&a
->mutex
)) {
659 THREAD_STAT(++(a
->stat_lock_loop
));
660 tsd_setspecific(arena_key
, (Void_t
*)a
);
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_
670 if(mutex_trylock(&list_lock
)) {
674 (void)mutex_unlock(&list_lock
);
676 /* Nothing immediately available, so generate a new arena. */
677 a
= _int_new_arena(size
);
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
;
689 (void)mutex_unlock(&list_lock
);
691 if(err
) /* locking failed; keep arena for further attempts later */
694 THREAD_STAT(++(a
->stat_lock_loop
));
698 /* Create a new arena with initial size "size". */
701 _int_new_arena(size_t size
)
706 unsigned long misalign
;
708 h
= new_heap(size
+ (sizeof(*h
) + sizeof(*a
) + MALLOC_ALIGNMENT
),
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
);
718 a
= h
->ar_ptr
= (mstate
)(h
+1);
719 malloc_init_state(a
);
721 a
->system_mem
= a
->max_system_mem
= h
->size
;
722 arena_mem
+= h
->size
;
724 if((unsigned long)(mp_
.mmapped_mem
+ arena_mem
+ main_arena
.system_mem
) >
726 mp_
.max_total_mem
= mp_
.mmapped_mem
+ arena_mem
+ main_arena
.system_mem
;
729 /* Set up the top chunk, with proper alignment. */
730 ptr
= (char *)(a
+ 1);
731 misalign
= (unsigned long)chunk2mem(ptr
) & MALLOC_ALIGN_MASK
;
733 ptr
+= MALLOC_ALIGNMENT
- misalign
;
734 top(a
) = (mchunkptr
)ptr
;
735 set_head(top(a
), (((char*)h
+ h
->size
) - ptr
) | PREV_INUSE
);
740 #endif /* USE_ARENAS */