2 * NMALLOC.C - New Malloc (ported from kernel slab allocator)
4 * Copyright (c) 2003,2004,2009,2010 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com> and by
8 * Venkatesh Srinivas <me@endeavour.zapto.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * 3. Neither the name of The DragonFly Project nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific, prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * $Id: nmalloc.c,v 1.37 2010/07/23 08:20:35 vsrinivas Exp $
40 * This module implements a slab allocator drop-in replacement for the
43 * A slab allocator reserves a ZONE for each chunk size, then lays the
44 * chunks out in an array within the zone. Allocation and deallocation
45 * is nearly instantaneous, and overhead losses are limited to a fixed
48 * The slab allocator does not have to pre-initialize the list of
49 * free chunks for each zone, and the underlying VM will not be
50 * touched at all beyond the zone header until an actual allocation
53 * Slab management and locking is done on a per-zone basis.
55 * Alloc Size Chunking Number of zones
66 * Allocations >= ZoneLimit (16K) go directly to mmap and a hash table
67 * is used to locate for free. One and Two-page allocations use the
68 * zone mechanic to avoid excessive mmap()/munmap() calls.
70 * API FEATURES AND SIDE EFFECTS
72 * + power-of-2 sized allocations up to a page will be power-of-2 aligned.
73 * Above that power-of-2 sized allocations are page-aligned. Non
74 * power-of-2 sized allocations are aligned the same as the chunk
75 * size for their zone.
76 * + malloc(0) returns a special non-NULL value
77 * + ability to allocate arbitrarily large chunks of memory
78 * + realloc will reuse the passed pointer if possible, within the
79 * limitations of the zone chunking.
81 * Multithreaded enhancements for small allocations introduced August 2010.
82 * These are in the spirit of 'libumem'. See:
83 * Bonwick, J.; Adams, J. (2001). "Magazines and Vmem: Extending the
84 * slab allocator to many CPUs and arbitrary resources". In Proc. 2001
85 * USENIX Technical Conference. USENIX Association.
87 * Oversized allocations employ the BIGCACHE mechanic whereby large
88 * allocations may be handed significantly larger buffers, allowing them
89 * to avoid mmap/munmap operations even through significant realloc()s.
90 * The excess space is only trimmed if too many large allocations have been
91 * given this treatment.
95 * The value of the environment variable MALLOC_OPTIONS is a character string
96 * containing various flags to tune nmalloc.
98 * 'U' / ['u'] Generate / do not generate utrace entries for ktrace(1)
99 * This will generate utrace events for all malloc,
100 * realloc, and free calls. There are tools (mtrplay) to
101 * replay and allocation pattern or to graph heap structure
102 * (mtrgraph) which can interpret these logs.
103 * 'Z' / ['z'] Zero out / do not zero all allocations.
104 * Each new byte of memory allocated by malloc, realloc, or
105 * reallocf will be initialized to 0. This is intended for
106 * debugging and will affect performance negatively.
107 * 'H' / ['h'] Pass a hint to the kernel about pages unused by the
108 * allocation functions.
111 /* cc -shared -fPIC -g -O -I/usr/src/lib/libc/include -o nmalloc.so nmalloc.c */
113 #include "libc_private.h"
115 #include <sys/param.h>
116 #include <sys/types.h>
117 #include <sys/mman.h>
118 #include <sys/queue.h>
120 #include <sys/ktrace.h>
131 #include <machine/atomic.h>
133 #include "spinlock.h"
134 #include "un-namespace.h"
138 * Linked list of large allocations
140 typedef struct bigalloc
{
141 struct bigalloc
*next
; /* hash link */
142 void *base
; /* base pointer */
143 u_long active
; /* bytes active */
144 u_long bytes
; /* bytes allocated */
148 * Note that any allocations which are exact multiples of PAGE_SIZE, or
149 * which are >= ZALLOC_ZONE_LIMIT, will fall through to the kmem subsystem.
151 #define ZALLOC_ZONE_LIMIT (16 * 1024) /* max slab-managed alloc */
152 #define ZALLOC_MIN_ZONE_SIZE (32 * 1024) /* minimum zone size */
153 #define ZALLOC_MAX_ZONE_SIZE (128 * 1024) /* maximum zone size */
154 #define ZALLOC_ZONE_SIZE (64 * 1024)
155 #define ZALLOC_SLAB_MAGIC 0x736c6162 /* magic sanity */
156 #define ZALLOC_SLAB_SLIDE 20 /* L1-cache skip */
158 #if ZALLOC_ZONE_LIMIT == 16384
160 #elif ZALLOC_ZONE_LIMIT == 32768
163 #error "I couldn't figure out NZONES"
167 * Chunk structure for free elements
169 typedef struct slchunk
{
170 struct slchunk
*c_Next
;
174 * The IN-BAND zone header is placed at the beginning of each zone.
178 typedef struct slzone
{
179 int32_t z_Magic
; /* magic number for sanity check */
180 int z_NFree
; /* total free chunks / ualloc space */
181 struct slzone
*z_Next
; /* ZoneAry[] link if z_NFree non-zero */
182 int z_NMax
; /* maximum free chunks */
183 char *z_BasePtr
; /* pointer to start of chunk array */
184 int z_UIndex
; /* current initial allocation index */
185 int z_UEndIndex
; /* last (first) allocation index */
186 int z_ChunkSize
; /* chunk size for validation */
187 int z_FirstFreePg
; /* chunk list on a page-by-page basis */
190 struct slchunk
*z_PageAry
[ZALLOC_ZONE_SIZE
/ PAGE_SIZE
];
193 typedef struct slglobaldata
{
195 slzone_t ZoneAry
[NZONES
];/* linked list of zones NFree > 0 */
199 #define SLZF_UNOTZEROD 0x0001
201 #define FASTSLABREALLOC 0x02
204 * Misc constants. Note that allocations that are exact multiples of
205 * PAGE_SIZE, or exceed the zone limit, fall through to the kmem module.
206 * IN_SAME_PAGE_MASK is used to sanity-check the per-page free lists.
208 #define MIN_CHUNK_SIZE 8 /* in bytes */
209 #define MIN_CHUNK_MASK (MIN_CHUNK_SIZE - 1)
210 #define IN_SAME_PAGE_MASK (~(intptr_t)PAGE_MASK | MIN_CHUNK_MASK)
213 * WARNING: A limited number of spinlocks are available, BIGXSIZE should
214 * not be larger then 64.
216 #define BIGHSHIFT 10 /* bigalloc hash table */
217 #define BIGHSIZE (1 << BIGHSHIFT)
218 #define BIGHMASK (BIGHSIZE - 1)
219 #define BIGXSIZE (BIGHSIZE / 16) /* bigalloc lock table */
220 #define BIGXMASK (BIGXSIZE - 1)
223 * BIGCACHE caches oversized allocations. Note that a linear search is
224 * performed, so do not make the cache too large.
226 * BIGCACHE will garbage-collect excess space when the excess exceeds the
227 * specified value. A relatively large number should be used here because
228 * garbage collection is expensive.
231 #define BIGCACHE_MASK (BIGCACHE - 1)
232 #define BIGCACHE_LIMIT (1024 * 1024) /* size limit */
233 #define BIGCACHE_EXCESS (16 * 1024 * 1024) /* garbage collect */
235 #define SAFLAG_ZERO 0x0001
236 #define SAFLAG_PASSIVE 0x0002
242 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
244 #define MASSERT(exp) do { if (__predict_false(!(exp))) \
245 _mpanic("assertion: %s in %s", \
253 #define M_MAX_ROUNDS 64
254 #define M_ZONE_ROUNDS 64
255 #define M_LOW_ROUNDS 32
256 #define M_INIT_ROUNDS 8
257 #define M_BURST_FACTOR 8
258 #define M_BURST_NSCALE 2
260 #define M_BURST 0x0001
261 #define M_BURST_EARLY 0x0002
264 SLIST_ENTRY(magazine
) nextmagazine
;
267 int capacity
; /* Max rounds in this magazine */
268 int rounds
; /* Current number of free rounds */
269 int burst_factor
; /* Number of blocks to prefill with */
270 int low_factor
; /* Free till low_factor from full mag */
271 void *objects
[M_MAX_ROUNDS
];
274 SLIST_HEAD(magazinelist
, magazine
);
276 static spinlock_t zone_mag_lock
;
277 static spinlock_t depot_spinlock
;
278 static struct magazine zone_magazine
= {
279 .flags
= M_BURST
| M_BURST_EARLY
,
280 .capacity
= M_ZONE_ROUNDS
,
282 .burst_factor
= M_BURST_FACTOR
,
283 .low_factor
= M_LOW_ROUNDS
286 #define MAGAZINE_FULL(mp) (mp->rounds == mp->capacity)
287 #define MAGAZINE_NOTFULL(mp) (mp->rounds < mp->capacity)
288 #define MAGAZINE_EMPTY(mp) (mp->rounds == 0)
289 #define MAGAZINE_NOTEMPTY(mp) (mp->rounds != 0)
292 * Each thread will have a pair of magazines per size-class (NZONES)
293 * The loaded magazine will support immediate allocations, the previous
294 * magazine will either be full or empty and can be swapped at need
296 typedef struct magazine_pair
{
297 struct magazine
*loaded
;
298 struct magazine
*prev
;
301 /* A depot is a collection of magazines for a single zone. */
302 typedef struct magazine_depot
{
303 struct magazinelist full
;
304 struct magazinelist empty
;
308 typedef struct thr_mags
{
309 magazine_pair mags
[NZONES
];
310 struct magazine
*newmag
;
315 * With this attribute set, do not require a function call for accessing
316 * this variable when the code is compiled -fPIC.
318 * Must be empty for libc_rtld (similar to __thread).
321 #define TLS_ATTRIBUTE
323 #define TLS_ATTRIBUTE __attribute__ ((tls_model ("initial-exec")))
326 static __thread thr_mags thread_mags TLS_ATTRIBUTE
;
327 static pthread_key_t thread_mags_key
;
328 static pthread_once_t thread_mags_once
= PTHREAD_ONCE_INIT
;
329 static magazine_depot depots
[NZONES
];
332 * Fixed globals (not per-cpu)
334 static const int ZoneSize
= ZALLOC_ZONE_SIZE
;
335 static const int ZoneLimit
= ZALLOC_ZONE_LIMIT
;
336 static const int ZonePageCount
= ZALLOC_ZONE_SIZE
/ PAGE_SIZE
;
337 static const int ZoneMask
= ZALLOC_ZONE_SIZE
- 1;
339 static int opt_madvise
= 0;
340 static int opt_utrace
= 0;
341 static int g_malloc_flags
= 0;
342 static struct slglobaldata SLGlobalData
;
343 static bigalloc_t bigalloc_array
[BIGHSIZE
];
344 static spinlock_t bigspin_array
[BIGXSIZE
];
345 static volatile void *bigcache_array
[BIGCACHE
]; /* atomic swap */
346 static volatile size_t bigcache_size_array
[BIGCACHE
]; /* SMP races ok */
347 static volatile int bigcache_index
; /* SMP races ok */
348 static int malloc_panic
;
349 static size_t excess_alloc
; /* excess big allocs */
351 static void *_slaballoc(size_t size
, int flags
);
352 static void *_slabrealloc(void *ptr
, size_t size
);
353 static void _slabfree(void *ptr
, int, bigalloc_t
*);
354 static void *_vmem_alloc(size_t bytes
, size_t align
, int flags
);
355 static void _vmem_free(void *ptr
, size_t bytes
);
356 static void *magazine_alloc(struct magazine
*, int *);
357 static int magazine_free(struct magazine
*, void *);
358 static void *mtmagazine_alloc(int zi
);
359 static int mtmagazine_free(int zi
, void *);
360 static void mtmagazine_init(void);
361 static void mtmagazine_destructor(void *);
362 static slzone_t
zone_alloc(int flags
);
363 static void zone_free(void *z
);
364 static void _mpanic(const char *ctl
, ...) __printflike(1, 2);
365 static void malloc_init(void) __constructor(101);
367 struct nmalloc_utrace
{
373 #define UTRACE(a, b, c) \
375 struct nmalloc_utrace ut = { \
380 utrace(&ut, sizeof(ut)); \
386 const char *p
= NULL
;
388 if (issetugid() == 0)
389 p
= getenv("MALLOC_OPTIONS");
391 for (; p
!= NULL
&& *p
!= '\0'; p
++) {
393 case 'u': opt_utrace
= 0; break;
394 case 'U': opt_utrace
= 1; break;
395 case 'h': opt_madvise
= 0; break;
396 case 'H': opt_madvise
= 1; break;
397 case 'z': g_malloc_flags
= 0; break;
398 case 'Z': g_malloc_flags
= SAFLAG_ZERO
; break;
404 UTRACE((void *) -1, 0, NULL
);
408 * We have to install a handler for nmalloc thread teardowns when
409 * the thread is created. We cannot delay this because destructors in
410 * sophisticated userland programs can call malloc() for the first time
411 * during their thread exit.
413 * This routine is called directly from pthreads.
416 _nmalloc_thr_init(void)
418 static int init_once
;
422 * Disallow mtmagazine operations until the mtmagazine is
428 if (init_once
== 0) {
430 pthread_once(&thread_mags_once
, mtmagazine_init
);
432 pthread_setspecific(thread_mags_key
, tp
);
437 _nmalloc_thr_prepfork(void)
440 _SPINLOCK(&zone_mag_lock
);
441 _SPINLOCK(&depot_spinlock
);
446 _nmalloc_thr_parentfork(void)
449 _SPINUNLOCK(&depot_spinlock
);
450 _SPINUNLOCK(&zone_mag_lock
);
455 _nmalloc_thr_childfork(void)
458 _SPINUNLOCK(&depot_spinlock
);
459 _SPINUNLOCK(&zone_mag_lock
);
467 slgd_lock(slglobaldata_t slgd
)
470 _SPINLOCK(&slgd
->Spinlock
);
474 slgd_unlock(slglobaldata_t slgd
)
477 _SPINUNLOCK(&slgd
->Spinlock
);
481 depot_lock(magazine_depot
*dp
)
484 _SPINLOCK(&depot_spinlock
);
487 _SPINLOCK(&dp
->lock
);
492 depot_unlock(magazine_depot
*dp
)
495 _SPINUNLOCK(&depot_spinlock
);
498 _SPINUNLOCK(&dp
->lock
);
503 zone_magazine_lock(void)
506 _SPINLOCK(&zone_mag_lock
);
510 zone_magazine_unlock(void)
513 _SPINUNLOCK(&zone_mag_lock
);
517 swap_mags(magazine_pair
*mp
)
519 struct magazine
*tmp
;
521 mp
->loaded
= mp
->prev
;
526 * bigalloc hashing and locking support.
528 * Return an unmasked hash code for the passed pointer.
531 _bigalloc_hash(void *ptr
)
535 hv
= ((int)(intptr_t)ptr
>> PAGE_SHIFT
) ^
536 ((int)(intptr_t)ptr
>> (PAGE_SHIFT
+ BIGHSHIFT
));
542 * Lock the hash chain and return a pointer to its base for the specified
545 static __inline bigalloc_t
*
546 bigalloc_lock(void *ptr
)
548 int hv
= _bigalloc_hash(ptr
);
551 bigp
= &bigalloc_array
[hv
& BIGHMASK
];
553 _SPINLOCK(&bigspin_array
[hv
& BIGXMASK
]);
558 * Lock the hash chain and return a pointer to its base for the specified
561 * BUT, if the hash chain is empty, just return NULL and do not bother
564 static __inline bigalloc_t
*
565 bigalloc_check_and_lock(void *ptr
)
567 int hv
= _bigalloc_hash(ptr
);
570 bigp
= &bigalloc_array
[hv
& BIGHMASK
];
574 _SPINLOCK(&bigspin_array
[hv
& BIGXMASK
]);
580 bigalloc_unlock(void *ptr
)
585 hv
= _bigalloc_hash(ptr
);
586 _SPINUNLOCK(&bigspin_array
[hv
& BIGXMASK
]);
591 * Find a bigcache entry that might work for the allocation. SMP races are
592 * ok here except for the swap (that is, it is ok if bigcache_size_array[i]
593 * is wrong or if a NULL or too-small big is returned).
595 * Generally speaking it is ok to find a large entry even if the bytes
596 * requested are relatively small (but still oversized), because we really
597 * don't know *what* the application is going to do with the buffer.
601 bigcache_find_alloc(size_t bytes
)
603 bigalloc_t big
= NULL
;
607 for (i
= 0; i
< BIGCACHE
; ++i
) {
608 test
= bigcache_size_array
[i
];
610 bigcache_size_array
[i
] = 0;
611 big
= atomic_swap_ptr(&bigcache_array
[i
], NULL
);
619 * Free a bigcache entry, possibly returning one that the caller really must
620 * free. This is used to cache recent oversized memory blocks. Only
621 * big blocks smaller than BIGCACHE_LIMIT will be cached this way, so try
622 * to collect the biggest ones we can that are under the limit.
626 bigcache_find_free(bigalloc_t big
)
632 b
= ++bigcache_index
;
633 for (i
= 0; i
< BIGCACHE
; ++i
) {
634 j
= (b
+ i
) & BIGCACHE_MASK
;
635 if (bigcache_size_array
[j
] < big
->bytes
) {
636 bigcache_size_array
[j
] = big
->bytes
;
637 big
= atomic_swap_ptr(&bigcache_array
[j
], big
);
646 handle_excess_big(void)
652 if (excess_alloc
<= BIGCACHE_EXCESS
)
655 for (i
= 0; i
< BIGHSIZE
; ++i
) {
656 bigp
= &bigalloc_array
[i
];
660 _SPINLOCK(&bigspin_array
[i
& BIGXMASK
]);
661 for (big
= *bigp
; big
; big
= big
->next
) {
662 if (big
->active
< big
->bytes
) {
663 MASSERT((big
->active
& PAGE_MASK
) == 0);
664 MASSERT((big
->bytes
& PAGE_MASK
) == 0);
665 munmap((char *)big
->base
+ big
->active
,
666 big
->bytes
- big
->active
);
667 atomic_add_long(&excess_alloc
,
668 big
->active
- big
->bytes
);
669 big
->bytes
= big
->active
;
673 _SPINUNLOCK(&bigspin_array
[i
& BIGXMASK
]);
678 * Calculate the zone index for the allocation request size and set the
679 * allocation request size to that particular zone's chunk size.
682 zoneindex(size_t *bytes
, size_t *chunking
)
684 size_t n
= (unsigned int)*bytes
; /* unsigned for shift opt */
687 * This used to be 8-byte chunks and 16 zones for n < 128.
688 * However some instructions may require 16-byte alignment
689 * (aka SIMD) and programs might not request an aligned size
690 * (aka GCC-7), so change this as follows:
692 * 0-15 bytes 8-byte alignment in two zones (0-1)
693 * 16-127 bytes 16-byte alignment in four zones (3-10)
694 * zone index 2 and 11-15 are currently unused.
697 *bytes
= n
= (n
+ 7) & ~7;
699 return(n
/ 8 - 1); /* 8 byte chunks, 2 zones */
700 /* zones 0,1, zone 2 is unused */
703 *bytes
= n
= (n
+ 15) & ~15;
705 return(n
/ 16 + 2); /* 16 byte chunks, 8 zones */
706 /* zones 3-10, zones 11-15 unused */
709 *bytes
= n
= (n
+ 15) & ~15;
715 *bytes
= n
= (n
+ 31) & ~31;
720 *bytes
= n
= (n
+ 63) & ~63;
725 *bytes
= n
= (n
+ 127) & ~127;
727 return(n
/ 128 + 31);
730 *bytes
= n
= (n
+ 255) & ~255;
732 return(n
/ 256 + 39);
734 *bytes
= n
= (n
+ 511) & ~511;
736 return(n
/ 512 + 47);
738 #if ZALLOC_ZONE_LIMIT > 8192
740 *bytes
= n
= (n
+ 1023) & ~1023;
742 return(n
/ 1024 + 55);
745 #if ZALLOC_ZONE_LIMIT > 16384
747 *bytes
= n
= (n
+ 2047) & ~2047;
749 return(n
/ 2048 + 63);
752 _mpanic("Unexpected byte count %zu", n
);
757 * malloc() - call internal slab allocator
760 __malloc(size_t size
)
764 ptr
= _slaballoc(size
, 0);
768 UTRACE(0, size
, ptr
);
772 #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
775 * calloc() - call internal slab allocator
778 __calloc(size_t number
, size_t size
)
782 if ((number
>= MUL_NO_OVERFLOW
|| size
>= MUL_NO_OVERFLOW
) &&
783 number
> 0 && SIZE_MAX
/ number
< size
) {
788 ptr
= _slaballoc(number
* size
, SAFLAG_ZERO
);
792 UTRACE(0, number
* size
, ptr
);
797 * realloc() (SLAB ALLOCATOR)
799 * We do not attempt to optimize this routine beyond reusing the same
800 * pointer if the new size fits within the chunking of the old pointer's
804 __realloc(void *ptr
, size_t size
)
807 ret
= _slabrealloc(ptr
, size
);
811 UTRACE(ptr
, size
, ret
);
818 * Allocate (size) bytes with a alignment of (alignment), where (alignment)
819 * is a power of 2 >= sizeof(void *).
821 * The slab allocator will allocate on power-of-2 boundaries up to
822 * at least PAGE_SIZE. We use the zoneindex mechanic to find a
823 * zone matching the requirements, and _vmem_alloc() otherwise.
826 __posix_memalign(void **memptr
, size_t alignment
, size_t size
)
834 * OpenGroup spec issue 6 checks
836 if ((alignment
| (alignment
- 1)) + 1 != (alignment
<< 1)) {
840 if (alignment
< sizeof(void *)) {
846 * Our zone mechanism guarantees same-sized alignment for any
847 * power-of-2 allocation. If size is a power-of-2 and reasonable
848 * we can just call _slaballoc() and be done. We round size up
849 * to the nearest alignment boundary to improve our odds of
850 * it becoming a power-of-2 if it wasn't before.
852 if (size
<= alignment
)
855 size
= (size
+ alignment
- 1) & ~(size_t)(alignment
- 1);
856 if (size
< PAGE_SIZE
&& (size
| (size
- 1)) + 1 == (size
<< 1)) {
857 *memptr
= _slaballoc(size
, 0);
858 return(*memptr
? 0 : ENOMEM
);
862 * Otherwise locate a zone with a chunking that matches
863 * the requested alignment, within reason. Consider two cases:
865 * (1) A 1K allocation on a 32-byte alignment. The first zoneindex
866 * we find will be the best fit because the chunking will be
867 * greater or equal to the alignment.
869 * (2) A 513 allocation on a 256-byte alignment. In this case
870 * the first zoneindex we find will be for 576 byte allocations
871 * with a chunking of 64, which is not sufficient. To fix this
872 * we simply find the nearest power-of-2 >= size and use the
873 * same side-effect of _slaballoc() which guarantees
874 * same-alignment on a power-of-2 allocation.
876 if (size
< PAGE_SIZE
) {
877 zi
= zoneindex(&size
, &chunking
);
878 if (chunking
>= alignment
) {
879 *memptr
= _slaballoc(size
, 0);
880 return(*memptr
? 0 : ENOMEM
);
886 while (alignment
< size
)
888 *memptr
= _slaballoc(alignment
, 0);
889 return(*memptr
? 0 : ENOMEM
);
893 * If the slab allocator cannot handle it use vmem_alloc().
895 * Alignment must be adjusted up to at least PAGE_SIZE in this case.
897 if (alignment
< PAGE_SIZE
)
898 alignment
= PAGE_SIZE
;
899 if (size
< alignment
)
901 size
= (size
+ PAGE_MASK
) & ~(size_t)PAGE_MASK
;
902 *memptr
= _vmem_alloc(size
, alignment
, 0);
906 big
= _slaballoc(sizeof(struct bigalloc
), 0);
908 _vmem_free(*memptr
, size
);
912 bigp
= bigalloc_lock(*memptr
);
915 big
->bytes
= size
; /* no excess */
918 bigalloc_unlock(*memptr
);
924 * free() (SLAB ALLOCATOR) - do the obvious
930 _slabfree(ptr
, 0, NULL
);
934 * _slaballoc() (SLAB ALLOCATOR)
936 * Allocate memory via the slab allocator. If the request is too large,
937 * or if it page-aligned beyond a certain size, we fall back to the
941 _slaballoc(size_t size
, int flags
)
952 * Handle the degenerate size == 0 case. Yes, this does happen.
953 * Return a special pointer. This is to maintain compatibility with
954 * the original malloc implementation. Certain devices, such as the
955 * adaptec driver, not only allocate 0 bytes, they check for NULL and
956 * also realloc() later on. Joy.
961 /* Capture global flags */
962 flags
|= g_malloc_flags
;
965 * Handle large allocations directly. There should not be very many
966 * of these so performance is not a big issue.
968 * The backend allocator is pretty nasty on a SMP system. Use the
969 * slab allocator for one and two page-sized chunks even though we
970 * lose some efficiency.
972 if (size
>= ZoneLimit
||
973 ((size
& PAGE_MASK
) == 0 && size
> PAGE_SIZE
*2)) {
978 * Page-align and cache-color in case of virtually indexed
979 * physically tagged L1 caches (aka SandyBridge). No sweat
980 * otherwise, so just do it.
982 * (don't count as excess).
984 size
= (size
+ PAGE_MASK
) & ~(size_t)PAGE_MASK
;
985 if ((size
& (PAGE_SIZE
* 2 - 1)) == 0)
989 * Try to reuse a cached big block to avoid mmap'ing. If it
990 * turns out not to fit our requirements we throw it away
991 * and allocate normally.
994 if (size
<= BIGCACHE_LIMIT
) {
995 big
= bigcache_find_alloc(size
);
996 if (big
&& big
->bytes
< size
) {
997 _slabfree(big
->base
, FASTSLABREALLOC
, &big
);
1003 if (flags
& SAFLAG_ZERO
)
1006 chunk
= _vmem_alloc(size
, PAGE_SIZE
, flags
);
1010 big
= _slaballoc(sizeof(struct bigalloc
), 0);
1012 _vmem_free(chunk
, size
);
1020 bigp
= bigalloc_lock(chunk
);
1021 if (big
->active
< big
->bytes
) {
1022 atomic_add_long(&excess_alloc
,
1023 big
->bytes
- big
->active
);
1027 bigalloc_unlock(chunk
);
1028 handle_excess_big();
1033 /* Compute allocation zone; zoneindex will panic on excessive sizes */
1034 zi
= zoneindex(&size
, &chunking
);
1035 MASSERT(zi
< NZONES
);
1037 obj
= mtmagazine_alloc(zi
);
1039 if (flags
& SAFLAG_ZERO
)
1044 slgd
= &SLGlobalData
;
1048 * Attempt to allocate out of an existing zone. If all zones are
1049 * exhausted pull one off the free list or allocate a new one.
1051 if ((z
= slgd
->ZoneAry
[zi
]) == NULL
) {
1052 z
= zone_alloc(flags
);
1057 * How big is the base structure?
1059 off
= sizeof(struct slzone
);
1062 * Align the storage in the zone based on the chunking.
1064 * Guarantee power-of-2 alignment for power-of-2-sized
1065 * chunks. Otherwise align based on the chunking size
1066 * (typically 8 or 16 bytes for small allocations).
1068 * NOTE: Allocations >= ZoneLimit are governed by the
1069 * bigalloc code and typically only guarantee page-alignment.
1071 * Set initial conditions for UIndex near the zone header
1072 * to reduce unecessary page faults, vs semi-randomization
1073 * to improve L1 cache saturation.
1075 if ((size
| (size
- 1)) + 1 == (size
<< 1))
1076 off
= roundup2(off
, size
);
1078 off
= roundup2(off
, chunking
);
1079 z
->z_Magic
= ZALLOC_SLAB_MAGIC
;
1080 z
->z_ZoneIndex
= zi
;
1081 z
->z_NMax
= (ZoneSize
- off
) / size
;
1082 z
->z_NFree
= z
->z_NMax
;
1083 z
->z_BasePtr
= (char *)z
+ off
;
1084 z
->z_UIndex
= z
->z_UEndIndex
= 0;
1085 z
->z_ChunkSize
= size
;
1086 z
->z_FirstFreePg
= ZonePageCount
;
1087 z
->z_Next
= slgd
->ZoneAry
[zi
];
1088 slgd
->ZoneAry
[zi
] = z
;
1089 if ((z
->z_Flags
& SLZF_UNOTZEROD
) == 0) {
1090 flags
&= ~SAFLAG_ZERO
; /* already zero'd */
1091 flags
|= SAFLAG_PASSIVE
;
1095 * Slide the base index for initial allocations out of the
1096 * next zone we create so we do not over-weight the lower
1097 * part of the cpu memory caches.
1099 slgd
->JunkIndex
= (slgd
->JunkIndex
+ ZALLOC_SLAB_SLIDE
)
1100 & (ZALLOC_MAX_ZONE_SIZE
- 1);
1104 * Ok, we have a zone from which at least one chunk is available.
1106 * Remove us from the ZoneAry[] when we become empty
1108 MASSERT(z
->z_NFree
> 0);
1110 if (--z
->z_NFree
== 0) {
1111 slgd
->ZoneAry
[zi
] = z
->z_Next
;
1116 * Locate a chunk in a free page. This attempts to localize
1117 * reallocations into earlier pages without us having to sort
1118 * the chunk list. A chunk may still overlap a page boundary.
1120 while (z
->z_FirstFreePg
< ZonePageCount
) {
1121 if ((chunk
= z
->z_PageAry
[z
->z_FirstFreePg
]) != NULL
) {
1122 MASSERT((uintptr_t)chunk
& ZoneMask
);
1123 z
->z_PageAry
[z
->z_FirstFreePg
] = chunk
->c_Next
;
1130 * No chunks are available but NFree said we had some memory,
1131 * so it must be available in the never-before-used-memory
1132 * area governed by UIndex. The consequences are very
1133 * serious if our zone got corrupted so we use an explicit
1134 * panic rather then a KASSERT.
1136 chunk
= (slchunk_t
)(z
->z_BasePtr
+ z
->z_UIndex
* size
);
1138 if (++z
->z_UIndex
== z
->z_NMax
)
1140 if (z
->z_UIndex
== z
->z_UEndIndex
) {
1141 if (z
->z_NFree
!= 0)
1142 _mpanic("slaballoc: corrupted zone");
1145 if ((z
->z_Flags
& SLZF_UNOTZEROD
) == 0) {
1146 flags
&= ~SAFLAG_ZERO
;
1147 flags
|= SAFLAG_PASSIVE
;
1152 if (flags
& SAFLAG_ZERO
)
1161 * Reallocate memory within the chunk
1164 _slabrealloc(void *ptr
, size_t size
)
1172 return(_slaballoc(size
, 0));
1179 * Handle oversized allocations.
1181 if ((bigp
= bigalloc_check_and_lock(ptr
)) != NULL
) {
1185 while ((big
= *bigp
) != NULL
) {
1186 if (big
->base
== ptr
) {
1187 size
= (size
+ PAGE_MASK
) & ~(size_t)PAGE_MASK
;
1188 bigbytes
= big
->bytes
;
1191 * If it already fits determine if it makes
1192 * sense to shrink/reallocate. Try to optimize
1193 * programs which stupidly make incremental
1194 * reallocations larger or smaller by scaling
1195 * the allocation. Also deal with potential
1198 if (size
>= (bigbytes
>> 1) &&
1200 if (big
->active
!= size
) {
1201 atomic_add_long(&excess_alloc
,
1206 bigalloc_unlock(ptr
);
1211 * For large reallocations, allocate more space
1212 * than we need to try to avoid excessive
1213 * reallocations later on.
1215 chunking
= size
+ (size
>> 3);
1216 chunking
= (chunking
+ PAGE_MASK
) &
1220 * Try to allocate adjacently in case the
1221 * program is idiotically realloc()ing a
1222 * huge memory block just slightly bigger.
1223 * (llvm's llc tends to do this a lot).
1225 * (MAP_TRYFIXED forces mmap to fail if there
1226 * is already something at the address).
1228 if (chunking
> bigbytes
) {
1230 int errno_save
= errno
;
1232 addr
= mmap((char *)ptr
+ bigbytes
,
1233 chunking
- bigbytes
,
1234 PROT_READ
|PROT_WRITE
,
1235 MAP_PRIVATE
|MAP_ANON
|
1239 if (addr
== (char *)ptr
+ bigbytes
) {
1240 atomic_add_long(&excess_alloc
,
1245 big
->bytes
= chunking
;
1247 bigalloc_unlock(ptr
);
1251 MASSERT((void *)addr
== MAP_FAILED
);
1255 * Failed, unlink big and allocate fresh.
1256 * (note that we have to leave (big) intact
1257 * in case the slaballoc fails).
1260 bigalloc_unlock(ptr
);
1261 if ((nptr
= _slaballoc(size
, 0)) == NULL
) {
1263 bigp
= bigalloc_lock(ptr
);
1266 bigalloc_unlock(ptr
);
1269 if (size
> bigbytes
)
1271 bcopy(ptr
, nptr
, size
);
1272 atomic_add_long(&excess_alloc
, big
->active
-
1274 _slabfree(ptr
, FASTSLABREALLOC
, &big
);
1280 bigalloc_unlock(ptr
);
1281 handle_excess_big();
1285 * Get the original allocation's zone. If the new request winds
1286 * up using the same chunk size we do not have to do anything.
1288 * NOTE: We don't have to lock the globaldata here, the fields we
1289 * access here will not change at least as long as we have control
1290 * over the allocation.
1292 z
= (slzone_t
)((uintptr_t)ptr
& ~(uintptr_t)ZoneMask
);
1293 MASSERT(z
->z_Magic
== ZALLOC_SLAB_MAGIC
);
1296 * Use zoneindex() to chunk-align the new size, as long as the
1297 * new size is not too large.
1299 if (size
< ZoneLimit
) {
1300 zoneindex(&size
, &chunking
);
1301 if (z
->z_ChunkSize
== size
) {
1307 * Allocate memory for the new request size and copy as appropriate.
1309 if ((nptr
= _slaballoc(size
, 0)) != NULL
) {
1310 if (size
> z
->z_ChunkSize
)
1311 size
= z
->z_ChunkSize
;
1312 bcopy(ptr
, nptr
, size
);
1313 _slabfree(ptr
, 0, NULL
);
1320 * free (SLAB ALLOCATOR)
1322 * Free a memory block previously allocated by malloc. Note that we do not
1323 * attempt to uplodate ks_loosememuse as MP races could prevent us from
1324 * checking memory limits in malloc.
1327 * FASTSLABREALLOC Fast call from realloc, *rbigp already
1333 _slabfree(void *ptr
, int flags
, bigalloc_t
*rbigp
)
1339 slglobaldata_t slgd
;
1344 /* Fast realloc path for big allocations */
1345 if (flags
& FASTSLABREALLOC
) {
1347 goto fastslabrealloc
;
1351 * Handle NULL frees and special 0-byte allocations
1357 * Handle oversized allocations.
1359 if ((bigp
= bigalloc_check_and_lock(ptr
)) != NULL
) {
1360 while ((big
= *bigp
) != NULL
) {
1361 if (big
->base
== ptr
) {
1363 atomic_add_long(&excess_alloc
, big
->active
-
1365 bigalloc_unlock(ptr
);
1368 * Try to stash the block we are freeing,
1369 * potentially receiving another block in
1370 * return which must be freed.
1373 if (big
->bytes
<= BIGCACHE_LIMIT
) {
1374 big
= bigcache_find_free(big
);
1378 ptr
= big
->base
; /* reload */
1380 _slabfree(big
, 0, NULL
);
1381 _vmem_free(ptr
, size
);
1386 bigalloc_unlock(ptr
);
1387 handle_excess_big();
1391 * Zone case. Figure out the zone based on the fact that it is
1394 z
= (slzone_t
)((uintptr_t)ptr
& ~(uintptr_t)ZoneMask
);
1395 MASSERT(z
->z_Magic
== ZALLOC_SLAB_MAGIC
);
1397 size
= z
->z_ChunkSize
;
1398 zi
= z
->z_ZoneIndex
;
1400 if (g_malloc_flags
& SAFLAG_ZERO
)
1403 if (mtmagazine_free(zi
, ptr
) == 0)
1406 pgno
= ((char *)ptr
- (char *)z
) >> PAGE_SHIFT
;
1408 slgd
= &SLGlobalData
;
1412 * Add this free non-zero'd chunk to a linked list for reuse, adjust
1415 chunk
->c_Next
= z
->z_PageAry
[pgno
];
1416 z
->z_PageAry
[pgno
] = chunk
;
1417 if (z
->z_FirstFreePg
> pgno
)
1418 z
->z_FirstFreePg
= pgno
;
1421 * Bump the number of free chunks. If it becomes non-zero the zone
1422 * must be added back onto the appropriate list.
1424 if (z
->z_NFree
++ == 0) {
1425 z
->z_Next
= slgd
->ZoneAry
[z
->z_ZoneIndex
];
1426 slgd
->ZoneAry
[z
->z_ZoneIndex
] = z
;
1430 * If the zone becomes totally free then release it.
1432 if (z
->z_NFree
== z
->z_NMax
) {
1435 pz
= &slgd
->ZoneAry
[z
->z_ZoneIndex
];
1437 pz
= &(*pz
)->z_Next
;
1442 /* slgd lock released */
1449 * Allocate and return a magazine. NULL is returned and *burst is adjusted
1450 * if the magazine is empty.
1452 static __inline
void *
1453 magazine_alloc(struct magazine
*mp
, int *burst
)
1459 if (MAGAZINE_NOTEMPTY(mp
)) {
1460 obj
= mp
->objects
[--mp
->rounds
];
1465 * Return burst factor to caller along with NULL
1467 if ((mp
->flags
& M_BURST
) && (burst
!= NULL
)) {
1468 *burst
= mp
->burst_factor
;
1470 /* Reduce burst factor by NSCALE; if it hits 1, disable BURST */
1471 if ((mp
->flags
& M_BURST
) && (mp
->flags
& M_BURST_EARLY
) &&
1473 mp
->burst_factor
-= M_BURST_NSCALE
;
1474 if (mp
->burst_factor
<= 1) {
1475 mp
->burst_factor
= 1;
1476 mp
->flags
&= ~(M_BURST
);
1477 mp
->flags
&= ~(M_BURST_EARLY
);
1484 magazine_free(struct magazine
*mp
, void *p
)
1486 if (mp
!= NULL
&& MAGAZINE_NOTFULL(mp
)) {
1487 mp
->objects
[mp
->rounds
++] = p
;
1495 mtmagazine_alloc(int zi
)
1498 struct magazine
*mp
, *emptymag
;
1503 * Do not try to access per-thread magazines while the mtmagazine
1504 * is being initialized or destroyed.
1511 * Primary per-thread allocation loop
1515 * If the loaded magazine has rounds, allocate and return
1517 mp
= tp
->mags
[zi
].loaded
;
1518 obj
= magazine_alloc(mp
, NULL
);
1523 * If the prev magazine is full, swap with the loaded
1524 * magazine and retry.
1526 mp
= tp
->mags
[zi
].prev
;
1527 if (mp
&& MAGAZINE_FULL(mp
)) {
1528 MASSERT(mp
->rounds
!= 0);
1529 swap_mags(&tp
->mags
[zi
]); /* prev now empty */
1534 * Try to get a full magazine from the depot. Cycle
1535 * through depot(full)->loaded->prev->depot(empty).
1536 * Retry if a full magazine was available from the depot.
1538 * Return NULL (caller will fall through) if no magazines
1539 * can be found anywhere.
1543 emptymag
= tp
->mags
[zi
].prev
;
1545 SLIST_INSERT_HEAD(&d
->empty
, emptymag
, nextmagazine
);
1546 tp
->mags
[zi
].prev
= tp
->mags
[zi
].loaded
;
1547 mp
= SLIST_FIRST(&d
->full
); /* loaded magazine */
1548 tp
->mags
[zi
].loaded
= mp
;
1550 SLIST_REMOVE_HEAD(&d
->full
, nextmagazine
);
1551 MASSERT(MAGAZINE_NOTEMPTY(mp
));
1563 mtmagazine_free(int zi
, void *ptr
)
1566 struct magazine
*mp
, *loadedmag
;
1571 * Do not try to access per-thread magazines while the mtmagazine
1572 * is being initialized or destroyed.
1579 * Primary per-thread freeing loop
1583 * Make sure a new magazine is available in case we have
1584 * to use it. Staging the newmag allows us to avoid
1585 * some locking/reentrancy complexity.
1587 * Temporarily disable the per-thread caches for this
1588 * allocation to avoid reentrancy and/or to avoid a
1589 * stack overflow if the [zi] happens to be the same that
1590 * would be used to allocate the new magazine.
1592 if (tp
->newmag
== NULL
) {
1594 tp
->newmag
= _slaballoc(sizeof(struct magazine
),
1597 if (tp
->newmag
== NULL
) {
1604 * If the loaded magazine has space, free directly to it
1606 rc
= magazine_free(tp
->mags
[zi
].loaded
, ptr
);
1611 * If the prev magazine is empty, swap with the loaded
1612 * magazine and retry.
1614 mp
= tp
->mags
[zi
].prev
;
1615 if (mp
&& MAGAZINE_EMPTY(mp
)) {
1616 MASSERT(mp
->rounds
== 0);
1617 swap_mags(&tp
->mags
[zi
]); /* prev now full */
1622 * Try to get an empty magazine from the depot. Cycle
1623 * through depot(empty)->loaded->prev->depot(full).
1624 * Retry if an empty magazine was available from the depot.
1629 if ((loadedmag
= tp
->mags
[zi
].prev
) != NULL
)
1630 SLIST_INSERT_HEAD(&d
->full
, loadedmag
, nextmagazine
);
1631 tp
->mags
[zi
].prev
= tp
->mags
[zi
].loaded
;
1632 mp
= SLIST_FIRST(&d
->empty
);
1634 tp
->mags
[zi
].loaded
= mp
;
1635 SLIST_REMOVE_HEAD(&d
->empty
, nextmagazine
);
1636 MASSERT(MAGAZINE_NOTFULL(mp
));
1640 mp
->capacity
= M_MAX_ROUNDS
;
1643 tp
->mags
[zi
].loaded
= mp
;
1652 mtmagazine_init(void)
1656 error
= pthread_key_create(&thread_mags_key
, mtmagazine_destructor
);
1662 * This function is only used by the thread exit destructor
1665 mtmagazine_drain(struct magazine
*mp
)
1669 while (MAGAZINE_NOTEMPTY(mp
)) {
1670 obj
= magazine_alloc(mp
, NULL
);
1671 _slabfree(obj
, 0, NULL
);
1676 * mtmagazine_destructor()
1678 * When a thread exits, we reclaim all its resources; all its magazines are
1679 * drained and the structures are freed.
1681 * WARNING! The destructor can be called multiple times if the larger user
1682 * program has its own destructors which run after ours which
1683 * allocate or free memory.
1686 mtmagazine_destructor(void *thrp
)
1688 thr_mags
*tp
= thrp
;
1689 struct magazine
*mp
;
1693 * Prevent further use of mtmagazines while we are destructing
1694 * them, as well as for any destructors which are run after us
1695 * prior to the thread actually being destroyed.
1699 for (i
= 0; i
< NZONES
; i
++) {
1700 mp
= tp
->mags
[i
].loaded
;
1701 tp
->mags
[i
].loaded
= NULL
;
1703 if (MAGAZINE_NOTEMPTY(mp
))
1704 mtmagazine_drain(mp
);
1705 _slabfree(mp
, 0, NULL
);
1708 mp
= tp
->mags
[i
].prev
;
1709 tp
->mags
[i
].prev
= NULL
;
1711 if (MAGAZINE_NOTEMPTY(mp
))
1712 mtmagazine_drain(mp
);
1713 _slabfree(mp
, 0, NULL
);
1720 _slabfree(mp
, 0, NULL
);
1727 * Attempt to allocate a zone from the zone magazine; the zone magazine has
1728 * M_BURST_EARLY enabled, so honor the burst request from the magazine.
1731 zone_alloc(int flags
)
1733 slglobaldata_t slgd
= &SLGlobalData
;
1738 zone_magazine_lock();
1741 z
= magazine_alloc(&zone_magazine
, &burst
);
1742 if (z
== NULL
&& burst
== 1) {
1743 zone_magazine_unlock();
1744 z
= _vmem_alloc(ZoneSize
* burst
, ZoneSize
, flags
);
1745 } else if (z
== NULL
) {
1746 z
= _vmem_alloc(ZoneSize
* burst
, ZoneSize
, flags
);
1748 for (i
= 1; i
< burst
; i
++) {
1749 j
= magazine_free(&zone_magazine
,
1750 (char *) z
+ (ZoneSize
* i
));
1754 zone_magazine_unlock();
1756 z
->z_Flags
|= SLZF_UNOTZEROD
;
1757 zone_magazine_unlock();
1766 * Release a zone and unlock the slgd lock.
1771 slglobaldata_t slgd
= &SLGlobalData
;
1772 void *excess
[M_ZONE_ROUNDS
- M_LOW_ROUNDS
] = {};
1775 zone_magazine_lock();
1778 bzero(z
, sizeof(struct slzone
));
1781 madvise(z
, ZoneSize
, MADV_FREE
);
1783 i
= magazine_free(&zone_magazine
, z
);
1786 * If we failed to free, collect excess magazines; release the zone
1787 * magazine lock, and then free to the system via _vmem_free. Re-enable
1788 * BURST mode for the magazine.
1791 j
= zone_magazine
.rounds
- zone_magazine
.low_factor
;
1792 for (i
= 0; i
< j
; i
++) {
1793 excess
[i
] = magazine_alloc(&zone_magazine
, NULL
);
1794 MASSERT(excess
[i
] != NULL
);
1797 zone_magazine_unlock();
1799 for (i
= 0; i
< j
; i
++)
1800 _vmem_free(excess
[i
], ZoneSize
);
1802 _vmem_free(z
, ZoneSize
);
1804 zone_magazine_unlock();
1811 * Directly map memory in PAGE_SIZE'd chunks with the specified
1814 * Alignment must be a multiple of PAGE_SIZE.
1816 * Size must be >= alignment.
1819 _vmem_alloc(size_t size
, size_t align
, int flags
)
1826 * Map anonymous private memory.
1828 addr
= mmap(NULL
, size
, PROT_READ
|PROT_WRITE
,
1829 MAP_PRIVATE
|MAP_ANON
, -1, 0);
1830 if (addr
== MAP_FAILED
)
1834 * Check alignment. The misaligned offset is also the excess
1835 * amount. If misaligned unmap the excess so we have a chance of
1836 * mapping at the next alignment point and recursively try again.
1838 * BBBBBBBBBBB BBBBBBBBBBB BBBBBBBBBBB block alignment
1839 * aaaaaaaaa aaaaaaaaaaa aa mis-aligned allocation
1840 * xxxxxxxxx final excess calculation
1841 * ^ returned address
1843 excess
= (uintptr_t)addr
& (align
- 1);
1846 excess
= align
- excess
;
1849 munmap(save
+ excess
, size
- excess
);
1850 addr
= _vmem_alloc(size
, align
, flags
);
1851 munmap(save
, excess
);
1853 return((void *)addr
);
1859 * Free a chunk of memory allocated with _vmem_alloc()
1862 _vmem_free(void *ptr
, size_t size
)
1868 * Panic on fatal conditions
1871 _mpanic(const char *ctl
, ...)
1875 if (malloc_panic
== 0) {
1878 vfprintf(stderr
, ctl
, va
);
1879 fprintf(stderr
, "\n");
1886 __weak_reference(__malloc
, malloc
);
1887 __weak_reference(__calloc
, calloc
);
1888 __weak_reference(__posix_memalign
, posix_memalign
);
1889 __weak_reference(__realloc
, realloc
);
1890 __weak_reference(__free
, free
);