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