malloc.h (25622B) - Raw
1 /* 2 * Copyright (c) 1999-2023 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 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 11 * file. 12 * 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. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 #ifndef _MALLOC_MALLOC_H_ 25 #define _MALLOC_MALLOC_H_ 26 27 #include <TargetConditionals.h> 28 #include <malloc/_platform.h> 29 #include <Availability.h> 30 #include <os/availability.h> 31 32 #include <malloc/_ptrcheck.h> 33 __ptrcheck_abi_assume_single() 34 35 #if __has_feature(ptrauth_calls) 36 #include <ptrauth.h> 37 38 // Zone function pointer, type-diversified but not address-diversified (because 39 // the zone can be copied). Process-independent because the zone structure may 40 // be in the shared library cache. 41 #define MALLOC_ZONE_FN_PTR(fn) __ptrauth(ptrauth_key_process_independent_code, \ 42 0, ptrauth_string_discriminator("malloc_zone_fn." #fn)) fn 43 44 // Introspection function pointer, address- and type-diversified. 45 // Process-independent because the malloc_introspection_t structure that contains 46 // these pointers may be in the shared library cache. 47 #define MALLOC_INTROSPECT_FN_PTR(fn) __ptrauth(ptrauth_key_process_independent_code, \ 48 1, ptrauth_string_discriminator("malloc_introspect_fn." #fn)) fn 49 50 // Pointer to the introspection pointer table, type-diversified but not 51 // address-diversified (because the zone can be copied). 52 // Process-independent because the table pointer may be in the shared library cache. 53 #define MALLOC_INTROSPECT_TBL_PTR(ptr) __ptrauth(ptrauth_key_process_independent_data,\ 54 0, ptrauth_string_discriminator("malloc_introspect_tbl")) ptr 55 56 #endif // __has_feature(ptrauth_calls) 57 58 #ifndef MALLOC_ZONE_FN_PTR 59 #define MALLOC_ZONE_FN_PTR(fn) fn 60 #define MALLOC_INTROSPECT_FN_PTR(fn) fn 61 #define MALLOC_INTROSPECT_TBL_PTR(ptr) ptr 62 #endif // MALLOC_ZONE_FN_PTR 63 64 __BEGIN_DECLS 65 /********* Type definitions ************/ 66 67 /* 68 * Only zone implementors should depend on the layout of this structure; 69 * Regular callers should use the access functions below 70 */ 71 typedef struct _malloc_zone_t { 72 void *reserved1; /* RESERVED FOR CFAllocator DO NOT USE */ 73 void *reserved2; /* RESERVED FOR CFAllocator DO NOT USE */ 74 75 /* 76 * Returns the size of a block or 0 if not in this zone; must be fast, 77 * especially for negative answers. 78 */ 79 size_t (* MALLOC_ZONE_FN_PTR(size))(struct _malloc_zone_t *zone, 80 const void * __unsafe_indexable ptr); 81 82 void * __sized_by_or_null(size) (* MALLOC_ZONE_FN_PTR(malloc))( 83 struct _malloc_zone_t *zone, size_t size); 84 85 /* Same as malloc, but block returned is set to zero */ 86 void * __sized_by_or_null(num_items * size) (* MALLOC_ZONE_FN_PTR(calloc))( 87 struct _malloc_zone_t *zone, size_t num_items, size_t size); 88 89 /* Same as malloc, but block returned is guaranteed to be page-aligned */ 90 void * __sized_by_or_null(size) (* MALLOC_ZONE_FN_PTR(valloc))( 91 struct _malloc_zone_t *zone, size_t size); 92 93 void (* MALLOC_ZONE_FN_PTR(free))(struct _malloc_zone_t *zone, 94 void * __unsafe_indexable ptr); 95 96 void * __sized_by_or_null(size) (* MALLOC_ZONE_FN_PTR(realloc))( 97 struct _malloc_zone_t *zone, void * __unsafe_indexable ptr, 98 size_t size); 99 100 /* Zone is destroyed and all memory reclaimed */ 101 void (* MALLOC_ZONE_FN_PTR(destroy))(struct _malloc_zone_t *zone); 102 103 const char * __null_terminated zone_name; 104 105 /* Optional batch callbacks; these may be NULL */ 106 107 /* 108 * Given a size, returns pointers capable of holding that size; returns the 109 * number of pointers allocated (maybe 0 or less than num_requested) 110 */ 111 unsigned (* MALLOC_ZONE_FN_PTR(batch_malloc))(struct _malloc_zone_t *zone, 112 size_t size, 113 void * __unsafe_indexable * __counted_by(num_requested) results, 114 unsigned num_requested); 115 116 /* 117 * Frees all the pointers in to_be_freed; note that to_be_freed may be 118 * overwritten during the process 119 */ 120 void (* MALLOC_ZONE_FN_PTR(batch_free))(struct _malloc_zone_t *zone, 121 void * __unsafe_indexable * __counted_by(num_to_be_freed) to_be_freed, 122 unsigned num_to_be_freed); 123 124 struct malloc_introspection_t * MALLOC_INTROSPECT_TBL_PTR(introspect); 125 unsigned version; 126 127 /* Aligned memory allocation. May be NULL. Present in version >= 5. */ 128 void * __sized_by_or_null(size) (* MALLOC_ZONE_FN_PTR(memalign))( 129 struct _malloc_zone_t *zone, size_t alignment, size_t size); 130 131 /* 132 * Free a pointer known to be in zone and known to have the given size. 133 * May be NULL. Present in version >= 6. 134 */ 135 void (* MALLOC_ZONE_FN_PTR(free_definite_size))(struct _malloc_zone_t *zone, 136 void * __sized_by(size) ptr, size_t size); 137 138 /* 139 * Empty out caches in the face of memory pressure. May be NULL. 140 * Present in version >= 8. 141 */ 142 size_t (* MALLOC_ZONE_FN_PTR(pressure_relief))(struct _malloc_zone_t *zone, 143 size_t goal); 144 145 /* 146 * Checks whether an address might belong to the zone. May be NULL. Present 147 * in version >= 10. False positives are allowed (e.g. the pointer was 148 * freed, or it's in zone space that has not yet been allocated. False 149 * negatives are not allowed. 150 */ 151 boolean_t (* MALLOC_ZONE_FN_PTR(claimed_address))( 152 struct _malloc_zone_t *zone, void * __unsafe_indexable ptr); 153 154 /* 155 * For libmalloc-internal zone 0 implementations only: try to free ptr, 156 * promising to call find_zone_and_free if it turns out not to belong to us. 157 * May be present in version >= 13. 158 */ 159 void (* MALLOC_ZONE_FN_PTR(try_free_default))(struct _malloc_zone_t *zone, 160 void * __unsafe_indexable ptr); 161 162 /* 163 * Memory allocation with an extensible binary flags option. Currently for 164 * libmalloc-internal zone implementations only - should be NULL otherwise. 165 * Added in version >= 15. 166 */ 167 void * __sized_by_or_null(size) (* MALLOC_ZONE_FN_PTR(malloc_with_options))( 168 struct _malloc_zone_t *zone, size_t align, size_t size, 169 uint64_t options); 170 171 /* 172 * Typed Memory Operations versions of zone functions. Present in 173 * version >= 16. 174 */ 175 176 void * __sized_by_or_null(size) (* MALLOC_ZONE_FN_PTR(malloc_type_malloc))( 177 struct _malloc_zone_t *zone, size_t size, malloc_type_id_t type_id); 178 179 void * __sized_by_or_null(count * size) (* MALLOC_ZONE_FN_PTR(malloc_type_calloc))( 180 struct _malloc_zone_t *zone, size_t count, size_t size, 181 malloc_type_id_t type_id); 182 183 void * __sized_by_or_null(size) (* MALLOC_ZONE_FN_PTR(malloc_type_realloc))( 184 struct _malloc_zone_t *zone, void * __unsafe_indexable ptr, 185 size_t size, malloc_type_id_t type_id); 186 187 void * __sized_by_or_null(size) (* MALLOC_ZONE_FN_PTR(malloc_type_memalign))( 188 struct _malloc_zone_t *zone, size_t alignment, size_t size, 189 malloc_type_id_t type_id); 190 191 /* Must be NULL for non-libmalloc zone implementations */ 192 void * __sized_by_or_null(size) (* MALLOC_ZONE_FN_PTR(malloc_type_malloc_with_options))( 193 struct _malloc_zone_t *zone, size_t align, size_t size, uint64_t options, 194 malloc_type_id_t type_id); 195 } malloc_zone_t; 196 197 /*! 198 * @enum malloc_type_callsite_flags_v0_t 199 * 200 * Information about where and how malloc was called 201 * 202 * @constant MALLOC_TYPE_CALLSITE_FLAGS_V0_FIXED_SIZE 203 * Set in malloc_type_summary_v0_t if the call to malloc was called with a fixed 204 * size. Note that, at present, this bit is set in all callsites where the 205 * compiler rewrites a call to malloc 206 * 207 * @constant MALLOC_TYPE_CALLSITE_FLAGS_V0_ARRAY 208 * Set in malloc_type_summary_v0_t if the type being allocated is an array, e.g. 209 * allocated via new[] or calloc(count, size) 210 */ 211 typedef enum { 212 MALLOC_TYPE_CALLSITE_FLAGS_V0_NONE = 0, 213 MALLOC_TYPE_CALLSITE_FLAGS_V0_FIXED_SIZE = 1 << 0, 214 MALLOC_TYPE_CALLSITE_FLAGS_V0_ARRAY = 1 << 1, 215 } malloc_type_callsite_flags_v0_t; 216 217 /*! 218 * @enum malloc_type_kind_v0_t 219 * 220 * @constant MALLOC_TYPE_KIND_V0_OTHER 221 * Default allocation type, used for most calls to malloc 222 * 223 * @constant MALLOC_TYPE_KIND_V0_OBJC 224 * Marks a type allocated by libobjc 225 * 226 * @constant MALLOC_TYPE_KIND_V0_SWIFT 227 * Marks a type allocated by the Swift runtime 228 * 229 * @constant MALLOC_TYPE_KIND_V0_CXX 230 * Marks a type allocated by the C++ runtime's operator new 231 */ 232 typedef enum { 233 MALLOC_TYPE_KIND_V0_OTHER = 0, 234 MALLOC_TYPE_KIND_V0_OBJC = 1, 235 MALLOC_TYPE_KIND_V0_SWIFT = 2, 236 MALLOC_TYPE_KIND_V0_CXX = 3 237 } malloc_type_kind_v0_t; 238 239 /*! 240 * @struct malloc_type_layout_semantics_v0_t 241 * 242 * @field contains_data_pointer 243 * True if the allocated type or any of its fields is a pointer 244 * to a data type (i.e. the pointee contains no pointers) 245 * 246 * @field contains_struct_pointer 247 * True if the allocated type or any of its fields is a pointer 248 * to a struct or union 249 * 250 * @field contains_immutable_pointer 251 * True if the allocated type or any of its fields is a const pointer 252 * 253 * @field contains_anonymous_pointer 254 * True if the allocated type or any of its fields is a pointer 255 * to something other than a struct or data type 256 * 257 * @field is_reference_counted 258 * True if the allocated type is reference counted 259 * 260 * @field contains_generic_data 261 * True if the allocated type or any of its fields are not pointers 262 */ 263 typedef struct { 264 bool contains_data_pointer : 1; 265 bool contains_struct_pointer : 1; 266 bool contains_immutable_pointer : 1; 267 bool contains_anonymous_pointer : 1; 268 bool is_reference_counted : 1; 269 uint16_t reserved_0 : 3; 270 bool contains_generic_data : 1; 271 uint16_t reserved_1 : 7; 272 } malloc_type_layout_semantics_v0_t; 273 274 /*! 275 * @struct malloc_type_summary_v0_t 276 * 277 * @field version 278 * Versioning field of the type summary. Set to 0 for the current verison. New 279 * fields can be added where the reserved fields currently are without 280 * incrementing the version, as long as they are non-breaking. 281 * 282 * @field callsite_flags 283 * Details from the callsite of malloc inferred by the compiler 284 * 285 * @field type_kind 286 * Details about the runtime making the allocation 287 * 288 * @field layout_semantics 289 * Details about what kinds of data are contained in the type being allocated 290 * 291 * @discussion 292 * The reserved fields should not be read from or written to, and may be 293 * used for additional fields and information in future versions 294 */ 295 typedef struct { 296 uint32_t version : 4; 297 uint32_t reserved_0 : 2; 298 malloc_type_callsite_flags_v0_t callsite_flags : 4; 299 malloc_type_kind_v0_t type_kind : 2; 300 uint32_t reserved_1 : 4; 301 malloc_type_layout_semantics_v0_t layout_semantics; 302 } malloc_type_summary_v0_t; 303 304 /*! 305 * @union malloc_type_descriptor_v0_t 306 * 307 * @field hash 308 * Hash of the type layout of the allocated type, or if type inference failed, 309 * the hash of the callsite's file, line and column. The hash allows the 310 * allocator to disambiguate between different types with the same summary, e.g. 311 * types that have the same fields in different orders. 312 * 313 * @field summary 314 * Details of the type being allocated 315 * 316 * @field type_id 317 * opaque type used for punning 318 * 319 * @discussion 320 * Use malloc_type_descriptor_v0_t to decode the opaque malloc_type_id_t with 321 * version == 0 into a malloc_type_summary_v0_t: 322 * 323 * <code> 324 * malloc_type_descriptor_v0_t desc = (malloc_type_descriptor_v0_t){ .type_id = id }; 325 * </code> 326 * 327 * See LLVM documentation for more details 328 */ 329 typedef union { 330 struct { 331 uint32_t hash; 332 malloc_type_summary_v0_t summary; 333 }; 334 malloc_type_id_t type_id; 335 } malloc_type_descriptor_v0_t; 336 337 /********* Creation and destruction ************/ 338 339 extern malloc_zone_t *malloc_default_zone(void); 340 /* The initial zone */ 341 342 #if !0 && !0 343 extern malloc_zone_t *malloc_create_zone(vm_size_t start_size, unsigned flags); 344 /* Creates a new zone with default behavior and registers it */ 345 346 extern void malloc_destroy_zone(malloc_zone_t *zone); 347 /* Destroys zone and everything it allocated */ 348 #endif 349 350 /********* Block creation and manipulation ************/ 351 352 extern void * __sized_by_or_null(size) malloc_zone_malloc(malloc_zone_t *zone, size_t size) __alloc_size(2) _MALLOC_TYPED(malloc_type_zone_malloc, 2); 353 /* Allocates a new pointer of size size; zone must be non-NULL */ 354 355 extern void * __sized_by_or_null(num_items * size) malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size) __alloc_size(2,3) _MALLOC_TYPED(malloc_type_zone_calloc, 3); 356 /* Allocates a new pointer of size num_items * size; block is cleared; zone must be non-NULL */ 357 358 extern void * __sized_by_or_null(size) malloc_zone_valloc(malloc_zone_t *zone, size_t size) __alloc_size(2) _MALLOC_TYPED(malloc_type_zone_valloc, 2); 359 /* Allocates a new pointer of size size; zone must be non-NULL; Pointer is guaranteed to be page-aligned and block is cleared */ 360 361 extern void malloc_zone_free(malloc_zone_t *zone, void * __unsafe_indexable ptr); 362 /* Frees pointer in zone; zone must be non-NULL */ 363 364 extern void * __sized_by_or_null(size) malloc_zone_realloc(malloc_zone_t *zone, void * __unsafe_indexable ptr, size_t size) __alloc_size(3) _MALLOC_TYPED(malloc_type_zone_realloc, 3); 365 /* Enlarges block if necessary; zone must be non-NULL */ 366 367 extern malloc_zone_t *malloc_zone_from_ptr(const void * __unsafe_indexable ptr); 368 /* Returns the zone for a pointer, or NULL if not in any zone. 369 The ptr must have been returned from a malloc or realloc call. */ 370 371 extern size_t malloc_size(const void * __unsafe_indexable ptr); 372 /* Returns size of given ptr, including any padding inserted by the allocator */ 373 374 extern size_t malloc_good_size(size_t size); 375 /* Returns number of bytes greater than or equal to size that can be allocated without padding */ 376 377 extern void * __sized_by_or_null(size) malloc_zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size) __alloc_align(2) __alloc_size(3) _MALLOC_TYPED(malloc_type_zone_memalign, 3) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0); 378 /* 379 * Allocates a new pointer of size size whose address is an exact multiple of alignment. 380 * alignment must be a power of two and at least as large as sizeof(void *). 381 * zone must be non-NULL. 382 */ 383 384 /********* Batch methods ************/ 385 386 #if !0 && !0 387 extern unsigned malloc_zone_batch_malloc(malloc_zone_t *zone, size_t size, void * __unsafe_indexable * __counted_by(num_requested) results, unsigned num_requested); 388 /* Allocates num blocks of the same size; Returns the number truly allocated (may be 0) */ 389 390 extern void malloc_zone_batch_free(malloc_zone_t *zone, void * __unsafe_indexable * __counted_by(num) to_be_freed, unsigned num); 391 /* frees all the pointers in to_be_freed; note that to_be_freed may be overwritten during the process; This function will always free even if the zone has no batch callback */ 392 #endif 393 394 /********* Functions for libcache ************/ 395 396 #if !0 && !0 397 extern malloc_zone_t *malloc_default_purgeable_zone(void) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0); 398 /* Returns a pointer to the default purgeable_zone. */ 399 400 extern void malloc_make_purgeable(void * __unsafe_indexable ptr) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0); 401 /* Make an allocation from the purgeable zone purgeable if possible. */ 402 403 extern int malloc_make_nonpurgeable(void * __unsafe_indexable ptr) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0); 404 /* Makes an allocation from the purgeable zone nonpurgeable. 405 * Returns zero if the contents were not purged since the last 406 * call to malloc_make_purgeable, else returns non-zero. */ 407 #endif 408 409 /********* Functions for zone implementors ************/ 410 411 #if !0 && !0 412 extern void malloc_zone_register(malloc_zone_t *zone); 413 /* Registers a custom malloc zone; Should typically be called after a 414 * malloc_zone_t has been filled in with custom methods by a client. See 415 * malloc_create_zone for creating additional malloc zones with the 416 * default allocation and free behavior. */ 417 418 extern void malloc_zone_unregister(malloc_zone_t *zone); 419 /* De-registers a zone 420 Should typically be called before calling the zone destruction routine */ 421 #endif 422 423 extern void malloc_set_zone_name(malloc_zone_t *zone, const char * __null_terminated name); 424 /* Sets the name of a zone */ 425 426 extern const char *malloc_get_zone_name(malloc_zone_t *zone); 427 /* Returns the name of a zone */ 428 429 #if !0 && !0 430 size_t malloc_zone_pressure_relief(malloc_zone_t *zone, size_t goal) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3); 431 /* malloc_zone_pressure_relief() advises the malloc subsystem that the process is under memory pressure and 432 * that the subsystem should make its best effort towards releasing (i.e. munmap()-ing) "goal" bytes from "zone". 433 * If "goal" is passed as zero, the malloc subsystem will attempt to achieve maximal pressure relief in "zone". 434 * If "zone" is passed as NULL, all zones are examined for pressure relief opportunities. 435 * malloc_zone_pressure_relief() returns the number of bytes released. 436 */ 437 #endif 438 439 typedef struct { 440 vm_address_t address; 441 vm_size_t size; 442 } vm_range_t; 443 444 typedef struct malloc_statistics_t { 445 unsigned blocks_in_use; 446 size_t size_in_use; 447 size_t max_size_in_use; /* high water mark of touched memory */ 448 size_t size_allocated; /* reserved in memory */ 449 } malloc_statistics_t; 450 451 typedef kern_return_t memory_reader_t(task_t remote_task, vm_address_t remote_address, vm_size_t size, void * __sized_by(size) *local_memory); 452 /* given a task, "reads" the memory at the given address and size 453 local_memory: set to a contiguous chunk of memory; validity of local_memory is assumed to be limited (until next call) */ 454 455 #define MALLOC_PTR_IN_USE_RANGE_TYPE 1 /* for allocated pointers */ 456 #define MALLOC_PTR_REGION_RANGE_TYPE 2 /* for region containing pointers */ 457 #define MALLOC_ADMIN_REGION_RANGE_TYPE 4 /* for region used internally */ 458 #define MALLOC_ZONE_SPECIFIC_FLAGS 0xff00 /* bits reserved for zone-specific purposes */ 459 460 typedef void vm_range_recorder_t(task_t, void *, unsigned type, vm_range_t *, unsigned); 461 /* given a task and context, "records" the specified addresses */ 462 463 /* Print function for the print_task() operation. */ 464 typedef void print_task_printer_t(const char * __null_terminated fmt, ...) __printflike(1,2); 465 466 typedef struct malloc_introspection_t { 467 kern_return_t (* MALLOC_INTROSPECT_FN_PTR(enumerator))(task_t task, void *, unsigned type_mask, vm_address_t zone_address, memory_reader_t reader, vm_range_recorder_t recorder); /* enumerates all the malloc pointers in use */ 468 size_t (* MALLOC_INTROSPECT_FN_PTR(good_size))(malloc_zone_t *zone, size_t size); 469 boolean_t (* MALLOC_INTROSPECT_FN_PTR(check))(malloc_zone_t *zone); /* Consistency checker */ 470 void (* MALLOC_INTROSPECT_FN_PTR(print))(malloc_zone_t *zone, boolean_t verbose); /* Prints zone */ 471 void (* MALLOC_INTROSPECT_FN_PTR(log))(malloc_zone_t *zone, void * __unsafe_indexable address); /* Enables logging of activity */ 472 void (* MALLOC_INTROSPECT_FN_PTR(force_lock))(malloc_zone_t *zone); /* Forces locking zone */ 473 void (* MALLOC_INTROSPECT_FN_PTR(force_unlock))(malloc_zone_t *zone); /* Forces unlocking zone */ 474 void (* MALLOC_INTROSPECT_FN_PTR(statistics))(malloc_zone_t *zone, malloc_statistics_t *stats); /* Fills statistics */ 475 boolean_t (* MALLOC_INTROSPECT_FN_PTR(zone_locked))(malloc_zone_t *zone); /* Are any zone locks held */ 476 477 /* Discharge checking. Present in version >= 7. */ 478 boolean_t (* MALLOC_INTROSPECT_FN_PTR(enable_discharge_checking))(malloc_zone_t *zone); 479 void (* MALLOC_INTROSPECT_FN_PTR(disable_discharge_checking))(malloc_zone_t *zone); 480 void (* MALLOC_INTROSPECT_FN_PTR(discharge))(malloc_zone_t *zone, void * __unsafe_indexable memory); 481 #ifdef __BLOCKS__ 482 void (* MALLOC_INTROSPECT_FN_PTR(enumerate_discharged_pointers))(malloc_zone_t *zone, void (^report_discharged)(void *memory, void *info)); 483 #else 484 void *enumerate_unavailable_without_blocks; 485 #endif /* __BLOCKS__ */ 486 void (* MALLOC_INTROSPECT_FN_PTR(reinit_lock))(malloc_zone_t *zone); /* Reinitialize zone locks, called only from atfork_child handler. Present in version >= 9. */ 487 void (* MALLOC_INTROSPECT_FN_PTR(print_task))(task_t task, unsigned level, vm_address_t zone_address, memory_reader_t reader, print_task_printer_t printer); /* debug print for another process. Present in version >= 11. */ 488 void (* MALLOC_INTROSPECT_FN_PTR(task_statistics))(task_t task, vm_address_t zone_address, memory_reader_t reader, malloc_statistics_t *stats); /* Present in version >= 12. */ 489 unsigned zone_type; /* Identifies the zone type. 0 means unknown/undefined zone type. Present in version >= 14. */ 490 } malloc_introspection_t; 491 492 // The value of "level" when passed to print_task() that corresponds to 493 // verbose passed to print() 494 #define MALLOC_VERBOSE_PRINT_LEVEL 2 495 496 #if !0 && !0 497 extern void malloc_printf(const char * __null_terminated format, ...) __printflike(1,2); 498 /* Convenience for logging errors and warnings; 499 No allocation is performed during execution of this function; 500 Only understands usual %p %d %s formats, and %y that expresses a number of bytes (5b,10KB,1MB...) 501 */ 502 #endif 503 504 /********* Functions for performance tools ************/ 505 506 #if !0 && !0 507 extern kern_return_t malloc_get_all_zones(task_t task, memory_reader_t reader, vm_address_t * __single * __counted_by(*count) addresses, unsigned *count); 508 /* Fills addresses and count with the addresses of the zones in task; 509 Note that the validity of the addresses returned correspond to the validity reader */ 510 #endif 511 512 /********* Debug helpers ************/ 513 514 extern void malloc_zone_print_ptr_info(void * __unsafe_indexable ptr); 515 /* print to stdout if this pointer is in the malloc heap, free status, and size */ 516 517 extern boolean_t malloc_zone_check(malloc_zone_t *zone); 518 /* Checks zone is well formed; if !zone, checks all zones */ 519 520 extern void malloc_zone_print(malloc_zone_t *zone, boolean_t verbose); 521 /* Prints summary on zone; if !zone, prints all zones */ 522 523 #if !0 && !0 524 extern void malloc_zone_statistics(malloc_zone_t *zone, malloc_statistics_t *stats); 525 /* Fills statistics for zone; if !zone, sums up all zones */ 526 527 extern void malloc_zone_log(malloc_zone_t *zone, void * __unsafe_indexable address); 528 /* Controls logging of all activity; if !zone, for all zones; 529 If address==0 nothing is logged; 530 If address==-1 all activity is logged; 531 Else only the activity regarding address is logged */ 532 #endif 533 534 struct mstats { 535 size_t bytes_total; 536 size_t chunks_used; 537 size_t bytes_used; 538 size_t chunks_free; 539 size_t bytes_free; 540 }; 541 542 #if !0 && !0 543 extern struct mstats mstats(void); 544 545 extern boolean_t malloc_zone_enable_discharge_checking(malloc_zone_t *zone) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3); 546 /* Increment the discharge checking enabled counter for a zone. Returns true if the zone supports checking, false if it does not. */ 547 548 extern void malloc_zone_disable_discharge_checking(malloc_zone_t *zone) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3); 549 /* Decrement the discharge checking enabled counter for a zone. */ 550 551 extern void malloc_zone_discharge(malloc_zone_t *zone, void * __unsafe_indexable memory) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3); 552 /* Register memory that the programmer expects to be freed soon. 553 zone may be NULL in which case the zone is determined using malloc_zone_from_ptr(). 554 If discharge checking is off for the zone this function is a no-op. */ 555 #endif 556 557 #if !0 && !0 558 #ifdef __BLOCKS__ 559 extern void malloc_zone_enumerate_discharged_pointers(malloc_zone_t *zone, void (^report_discharged)(void *memory, void *info)) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3); 560 /* Calls report_discharged for each block that was registered using malloc_zone_discharge() but has not yet been freed. 561 info is used to provide zone defined information about the memory block. 562 If zone is NULL then the enumeration covers all zones. */ 563 #else 564 extern void malloc_zone_enumerate_discharged_pointers(malloc_zone_t *zone, void *) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3); 565 #endif /* __BLOCKS__ */ 566 #endif 567 568 /********* Zone version summary ************/ 569 // Version 0, but optional: 570 // malloc_zone_t::batch_malloc 571 // malloc_zone_t::batch_free 572 // Version 5: 573 // malloc_zone_t::memalign 574 // Version 6: 575 // malloc_zone_t::free_definite_size 576 // Version 7: 577 // malloc_introspection_t::enable_discharge_checking 578 // malloc_introspection_t::disable_discharge_checking 579 // malloc_introspection_t::discharge 580 // Version 8: 581 // malloc_zone_t::pressure_relief 582 // Version 9: 583 // malloc_introspection_t::reinit_lock 584 // Version 10: 585 // malloc_zone_t::claimed_address 586 // Version 11: 587 // malloc_introspection_t::print_task 588 // Version 12: 589 // malloc_introspection_t::task_statistics 590 // Version 13: 591 // - malloc_zone_t::malloc and malloc_zone_t::calloc assume responsibility for 592 // setting errno to ENOMEM on failure 593 // - malloc_zone_t::try_free_default 594 // Version 14: 595 // malloc_introspection_t::zone_type 596 // Version 15: 597 // malloc_zone_t::malloc_with_options 598 // Version 16: 599 // malloc_zone_t::malloc_type_malloc 600 // malloc_zone_t::malloc_type_calloc 601 // malloc_zone_t::malloc_type_realloc 602 // malloc_zone_t::malloc_type_memalign 603 // malloc_zone_t::malloc_type_malloc_with_options 604 605 // These functions are optional and calling them requires two checks: 606 // * Check zone version to ensure zone struct is large enough to include the member. 607 // * Check that the function pointer is not null. 608 609 __END_DECLS 610 611 #endif /* _MALLOC_MALLOC_H_ */