00001 /* 00002 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 00003 * 00004 * @APPLE_LICENSE_HEADER_START@ 00005 * 00006 * The contents of this file constitute Original Code as defined in and 00007 * are subject to the Apple Public Source License Version 1.1 (the 00008 * "License"). You may not use this file except in compliance with the 00009 * License. Please obtain a copy of the License at 00010 * http://www.apple.com/publicsource and read it before using this file. 00011 * 00012 * This Original Code and all software distributed under the License are 00013 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 00014 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 00015 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the 00017 * License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * @APPLE_LICENSE_HEADER_END@ 00021 */ 00022 00023 #import <stddef.h> 00024 #import <mach/mach.h> 00025 00026 /********* Type definitions ************/ 00027 00028 typedef struct _malloc_zone_t { 00029 /* Only zone implementors should depend on the layout of this structure; 00030 Regular callers should use the access functions below */ 00031 void *reserved1; 00032 void *reserved2; 00033 size_t (*size)(struct _malloc_zone_t *zone, const void *ptr); /* returns the size of a block or 0 if not in this zone; must be fast, especially for negative answers */ 00034 void *(*malloc)(struct _malloc_zone_t *zone, size_t size); 00035 void *(*calloc)(struct _malloc_zone_t *zone, size_t num_items, size_t size); /* same as malloc, but block returned is set to zero */ 00036 void *(*valloc)(struct _malloc_zone_t *zone, size_t size); /* same as malloc, but block returned is set to zero and is guaranteed to be page aligned */ 00037 void (*free)(struct _malloc_zone_t *zone, void *ptr); 00038 void *(*realloc)(struct _malloc_zone_t *zone, void *ptr, size_t size); 00039 void (*destroy)(struct _malloc_zone_t *zone); /* zone is destroyed and all memory reclaimed */ 00040 const char *zone_name; 00041 void *reserved3; 00042 void *reserved4; 00043 struct malloc_introspection_t *introspect; 00044 void *reserved5; 00045 } malloc_zone_t; 00046 00047 /********* Creation and destruction ************/ 00048 00049 extern malloc_zone_t *malloc_default_zone(void); 00050 /* The initial zone */ 00051 00052 extern malloc_zone_t *malloc_create_zone(vm_size_t start_size, unsigned flags); 00053 /* Create a new zone */ 00054 00055 extern void malloc_destroy_zone(malloc_zone_t *zone); 00056 /* Destroys zone and everything it allocated */ 00057 00058 /********* Block creation and manipulation ************/ 00059 00060 extern void *malloc_zone_malloc(malloc_zone_t *zone, size_t size); 00061 /* Allocates a new pointer of size size; zone must be non-NULL */ 00062 00063 extern void *malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size); 00064 /* Allocates a new pointer of size num_items * size; block is cleared; zone must be non-NULL */ 00065 00066 extern void *malloc_zone_valloc(malloc_zone_t *zone, size_t size); 00067 /* Allocates a new pointer of size size; zone must be non-NULL; Pointer is guaranteed to be page-aligned and block is cleared */ 00068 00069 extern void malloc_zone_free(malloc_zone_t *zone, void *ptr); 00070 /* Frees pointer in zone; zone must be non-NULL */ 00071 00072 extern void *malloc_zone_realloc(malloc_zone_t *zone, void *ptr, size_t size); 00073 /* Enlarges block if necessary; zone must be non-NULL */ 00074 00075 extern malloc_zone_t *malloc_zone_from_ptr(const void *ptr); 00076 /* Returns the zone for a pointer, or NULL if not in any zone. 00077 The ptr must have been returned from a malloc or realloc call. */ 00078 00079 extern size_t malloc_size(const void *ptr); 00080 /* Returns size of given ptr */ 00081 00082 /********* Functions for zone implementors ************/ 00083 00084 extern void malloc_zone_register(malloc_zone_t *zone); 00085 /* Registers a freshly created zone; 00086 Should typically be called after a zone has been created */ 00087 00088 extern void malloc_zone_unregister(malloc_zone_t *zone); 00089 /* De-registers a zone 00090 Should typically be called before calling the zone destruction routine */ 00091 00092 extern void malloc_set_zone_name(malloc_zone_t *zone, const char *name); 00093 /* Sets the name of a zone */ 00094 00095 extern const char *malloc_get_zone_name(malloc_zone_t *zone); 00096 /* Returns the name of a zone */ 00097 00098 typedef struct { 00099 vm_address_t address; 00100 vm_size_t size; 00101 } vm_range_t; 00102 00103 typedef kern_return_t memory_reader_t(task_t remote_task, vm_address_t remote_address, vm_size_t size, void **local_memory); 00104 /* given a task, "reads" the memory at the given address and size 00105 local_memory: set to a contiguous chunk of memory; validity of local_memory is assumed to be limited (until next call) */ 00106 00107 #define MALLOC_PTR_IN_USE_RANGE_TYPE 1 /* for allocated pointers */ 00108 #define MALLOC_PTR_REGION_RANGE_TYPE 2 /* for region containing pointers */ 00109 #define MALLOC_ADMIN_REGION_RANGE_TYPE 4 /* for region used internally */ 00110 00111 typedef void vm_range_recorder_t(task_t, void *, unsigned type, vm_range_t *, unsigned); 00112 /* given a task and context, "records" the specified addresses */ 00113 00114 typedef struct malloc_introspection_t { 00115 kern_return_t (*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 */ 00116 size_t (*good_size)(malloc_zone_t *zone, size_t size); 00117 boolean_t (*check)(malloc_zone_t *zone); /* Consistency checker */ 00118 void (*print)(malloc_zone_t *zone, boolean_t verbose); /* Prints zone */ 00119 void (*log)(malloc_zone_t *zone, void *address); /* Enables logging of activity */ 00120 void (*force_lock)(malloc_zone_t *zone); /* Forces locking zone */ 00121 void (*force_unlock)(malloc_zone_t *zone); /* Forces unlocking zone */ 00122 } malloc_introspection_t; 00123 00124 extern void malloc_printf(const char *format, ...); 00125 /* Convenience for logging errors and warnings; 00126 No allocation is performed during execution of this function; 00127 Only understand %p %d %x %s formats 00128 */ 00129 00130 /********* Functions for performance tools ************/ 00131 00132 extern kern_return_t malloc_get_all_zones(task_t task, memory_reader_t reader, vm_address_t **addresses, unsigned *count); 00133 /* Fills addresses and count with the addresses of the zones in task; 00134 Note that the validity of the addresses returned correspond to the validity of the memory returned by reader */ 00135 00136 /********* Debug helpers ************/ 00137 00138 extern void malloc_zone_print_ptr_info(void *ptr); 00139 /* print to stdout if this pointer is in the malloc heap, free status, and size */ 00140 00141 extern boolean_t malloc_zone_check(malloc_zone_t *zone); 00142 /* Checks zone is well formed; if !zone, checks all zones */ 00143 00144 extern void malloc_zone_print(malloc_zone_t *zone, boolean_t verbose); 00145 /* Prints summary on zone; if !zone, prints all zones */ 00146 00147 extern void malloc_zone_log(malloc_zone_t *zone, void *address); 00148 /* Controls logging of all activity; if !zone, for all zones; 00149 If address==0 nothing is logged; 00150 If address==-1 all activity is logged; 00151 Else only the activity regarding address is logged */