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
) {
227 if (!malloc_num_zones
) {
228 char **env
= * _NSGetEnviron();
231 /* Given that all environment variables start with "Malloc" we optimize by scanning quickly first the environment, therefore avoiding repeated calls to getenv() */
232 malloc_debug_file
= STDERR_FILENO
;
233 for (p
= env
; (c
= *p
) != NULL
; ++p
) {
234 if (!strncmp(c
, "Malloc", 6)) {
235 set_flags_from_environment();
241 zone
= create_scalable_zone(start_size
, malloc_debug_flags
);
242 malloc_zone_register(zone
);
247 malloc_destroy_zone(malloc_zone_t
*zone
) {
248 malloc_zone_unregister(zone
);
252 /********* Block creation and manipulation ************/
255 internal_check(void) {
256 static vm_address_t
*frames
= NULL
;
257 static unsigned num_frames
;
258 if (malloc_zone_check(NULL
)) {
259 malloc_printf("MallocCheckHeap: PASSED check at %dth operation\n", malloc_check_counter
-1);
260 if (!frames
) vm_allocate(mach_task_self(), (void *)&frames
, vm_page_size
, 1);
261 thread_stack_pcs(frames
, vm_page_size
/sizeof(vm_address_t
) - 1, &num_frames
);
263 malloc_printf("*** MallocCheckHeap: FAILED check at %dth operation\n", malloc_check_counter
-1);
266 malloc_printf("Stack for last operation where the malloc check succeeded: ");
267 while (index
< num_frames
) malloc_printf("%p ", frames
[index
++]);
268 malloc_printf("\n(Use 'atos' for a symbolic stack)\n");
270 if (malloc_check_each
> 1) {
271 unsigned recomm_each
= (malloc_check_each
> 10) ? malloc_check_each
/10 : 1;
272 unsigned recomm_start
= (malloc_check_counter
> malloc_check_each
+1) ? malloc_check_counter
-1-malloc_check_each
: 1;
273 malloc_printf("*** Recommend using 'setenv MallocCheckHeapStart %d; setenv MallocCheckHeapEach %d' to narrow down failure\n", recomm_start
, recomm_each
);
275 if (malloc_check_abort
)
277 if (malloc_check_sleep
> 0) {
278 malloc_printf("*** Sleeping for %d seconds to leave time to attach\n",
280 sleep(malloc_check_sleep
);
281 } else if (malloc_check_sleep
< 0) {
282 malloc_printf("*** Sleeping once for %d seconds to leave time to attach\n",
283 -malloc_check_sleep
);
284 sleep(-malloc_check_sleep
);
285 malloc_check_sleep
= 0;
288 malloc_check_start
+= malloc_check_each
;
292 malloc_zone_malloc(malloc_zone_t
*zone
, size_t size
) {
294 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
297 ptr
= zone
->malloc(zone
, size
);
298 if (malloc_logger
) malloc_logger(MALLOC_LOG_TYPE_ALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, size
, 0, (unsigned)ptr
, 0);
303 malloc_zone_calloc(malloc_zone_t
*zone
, size_t num_items
, size_t size
) {
305 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
308 ptr
= zone
->calloc(zone
, num_items
, size
);
309 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);
314 malloc_zone_valloc(malloc_zone_t
*zone
, size_t size
) {
316 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
319 ptr
= zone
->valloc(zone
, size
);
320 if (malloc_logger
) malloc_logger(MALLOC_LOG_TYPE_ALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, size
, 0, (unsigned)ptr
, 0);
325 malloc_zone_realloc(malloc_zone_t
*zone
, void *ptr
, size_t size
) {
327 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
330 new_ptr
= zone
->realloc(zone
, ptr
, size
);
331 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);
336 malloc_zone_free(malloc_zone_t
*zone
, void *ptr
) {
337 if (malloc_logger
) malloc_logger(MALLOC_LOG_TYPE_DEALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, (unsigned)ptr
, 0, 0, 0);
338 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
341 zone
->free(zone
, ptr
);
345 malloc_zone_from_ptr(const void *ptr
) {
347 if (!ptr
) return NULL
;
348 zone
= find_registered_zone(ptr
, NULL
);
352 /********* Functions for zone implementors ************/
355 malloc_zone_register(malloc_zone_t
*zone
) {
356 /* 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 */
358 if (malloc_num_zones
>= INITIAL_ZONES
) {
359 malloc_zone_t
**zones
= malloc_zones
;
360 malloc_zone_t
*pzone
= malloc_zones
[0];
361 boolean_t copy
= malloc_num_zones
== INITIAL_ZONES
;
362 if (copy
) zones
= NULL
; // to avoid realloc on something not allocated
364 zones
= pzone
->realloc(pzone
, zones
, (malloc_num_zones
+ 1) * sizeof(malloc_zone_t
*)); // we leak initial_malloc_zones, not worth tracking it
366 if (copy
) memcpy(zones
, malloc_zones
, malloc_num_zones
* sizeof(malloc_zone_t
*));
367 malloc_zones
= zones
;
369 malloc_zones
[malloc_num_zones
] = zone
;
370 malloc_num_zones
++; // note that we do this after setting malloc_num_zones, so enumerations without taking the lock are safe
372 // malloc_printf("Registered %p malloc_zones at address %p is %p [%d zones]\n", zone, &malloc_zones, malloc_zones, malloc_num_zones);
376 malloc_zone_unregister(malloc_zone_t
*z
) {
379 index
= malloc_num_zones
;
381 malloc_zone_t
*zone
= malloc_zones
[index
];
383 malloc_zones
[index
] = malloc_zones
[--malloc_num_zones
];
389 malloc_printf("*** malloc_zone_unregister() failed for %p\n", z
);
393 malloc_set_zone_name(malloc_zone_t
*z
, const char *name
) {
396 free((char *)z
->zone_name
);
399 newName
= malloc_zone_malloc(z
, strlen(name
) + 1);
400 strcpy(newName
, name
);
401 z
->zone_name
= (const char *)newName
;
405 malloc_get_zone_name(malloc_zone_t
*zone
) {
406 return zone
->zone_name
;
410 * XXX malloc_printf now uses _simple_{,v}dprintf. It only deals with a
411 * subset of printf format specifiers, but it doesn't call malloc.
413 void _simple_dprintf(int, const char *, ...);
414 void _simple_vdprintf(int, const char *, va_list);
417 malloc_printf(const char *format
, ...)
422 /* XXX somewhat rude 'knowing' that pthread_t is a pointer */
423 _simple_dprintf(malloc_debug_file
, "%s(%d,%p) malloc: ", getprogname(), getpid(), (void *)pthread_self());
425 _simple_dprintf(malloc_debug_file
, "%s(%d) malloc: ", getprogname(), getpid());
427 va_start(ap
, format
);
428 _simple_vdprintf(malloc_debug_file
, format
, ap
);
432 /********* Generic ANSI callouts ************/
435 malloc(size_t size
) {
437 retval
= malloc_zone_malloc(inline_malloc_default_zone(), size
);
438 if (retval
== NULL
) {
445 calloc(size_t num_items
, size_t size
) {
447 retval
= malloc_zone_calloc(inline_malloc_default_zone(), num_items
, size
);
448 if (retval
== NULL
) {
458 zone
= find_registered_zone(ptr
, NULL
);
460 malloc_zone_free(zone
, ptr
);
462 malloc_printf("*** Deallocation of a pointer not malloced: %p; "
463 "This could be a double free(), or free() called with the middle of an allocated block; "
464 "Try setting environment variable MallocHelp to see tools to help debug\n", ptr
);
465 if (malloc_free_abort
)
471 realloc(void *old_ptr
, size_t new_size
) {
476 retval
= malloc_zone_malloc(inline_malloc_default_zone(), new_size
);
478 zone
= find_registered_zone(old_ptr
, &old_size
);
479 if (zone
&& (old_size
>= new_size
)) return old_ptr
;
480 if (!zone
) zone
= inline_malloc_default_zone();
481 retval
= malloc_zone_realloc(zone
, old_ptr
, new_size
);
483 if (retval
== NULL
) {
490 valloc(size_t size
) {
492 malloc_zone_t
*zone
= inline_malloc_default_zone();
493 retval
= malloc_zone_valloc(zone
, size
);
494 if (retval
== NULL
) {
506 malloc_size(const void *ptr
) {
508 if (!ptr
) return size
;
509 (void)find_registered_zone(ptr
, &size
);
514 malloc_good_size (size_t size
) {
515 malloc_zone_t
*zone
= inline_malloc_default_zone();
516 return zone
->introspect
->good_size(zone
, size
);
519 /********* Batch methods ************/
522 malloc_zone_batch_malloc(malloc_zone_t
*zone
, size_t size
, void **results
, unsigned num_requested
) {
523 unsigned (*batch_malloc
)(malloc_zone_t
*, size_t, void **, unsigned) = zone
-> batch_malloc
;
524 if (! batch_malloc
) return 0;
525 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
528 unsigned batched
= batch_malloc(zone
, size
, results
, num_requested
);
531 while (index
< batched
) {
532 malloc_logger(MALLOC_LOG_TYPE_ALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, size
, 0, (unsigned)results
[index
], 0);
540 malloc_zone_batch_free(malloc_zone_t
*zone
, void **to_be_freed
, unsigned num
) {
541 if (malloc_check_start
&& (malloc_check_counter
++ >= malloc_check_start
)) {
546 while (index
< num
) {
547 malloc_logger(MALLOC_LOG_TYPE_DEALLOCATE
| MALLOC_LOG_TYPE_HAS_ZONE
, (unsigned)zone
, (unsigned)to_be_freed
[index
], 0, 0, 0);
551 void (*batch_free
)(malloc_zone_t
*, void **, unsigned) = zone
-> batch_free
;
553 batch_free(zone
, to_be_freed
, num
);
555 void (*free_fun
)(malloc_zone_t
*, void *) = zone
->free
;
557 void *ptr
= *to_be_freed
++;
563 /********* Functions for performance tools ************/
566 _malloc_default_reader(task_t task
, vm_address_t address
, vm_size_t size
, void **ptr
) {
567 *ptr
= (void *)address
;
572 malloc_get_all_zones(task_t task
, memory_reader_t reader
, vm_address_t
**addresses
, unsigned *count
) {
573 // 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 )
574 vm_address_t remote_malloc_zones
= (vm_address_t
)&malloc_zones
;
575 vm_address_t remote_malloc_num_zones
= (vm_address_t
)&malloc_num_zones
;
577 vm_address_t zones_address
;
578 vm_address_t
*zones_address_ref
;
580 unsigned *num_zones_ref
;
581 if (!reader
) reader
= _malloc_default_reader
;
582 // printf("Read malloc_zones at address %p should be %p\n", &malloc_zones, malloc_zones);
583 err
= reader(task
, remote_malloc_zones
, sizeof(void *), (void **)&zones_address_ref
);
584 // printf("Read malloc_zones[%p]=%p\n", remote_malloc_zones, *zones_address_ref);
586 malloc_printf("*** malloc_get_all_zones: error reading zones_address at %p\n", (unsigned)remote_malloc_zones
);
589 zones_address
= *zones_address_ref
;
590 // printf("Reading num_zones at address %p\n", remote_malloc_num_zones);
591 err
= reader(task
, remote_malloc_num_zones
, sizeof(unsigned), (void **)&num_zones_ref
);
593 malloc_printf("*** malloc_get_all_zones: error reading num_zones at %p\n", (unsigned)remote_malloc_num_zones
);
596 num_zones
= *num_zones_ref
;
597 // printf("Read malloc_num_zones[%p]=%d\n", remote_malloc_num_zones, num_zones);
599 // printf("malloc_get_all_zones succesfully found %d zones\n", num_zones);
600 err
= reader(task
, zones_address
, sizeof(malloc_zone_t
*) * num_zones
, (void **)addresses
);
602 malloc_printf("*** malloc_get_all_zones: error reading zones at %p\n", (unsigned)&zones_address
);
605 // printf("malloc_get_all_zones succesfully read %d zones\n", num_zones);
609 /********* Debug helpers ************/
612 malloc_zone_print_ptr_info(void *ptr
) {
615 zone
= find_registered_zone(ptr
, NULL
);
617 printf("ptr %p in registered zone %p\n", ptr
, zone
);
619 printf("ptr %p not in heap\n", ptr
);
624 malloc_zone_check(malloc_zone_t
*zone
) {
628 while (index
< malloc_num_zones
) {
629 zone
= malloc_zones
[index
++];
630 if (!zone
->introspect
->check(zone
)) ok
= 0;
633 ok
= zone
->introspect
->check(zone
);
639 malloc_zone_print(malloc_zone_t
*zone
, boolean_t verbose
) {
642 while (index
< malloc_num_zones
) {
643 zone
= malloc_zones
[index
++];
644 zone
->introspect
->print(zone
, verbose
);
647 zone
->introspect
->print(zone
, verbose
);
652 malloc_zone_statistics(malloc_zone_t
*zone
, malloc_statistics_t
*stats
) {
654 memset(stats
, 0, sizeof(stats
));
656 while (index
< malloc_num_zones
) {
657 zone
= malloc_zones
[index
++];
658 malloc_statistics_t this_stats
;
659 zone
->introspect
->statistics(zone
, &this_stats
);
660 stats
->blocks_in_use
+= this_stats
.blocks_in_use
;
661 stats
->size_in_use
+= this_stats
.size_in_use
;
662 stats
->max_size_in_use
+= this_stats
.max_size_in_use
;
663 stats
->size_allocated
+= this_stats
.size_allocated
;
666 zone
->introspect
->statistics(zone
, stats
);
671 malloc_zone_log(malloc_zone_t
*zone
, void *address
) {
674 while (index
< malloc_num_zones
) {
675 zone
= malloc_zones
[index
++];
676 zone
->introspect
->log(zone
, address
);
679 zone
->introspect
->log(zone
, address
);
683 /********* Misc other entry points ************/
686 DefaultMallocError(int x
) {
687 malloc_printf("*** error %d\n", x
);
688 #if USE_SLEEP_RATHER_THAN_ABORT
696 malloc_error(void (*func
)(int)))(int) {
697 return DefaultMallocError
;
701 _malloc_fork_prepare() {
702 /* Prepare the malloc module for a fork by insuring that no thread is in a malloc critical section */
705 while (index
< malloc_num_zones
) {
706 malloc_zone_t
*zone
= malloc_zones
[index
++];
707 zone
->introspect
->force_lock(zone
);
712 _malloc_fork_parent() {
713 /* Called in the parent process after a fork() to resume normal operation. */
716 while (index
< malloc_num_zones
) {
717 malloc_zone_t
*zone
= malloc_zones
[index
++];
718 zone
->introspect
->force_unlock(zone
);
723 _malloc_fork_child() {
724 /* 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. */
727 while (index
< malloc_num_zones
) {
728 malloc_zone_t
*zone
= malloc_zones
[index
++];
729 zone
->introspect
->force_unlock(zone
);
734 * A Glibc-like mstats() interface.
736 * Note that this interface really isn't very good, as it doesn't understand
737 * that we may have multiple allocators running at once. We just massage
738 * the result from malloc_zone_statistics in any case.
743 malloc_statistics_t s
;
746 malloc_zone_statistics(NULL
, &s
);
747 m
.bytes_total
= s
.size_allocated
;
748 m
.chunks_used
= s
.blocks_in_use
;
749 m
.bytes_used
= s
.size_in_use
;
751 m
.bytes_free
= m
.bytes_total
- m
.bytes_used
; /* isn't this somewhat obvious? */
756 /***************** OBSOLETE ENTRY POINTS ********************/
758 #if PHASE_OUT_OLD_MALLOC
759 #error PHASE OUT THE FOLLOWING FUNCTIONS
761 #warning PHASE OUT THE FOLLOWING FUNCTIONS
765 set_malloc_singlethreaded(boolean_t single
) {
766 static boolean_t warned
= 0;
768 #if PHASE_OUT_OLD_MALLOC
769 malloc_printf("*** OBSOLETE: set_malloc_singlethreaded(%d)\n", single
);
776 malloc_singlethreaded() {
777 static boolean_t warned
= 0;
779 malloc_printf("*** OBSOLETE: malloc_singlethreaded()\n");
785 malloc_debug(int level
) {
786 malloc_printf("*** OBSOLETE: malloc_debug()\n");