2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <pthread_internals.h>
31 #import <malloc/malloc.h>
33 #import <crt_externs.h>
35 #import <pthread_internals.h>
37 #import "scalable_malloc.h"
38 #import "stack_logging.h"
40 #define USE_SLEEP_RATHER_THAN_ABORT 0
42 #define INITIAL_ZONES 8 // After this number, we reallocate for new zones
44 typedef void (malloc_logger_t
)(unsigned type
, unsigned arg1
, unsigned arg2
, unsigned arg3
, unsigned result
, unsigned num_hot_frames_to_skip
);
46 static pthread_lock_t _malloc_lock
;
47 static malloc_zone_t
*initial_malloc_zones
[INITIAL_ZONES
] = {0};
49 /* The following variables are exported for the benefit of performance tools */
50 unsigned malloc_num_zones
= 0;
51 malloc_zone_t
**malloc_zones
= initial_malloc_zones
;
52 malloc_logger_t
*malloc_logger
= NULL
;
54 unsigned malloc_debug_flags
= 0;
56 unsigned malloc_check_start
= 0; // 0 means don't check
57 unsigned malloc_check_counter
= 0;
58 unsigned malloc_check_each
= 1000;
60 static int malloc_check_sleep
= 100; // default 100 second sleep
61 static int malloc_check_abort
= 0; // default is to sleep, not abort
63 static int malloc_free_abort
= 0; // default is not to abort
65 static int malloc_debug_file
;
67 #define MALLOC_LOCK() LOCK(_malloc_lock)
68 #define MALLOC_UNLOCK() UNLOCK(_malloc_lock)
70 #define MALLOC_LOG_TYPE_ALLOCATE stack_logging_type_alloc
71 #define MALLOC_LOG_TYPE_DEALLOCATE stack_logging_type_dealloc
72 #define MALLOC_LOG_TYPE_HAS_ZONE stack_logging_flag_zone
73 #define MALLOC_LOG_TYPE_CLEARED stack_logging_flag_cleared
75 /********* Utilities ************/
77 static inline malloc_zone_t
* find_registered_zone(const void *, size_t *) __attribute__((always_inline
));
78 static inline malloc_zone_t
*
79 find_registered_zone(const void *ptr
, size_t *returned_size
) {
80 // locates the proper zone
81 // if zone found fills returnedSize; else returns NULL
82 // See comment in malloc_zone_register() about clients non locking to call this function
83 // Speed is critical for this function
84 unsigned index
= malloc_num_zones
;
85 malloc_zone_t
**zones
= malloc_zones
;
87 malloc_zone_t
*zone
= *zones
++;
89 size
= zone
->size(zone
, ptr
);
91 if (returned_size
) *returned_size
= size
;
98 /********* Creation and destruction ************/
101 _malloc_initialize(void) {
102 // guaranteed to be called only once
103 (void)malloc_create_zone(0, 0);
104 malloc_set_zone_name(malloc_zones
[0], "DefaultMallocZone");
105 LOCK_INIT(_malloc_lock
);
106 // malloc_printf("%d registered zones\n", malloc_num_zones);
107 // malloc_printf("malloc_zones is at %p; malloc_num_zones is at %p\n", (unsigned)&malloc_zones, (unsigned)&malloc_num_zones);
110 static inline malloc_zone_t
*inline_malloc_default_zone(void) __attribute__((always_inline
));
111 static inline malloc_zone_t
*
112 inline_malloc_default_zone(void) {
113 if (!malloc_num_zones
) _malloc_initialize();
114 // malloc_printf("In inline_malloc_default_zone with %d %d\n", malloc_num_zones, malloc_has_debug_zone);
115 return malloc_zones
[0];
119 malloc_default_zone(void) {
120 return inline_malloc_default_zone();
124 set_flags_from_environment(void) {
128 flag
= getenv("MallocLogFile");
130 fd
= open(flag
, O_WRONLY
|O_APPEND
|O_CREAT
, 0644);
132 malloc_debug_file
= fd
;
133 fcntl(fd
, F_SETFD
, 0); // clear close-on-exec flag XXX why?
135 malloc_printf("Could not open %s, using stderr\n", flag
);
138 if (getenv("MallocGuardEdges")) {
139 malloc_debug_flags
= SCALABLE_MALLOC_ADD_GUARD_PAGES
;
140 malloc_printf("protecting edges\n");
141 if (getenv("MallocDoNotProtectPrelude")) {
142 malloc_debug_flags
|= SCALABLE_MALLOC_DONT_PROTECT_PRELUDE
;
143 malloc_printf("... but not protecting prelude guard page\n");
145 if (getenv("MallocDoNotProtectPostlude")) {
146 malloc_debug_flags
|= SCALABLE_MALLOC_DONT_PROTECT_POSTLUDE
;
147 malloc_printf("... but not protecting postlude guard page\n");
150 flag
= getenv("MallocStackLogging");
152 flag
= getenv("MallocStackLoggingNoCompact");
153 stack_logging_dontcompact
= 1;
156 unsigned val
= strtoul(flag
, NULL
, 0);
157 if (val
== 1) val
= 0;
158 if (val
== -1) val
= 0;
159 malloc_logger
= (val
) ? (void *)val
: stack_logging_log_stack
;
160 stack_logging_enable_logging
= 1;
161 if (malloc_logger
== stack_logging_log_stack
) {
162 malloc_printf("recording stacks using standard recorder\n");
164 malloc_printf("recording stacks using recorder %p\n", malloc_logger
);
166 if (stack_logging_dontcompact
) malloc_printf("stack logging compaction turned off; VM can increase rapidly\n");
168 if (getenv("MallocScribble")) {
169 malloc_debug_flags
|= SCALABLE_MALLOC_DO_SCRIBBLE
;
170 malloc_printf("enabling scribbling to detect mods to free blocks\n");
172 flag
= getenv("MallocCheckHeapStart");
174 malloc_check_start
= strtoul(flag
, NULL
, 0);
175 if (malloc_check_start
== 0) malloc_check_start
= 1;
176 if (malloc_check_start
== -1) malloc_check_start
= 1;
177 flag
= getenv("MallocCheckHeapEach");
179 malloc_check_each
= strtoul(flag
, NULL
, 0);
180 if (malloc_check_each
== 0) malloc_check_each
= 1;
181 if (malloc_check_each
== -1) malloc_check_each
= 1;
183 malloc_printf("checks heap after %dth operation and each %d operations\n", malloc_check_start
, malloc_check_each
);
184 flag
= getenv("MallocCheckHeapAbort");
186 malloc_check_abort
= strtol(flag
, NULL
, 0);
187 if (malloc_check_abort
)
188 malloc_printf("will abort on heap corruption\n");
190 flag
= getenv("MallocCheckHeapSleep");
192 malloc_check_sleep
= strtol(flag
, NULL
, 0);
193 if (malloc_check_sleep
> 0)
194 malloc_printf("will sleep for %d seconds on heap corruption\n", malloc_check_sleep
);
195 else if (malloc_check_sleep
< 0)
196 malloc_printf("will sleep once for %d seconds on heap corruption\n", -malloc_check_sleep
);
198 malloc_printf("no sleep on heap corruption\n");
201 flag
= getenv("MallocBadFreeAbort");
203 malloc_free_abort
= strtol(flag
, NULL
, 0);
204 if (getenv("MallocHelp")) {
206 "environment variables that can be set for debug:\n"
207 "- MallocLogFile <f> to create/append messages to file <f> instead of stderr\n"
208 "- MallocGuardEdges to add 2 guard pages for each large block\n"
209 "- MallocDoNotProtectPrelude to disable protection (when previous flag set)\n"
210 "- MallocDoNotProtectPostlude to disable protection (when previous flag set)\n"
211 "- MallocStackLogging to record all stacks. Tools like leaks can then be applied\n"
212 "- MallocStackLoggingNoCompact to record all stacks. Needed for malloc_history\n"
213 "- MallocScribble to detect writing on free blocks and missing initializers:\n"
214 " 0x55 is written upon free and 0xaa is written on allocation\n"
215 "- MallocCheckHeapStart <n> to start checking the heap after <n> operations\n"
216 "- MallocCheckHeapEach <s> to repeat the checking of the heap after <s> operations\n"
217 "- MallocCheckHeapSleep <t> to sleep <t> seconds on heap corruption\n"
218 "- MallocCheckHeapAbort <b> to abort on heap corruption if <b> is non-zero\n"
219 "- MallocBadFreeAbort <b> to abort on a bad free if <b> is non-zero\n"
220 "- MallocHelp - this help!\n");
225 malloc_create_zone(vm_size_t start_size
, unsigned flags
)
229 if (!malloc_num_zones
) {
230 char **env
= * _NSGetEnviron();
234 malloc_debug_file
= STDERR_FILENO
;
237 * Given that all environment variables start with "Malloc" we optimize by scanning quickly
238 * first the environment, therefore avoiding repeated calls to getenv().
239 * If we are setu/gid these flags are ignored to prevent a malicious invoker from changing
242 for (p
= env
; (c
= *p
) != NULL
; ++p
) {
243 if (!strncmp(c
, "Malloc", 6)) {
245 set_flags_from_environment();
250 zone
= create_scalable_zone(start_size
, malloc_debug_flags
);
251 malloc_zone_register(zone
);
256 malloc_destroy_zone(malloc_zone_t
*zone
) {
257 malloc_zone_unregister(zone
);
261 /********* Block creation and manipulation ************/
264 internal_check(void) {
265 static vm_address_t
*frames
= NULL
;
266 static unsigned num_frames
;
267 if (malloc_zone_check(NULL
)) {
268 malloc_printf("MallocCheckHeap: PASSED check at %dth operation\n", malloc_check_counter
-1);
269 if (!frames
) vm_allocate(mach_task_self(), (void *)&frames
, vm_page_size
, 1);
270 thread_stack_pcs(frames
, vm_page_size
/sizeof(vm_address_t
) - 1, &num_frames
);
272 malloc_printf("*** MallocCheckHeap: FAILED check at %dth operation\n", malloc_check_counter
-1);
275 malloc_printf("Stack for last operation where the malloc check succeeded: ");
276 while (index
< num_frames
) malloc_printf("%p ", frames
[index
++]);
277 malloc_printf("\n(Use 'atos' for a symbolic stack)\n");
279 if (malloc_check_each
> 1) {
280 unsigned recomm_each
= (malloc_check_each
> 10) ? malloc_check_each
/10 : 1;
281 unsigned recomm_start
= (malloc_check_counter
> malloc_check_each
+1) ? malloc_check_counter
-1-malloc_check_each
: 1;
282 malloc_printf("*** Recommend using 'setenv MallocCheckHeapStart %d; setenv MallocCheckHeapEach %d' to narrow down failure\n", recomm_start
, recomm_each
);
284 if (malloc_check_abort
)
286 if (malloc_check_sleep
> 0) {
287 malloc_printf("*** Sleeping for %d seconds to leave time to attach\n",
289 sleep(malloc_check_sleep
);
290 } else if (malloc_check_sleep
< 0) {
291 malloc_printf("*** Sleeping once for %d seconds to leave time to attach\n",
292 -malloc_check_sleep
);
293 sleep(-malloc_check_sleep
);
294 malloc_check_sleep
= 0;
297 malloc_check_start
+= malloc_check_each
;
301 malloc_zone_malloc(malloc_zone_t
*zone
, size_t size
) {
303 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
306 ptr
= zone
->malloc(zone
, size
);
307 if (malloc_logger
) malloc_logger(MALLOC_LOG_TYPE_ALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, size
, 0, (unsigned)ptr
, 0);
312 malloc_zone_calloc(malloc_zone_t
*zone
, size_t num_items
, size_t size
) {
314 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
317 ptr
= zone
->calloc(zone
, num_items
, size
);
318 if (malloc_logger
) malloc_logger(MALLOC_LOG_TYPE_ALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
| MALLOC_LOG_TYPE_CLEARED
, (unsigned)zone
, num_items
* size
, 0, (unsigned)ptr
, 0);
323 malloc_zone_valloc(malloc_zone_t
*zone
, size_t size
) {
325 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
328 ptr
= zone
->valloc(zone
, size
);
329 if (malloc_logger
) malloc_logger(MALLOC_LOG_TYPE_ALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, size
, 0, (unsigned)ptr
, 0);
334 malloc_zone_realloc(malloc_zone_t
*zone
, void *ptr
, size_t size
) {
336 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
339 new_ptr
= zone
->realloc(zone
, ptr
, size
);
340 if (malloc_logger
) malloc_logger(MALLOC_LOG_TYPE_ALLOCATE
| MALLOC_LOG_TYPE_DEALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, (unsigned)ptr
, size
, (unsigned)new_ptr
, 0);
345 malloc_zone_free(malloc_zone_t
*zone
, void *ptr
) {
346 if (malloc_logger
) malloc_logger(MALLOC_LOG_TYPE_DEALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, (unsigned)ptr
, 0, 0, 0);
347 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
350 zone
->free(zone
, ptr
);
354 malloc_zone_from_ptr(const void *ptr
) {
356 if (!ptr
) return NULL
;
357 zone
= find_registered_zone(ptr
, NULL
);
361 /********* Functions for zone implementors ************/
364 malloc_zone_register(malloc_zone_t
*zone
) {
365 /* Note that given the sequencing it is always safe to first get the number of zones, then get malloc_zones without taking the lock, if all you need is to iterate through the list */
367 if (malloc_num_zones
>= INITIAL_ZONES
) {
368 malloc_zone_t
**zones
= malloc_zones
;
369 malloc_zone_t
*pzone
= malloc_zones
[0];
370 boolean_t copy
= malloc_num_zones
== INITIAL_ZONES
;
371 if (copy
) zones
= NULL
; // to avoid realloc on something not allocated
373 zones
= pzone
->realloc(pzone
, zones
, (malloc_num_zones
+ 1) * sizeof(malloc_zone_t
*)); // we leak initial_malloc_zones, not worth tracking it
375 if (copy
) memcpy(zones
, malloc_zones
, malloc_num_zones
* sizeof(malloc_zone_t
*));
376 malloc_zones
= zones
;
378 malloc_zones
[malloc_num_zones
] = zone
;
379 malloc_num_zones
++; // note that we do this after setting malloc_num_zones, so enumerations without taking the lock are safe
381 // malloc_printf("Registered %p malloc_zones at address %p is %p [%d zones]\n", zone, &malloc_zones, malloc_zones, malloc_num_zones);
385 malloc_zone_unregister(malloc_zone_t
*z
) {
388 index
= malloc_num_zones
;
390 malloc_zone_t
*zone
= malloc_zones
[index
];
392 malloc_zones
[index
] = malloc_zones
[--malloc_num_zones
];
398 malloc_printf("*** malloc_zone_unregister() failed for %p\n", z
);
402 malloc_set_zone_name(malloc_zone_t
*z
, const char *name
) {
405 free((char *)z
->zone_name
);
408 newName
= malloc_zone_malloc(z
, strlen(name
) + 1);
409 strcpy(newName
, name
);
410 z
->zone_name
= (const char *)newName
;
414 malloc_get_zone_name(malloc_zone_t
*zone
) {
415 return zone
->zone_name
;
419 * XXX malloc_printf now uses _simple_{,v}dprintf. It only deals with a
420 * subset of printf format specifiers, but it doesn't call malloc.
422 void _simple_dprintf(int, const char *, ...);
423 void _simple_vdprintf(int, const char *, va_list);
426 malloc_printf(const char *format
, ...)
431 /* XXX somewhat rude 'knowing' that pthread_t is a pointer */
432 _simple_dprintf(malloc_debug_file
, "%s(%d,%p) malloc: ", getprogname(), getpid(), (void *)pthread_self());
434 _simple_dprintf(malloc_debug_file
, "%s(%d) malloc: ", getprogname(), getpid());
436 va_start(ap
, format
);
437 _simple_vdprintf(malloc_debug_file
, format
, ap
);
441 /********* Generic ANSI callouts ************/
444 malloc(size_t size
) {
446 retval
= malloc_zone_malloc(inline_malloc_default_zone(), size
);
447 if (retval
== NULL
) {
454 calloc(size_t num_items
, size_t size
) {
456 retval
= malloc_zone_calloc(inline_malloc_default_zone(), num_items
, size
);
457 if (retval
== NULL
) {
467 zone
= find_registered_zone(ptr
, NULL
);
469 malloc_zone_free(zone
, ptr
);
471 malloc_printf("*** Deallocation of a pointer not malloced: %p; "
472 "This could be a double free(), or free() called with the middle of an allocated block; "
473 "Try setting environment variable MallocHelp to see tools to help debug\n", ptr
);
474 if (malloc_free_abort
)
480 realloc(void *old_ptr
, size_t new_size
) {
485 retval
= malloc_zone_malloc(inline_malloc_default_zone(), new_size
);
487 zone
= find_registered_zone(old_ptr
, &old_size
);
488 if (zone
&& (old_size
>= new_size
)) return old_ptr
;
489 if (!zone
) zone
= inline_malloc_default_zone();
490 retval
= malloc_zone_realloc(zone
, old_ptr
, new_size
);
492 if (retval
== NULL
) {
499 valloc(size_t size
) {
501 malloc_zone_t
*zone
= inline_malloc_default_zone();
502 retval
= malloc_zone_valloc(zone
, size
);
503 if (retval
== NULL
) {
515 malloc_size(const void *ptr
) {
517 if (!ptr
) return size
;
518 (void)find_registered_zone(ptr
, &size
);
523 malloc_good_size (size_t size
) {
524 malloc_zone_t
*zone
= inline_malloc_default_zone();
525 return zone
->introspect
->good_size(zone
, size
);
528 /********* Batch methods ************/
531 malloc_zone_batch_malloc(malloc_zone_t
*zone
, size_t size
, void **results
, unsigned num_requested
) {
532 unsigned (*batch_malloc
)(malloc_zone_t
*, size_t, void **, unsigned) = zone
-> batch_malloc
;
533 if (! batch_malloc
) return 0;
534 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
537 unsigned batched
= batch_malloc(zone
, size
, results
, num_requested
);
540 while (index
< batched
) {
541 malloc_logger(MALLOC_LOG_TYPE_ALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, size
, 0, (unsigned)results
[index
], 0);
549 malloc_zone_batch_free(malloc_zone_t
*zone
, void **to_be_freed
, unsigned num
) {
550 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
555 while (index
< num
) {
556 malloc_logger(MALLOC_LOG_TYPE_DEALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, (unsigned)to_be_freed
[index
], 0, 0, 0);
560 void (*batch_free
)(malloc_zone_t
*, void **, unsigned) = zone
-> batch_free
;
562 batch_free(zone
, to_be_freed
, num
);
564 void (*free_fun
)(malloc_zone_t
*, void *) = zone
->free
;
566 void *ptr
= *to_be_freed
++;
572 /********* Functions for performance tools ************/
575 _malloc_default_reader(task_t task
, vm_address_t address
, vm_size_t size
, void **ptr
) {
576 *ptr
= (void *)address
;
581 malloc_get_all_zones(task_t task
, memory_reader_t reader
, vm_address_t
**addresses
, unsigned *count
) {
582 // Note that the 2 following addresses are not correct if the address of the target is different from your own. This notably occurs if the address of System.framework is slid (e.g. different than at B & I )
583 vm_address_t remote_malloc_zones
= (vm_address_t
)&malloc_zones
;
584 vm_address_t remote_malloc_num_zones
= (vm_address_t
)&malloc_num_zones
;
586 vm_address_t zones_address
;
587 vm_address_t
*zones_address_ref
;
589 unsigned *num_zones_ref
;
590 if (!reader
) reader
= _malloc_default_reader
;
591 // printf("Read malloc_zones at address %p should be %p\n", &malloc_zones, malloc_zones);
592 err
= reader(task
, remote_malloc_zones
, sizeof(void *), (void **)&zones_address_ref
);
593 // printf("Read malloc_zones[%p]=%p\n", remote_malloc_zones, *zones_address_ref);
595 malloc_printf("*** malloc_get_all_zones: error reading zones_address at %p\n", (unsigned)remote_malloc_zones
);
598 zones_address
= *zones_address_ref
;
599 // printf("Reading num_zones at address %p\n", remote_malloc_num_zones);
600 err
= reader(task
, remote_malloc_num_zones
, sizeof(unsigned), (void **)&num_zones_ref
);
602 malloc_printf("*** malloc_get_all_zones: error reading num_zones at %p\n", (unsigned)remote_malloc_num_zones
);
605 num_zones
= *num_zones_ref
;
606 // printf("Read malloc_num_zones[%p]=%d\n", remote_malloc_num_zones, num_zones);
608 // printf("malloc_get_all_zones succesfully found %d zones\n", num_zones);
609 err
= reader(task
, zones_address
, sizeof(malloc_zone_t
*) * num_zones
, (void **)addresses
);
611 malloc_printf("*** malloc_get_all_zones: error reading zones at %p\n", (unsigned)&zones_address
);
614 // printf("malloc_get_all_zones succesfully read %d zones\n", num_zones);
618 /********* Debug helpers ************/
621 malloc_zone_print_ptr_info(void *ptr
) {
624 zone
= find_registered_zone(ptr
, NULL
);
626 printf("ptr %p in registered zone %p\n", ptr
, zone
);
628 printf("ptr %p not in heap\n", ptr
);
633 malloc_zone_check(malloc_zone_t
*zone
) {
637 while (index
< malloc_num_zones
) {
638 zone
= malloc_zones
[index
++];
639 if (!zone
->introspect
->check(zone
)) ok
= 0;
642 ok
= zone
->introspect
->check(zone
);
648 malloc_zone_print(malloc_zone_t
*zone
, boolean_t verbose
) {
651 while (index
< malloc_num_zones
) {
652 zone
= malloc_zones
[index
++];
653 zone
->introspect
->print(zone
, verbose
);
656 zone
->introspect
->print(zone
, verbose
);
661 malloc_zone_statistics(malloc_zone_t
*zone
, malloc_statistics_t
*stats
) {
663 memset(stats
, 0, sizeof(stats
));
665 while (index
< malloc_num_zones
) {
666 zone
= malloc_zones
[index
++];
667 malloc_statistics_t this_stats
;
668 zone
->introspect
->statistics(zone
, &this_stats
);
669 stats
->blocks_in_use
+= this_stats
.blocks_in_use
;
670 stats
->size_in_use
+= this_stats
.size_in_use
;
671 stats
->max_size_in_use
+= this_stats
.max_size_in_use
;
672 stats
->size_allocated
+= this_stats
.size_allocated
;
675 zone
->introspect
->statistics(zone
, stats
);
680 malloc_zone_log(malloc_zone_t
*zone
, void *address
) {
683 while (index
< malloc_num_zones
) {
684 zone
= malloc_zones
[index
++];
685 zone
->introspect
->log(zone
, address
);
688 zone
->introspect
->log(zone
, address
);
692 /********* Misc other entry points ************/
695 DefaultMallocError(int x
) {
696 malloc_printf("*** error %d\n", x
);
697 #if USE_SLEEP_RATHER_THAN_ABORT
705 malloc_error(void (*func
)(int)))(int) {
706 return DefaultMallocError
;
710 _malloc_fork_prepare() {
711 /* Prepare the malloc module for a fork by insuring that no thread is in a malloc critical section */
714 while (index
< malloc_num_zones
) {
715 malloc_zone_t
*zone
= malloc_zones
[index
++];
716 zone
->introspect
->force_lock(zone
);
721 _malloc_fork_parent() {
722 /* Called in the parent process after a fork() to resume normal operation. */
725 while (index
< malloc_num_zones
) {
726 malloc_zone_t
*zone
= malloc_zones
[index
++];
727 zone
->introspect
->force_unlock(zone
);
732 _malloc_fork_child() {
733 /* Called in the child process after a fork() to resume normal operation. In the MTASK case we also have to change memory inheritance so that the child does not share memory with the parent. */
736 while (index
< malloc_num_zones
) {
737 malloc_zone_t
*zone
= malloc_zones
[index
++];
738 zone
->introspect
->force_unlock(zone
);
743 * A Glibc-like mstats() interface.
745 * Note that this interface really isn't very good, as it doesn't understand
746 * that we may have multiple allocators running at once. We just massage
747 * the result from malloc_zone_statistics in any case.
752 malloc_statistics_t s
;
755 malloc_zone_statistics(NULL
, &s
);
756 m
.bytes_total
= s
.size_allocated
;
757 m
.chunks_used
= s
.blocks_in_use
;
758 m
.bytes_used
= s
.size_in_use
;
760 m
.bytes_free
= m
.bytes_total
- m
.bytes_used
; /* isn't this somewhat obvious? */
765 /***************** OBSOLETE ENTRY POINTS ********************/
767 #if PHASE_OUT_OLD_MALLOC
768 #error PHASE OUT THE FOLLOWING FUNCTIONS
770 #warning PHASE OUT THE FOLLOWING FUNCTIONS
774 set_malloc_singlethreaded(boolean_t single
) {
775 static boolean_t warned
= 0;
777 #if PHASE_OUT_OLD_MALLOC
778 malloc_printf("*** OBSOLETE: set_malloc_singlethreaded(%d)\n", single
);
785 malloc_singlethreaded() {
786 static boolean_t warned
= 0;
788 malloc_printf("*** OBSOLETE: malloc_singlethreaded()\n");
794 malloc_debug(int level
) {
795 malloc_printf("*** OBSOLETE: malloc_debug()\n");