2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
67 #ifndef _KERN_ZALLOC_H_
68 #define _KERN_ZALLOC_H_
70 #include <mach/machine/vm_types.h>
71 #include <kern/kern_types.h>
72 #include <sys/cdefs.h>
74 #ifdef MACH_KERNEL_PRIVATE
76 #include <zone_debug.h>
77 #include <kern/lock.h>
78 #include <kern/locks.h>
79 #include <kern/queue.h>
80 #include <kern/thread_call.h>
83 typedef struct gzalloc_data
{
90 * A zone is a collection of fixed size blocks for which there
91 * is fast allocation/deallocation access. Kernel routines can
92 * use zones to manage data structures dynamically, creating a zone
93 * for each type of data structure to be managed.
98 int count
; /* Number of elements used now */
99 vm_offset_t free_elements
;
100 decl_lck_mtx_data(,lock
) /* zone lock */
101 lck_mtx_ext_t lock_ext
; /* placeholder for indirect mutex */
102 lck_attr_t lock_attr
; /* zone lock attribute */
103 lck_grp_t lock_grp
; /* zone lock group */
104 lck_grp_attr_t lock_grp_attr
; /* zone lock group attribute */
105 vm_size_t cur_size
; /* current memory utilization */
106 vm_size_t max_size
; /* how large can this zone grow */
107 vm_size_t elem_size
; /* size of an element */
108 vm_size_t alloc_size
; /* size used for more memory */
109 uint64_t sum_count
; /* count of allocs (life of zone) */
111 /* boolean_t */ exhaustible
:1, /* (F) merely return if empty? */
112 /* boolean_t */ collectable
:1, /* (F) garbage collect empty pages */
113 /* boolean_t */ expandable
:1, /* (T) expand zone (with message)? */
114 /* boolean_t */ allows_foreign
:1,/* (F) allow non-zalloc space */
115 /* boolean_t */ doing_alloc
:1, /* is zone expanding now? */
116 /* boolean_t */ waiting
:1, /* is thread waiting for expansion? */
117 /* boolean_t */ async_pending
:1, /* asynchronous allocation pending? */
119 /* boolean_t */ zleak_on
:1, /* Are we collecting allocation information? */
120 #endif /* CONFIG_ZLEAKS */
121 /* boolean_t */ caller_acct
: 1, /* do we account allocation/free to the caller? */
122 /* boolean_t */ doing_gc
:1, /* garbage collect in progress? */
123 /* boolean_t */ noencrypt
:1,
124 /* boolean_t */ no_callout
:1,
125 /* boolean_t */ async_prio_refill
:1,
126 /* boolean_t */ gzalloc_exempt
:1,
127 /* boolean_t */ alignment_required
:1;
128 int index
; /* index into zone_info arrays for this zone */
129 struct zone
* next_zone
; /* Link for all-zones list */
130 thread_call_data_t call_async_alloc
; /* callout for asynchronous alloc */
131 const char *zone_name
; /* a name for the zone */
133 queue_head_t active_zones
; /* active elements */
134 #endif /* ZONE_DEBUG */
137 uint32_t num_allocs
; /* alloc stats for zleak benchmarks */
138 uint32_t num_frees
; /* free stats for zleak benchmarks */
139 uint32_t zleak_capture
; /* per-zone counter for capturing every N allocations */
140 #endif /* CONFIG_ZLEAKS */
141 uint32_t free_check_count
; /* counter for poisoning/checking every N frees */
142 vm_size_t prio_refill_watermark
;
143 thread_t zone_replenish_thread
;
146 #endif /* CONFIG_GZALLOC */
150 * structure for tracking zone usage
151 * Used either one per task/thread for all zones or <per-task,per-zone>.
153 typedef struct zinfo_usage_store_t
{
154 /* These fields may be updated atomically, and so must be 8 byte aligned */
155 uint64_t alloc
__attribute__((aligned(8))); /* allocation counter */
156 uint64_t free
__attribute__((aligned(8))); /* free counter */
157 } zinfo_usage_store_t
;
158 typedef zinfo_usage_store_t
*zinfo_usage_t
;
160 extern void zone_gc(boolean_t
);
161 extern void consider_zone_gc(boolean_t
);
163 /* Steal memory for zone module */
164 extern void zone_steal_memory(void);
166 /* Bootstrap zone module (create zone zone) */
167 extern void zone_bootstrap(void) __attribute__((section("__TEXT, initcode")));
169 /* Init zone module */
170 extern void zone_init(
171 vm_size_t map_size
) __attribute__((section("__TEXT, initcode")));
173 /* Handle per-task zone info */
174 extern void zinfo_task_init(task_t task
);
175 extern void zinfo_task_free(task_t task
);
178 /* Stack use statistics */
179 extern void stack_fake_zone_init(int zone_index
);
180 extern void stack_fake_zone_info(
184 vm_size_t
*elem_size
,
185 vm_size_t
*alloc_size
,
193 extern void zone_debug_enable(
196 extern void zone_debug_disable(
199 #define zone_debug_enabled(z) z->active_zones.next
200 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
201 #define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
202 #endif /* ZONE_DEBUG */
204 #endif /* MACH_KERNEL_PRIVATE */
208 #ifdef XNU_KERNEL_PRIVATE
210 /* Allocate from zone */
211 extern void * zalloc(
214 /* Free zone element */
221 vm_size_t size
, /* the size of an element */
222 vm_size_t maxmem
, /* maximum memory to use */
223 vm_size_t alloc
, /* allocation size */
224 const char *name
); /* a name for the zone */
227 /* Non-blocking version of zalloc */
228 extern void * zalloc_noblock(
231 /* direct (non-wrappered) interface */
232 extern void * zalloc_canblock(
236 /* Get from zone free list */
240 /* Fill zone with memory */
246 /* Initially fill zone with specified number of elements */
251 /* Change zone parameters */
252 extern void zone_change(
256 extern void zone_prio_refill_configure(zone_t
, vm_size_t
);
257 /* Item definitions */
258 #define Z_EXHAUST 1 /* Make zone exhaustible */
259 #define Z_COLLECT 2 /* Make zone collectable */
260 #define Z_EXPAND 3 /* Make zone expandable */
261 #define Z_FOREIGN 4 /* Allow collectable zone to contain foreign elements */
262 #define Z_CALLERACCT 5 /* Account alloc/free against the caller */
263 #define Z_NOENCRYPT 6 /* Don't encrypt zone during hibernation */
264 #define Z_NOCALLOUT 7 /* Don't asynchronously replenish the zone via
267 #define Z_ALIGNMENT_REQUIRED 8
268 #define Z_GZALLOC_EXEMPT 9 /* Not tracked in guard allocation mode */
269 /* Preallocate space for zone from zone map */
270 extern void zprealloc(
274 extern integer_t
zone_free_count(
278 * MAX_ZTRACE_DEPTH configures how deep of a stack trace is taken on each zalloc in the zone of interest. 15
279 * levels is usually enough to get past all the layers of code in kalloc and IOKit and see who the actual
280 * caller is up above these lower levels.
282 * This is used both for the zone leak detector and the zone corruption log.
285 #define MAX_ZTRACE_DEPTH 15
288 * Structure for keeping track of a backtrace, used for leak detection.
289 * This is in the .h file because it is used during panic, see kern/debug.c
290 * A non-zero size indicates that the trace is in use.
293 vm_size_t zt_size
; /* How much memory are all the allocations referring to this trace taking up? */
294 uint32_t zt_depth
; /* depth of stack (0 to MAX_ZTRACE_DEPTH) */
295 void* zt_stack
[MAX_ZTRACE_DEPTH
]; /* series of return addresses from OSBacktrace */
296 uint32_t zt_collisions
; /* How many times did a different stack land here while it was occupied? */
297 uint32_t zt_hit_count
; /* for determining effectiveness of hash function */
302 /* support for the kern.zleak.* sysctls */
304 extern kern_return_t
zleak_activate(void);
305 extern vm_size_t zleak_max_zonemap_size
;
306 extern vm_size_t zleak_global_tracking_threshold
;
307 extern vm_size_t zleak_per_zone_tracking_threshold
;
309 extern int get_zleak_state(void);
311 #endif /* CONFIG_ZLEAKS */
313 /* These functions used for leak detection both in zalloc.c and mbuf.c */
314 extern uint32_t fastbacktrace(uintptr_t* bt
, uint32_t max_frames
) __attribute__((noinline
));
315 extern uintptr_t hash_mix(uintptr_t);
316 extern uint32_t hashbacktrace(uintptr_t *, uint32_t, uint32_t);
317 extern uint32_t hashaddr(uintptr_t, uint32_t);
319 #define lock_zone(zone) \
321 lck_mtx_lock_spin(&(zone)->lock); \
324 #define unlock_zone(zone) \
326 lck_mtx_unlock(&(zone)->lock); \
330 void gzalloc_init(vm_size_t
);
331 void gzalloc_zone_init(zone_t
);
332 void gzalloc_configure(void);
333 void gzalloc_reconfigure(zone_t
);
334 boolean_t
gzalloc_enabled(void);
336 vm_offset_t
gzalloc_alloc(zone_t
, boolean_t
);
337 boolean_t
gzalloc_free(zone_t
, void *);
338 #endif /* CONFIG_GZALLOC */
340 #endif /* XNU_KERNEL_PRIVATE */
344 #endif /* _KERN_ZALLOC_H_ */
346 #endif /* KERNEL_PRIVATE */