]> git.saurik.com Git - apple/libc.git/blame - include/malloc/malloc.h
Libc-583.tar.gz
[apple/libc.git] / include / malloc / malloc.h
CommitLineData
9385eb3d
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
9385eb3d
A
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 <stddef.h>
28#include <mach/mach_types.h>
29#include <sys/cdefs.h>
30
31__BEGIN_DECLS
32/********* Type definitions ************/
33
34typedef struct _malloc_zone_t {
35 /* Only zone implementors should depend on the layout of this structure;
36 Regular callers should use the access functions below */
59e0d9fe
A
37 void *reserved1; /* RESERVED FOR CFAllocator DO NOT USE */
38 void *reserved2; /* RESERVED FOR CFAllocator DO NOT USE */
9385eb3d
A
39 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 */
40 void *(*malloc)(struct _malloc_zone_t *zone, size_t size);
41 void *(*calloc)(struct _malloc_zone_t *zone, size_t num_items, size_t size); /* same as malloc, but block returned is set to zero */
42 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 */
43 void (*free)(struct _malloc_zone_t *zone, void *ptr);
44 void *(*realloc)(struct _malloc_zone_t *zone, void *ptr, size_t size);
45 void (*destroy)(struct _malloc_zone_t *zone); /* zone is destroyed and all memory reclaimed */
46 const char *zone_name;
47
48 /* Optional batch callbacks; these may be NULL */
49 unsigned (*batch_malloc)(struct _malloc_zone_t *zone, size_t size, void **results, unsigned num_requested); /* given a size, returns pointers capable of holding that size; returns the number of pointers allocated (maybe 0 or less than num_requested) */
50 void (*batch_free)(struct _malloc_zone_t *zone, void **to_be_freed, unsigned num_to_be_freed); /* frees all the pointers in to_be_freed; note that to_be_freed may be overwritten during the process */
51
52 struct malloc_introspection_t *introspect;
59e0d9fe 53 unsigned version;
34e8f829
A
54
55 /* aligned memory allocation. The callback may be NULL. */
56 void *(*memalign)(struct _malloc_zone_t *zone, size_t alignment, size_t size);
57
58 /* free a pointer known to be in zone and known to have the given size. The callback may be NULL. */
59 void (*free_definite_size)(struct _malloc_zone_t *zone, void *ptr, size_t size);
9385eb3d
A
60} malloc_zone_t;
61
62/********* Creation and destruction ************/
63
64extern malloc_zone_t *malloc_default_zone(void);
65 /* The initial zone */
66
67extern malloc_zone_t *malloc_create_zone(vm_size_t start_size, unsigned flags);
34e8f829 68 /* Creates a new zone with default behavior and registers it */
9385eb3d
A
69
70extern void malloc_destroy_zone(malloc_zone_t *zone);
71 /* Destroys zone and everything it allocated */
72
73/********* Block creation and manipulation ************/
74
75extern void *malloc_zone_malloc(malloc_zone_t *zone, size_t size);
76 /* Allocates a new pointer of size size; zone must be non-NULL */
77
78extern void *malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size);
79 /* Allocates a new pointer of size num_items * size; block is cleared; zone must be non-NULL */
80
81extern void *malloc_zone_valloc(malloc_zone_t *zone, size_t size);
82 /* Allocates a new pointer of size size; zone must be non-NULL; Pointer is guaranteed to be page-aligned and block is cleared */
83
84extern void malloc_zone_free(malloc_zone_t *zone, void *ptr);
85 /* Frees pointer in zone; zone must be non-NULL */
86
87extern void *malloc_zone_realloc(malloc_zone_t *zone, void *ptr, size_t size);
88 /* Enlarges block if necessary; zone must be non-NULL */
89
90extern malloc_zone_t *malloc_zone_from_ptr(const void *ptr);
91 /* Returns the zone for a pointer, or NULL if not in any zone.
92 The ptr must have been returned from a malloc or realloc call. */
93
94extern size_t malloc_size(const void *ptr);
95 /* Returns size of given ptr */
96
224c7076
A
97extern size_t malloc_good_size(size_t size);
98 /* Returns number of bytes greater than or equal to size that can be allocated without padding */
99
34e8f829
A
100extern void *malloc_zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size);
101 /*
102 * Allocates a new pointer of size size whose address is an exact multiple of alignment.
103 * alignment must be a power of two and at least as large as sizeof(void *).
104 * zone must be non-NULL.
105 */
106
9385eb3d
A
107/********* Batch methods ************/
108
109extern unsigned malloc_zone_batch_malloc(malloc_zone_t *zone, size_t size, void **results, unsigned num_requested);
110 /* Allocates num blocks of the same size; Returns the number truly allocated (may be 0) */
111
112extern void malloc_zone_batch_free(malloc_zone_t *zone, void **to_be_freed, unsigned num);
113 /* 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 */
114
34e8f829
A
115/********* Functions for libcache ************/
116
117extern malloc_zone_t *malloc_default_purgeable_zone(void);
118 /* Returns a pointer to the default purgeable_zone. */
119
120extern void malloc_make_purgeable(void *ptr);
121 /* Make an allocation from the purgeable zone purgeable if possible. */
122
123extern int malloc_make_nonpurgeable(void *ptr);
124 /* Makes an allocation from the purgeable zone nonpurgeable.
125 * Returns zero if the contents were not purged since the last
126 * call to malloc_make_purgeable, else returns non-zero. */
127
9385eb3d
A
128/********* Functions for zone implementors ************/
129
130extern void malloc_zone_register(malloc_zone_t *zone);
34e8f829
A
131 /* Registers a custom malloc zone; Should typically be called after a
132 * malloc_zone_t has been filled in with custom methods by a client. See
133 * malloc_create_zone for creating additional malloc zones with the
134 * default allocation and free behavior. */
9385eb3d
A
135
136extern void malloc_zone_unregister(malloc_zone_t *zone);
137 /* De-registers a zone
138 Should typically be called before calling the zone destruction routine */
139
140extern void malloc_set_zone_name(malloc_zone_t *zone, const char *name);
141 /* Sets the name of a zone */
142
143extern const char *malloc_get_zone_name(malloc_zone_t *zone);
144 /* Returns the name of a zone */
145
146typedef struct {
147 vm_address_t address;
148 vm_size_t size;
149} vm_range_t;
150
151typedef struct malloc_statistics_t {
152 unsigned blocks_in_use;
153 size_t size_in_use;
154 size_t max_size_in_use; /* high water mark of touched memory */
155 size_t size_allocated; /* reserved in memory */
156} malloc_statistics_t;
157
158typedef kern_return_t memory_reader_t(task_t remote_task, vm_address_t remote_address, vm_size_t size, void **local_memory);
159 /* given a task, "reads" the memory at the given address and size
160local_memory: set to a contiguous chunk of memory; validity of local_memory is assumed to be limited (until next call) */
161
162#define MALLOC_PTR_IN_USE_RANGE_TYPE 1 /* for allocated pointers */
163#define MALLOC_PTR_REGION_RANGE_TYPE 2 /* for region containing pointers */
164#define MALLOC_ADMIN_REGION_RANGE_TYPE 4 /* for region used internally */
34e8f829 165#define MALLOC_ZONE_SPECIFIC_FLAGS 0xff00 /* bits reserved for zone-specific purposes */
9385eb3d
A
166
167typedef void vm_range_recorder_t(task_t, void *, unsigned type, vm_range_t *, unsigned);
168 /* given a task and context, "records" the specified addresses */
169
170typedef struct malloc_introspection_t {
171 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 */
172 size_t (*good_size)(malloc_zone_t *zone, size_t size);
173 boolean_t (*check)(malloc_zone_t *zone); /* Consistency checker */
174 void (*print)(malloc_zone_t *zone, boolean_t verbose); /* Prints zone */
175 void (*log)(malloc_zone_t *zone, void *address); /* Enables logging of activity */
176 void (*force_lock)(malloc_zone_t *zone); /* Forces locking zone */
177 void (*force_unlock)(malloc_zone_t *zone); /* Forces unlocking zone */
178 void (*statistics)(malloc_zone_t *zone, malloc_statistics_t *stats); /* Fills statistics */
34e8f829 179 boolean_t (*zone_locked)(malloc_zone_t *zone); /* Are any zone locks held */
9385eb3d
A
180} malloc_introspection_t;
181
182extern void malloc_printf(const char *format, ...);
183 /* Convenience for logging errors and warnings;
184 No allocation is performed during execution of this function;
185 Only understands usual %p %d %s formats, and %y that expresses a number of bytes (5b,10KB,1MB...)
186 */
187
188/********* Functions for performance tools ************/
189
190extern kern_return_t malloc_get_all_zones(task_t task, memory_reader_t reader, vm_address_t **addresses, unsigned *count);
191 /* Fills addresses and count with the addresses of the zones in task;
192 Note that the validity of the addresses returned correspond to the validity of the memory returned by reader */
193
194/********* Debug helpers ************/
195
196extern void malloc_zone_print_ptr_info(void *ptr);
197 /* print to stdout if this pointer is in the malloc heap, free status, and size */
198
199extern boolean_t malloc_zone_check(malloc_zone_t *zone);
200 /* Checks zone is well formed; if !zone, checks all zones */
201
202extern void malloc_zone_print(malloc_zone_t *zone, boolean_t verbose);
203 /* Prints summary on zone; if !zone, prints all zones */
204
205extern void malloc_zone_statistics(malloc_zone_t *zone, malloc_statistics_t *stats);
206 /* Fills statistics for zone; if !zone, sums up all zones */
207
208extern void malloc_zone_log(malloc_zone_t *zone, void *address);
209 /* Controls logging of all activity; if !zone, for all zones;
210 If address==0 nothing is logged;
211 If address==-1 all activity is logged;
212 Else only the activity regarding address is logged */
59e0d9fe
A
213
214struct mstats {
215 size_t bytes_total;
216 size_t chunks_used;
217 size_t bytes_used;
218 size_t chunks_free;
219 size_t bytes_free;
220};
221
222extern struct mstats mstats(void);
223
9385eb3d
A
224__END_DECLS
225
226#endif /* _MALLOC_MALLOC_H_ */