]>
Commit | Line | Data |
---|---|---|
fa8d436c | 1 | /* Malloc implementation for multiple threads without lock contention. |
06d6611a | 2 | Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
fa8d436c UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Wolfram Gloger <wg@malloc.de>, 2001. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
cc7375ce RM |
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 | |
fa8d436c UD |
9 | License, or (at your option) any later version. |
10 | ||
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 | |
cc7375ce | 14 | Lesser General Public License for more details. |
fa8d436c | 15 | |
cc7375ce | 16 | You should have received a copy of the GNU Lesser General Public |
fa8d436c UD |
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. */ | |
20 | ||
21 | /* $Id$ */ | |
22 | ||
23 | /* Compile-time constants. */ | |
24 | ||
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 | |
29 | ||
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. */ | |
36 | ||
37 | ||
38 | #ifndef THREAD_STATS | |
39 | #define THREAD_STATS 0 | |
40 | #endif | |
41 | ||
42 | /* If THREAD_STATS is non-zero, some statistics on mutex locking are | |
43 | computed. */ | |
44 | ||
45 | /***************************************************************************/ | |
46 | ||
47 | #define top(ar_ptr) ((ar_ptr)->top) | |
48 | ||
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. */ | |
53 | ||
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; | |
60 | ||
61 | /* Thread specific data */ | |
62 | ||
63 | static tsd_key_t arena_key; | |
64 | static mutex_t list_lock; | |
65 | ||
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 | |
72 | ||
73 | /* Mapped memory in non-main arenas (reliable only for NO_THREADS). */ | |
74 | static unsigned long arena_mem; | |
75 | ||
2a652f5a RM |
76 | /* Already initialized? */ |
77 | int __malloc_initialized = -1; | |
78 | ||
fa8d436c UD |
79 | /**************************************************************************/ |
80 | ||
81 | #if USE_ARENAS | |
82 | ||
83 | /* arena_get() acquires an arena and locks the corresponding mutex. | |
84 | First, try the one last locked successfully by this thread. (This | |
85 | is the common case and handled with a macro for speed.) Then, loop | |
86 | once over the circularly linked list of arenas. If no arena is | |
87 | readily available, create a new one. In this latter case, `size' | |
88 | is just a hint as to how much memory will be required immediately | |
89 | in the new arena. */ | |
90 | ||
91 | #define arena_get(ptr, size) do { \ | |
92 | Void_t *vptr = NULL; \ | |
93 | ptr = (mstate)tsd_getspecific(arena_key, vptr); \ | |
94 | if(ptr && !mutex_trylock(&ptr->mutex)) { \ | |
95 | THREAD_STAT(++(ptr->stat_lock_direct)); \ | |
96 | } else \ | |
97 | ptr = arena_get2(ptr, (size)); \ | |
98 | } while(0) | |
99 | ||
100 | /* find the heap and corresponding arena for a given ptr */ | |
101 | ||
102 | #define heap_for_ptr(ptr) \ | |
103 | ((heap_info *)((unsigned long)(ptr) & ~(HEAP_MAX_SIZE-1))) | |
104 | #define arena_for_chunk(ptr) \ | |
105 | (chunk_non_main_arena(ptr) ? heap_for_ptr(ptr)->ar_ptr : &main_arena) | |
106 | ||
107 | #else /* !USE_ARENAS */ | |
108 | ||
109 | /* There is only one arena, main_arena. */ | |
110 | ||
111 | #if THREAD_STATS | |
112 | #define arena_get(ar_ptr, sz) do { \ | |
113 | ar_ptr = &main_arena; \ | |
114 | if(!mutex_trylock(&ar_ptr->mutex)) \ | |
115 | ++(ar_ptr->stat_lock_direct); \ | |
116 | else { \ | |
117 | (void)mutex_lock(&ar_ptr->mutex); \ | |
118 | ++(ar_ptr->stat_lock_wait); \ | |
119 | } \ | |
120 | } while(0) | |
121 | #else | |
122 | #define arena_get(ar_ptr, sz) do { \ | |
123 | ar_ptr = &main_arena; \ | |
124 | (void)mutex_lock(&ar_ptr->mutex); \ | |
125 | } while(0) | |
126 | #endif | |
127 | #define arena_for_chunk(ptr) (&main_arena) | |
128 | ||
129 | #endif /* USE_ARENAS */ | |
130 | ||
131 | /**************************************************************************/ | |
132 | ||
133 | #ifndef NO_THREADS | |
134 | ||
135 | /* atfork support. */ | |
136 | ||
06d6611a UD |
137 | static __malloc_ptr_t (*save_malloc_hook) (size_t __size, |
138 | __const __malloc_ptr_t); | |
fde89ad0 | 139 | # if !defined _LIBC || !defined USE_TLS || (defined SHARED && !USE___THREAD) |
06d6611a UD |
140 | static __malloc_ptr_t (*save_memalign_hook) (size_t __align, size_t __size, |
141 | __const __malloc_ptr_t); | |
fde89ad0 | 142 | # endif |
06d6611a UD |
143 | static void (*save_free_hook) (__malloc_ptr_t __ptr, |
144 | __const __malloc_ptr_t); | |
fa8d436c UD |
145 | static Void_t* save_arena; |
146 | ||
147 | /* Magic value for the thread-specific arena pointer when | |
148 | malloc_atfork() is in use. */ | |
149 | ||
150 | #define ATFORK_ARENA_PTR ((Void_t*)-1) | |
151 | ||
152 | /* The following hooks are used while the `atfork' handling mechanism | |
153 | is active. */ | |
154 | ||
155 | static Void_t* | |
156 | malloc_atfork(size_t sz, const Void_t *caller) | |
157 | { | |
158 | Void_t *vptr = NULL; | |
159 | Void_t *victim; | |
160 | ||
161 | tsd_getspecific(arena_key, vptr); | |
162 | if(vptr == ATFORK_ARENA_PTR) { | |
163 | /* We are the only thread that may allocate at all. */ | |
164 | if(save_malloc_hook != malloc_check) { | |
165 | return _int_malloc(&main_arena, sz); | |
166 | } else { | |
167 | if(top_check()<0) | |
168 | return 0; | |
169 | victim = _int_malloc(&main_arena, sz+1); | |
170 | return mem2mem_check(victim, sz); | |
171 | } | |
172 | } else { | |
173 | /* Suspend the thread until the `atfork' handlers have completed. | |
174 | By that time, the hooks will have been reset as well, so that | |
175 | mALLOc() can be used again. */ | |
176 | (void)mutex_lock(&list_lock); | |
177 | (void)mutex_unlock(&list_lock); | |
178 | return public_mALLOc(sz); | |
179 | } | |
180 | } | |
181 | ||
182 | static void | |
183 | free_atfork(Void_t* mem, const Void_t *caller) | |
184 | { | |
185 | Void_t *vptr = NULL; | |
186 | mstate ar_ptr; | |
187 | mchunkptr p; /* chunk corresponding to mem */ | |
188 | ||
189 | if (mem == 0) /* free(0) has no effect */ | |
190 | return; | |
191 | ||
192 | p = mem2chunk(mem); /* do not bother to replicate free_check here */ | |
193 | ||
194 | #if HAVE_MMAP | |
195 | if (chunk_is_mmapped(p)) /* release mmapped memory. */ | |
196 | { | |
197 | munmap_chunk(p); | |
198 | return; | |
199 | } | |
200 | #endif | |
201 | ||
202 | ar_ptr = arena_for_chunk(p); | |
203 | tsd_getspecific(arena_key, vptr); | |
204 | if(vptr != ATFORK_ARENA_PTR) | |
205 | (void)mutex_lock(&ar_ptr->mutex); | |
206 | _int_free(ar_ptr, mem); | |
207 | if(vptr != ATFORK_ARENA_PTR) | |
208 | (void)mutex_unlock(&ar_ptr->mutex); | |
209 | } | |
210 | ||
211 | /* The following two functions are registered via thread_atfork() to | |
212 | make sure that the mutexes remain in a consistent state in the | |
213 | fork()ed version of a thread. Also adapt the malloc and free hooks | |
214 | temporarily, because the `atfork' handler mechanism may use | |
215 | malloc/free internally (e.g. in LinuxThreads). */ | |
216 | ||
217 | static void | |
06d6611a | 218 | ptmalloc_lock_all (void) |
fa8d436c UD |
219 | { |
220 | mstate ar_ptr; | |
221 | ||
2a652f5a RM |
222 | if(__malloc_initialized < 1) |
223 | return; | |
fa8d436c UD |
224 | (void)mutex_lock(&list_lock); |
225 | for(ar_ptr = &main_arena;;) { | |
226 | (void)mutex_lock(&ar_ptr->mutex); | |
227 | ar_ptr = ar_ptr->next; | |
228 | if(ar_ptr == &main_arena) break; | |
229 | } | |
230 | save_malloc_hook = __malloc_hook; | |
231 | save_free_hook = __free_hook; | |
232 | __malloc_hook = malloc_atfork; | |
233 | __free_hook = free_atfork; | |
234 | /* Only the current thread may perform malloc/free calls now. */ | |
235 | tsd_getspecific(arena_key, save_arena); | |
236 | tsd_setspecific(arena_key, ATFORK_ARENA_PTR); | |
237 | } | |
238 | ||
239 | static void | |
06d6611a | 240 | ptmalloc_unlock_all (void) |
fa8d436c UD |
241 | { |
242 | mstate ar_ptr; | |
243 | ||
2a652f5a RM |
244 | if(__malloc_initialized < 1) |
245 | return; | |
fa8d436c UD |
246 | tsd_setspecific(arena_key, save_arena); |
247 | __malloc_hook = save_malloc_hook; | |
248 | __free_hook = save_free_hook; | |
249 | for(ar_ptr = &main_arena;;) { | |
250 | (void)mutex_unlock(&ar_ptr->mutex); | |
251 | ar_ptr = ar_ptr->next; | |
252 | if(ar_ptr == &main_arena) break; | |
253 | } | |
254 | (void)mutex_unlock(&list_lock); | |
255 | } | |
256 | ||
257 | #ifdef __linux__ | |
258 | ||
259 | /* In LinuxThreads, unlocking a mutex in the child process after a | |
260 | fork() is currently unsafe, whereas re-initializing it is safe and | |
261 | does not leak resources. Therefore, a special atfork handler is | |
262 | installed for the child. */ | |
263 | ||
264 | static void | |
06d6611a | 265 | ptmalloc_unlock_all2 (void) |
fa8d436c UD |
266 | { |
267 | mstate ar_ptr; | |
268 | ||
2a652f5a RM |
269 | if(__malloc_initialized < 1) |
270 | return; | |
fa8d436c UD |
271 | #if defined _LIBC || defined MALLOC_HOOKS |
272 | tsd_setspecific(arena_key, save_arena); | |
273 | __malloc_hook = save_malloc_hook; | |
274 | __free_hook = save_free_hook; | |
275 | #endif | |
276 | for(ar_ptr = &main_arena;;) { | |
fdb933e2 | 277 | mutex_init(&ar_ptr->mutex); |
fa8d436c UD |
278 | ar_ptr = ar_ptr->next; |
279 | if(ar_ptr == &main_arena) break; | |
280 | } | |
fdb933e2 | 281 | mutex_init(&list_lock); |
fa8d436c UD |
282 | } |
283 | ||
284 | #else | |
285 | ||
286 | #define ptmalloc_unlock_all2 ptmalloc_unlock_all | |
287 | ||
288 | #endif | |
289 | ||
290 | #endif /* !defined NO_THREADS */ | |
291 | ||
fa8d436c UD |
292 | /* Initialization routine. */ |
293 | #ifdef _LIBC | |
294 | #include <string.h> | |
295 | extern char **_environ; | |
296 | ||
297 | static char * | |
298 | internal_function | |
299 | next_env_entry (char ***position) | |
300 | { | |
301 | char **current = *position; | |
302 | char *result = NULL; | |
303 | ||
304 | while (*current != NULL) | |
305 | { | |
306 | if (__builtin_expect ((*current)[0] == 'M', 0) | |
307 | && (*current)[1] == 'A' | |
308 | && (*current)[2] == 'L' | |
309 | && (*current)[3] == 'L' | |
310 | && (*current)[4] == 'O' | |
311 | && (*current)[5] == 'C' | |
312 | && (*current)[6] == '_') | |
313 | { | |
314 | result = &(*current)[7]; | |
315 | ||
316 | /* Save current position for next visit. */ | |
317 | *position = ++current; | |
318 | ||
319 | break; | |
320 | } | |
321 | ||
322 | ++current; | |
323 | } | |
324 | ||
325 | return result; | |
326 | } | |
327 | #endif /* _LIBC */ | |
328 | ||
fde89ad0 RM |
329 | /* Set up basic state so that _int_malloc et al can work. */ |
330 | static void | |
06d6611a | 331 | ptmalloc_init_minimal (void) |
fde89ad0 RM |
332 | { |
333 | #if DEFAULT_TOP_PAD != 0 | |
334 | mp_.top_pad = DEFAULT_TOP_PAD; | |
335 | #endif | |
336 | mp_.n_mmaps_max = DEFAULT_MMAP_MAX; | |
337 | mp_.mmap_threshold = DEFAULT_MMAP_THRESHOLD; | |
338 | mp_.trim_threshold = DEFAULT_TRIM_THRESHOLD; | |
339 | mp_.pagesize = malloc_getpagesize; | |
340 | } | |
341 | ||
c0f62c56 | 342 | |
fde89ad0 | 343 | #ifdef _LIBC |
c0f62c56 UD |
344 | # ifdef SHARED |
345 | static void * | |
346 | __failing_morecore (ptrdiff_t d) | |
347 | { | |
348 | return (void *) MORECORE_FAILURE; | |
349 | } | |
350 | # endif | |
351 | ||
fde89ad0 RM |
352 | # if defined SHARED && defined USE_TLS && !USE___THREAD |
353 | # include <stdbool.h> | |
354 | ||
355 | /* This is called by __pthread_initialize_minimal when it needs to use | |
356 | malloc to set up the TLS state. We cannot do the full work of | |
357 | ptmalloc_init (below) until __pthread_initialize_minimal has finished, | |
358 | so it has to switch to using the special startup-time hooks while doing | |
359 | those allocations. */ | |
360 | void | |
361 | __libc_malloc_pthread_startup (bool first_time) | |
362 | { | |
363 | if (first_time) | |
364 | { | |
365 | ptmalloc_init_minimal (); | |
366 | save_malloc_hook = __malloc_hook; | |
367 | save_memalign_hook = __memalign_hook; | |
368 | save_free_hook = __free_hook; | |
369 | __malloc_hook = malloc_starter; | |
370 | __memalign_hook = memalign_starter; | |
371 | __free_hook = free_starter; | |
372 | } | |
373 | else | |
374 | { | |
375 | __malloc_hook = save_malloc_hook; | |
376 | __memalign_hook = save_memalign_hook; | |
377 | __free_hook = save_free_hook; | |
378 | } | |
379 | } | |
380 | # endif | |
381 | #endif | |
382 | ||
fa8d436c | 383 | static void |
06d6611a | 384 | ptmalloc_init (void) |
fa8d436c UD |
385 | { |
386 | #if __STD_C | |
387 | const char* s; | |
388 | #else | |
389 | char* s; | |
390 | #endif | |
391 | int secure = 0; | |
392 | ||
393 | if(__malloc_initialized >= 0) return; | |
394 | __malloc_initialized = 0; | |
395 | ||
fde89ad0 RM |
396 | #ifdef _LIBC |
397 | # if defined SHARED && defined USE_TLS && !USE___THREAD | |
398 | /* ptmalloc_init_minimal may already have been called via | |
399 | __libc_malloc_pthread_startup, above. */ | |
400 | if (mp_.pagesize == 0) | |
401 | # endif | |
402 | #endif | |
403 | ptmalloc_init_minimal(); | |
fa8d436c UD |
404 | |
405 | #ifndef NO_THREADS | |
fde89ad0 RM |
406 | # if defined _LIBC && defined USE_TLS |
407 | /* We know __pthread_initialize_minimal has already been called, | |
408 | and that is enough. */ | |
409 | # define NO_STARTER | |
410 | # endif | |
411 | # ifndef NO_STARTER | |
fa8d436c UD |
412 | /* With some threads implementations, creating thread-specific data |
413 | or initializing a mutex may call malloc() itself. Provide a | |
414 | simple starter version (realloc() won't work). */ | |
415 | save_malloc_hook = __malloc_hook; | |
fde89ad0 | 416 | save_memalign_hook = __memalign_hook; |
fa8d436c UD |
417 | save_free_hook = __free_hook; |
418 | __malloc_hook = malloc_starter; | |
fde89ad0 | 419 | __memalign_hook = memalign_starter; |
fa8d436c | 420 | __free_hook = free_starter; |
fde89ad0 | 421 | # ifdef _LIBC |
fa8d436c UD |
422 | /* Initialize the pthreads interface. */ |
423 | if (__pthread_initialize != NULL) | |
424 | __pthread_initialize(); | |
fde89ad0 RM |
425 | # endif /* !defined _LIBC */ |
426 | # endif /* !defined NO_STARTER */ | |
fa8d436c UD |
427 | #endif /* !defined NO_THREADS */ |
428 | mutex_init(&main_arena.mutex); | |
429 | main_arena.next = &main_arena; | |
430 | ||
c0f62c56 UD |
431 | #if defined _LIBC && defined SHARED |
432 | /* In case this libc copy is in a non-default namespace, never use brk. */ | |
433 | Dl_info di; | |
434 | struct link_map *l; | |
435 | if (_dl_addr (ptmalloc_init, &di, &l, NULL) != 0 && l->l_ns != LM_ID_BASE) | |
436 | __morecore = __failing_morecore; | |
437 | #endif | |
438 | ||
fa8d436c UD |
439 | mutex_init(&list_lock); |
440 | tsd_key_create(&arena_key, NULL); | |
441 | tsd_setspecific(arena_key, (Void_t *)&main_arena); | |
442 | thread_atfork(ptmalloc_lock_all, ptmalloc_unlock_all, ptmalloc_unlock_all2); | |
443 | #ifndef NO_THREADS | |
fde89ad0 | 444 | # ifndef NO_STARTER |
fa8d436c | 445 | __malloc_hook = save_malloc_hook; |
fde89ad0 | 446 | __memalign_hook = save_memalign_hook; |
fa8d436c | 447 | __free_hook = save_free_hook; |
fde89ad0 RM |
448 | # else |
449 | # undef NO_STARTER | |
450 | # endif | |
fa8d436c UD |
451 | #endif |
452 | #ifdef _LIBC | |
453 | secure = __libc_enable_secure; | |
454 | s = NULL; | |
08e49216 RM |
455 | if (__builtin_expect (_environ != NULL, 1)) |
456 | { | |
457 | char **runp = _environ; | |
458 | char *envline; | |
459 | ||
460 | while (__builtin_expect ((envline = next_env_entry (&runp)) != NULL, | |
461 | 0)) | |
462 | { | |
463 | size_t len = strcspn (envline, "="); | |
464 | ||
465 | if (envline[len] != '=') | |
466 | /* This is a "MALLOC_" variable at the end of the string | |
467 | without a '=' character. Ignore it since otherwise we | |
468 | will access invalid memory below. */ | |
469 | continue; | |
470 | ||
471 | switch (len) | |
472 | { | |
473 | case 6: | |
474 | if (memcmp (envline, "CHECK_", 6) == 0) | |
475 | s = &envline[7]; | |
476 | break; | |
477 | case 8: | |
478 | if (! secure && memcmp (envline, "TOP_PAD_", 8) == 0) | |
479 | mALLOPt(M_TOP_PAD, atoi(&envline[9])); | |
480 | break; | |
481 | case 9: | |
482 | if (! secure && memcmp (envline, "MMAP_MAX_", 9) == 0) | |
483 | mALLOPt(M_MMAP_MAX, atoi(&envline[10])); | |
484 | break; | |
485 | case 15: | |
486 | if (! secure) | |
487 | { | |
488 | if (memcmp (envline, "TRIM_THRESHOLD_", 15) == 0) | |
489 | mALLOPt(M_TRIM_THRESHOLD, atoi(&envline[16])); | |
490 | else if (memcmp (envline, "MMAP_THRESHOLD_", 15) == 0) | |
491 | mALLOPt(M_MMAP_THRESHOLD, atoi(&envline[16])); | |
492 | } | |
493 | break; | |
494 | default: | |
495 | break; | |
496 | } | |
497 | } | |
498 | } | |
fa8d436c UD |
499 | #else |
500 | if (! secure) | |
501 | { | |
502 | if((s = getenv("MALLOC_TRIM_THRESHOLD_"))) | |
503 | mALLOPt(M_TRIM_THRESHOLD, atoi(s)); | |
504 | if((s = getenv("MALLOC_TOP_PAD_"))) | |
505 | mALLOPt(M_TOP_PAD, atoi(s)); | |
506 | if((s = getenv("MALLOC_MMAP_THRESHOLD_"))) | |
507 | mALLOPt(M_MMAP_THRESHOLD, atoi(s)); | |
508 | if((s = getenv("MALLOC_MMAP_MAX_"))) | |
509 | mALLOPt(M_MMAP_MAX, atoi(s)); | |
510 | } | |
511 | s = getenv("MALLOC_CHECK_"); | |
512 | #endif | |
513 | if(s) { | |
514 | if(s[0]) mALLOPt(M_CHECK_ACTION, (int)(s[0] - '0')); | |
a19fe332 UD |
515 | if (check_action != 0) |
516 | __malloc_check_init(); | |
fa8d436c UD |
517 | } |
518 | if(__malloc_initialize_hook != NULL) | |
519 | (*__malloc_initialize_hook)(); | |
520 | __malloc_initialized = 1; | |
521 | } | |
522 | ||
523 | /* There are platforms (e.g. Hurd) with a link-time hook mechanism. */ | |
524 | #ifdef thread_atfork_static | |
525 | thread_atfork_static(ptmalloc_lock_all, ptmalloc_unlock_all, \ | |
526 | ptmalloc_unlock_all2) | |
527 | #endif | |
528 | ||
529 | \f | |
530 | ||
531 | /* Managing heaps and arenas (for concurrent threads) */ | |
532 | ||
533 | #if USE_ARENAS | |
534 | ||
535 | #if MALLOC_DEBUG > 1 | |
536 | ||
537 | /* Print the complete contents of a single heap to stderr. */ | |
538 | ||
539 | static void | |
540 | #if __STD_C | |
541 | dump_heap(heap_info *heap) | |
542 | #else | |
543 | dump_heap(heap) heap_info *heap; | |
544 | #endif | |
545 | { | |
546 | char *ptr; | |
547 | mchunkptr p; | |
548 | ||
549 | fprintf(stderr, "Heap %p, size %10lx:\n", heap, (long)heap->size); | |
550 | ptr = (heap->ar_ptr != (mstate)(heap+1)) ? | |
551 | (char*)(heap + 1) : (char*)(heap + 1) + sizeof(struct malloc_state); | |
552 | p = (mchunkptr)(((unsigned long)ptr + MALLOC_ALIGN_MASK) & | |
553 | ~MALLOC_ALIGN_MASK); | |
554 | for(;;) { | |
555 | fprintf(stderr, "chunk %p size %10lx", p, (long)p->size); | |
556 | if(p == top(heap->ar_ptr)) { | |
557 | fprintf(stderr, " (top)\n"); | |
558 | break; | |
559 | } else if(p->size == (0|PREV_INUSE)) { | |
560 | fprintf(stderr, " (fence)\n"); | |
561 | break; | |
562 | } | |
563 | fprintf(stderr, "\n"); | |
564 | p = next_chunk(p); | |
565 | } | |
566 | } | |
567 | ||
568 | #endif /* MALLOC_DEBUG > 1 */ | |
569 | ||
26d550d3 UD |
570 | /* If consecutive mmap (0, HEAP_MAX_SIZE << 1, ...) calls return decreasing |
571 | addresses as opposed to increasing, new_heap would badly fragment the | |
572 | address space. In that case remember the second HEAP_MAX_SIZE part | |
573 | aligned to HEAP_MAX_SIZE from last mmap (0, HEAP_MAX_SIZE << 1, ...) | |
574 | call (if it is already aligned) and try to reuse it next time. We need | |
575 | no locking for it, as kernel ensures the atomicity for us - worst case | |
576 | we'll call mmap (addr, HEAP_MAX_SIZE, ...) for some value of addr in | |
577 | multiple threads, but only one will succeed. */ | |
578 | static char *aligned_heap_area; | |
579 | ||
fa8d436c UD |
580 | /* Create a new heap. size is automatically rounded up to a multiple |
581 | of the page size. */ | |
582 | ||
583 | static heap_info * | |
584 | internal_function | |
585 | #if __STD_C | |
586 | new_heap(size_t size, size_t top_pad) | |
587 | #else | |
588 | new_heap(size, top_pad) size_t size, top_pad; | |
589 | #endif | |
590 | { | |
591 | size_t page_mask = malloc_getpagesize - 1; | |
592 | char *p1, *p2; | |
593 | unsigned long ul; | |
594 | heap_info *h; | |
595 | ||
596 | if(size+top_pad < HEAP_MIN_SIZE) | |
597 | size = HEAP_MIN_SIZE; | |
598 | else if(size+top_pad <= HEAP_MAX_SIZE) | |
599 | size += top_pad; | |
600 | else if(size > HEAP_MAX_SIZE) | |
601 | return 0; | |
602 | else | |
603 | size = HEAP_MAX_SIZE; | |
604 | size = (size + page_mask) & ~page_mask; | |
605 | ||
606 | /* A memory region aligned to a multiple of HEAP_MAX_SIZE is needed. | |
607 | No swap space needs to be reserved for the following large | |
608 | mapping (on Linux, this is the case for all non-writable mappings | |
609 | anyway). */ | |
26d550d3 UD |
610 | p2 = MAP_FAILED; |
611 | if(aligned_heap_area) { | |
612 | p2 = (char *)MMAP(aligned_heap_area, HEAP_MAX_SIZE, PROT_NONE, | |
613 | MAP_PRIVATE|MAP_NORESERVE); | |
614 | aligned_heap_area = NULL; | |
615 | if (p2 != MAP_FAILED && ((unsigned long)p2 & (HEAP_MAX_SIZE-1))) { | |
fa8d436c | 616 | munmap(p2, HEAP_MAX_SIZE); |
26d550d3 UD |
617 | p2 = MAP_FAILED; |
618 | } | |
619 | } | |
620 | if(p2 == MAP_FAILED) { | |
621 | p1 = (char *)MMAP(0, HEAP_MAX_SIZE<<1, PROT_NONE, | |
622 | MAP_PRIVATE|MAP_NORESERVE); | |
623 | if(p1 != MAP_FAILED) { | |
624 | p2 = (char *)(((unsigned long)p1 + (HEAP_MAX_SIZE-1)) | |
625 | & ~(HEAP_MAX_SIZE-1)); | |
626 | ul = p2 - p1; | |
627 | if (ul) | |
628 | munmap(p1, ul); | |
629 | else | |
630 | aligned_heap_area = p2 + HEAP_MAX_SIZE; | |
631 | munmap(p2 + HEAP_MAX_SIZE, HEAP_MAX_SIZE - ul); | |
632 | } else { | |
633 | /* Try to take the chance that an allocation of only HEAP_MAX_SIZE | |
634 | is already aligned. */ | |
635 | p2 = (char *)MMAP(0, HEAP_MAX_SIZE, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE); | |
636 | if(p2 == MAP_FAILED) | |
637 | return 0; | |
638 | if((unsigned long)p2 & (HEAP_MAX_SIZE-1)) { | |
639 | munmap(p2, HEAP_MAX_SIZE); | |
640 | return 0; | |
641 | } | |
fa8d436c UD |
642 | } |
643 | } | |
644 | if(mprotect(p2, size, PROT_READ|PROT_WRITE) != 0) { | |
645 | munmap(p2, HEAP_MAX_SIZE); | |
646 | return 0; | |
647 | } | |
648 | h = (heap_info *)p2; | |
649 | h->size = size; | |
650 | THREAD_STAT(stat_n_heaps++); | |
651 | return h; | |
652 | } | |
653 | ||
654 | /* Grow or shrink a heap. size is automatically rounded up to a | |
655 | multiple of the page size if it is positive. */ | |
656 | ||
657 | static int | |
658 | #if __STD_C | |
659 | grow_heap(heap_info *h, long diff) | |
660 | #else | |
661 | grow_heap(h, diff) heap_info *h; long diff; | |
662 | #endif | |
663 | { | |
664 | size_t page_mask = malloc_getpagesize - 1; | |
665 | long new_size; | |
666 | ||
667 | if(diff >= 0) { | |
668 | diff = (diff + page_mask) & ~page_mask; | |
669 | new_size = (long)h->size + diff; | |
670 | if(new_size > HEAP_MAX_SIZE) | |
671 | return -1; | |
672 | if(mprotect((char *)h + h->size, diff, PROT_READ|PROT_WRITE) != 0) | |
673 | return -2; | |
674 | } else { | |
675 | new_size = (long)h->size + diff; | |
676 | if(new_size < (long)sizeof(*h)) | |
677 | return -1; | |
678 | /* Try to re-map the extra heap space freshly to save memory, and | |
679 | make it inaccessible. */ | |
680 | if((char *)MMAP((char *)h + new_size, -diff, PROT_NONE, | |
681 | MAP_PRIVATE|MAP_FIXED) == (char *) MAP_FAILED) | |
682 | return -2; | |
683 | /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/ | |
684 | } | |
685 | h->size = new_size; | |
686 | return 0; | |
687 | } | |
688 | ||
689 | /* Delete a heap. */ | |
690 | ||
26d550d3 UD |
691 | #define delete_heap(heap) \ |
692 | do { \ | |
693 | if ((char *)(heap) + HEAP_MAX_SIZE == aligned_heap_area) \ | |
694 | aligned_heap_area = NULL; \ | |
695 | munmap((char*)(heap), HEAP_MAX_SIZE); \ | |
696 | } while (0) | |
fa8d436c UD |
697 | |
698 | static int | |
699 | internal_function | |
700 | #if __STD_C | |
701 | heap_trim(heap_info *heap, size_t pad) | |
702 | #else | |
703 | heap_trim(heap, pad) heap_info *heap; size_t pad; | |
704 | #endif | |
705 | { | |
706 | mstate ar_ptr = heap->ar_ptr; | |
707 | unsigned long pagesz = mp_.pagesize; | |
708 | mchunkptr top_chunk = top(ar_ptr), p, bck, fwd; | |
709 | heap_info *prev_heap; | |
710 | long new_size, top_size, extra; | |
711 | ||
712 | /* Can this heap go away completely? */ | |
713 | while(top_chunk == chunk_at_offset(heap, sizeof(*heap))) { | |
714 | prev_heap = heap->prev; | |
715 | p = chunk_at_offset(prev_heap, prev_heap->size - (MINSIZE-2*SIZE_SZ)); | |
716 | assert(p->size == (0|PREV_INUSE)); /* must be fencepost */ | |
717 | p = prev_chunk(p); | |
718 | new_size = chunksize(p) + (MINSIZE-2*SIZE_SZ); | |
719 | assert(new_size>0 && new_size<(long)(2*MINSIZE)); | |
720 | if(!prev_inuse(p)) | |
721 | new_size += p->prev_size; | |
722 | assert(new_size>0 && new_size<HEAP_MAX_SIZE); | |
723 | if(new_size + (HEAP_MAX_SIZE - prev_heap->size) < pad + MINSIZE + pagesz) | |
724 | break; | |
725 | ar_ptr->system_mem -= heap->size; | |
726 | arena_mem -= heap->size; | |
727 | delete_heap(heap); | |
728 | heap = prev_heap; | |
729 | if(!prev_inuse(p)) { /* consolidate backward */ | |
730 | p = prev_chunk(p); | |
731 | unlink(p, bck, fwd); | |
732 | } | |
733 | assert(((unsigned long)((char*)p + new_size) & (pagesz-1)) == 0); | |
734 | assert( ((char*)p + new_size) == ((char*)heap + heap->size) ); | |
735 | top(ar_ptr) = top_chunk = p; | |
736 | set_head(top_chunk, new_size | PREV_INUSE); | |
737 | /*check_chunk(ar_ptr, top_chunk);*/ | |
738 | } | |
739 | top_size = chunksize(top_chunk); | |
740 | extra = ((top_size - pad - MINSIZE + (pagesz-1))/pagesz - 1) * pagesz; | |
741 | if(extra < (long)pagesz) | |
742 | return 0; | |
743 | /* Try to shrink. */ | |
744 | if(grow_heap(heap, -extra) != 0) | |
745 | return 0; | |
746 | ar_ptr->system_mem -= extra; | |
747 | arena_mem -= extra; | |
748 | ||
749 | /* Success. Adjust top accordingly. */ | |
750 | set_head(top_chunk, (top_size - extra) | PREV_INUSE); | |
751 | /*check_chunk(ar_ptr, top_chunk);*/ | |
752 | return 1; | |
753 | } | |
754 | ||
755 | static mstate | |
756 | internal_function | |
757 | #if __STD_C | |
758 | arena_get2(mstate a_tsd, size_t size) | |
759 | #else | |
760 | arena_get2(a_tsd, size) mstate a_tsd; size_t size; | |
761 | #endif | |
762 | { | |
763 | mstate a; | |
fa8d436c UD |
764 | |
765 | if(!a_tsd) | |
766 | a = a_tsd = &main_arena; | |
767 | else { | |
768 | a = a_tsd->next; | |
769 | if(!a) { | |
770 | /* This can only happen while initializing the new arena. */ | |
771 | (void)mutex_lock(&main_arena.mutex); | |
772 | THREAD_STAT(++(main_arena.stat_lock_wait)); | |
773 | return &main_arena; | |
774 | } | |
775 | } | |
776 | ||
777 | /* Check the global, circularly linked list for available arenas. */ | |
778 | repeat: | |
779 | do { | |
780 | if(!mutex_trylock(&a->mutex)) { | |
781 | THREAD_STAT(++(a->stat_lock_loop)); | |
782 | tsd_setspecific(arena_key, (Void_t *)a); | |
783 | return a; | |
784 | } | |
785 | a = a->next; | |
786 | } while(a != a_tsd); | |
787 | ||
788 | /* If not even the list_lock can be obtained, try again. This can | |
789 | happen during `atfork', or for example on systems where thread | |
790 | creation makes it temporarily impossible to obtain _any_ | |
791 | locks. */ | |
792 | if(mutex_trylock(&list_lock)) { | |
793 | a = a_tsd; | |
794 | goto repeat; | |
795 | } | |
796 | (void)mutex_unlock(&list_lock); | |
797 | ||
798 | /* Nothing immediately available, so generate a new arena. */ | |
799 | a = _int_new_arena(size); | |
800 | if(!a) | |
801 | return 0; | |
802 | ||
803 | tsd_setspecific(arena_key, (Void_t *)a); | |
804 | mutex_init(&a->mutex); | |
fdb933e2 | 805 | mutex_lock(&a->mutex); /* remember result */ |
fa8d436c UD |
806 | |
807 | /* Add the new arena to the global list. */ | |
808 | (void)mutex_lock(&list_lock); | |
809 | a->next = main_arena.next; | |
b5d5b67b | 810 | atomic_write_barrier (); |
fa8d436c UD |
811 | main_arena.next = a; |
812 | (void)mutex_unlock(&list_lock); | |
813 | ||
fa8d436c UD |
814 | THREAD_STAT(++(a->stat_lock_loop)); |
815 | return a; | |
816 | } | |
817 | ||
818 | /* Create a new arena with initial size "size". */ | |
819 | ||
820 | mstate | |
821 | _int_new_arena(size_t size) | |
822 | { | |
823 | mstate a; | |
824 | heap_info *h; | |
825 | char *ptr; | |
826 | unsigned long misalign; | |
827 | ||
828 | h = new_heap(size + (sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT), | |
829 | mp_.top_pad); | |
830 | if(!h) { | |
831 | /* Maybe size is too large to fit in a single heap. So, just try | |
832 | to create a minimally-sized arena and let _int_malloc() attempt | |
833 | to deal with the large request via mmap_chunk(). */ | |
834 | h = new_heap(sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT, mp_.top_pad); | |
835 | if(!h) | |
836 | return 0; | |
837 | } | |
838 | a = h->ar_ptr = (mstate)(h+1); | |
839 | malloc_init_state(a); | |
840 | /*a->next = NULL;*/ | |
841 | a->system_mem = a->max_system_mem = h->size; | |
842 | arena_mem += h->size; | |
843 | #ifdef NO_THREADS | |
844 | if((unsigned long)(mp_.mmapped_mem + arena_mem + main_arena.system_mem) > | |
845 | mp_.max_total_mem) | |
846 | mp_.max_total_mem = mp_.mmapped_mem + arena_mem + main_arena.system_mem; | |
847 | #endif | |
848 | ||
849 | /* Set up the top chunk, with proper alignment. */ | |
850 | ptr = (char *)(a + 1); | |
851 | misalign = (unsigned long)chunk2mem(ptr) & MALLOC_ALIGN_MASK; | |
852 | if (misalign > 0) | |
853 | ptr += MALLOC_ALIGNMENT - misalign; | |
854 | top(a) = (mchunkptr)ptr; | |
855 | set_head(top(a), (((char*)h + h->size) - ptr) | PREV_INUSE); | |
856 | ||
857 | return a; | |
858 | } | |
859 | ||
860 | #endif /* USE_ARENAS */ | |
861 | ||
862 | /* | |
863 | * Local variables: | |
864 | * c-basic-offset: 2 | |
865 | * End: | |
866 | */ |