1 /* Malloc implementation for multiple threads without lock contention.
2 Copyright (C) 2001-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
22 # define TUNABLE_NAMESPACE malloc
24 #include <elf/dl-tunables.h>
26 /* Compile-time constants. */
28 #define HEAP_MIN_SIZE (32 * 1024)
30 # ifdef DEFAULT_MMAP_THRESHOLD_MAX
31 # define HEAP_MAX_SIZE (2 * DEFAULT_MMAP_THRESHOLD_MAX)
33 # define HEAP_MAX_SIZE (1024 * 1024) /* must be a power of two */
37 /* HEAP_MIN_SIZE and HEAP_MAX_SIZE limit the size of mmap()ed heaps
38 that are dynamically created for multi-threaded programs. The
39 maximum size must be a power of two, for fast determination of
40 which heap belongs to a chunk. It should be much larger than the
41 mmap threshold, so that requests with a size just below that
42 threshold can be fulfilled without creating too many heaps. */
44 /* When huge pages are used to create new arenas, the maximum and minumum
45 size are based on the runtime defined huge page size. */
51 return mp_
.hp_pagesize
== 0 ? HEAP_MIN_SIZE
: mp_
.hp_pagesize
;
61 return mp_
.hp_pagesize
== 0 ? HEAP_MAX_SIZE
: mp_
.hp_pagesize
* 4;
67 /***************************************************************************/
69 #define top(ar_ptr) ((ar_ptr)->top)
71 /* A heap is a single contiguous memory region holding (coalesceable)
72 malloc_chunks. It is allocated with mmap() and always starts at an
73 address aligned to HEAP_MAX_SIZE. */
75 typedef struct _heap_info
77 mstate ar_ptr
; /* Arena for this heap. */
78 struct _heap_info
*prev
; /* Previous heap. */
79 size_t size
; /* Current size in bytes. */
80 size_t mprotect_size
; /* Size in bytes that has been mprotected
81 PROT_READ|PROT_WRITE. */
82 size_t pagesize
; /* Page size used when allocating the arena. */
83 /* Make sure the following data is properly aligned, particularly
84 that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of
86 char pad
[-3 * SIZE_SZ
& MALLOC_ALIGN_MASK
];
89 /* Get a compile-time error if the heap_info padding is not correct
90 to make alignment work as expected in sYSMALLOc. */
91 extern int sanity_check_heap_info_alignment
[(sizeof (heap_info
)
92 + 2 * SIZE_SZ
) % MALLOC_ALIGNMENT
95 /* Thread specific data. */
97 static __thread mstate thread_arena attribute_tls_model_ie
;
99 /* Arena free list. free_list_lock synchronizes access to the
100 free_list variable below, and the next_free and attached_threads
101 members of struct malloc_state objects. No other locks must be
102 acquired after free_list_lock has been acquired. */
104 __libc_lock_define_initialized (static, free_list_lock
);
106 static size_t narenas
= 1;
108 static mstate free_list
;
110 /* list_lock prevents concurrent writes to the next member of struct
111 malloc_state objects.
113 Read access to the next member is supposed to synchronize with the
114 atomic_write_barrier and the write to the next member in
115 _int_new_arena. This suffers from data races; see the FIXME
116 comments in _int_new_arena and reused_arena.
118 list_lock also prevents concurrent forks. At the time list_lock is
119 acquired, no arena lock must have been acquired, but it is
120 permitted to acquire arena locks subsequently, while list_lock is
122 __libc_lock_define_initialized (static, list_lock
);
124 /* Already initialized? */
125 static bool __malloc_initialized
= false;
127 /**************************************************************************/
130 /* arena_get() acquires an arena and locks the corresponding mutex.
131 First, try the one last locked successfully by this thread. (This
132 is the common case and handled with a macro for speed.) Then, loop
133 once over the circularly linked list of arenas. If no arena is
134 readily available, create a new one. In this latter case, `size'
135 is just a hint as to how much memory will be required immediately
138 #define arena_get(ptr, size) do { \
139 ptr = thread_arena; \
140 arena_lock (ptr, size); \
143 #define arena_lock(ptr, size) do { \
145 __libc_lock_lock (ptr->mutex); \
147 ptr = arena_get2 ((size), NULL); \
150 /* find the heap and corresponding arena for a given ptr */
152 static inline heap_info
*
153 heap_for_ptr (void *ptr
)
155 size_t max_size
= heap_max_size ();
156 return PTR_ALIGN_DOWN (ptr
, max_size
);
159 static inline struct malloc_state
*
160 arena_for_chunk (mchunkptr ptr
)
162 return chunk_main_arena (ptr
) ? &main_arena
: heap_for_ptr (ptr
)->ar_ptr
;
166 /**************************************************************************/
168 /* atfork support. */
170 /* The following three functions are called around fork from a
171 multi-threaded process. We do not use the general fork handler
172 mechanism to make sure that our handlers are the last ones being
173 called, so that other fork handlers can use the malloc
177 __malloc_fork_lock_parent (void)
179 if (!__malloc_initialized
)
182 /* We do not acquire free_list_lock here because we completely
183 reconstruct free_list in __malloc_fork_unlock_child. */
185 __libc_lock_lock (list_lock
);
187 for (mstate ar_ptr
= &main_arena
;; )
189 __libc_lock_lock (ar_ptr
->mutex
);
190 ar_ptr
= ar_ptr
->next
;
191 if (ar_ptr
== &main_arena
)
197 __malloc_fork_unlock_parent (void)
199 if (!__malloc_initialized
)
202 for (mstate ar_ptr
= &main_arena
;; )
204 __libc_lock_unlock (ar_ptr
->mutex
);
205 ar_ptr
= ar_ptr
->next
;
206 if (ar_ptr
== &main_arena
)
209 __libc_lock_unlock (list_lock
);
213 __malloc_fork_unlock_child (void)
215 if (!__malloc_initialized
)
218 /* Push all arenas to the free list, except thread_arena, which is
219 attached to the current thread. */
220 __libc_lock_init (free_list_lock
);
221 if (thread_arena
!= NULL
)
222 thread_arena
->attached_threads
= 1;
224 for (mstate ar_ptr
= &main_arena
;; )
226 __libc_lock_init (ar_ptr
->mutex
);
227 if (ar_ptr
!= thread_arena
)
229 /* This arena is no longer attached to any thread. */
230 ar_ptr
->attached_threads
= 0;
231 ar_ptr
->next_free
= free_list
;
234 ar_ptr
= ar_ptr
->next
;
235 if (ar_ptr
== &main_arena
)
239 __libc_lock_init (list_lock
);
243 # define TUNABLE_CALLBACK_FNDECL(__name, __type) \
244 static inline int do_ ## __name (__type value); \
246 TUNABLE_CALLBACK (__name) (tunable_val_t *valp) \
248 __type value = (__type) (valp)->numval; \
249 do_ ## __name (value); \
252 TUNABLE_CALLBACK_FNDECL (set_mmap_threshold
, size_t)
253 TUNABLE_CALLBACK_FNDECL (set_mmaps_max
, int32_t)
254 TUNABLE_CALLBACK_FNDECL (set_top_pad
, size_t)
255 TUNABLE_CALLBACK_FNDECL (set_perturb_byte
, int32_t)
256 TUNABLE_CALLBACK_FNDECL (set_trim_threshold
, size_t)
257 TUNABLE_CALLBACK_FNDECL (set_arena_max
, size_t)
258 TUNABLE_CALLBACK_FNDECL (set_arena_test
, size_t)
260 TUNABLE_CALLBACK_FNDECL (set_tcache_max
, size_t)
261 TUNABLE_CALLBACK_FNDECL (set_tcache_count
, size_t)
262 TUNABLE_CALLBACK_FNDECL (set_tcache_unsorted_limit
, size_t)
264 TUNABLE_CALLBACK_FNDECL (set_mxfast
, size_t)
265 TUNABLE_CALLBACK_FNDECL (set_hugetlb
, size_t)
267 /* Initialization routine. */
269 extern char **_environ
;
272 next_env_entry (char ***position
)
274 char **current
= *position
;
277 while (*current
!= NULL
)
279 if (__builtin_expect ((*current
)[0] == 'M', 0)
280 && (*current
)[1] == 'A'
281 && (*current
)[2] == 'L'
282 && (*current
)[3] == 'L'
283 && (*current
)[4] == 'O'
284 && (*current
)[5] == 'C'
285 && (*current
)[6] == '_')
287 result
= &(*current
)[7];
289 /* Save current position for next visit. */
290 *position
= ++current
;
304 static void tcache_key_initialize (void);
310 if (__malloc_initialized
)
313 __malloc_initialized
= true;
316 tcache_key_initialize ();
320 if ((TUNABLE_GET_FULL (glibc
, mem
, tagging
, int32_t, NULL
) & 1) != 0)
322 /* If the tunable says that we should be using tagged memory
323 and that morecore does not support tagged regions, then
325 if (__MTAG_SBRK_UNTAGGED
)
326 __always_fail_morecore
= true;
329 mtag_mmap_flags
= __MTAG_MMAP_FLAGS
;
333 #if defined SHARED && IS_IN (libc)
334 /* In case this libc copy is in a non-default namespace, never use
335 brk. Likewise if dlopened from statically linked program. The
336 generic sbrk implementation also enforces this, but it is not
339 __always_fail_morecore
= true;
342 thread_arena
= &main_arena
;
344 malloc_init_state (&main_arena
);
347 TUNABLE_GET (top_pad
, size_t, TUNABLE_CALLBACK (set_top_pad
));
348 TUNABLE_GET (perturb
, int32_t, TUNABLE_CALLBACK (set_perturb_byte
));
349 TUNABLE_GET (mmap_threshold
, size_t, TUNABLE_CALLBACK (set_mmap_threshold
));
350 TUNABLE_GET (trim_threshold
, size_t, TUNABLE_CALLBACK (set_trim_threshold
));
351 TUNABLE_GET (mmap_max
, int32_t, TUNABLE_CALLBACK (set_mmaps_max
));
352 TUNABLE_GET (arena_max
, size_t, TUNABLE_CALLBACK (set_arena_max
));
353 TUNABLE_GET (arena_test
, size_t, TUNABLE_CALLBACK (set_arena_test
));
355 TUNABLE_GET (tcache_max
, size_t, TUNABLE_CALLBACK (set_tcache_max
));
356 TUNABLE_GET (tcache_count
, size_t, TUNABLE_CALLBACK (set_tcache_count
));
357 TUNABLE_GET (tcache_unsorted_limit
, size_t,
358 TUNABLE_CALLBACK (set_tcache_unsorted_limit
));
360 TUNABLE_GET (mxfast
, size_t, TUNABLE_CALLBACK (set_mxfast
));
361 TUNABLE_GET (hugetlb
, size_t, TUNABLE_CALLBACK (set_hugetlb
));
362 if (mp_
.hp_pagesize
> 0)
363 /* Force mmap for main arena instead of sbrk, so hugepages are explicitly
365 __always_fail_morecore
= true;
367 if (__glibc_likely (_environ
!= NULL
))
369 char **runp
= _environ
;
372 while (__builtin_expect ((envline
= next_env_entry (&runp
)) != NULL
,
375 size_t len
= strcspn (envline
, "=");
377 if (envline
[len
] != '=')
378 /* This is a "MALLOC_" variable at the end of the string
379 without a '=' character. Ignore it since otherwise we
380 will access invalid memory below. */
386 if (!__builtin_expect (__libc_enable_secure
, 0))
388 if (memcmp (envline
, "TOP_PAD_", 8) == 0)
389 __libc_mallopt (M_TOP_PAD
, strtol (&envline
[9], NULL
, 10));
390 else if (memcmp (envline
, "PERTURB_", 8) == 0)
391 __libc_mallopt (M_PERTURB
, strtol (&envline
[9], NULL
, 10));
395 if (!__builtin_expect (__libc_enable_secure
, 0))
397 if (memcmp (envline
, "MMAP_MAX_", 9) == 0)
398 __libc_mallopt (M_MMAP_MAX
, strtol (&envline
[10],
400 else if (memcmp (envline
, "ARENA_MAX", 9) == 0)
401 __libc_mallopt (M_ARENA_MAX
, strtol (&envline
[10],
406 if (!__builtin_expect (__libc_enable_secure
, 0))
408 if (memcmp (envline
, "ARENA_TEST", 10) == 0)
409 __libc_mallopt (M_ARENA_TEST
, strtol (&envline
[11],
414 if (!__builtin_expect (__libc_enable_secure
, 0))
416 if (memcmp (envline
, "TRIM_THRESHOLD_", 15) == 0)
417 __libc_mallopt (M_TRIM_THRESHOLD
, strtol (&envline
[16],
419 else if (memcmp (envline
, "MMAP_THRESHOLD_", 15) == 0)
420 __libc_mallopt (M_MMAP_THRESHOLD
, strtol (&envline
[16],
432 /* Managing heaps and arenas (for concurrent threads) */
436 /* Print the complete contents of a single heap to stderr. */
439 dump_heap (heap_info
*heap
)
444 fprintf (stderr
, "Heap %p, size %10lx:\n", heap
, (long) heap
->size
);
445 ptr
= (heap
->ar_ptr
!= (mstate
) (heap
+ 1)) ?
446 (char *) (heap
+ 1) : (char *) (heap
+ 1) + sizeof (struct malloc_state
);
447 p
= (mchunkptr
) (((uintptr_t) ptr
+ MALLOC_ALIGN_MASK
) &
451 fprintf (stderr
, "chunk %p size %10lx", p
, (long) chunksize_nomask(p
));
452 if (p
== top (heap
->ar_ptr
))
454 fprintf (stderr
, " (top)\n");
457 else if (chunksize_nomask(p
) == (0 | PREV_INUSE
))
459 fprintf (stderr
, " (fence)\n");
462 fprintf (stderr
, "\n");
466 #endif /* MALLOC_DEBUG > 1 */
468 /* If consecutive mmap (0, HEAP_MAX_SIZE << 1, ...) calls return decreasing
469 addresses as opposed to increasing, new_heap would badly fragment the
470 address space. In that case remember the second HEAP_MAX_SIZE part
471 aligned to HEAP_MAX_SIZE from last mmap (0, HEAP_MAX_SIZE << 1, ...)
472 call (if it is already aligned) and try to reuse it next time. We need
473 no locking for it, as kernel ensures the atomicity for us - worst case
474 we'll call mmap (addr, HEAP_MAX_SIZE, ...) for some value of addr in
475 multiple threads, but only one will succeed. */
476 static char *aligned_heap_area
;
478 /* Create a new heap. size is automatically rounded up to a multiple
482 alloc_new_heap (size_t size
, size_t top_pad
, size_t pagesize
,
488 size_t min_size
= heap_min_size ();
489 size_t max_size
= heap_max_size ();
491 if (size
+ top_pad
< min_size
)
493 else if (size
+ top_pad
<= max_size
)
495 else if (size
> max_size
)
499 size
= ALIGN_UP (size
, pagesize
);
501 /* A memory region aligned to a multiple of max_size is needed.
502 No swap space needs to be reserved for the following large
503 mapping (on Linux, this is the case for all non-writable mappings
506 if (aligned_heap_area
)
508 p2
= (char *) MMAP (aligned_heap_area
, max_size
, PROT_NONE
, mmap_flags
);
509 aligned_heap_area
= NULL
;
510 if (p2
!= MAP_FAILED
&& ((unsigned long) p2
& (max_size
- 1)))
512 __munmap (p2
, max_size
);
516 if (p2
== MAP_FAILED
)
518 p1
= (char *) MMAP (0, max_size
<< 1, PROT_NONE
, mmap_flags
);
519 if (p1
!= MAP_FAILED
)
521 p2
= (char *) (((uintptr_t) p1
+ (max_size
- 1))
527 aligned_heap_area
= p2
+ max_size
;
528 __munmap (p2
+ max_size
, max_size
- ul
);
532 /* Try to take the chance that an allocation of only max_size
533 is already aligned. */
534 p2
= (char *) MMAP (0, max_size
, PROT_NONE
, mmap_flags
);
535 if (p2
== MAP_FAILED
)
538 if ((unsigned long) p2
& (max_size
- 1))
540 __munmap (p2
, max_size
);
545 if (__mprotect (p2
, size
, mtag_mmap_flags
| PROT_READ
| PROT_WRITE
) != 0)
547 __munmap (p2
, max_size
);
551 madvise_thp (p2
, size
);
553 h
= (heap_info
*) p2
;
555 h
->mprotect_size
= size
;
556 h
->pagesize
= pagesize
;
557 LIBC_PROBE (memory_heap_new
, 2, h
, h
->size
);
562 new_heap (size_t size
, size_t top_pad
)
565 if (__glibc_unlikely (mp_
.hp_pagesize
!= 0))
567 heap_info
*h
= alloc_new_heap (size
, top_pad
, mp_
.hp_pagesize
,
573 return alloc_new_heap (size
, top_pad
, GLRO (dl_pagesize
), 0);
576 /* Grow a heap. size is automatically rounded up to a
577 multiple of the page size. */
580 grow_heap (heap_info
*h
, long diff
)
582 size_t pagesize
= h
->pagesize
;
583 size_t max_size
= heap_max_size ();
586 diff
= ALIGN_UP (diff
, pagesize
);
587 new_size
= (long) h
->size
+ diff
;
588 if ((unsigned long) new_size
> (unsigned long) max_size
)
591 if ((unsigned long) new_size
> h
->mprotect_size
)
593 if (__mprotect ((char *) h
+ h
->mprotect_size
,
594 (unsigned long) new_size
- h
->mprotect_size
,
595 mtag_mmap_flags
| PROT_READ
| PROT_WRITE
) != 0)
598 h
->mprotect_size
= new_size
;
602 LIBC_PROBE (memory_heap_more
, 2, h
, h
->size
);
609 shrink_heap (heap_info
*h
, long diff
)
613 new_size
= (long) h
->size
- diff
;
614 if (new_size
< (long) sizeof (*h
))
617 /* Try to re-map the extra heap space freshly to save memory, and make it
618 inaccessible. See malloc-sysdep.h to know when this is true. */
619 if (__glibc_unlikely (check_may_shrink_heap ()))
621 if ((char *) MMAP ((char *) h
+ new_size
, diff
, PROT_NONE
,
622 MAP_FIXED
) == (char *) MAP_FAILED
)
625 h
->mprotect_size
= new_size
;
628 __madvise ((char *) h
+ new_size
, diff
, MADV_DONTNEED
);
629 /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/
632 LIBC_PROBE (memory_heap_less
, 2, h
, h
->size
);
639 heap_trim (heap_info
*heap
, size_t pad
)
641 mstate ar_ptr
= heap
->ar_ptr
;
642 mchunkptr top_chunk
= top (ar_ptr
), p
;
643 heap_info
*prev_heap
;
644 long new_size
, top_size
, top_area
, extra
, prev_size
, misalign
;
645 size_t max_size
= heap_max_size ();
647 /* Can this heap go away completely? */
648 while (top_chunk
== chunk_at_offset (heap
, sizeof (*heap
)))
650 prev_heap
= heap
->prev
;
651 prev_size
= prev_heap
->size
- (MINSIZE
- 2 * SIZE_SZ
);
652 p
= chunk_at_offset (prev_heap
, prev_size
);
653 /* fencepost must be properly aligned. */
654 misalign
= ((long) p
) & MALLOC_ALIGN_MASK
;
655 p
= chunk_at_offset (prev_heap
, prev_size
- misalign
);
656 assert (chunksize_nomask (p
) == (0 | PREV_INUSE
)); /* must be fencepost */
658 new_size
= chunksize (p
) + (MINSIZE
- 2 * SIZE_SZ
) + misalign
;
659 assert (new_size
> 0 && new_size
< (long) (2 * MINSIZE
));
661 new_size
+= prev_size (p
);
662 assert (new_size
> 0 && new_size
< max_size
);
663 if (new_size
+ (max_size
- prev_heap
->size
) < pad
+ MINSIZE
666 ar_ptr
->system_mem
-= heap
->size
;
667 LIBC_PROBE (memory_heap_free
, 2, heap
, heap
->size
);
668 if ((char *) heap
+ max_size
== aligned_heap_area
)
669 aligned_heap_area
= NULL
;
670 __munmap (heap
, max_size
);
672 if (!prev_inuse (p
)) /* consolidate backward */
675 unlink_chunk (ar_ptr
, p
);
677 assert (((unsigned long) ((char *) p
+ new_size
) & (heap
->pagesize
- 1))
679 assert (((char *) p
+ new_size
) == ((char *) heap
+ heap
->size
));
680 top (ar_ptr
) = top_chunk
= p
;
681 set_head (top_chunk
, new_size
| PREV_INUSE
);
682 /*check_chunk(ar_ptr, top_chunk);*/
685 /* Uses similar logic for per-thread arenas as the main arena with systrim
686 and _int_free by preserving the top pad and rounding down to the nearest
688 top_size
= chunksize (top_chunk
);
689 if ((unsigned long)(top_size
) <
690 (unsigned long)(mp_
.trim_threshold
))
693 top_area
= top_size
- MINSIZE
- 1;
694 if (top_area
< 0 || (size_t) top_area
<= pad
)
697 /* Release in pagesize units and round down to the nearest page. */
698 extra
= ALIGN_DOWN(top_area
- pad
, heap
->pagesize
);
703 if (shrink_heap (heap
, extra
) != 0)
706 ar_ptr
->system_mem
-= extra
;
708 /* Success. Adjust top accordingly. */
709 set_head (top_chunk
, (top_size
- extra
) | PREV_INUSE
);
710 /*check_chunk(ar_ptr, top_chunk);*/
714 /* Create a new arena with initial size "size". */
717 /* If REPLACED_ARENA is not NULL, detach it from this thread. Must be
718 called while free_list_lock is held. */
720 detach_arena (mstate replaced_arena
)
722 if (replaced_arena
!= NULL
)
724 assert (replaced_arena
->attached_threads
> 0);
725 /* The current implementation only detaches from main_arena in
726 case of allocation failure. This means that it is likely not
727 beneficial to put the arena on free_list even if the
728 reference count reaches zero. */
729 --replaced_arena
->attached_threads
;
734 _int_new_arena (size_t size
)
739 unsigned long misalign
;
741 h
= new_heap (size
+ (sizeof (*h
) + sizeof (*a
) + MALLOC_ALIGNMENT
),
745 /* Maybe size is too large to fit in a single heap. So, just try
746 to create a minimally-sized arena and let _int_malloc() attempt
747 to deal with the large request via mmap_chunk(). */
748 h
= new_heap (sizeof (*h
) + sizeof (*a
) + MALLOC_ALIGNMENT
, mp_
.top_pad
);
752 a
= h
->ar_ptr
= (mstate
) (h
+ 1);
753 malloc_init_state (a
);
754 a
->attached_threads
= 1;
756 a
->system_mem
= a
->max_system_mem
= h
->size
;
758 /* Set up the top chunk, with proper alignment. */
759 ptr
= (char *) (a
+ 1);
760 misalign
= (uintptr_t) chunk2mem (ptr
) & MALLOC_ALIGN_MASK
;
762 ptr
+= MALLOC_ALIGNMENT
- misalign
;
763 top (a
) = (mchunkptr
) ptr
;
764 set_head (top (a
), (((char *) h
+ h
->size
) - ptr
) | PREV_INUSE
);
766 LIBC_PROBE (memory_arena_new
, 2, a
, size
);
767 mstate replaced_arena
= thread_arena
;
769 __libc_lock_init (a
->mutex
);
771 __libc_lock_lock (list_lock
);
773 /* Add the new arena to the global list. */
774 a
->next
= main_arena
.next
;
775 /* FIXME: The barrier is an attempt to synchronize with read access
776 in reused_arena, which does not acquire list_lock while
777 traversing the list. */
778 atomic_write_barrier ();
781 __libc_lock_unlock (list_lock
);
783 __libc_lock_lock (free_list_lock
);
784 detach_arena (replaced_arena
);
785 __libc_lock_unlock (free_list_lock
);
787 /* Lock this arena. NB: Another thread may have been attached to
788 this arena because the arena is now accessible from the
789 main_arena.next list and could have been picked by reused_arena.
790 This can only happen for the last arena created (before the arena
791 limit is reached). At this point, some arena has to be attached
792 to two threads. We could acquire the arena lock before list_lock
793 to make it less likely that reused_arena picks this new arena,
794 but this could result in a deadlock with
795 __malloc_fork_lock_parent. */
797 __libc_lock_lock (a
->mutex
);
803 /* Remove an arena from free_list. */
807 mstate replaced_arena
= thread_arena
;
808 mstate result
= free_list
;
811 __libc_lock_lock (free_list_lock
);
815 free_list
= result
->next_free
;
817 /* The arena will be attached to this thread. */
818 assert (result
->attached_threads
== 0);
819 result
->attached_threads
= 1;
821 detach_arena (replaced_arena
);
823 __libc_lock_unlock (free_list_lock
);
827 LIBC_PROBE (memory_arena_reuse_free_list
, 1, result
);
828 __libc_lock_lock (result
->mutex
);
829 thread_arena
= result
;
836 /* Remove the arena from the free list (if it is present).
837 free_list_lock must have been acquired by the caller. */
839 remove_from_free_list (mstate arena
)
841 mstate
*previous
= &free_list
;
842 for (mstate p
= free_list
; p
!= NULL
; p
= p
->next_free
)
844 assert (p
->attached_threads
== 0);
847 /* Remove the requested arena from the list. */
848 *previous
= p
->next_free
;
852 previous
= &p
->next_free
;
856 /* Lock and return an arena that can be reused for memory allocation.
857 Avoid AVOID_ARENA as we have already failed to allocate memory in
858 it and it is currently locked. */
860 reused_arena (mstate avoid_arena
)
863 /* FIXME: Access to next_to_use suffers from data races. */
864 static mstate next_to_use
;
865 if (next_to_use
== NULL
)
866 next_to_use
= &main_arena
;
868 /* Iterate over all arenas (including those linked from
870 result
= next_to_use
;
873 if (!__libc_lock_trylock (result
->mutex
))
876 /* FIXME: This is a data race, see _int_new_arena. */
877 result
= result
->next
;
879 while (result
!= next_to_use
);
881 /* Avoid AVOID_ARENA as we have already failed to allocate memory
882 in that arena and it is currently locked. */
883 if (result
== avoid_arena
)
884 result
= result
->next
;
886 /* No arena available without contention. Wait for the next in line. */
887 LIBC_PROBE (memory_arena_reuse_wait
, 3, &result
->mutex
, result
, avoid_arena
);
888 __libc_lock_lock (result
->mutex
);
891 /* Attach the arena to the current thread. */
893 /* Update the arena thread attachment counters. */
894 mstate replaced_arena
= thread_arena
;
895 __libc_lock_lock (free_list_lock
);
896 detach_arena (replaced_arena
);
898 /* We may have picked up an arena on the free list. We need to
899 preserve the invariant that no arena on the free list has a
900 positive attached_threads counter (otherwise,
901 arena_thread_freeres cannot use the counter to determine if the
902 arena needs to be put on the free list). We unconditionally
903 remove the selected arena from the free list. The caller of
904 reused_arena checked the free list and observed it to be empty,
905 so the list is very short. */
906 remove_from_free_list (result
);
908 ++result
->attached_threads
;
910 __libc_lock_unlock (free_list_lock
);
913 LIBC_PROBE (memory_arena_reuse
, 2, result
, avoid_arena
);
914 thread_arena
= result
;
915 next_to_use
= result
->next
;
921 arena_get2 (size_t size
, mstate avoid_arena
)
925 static size_t narenas_limit
;
927 a
= get_free_list ();
930 /* Nothing immediately available, so generate a new arena. */
931 if (narenas_limit
== 0)
933 if (mp_
.arena_max
!= 0)
934 narenas_limit
= mp_
.arena_max
;
935 else if (narenas
> mp_
.arena_test
)
937 int n
= __get_nprocs_sched ();
940 narenas_limit
= NARENAS_FROM_NCORES (n
);
942 /* We have no information about the system. Assume two
944 narenas_limit
= NARENAS_FROM_NCORES (2);
949 /* NB: the following depends on the fact that (size_t)0 - 1 is a
950 very large number and that the underflow is OK. If arena_max
951 is set the value of arena_test is irrelevant. If arena_test
952 is set but narenas is not yet larger or equal to arena_test
953 narenas_limit is 0. There is no possibility for narenas to
954 be too big for the test to always fail since there is not
955 enough address space to create that many arenas. */
956 if (__glibc_unlikely (n
<= narenas_limit
- 1))
958 if (catomic_compare_and_exchange_bool_acq (&narenas
, n
+ 1, n
))
960 a
= _int_new_arena (size
);
961 if (__glibc_unlikely (a
== NULL
))
962 catomic_decrement (&narenas
);
965 a
= reused_arena (avoid_arena
);
970 /* If we don't have the main arena, then maybe the failure is due to running
971 out of mmapped areas, so we can try allocating on the main arena.
972 Otherwise, it is likely that sbrk() has failed and there is still a chance
973 to mmap(), so try one of the other arenas. */
975 arena_get_retry (mstate ar_ptr
, size_t bytes
)
977 LIBC_PROBE (memory_arena_retry
, 2, bytes
, ar_ptr
);
978 if (ar_ptr
!= &main_arena
)
980 __libc_lock_unlock (ar_ptr
->mutex
);
981 ar_ptr
= &main_arena
;
982 __libc_lock_lock (ar_ptr
->mutex
);
986 __libc_lock_unlock (ar_ptr
->mutex
);
987 ar_ptr
= arena_get2 (bytes
, ar_ptr
);
995 __malloc_arena_thread_freeres (void)
997 /* Shut down the thread cache first. This could deallocate data for
998 the thread arena, so do this before we put the arena on the free
1000 tcache_thread_shutdown ();
1002 mstate a
= thread_arena
;
1003 thread_arena
= NULL
;
1007 __libc_lock_lock (free_list_lock
);
1008 /* If this was the last attached thread for this arena, put the
1009 arena on the free list. */
1010 assert (a
->attached_threads
> 0);
1011 if (--a
->attached_threads
== 0)
1013 a
->next_free
= free_list
;
1016 __libc_lock_unlock (free_list_lock
);