]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSMalloc.h
b7aecdd521cfc880eb6a00df4766583be6de3be6
2 * Copyright (c) 2003-2004 Apple Computer, 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@
29 #ifndef LIBKERN_OSMALLOC_h
30 #define LIBKERN_OSMALLOC_h
32 #include <sys/cdefs.h>
37 #ifdef MACH_KERNEL_PRIVATE
38 #include <kern/queue.h>
40 #if defined(XNU_KERNEL_PRIVATE)
41 #include <kern/kalloc.h>
42 #ifndef OSMallocDeprecated
43 #define OSMallocDeprecated __deprecated_msg("Use kalloc heaps")
45 #endif /* XNU_KERNEL_PRIVATE */
51 * This header declares the OSMalloc memory-allocation KPI.
54 * Kernel extensions can use these functions to allocate and deallocate
55 * memory blocks that are tracked under named tags.
56 * A kernel extension can create whatever tags it needs,
57 * but typically just creates one with its bundle identifier.
59 * Tags are required; attempting to use these functions without one
60 * will result in a panic.
62 * <b>Use Restrictions</b>
64 * None of the OSMalloc functions are safe to call
65 * in a primary interrupt handler.
68 #ifdef MACH_KERNEL_PRIVATE
70 #define OSMT_MAX_NAME (64)
72 typedef struct _OSMallocTag_
{
73 queue_chain_t OSMT_link
;
77 char OSMT_name
[OSMT_MAX_NAME
];
80 #define OSMT_VALID_MASK 0xFFFF0000
81 #define OSMT_VALID 0xDEAB0000
82 #define OSMT_RELEASED 0x00000001
85 #define OSMT_ATTR_PAGEABLE 0x01
89 * @typedef OSMallocTag
92 * An opaque type used to track memory allocations.
94 typedef struct __OSMallocTag__
* OSMallocTag
;
98 * @typedef OSMallocTag_t
101 * See <code>@link OSMallocTag OSMallocTag@/link</code>.
103 typedef struct __OSMallocTag__
* OSMallocTag_t
;
107 * @define OSMT_DEFAULT
110 * Indicates that an <code>@link OSMallocTag OSMallocTag@/link</code>
111 * be created with default attributes.
114 * An <code>@link OSMallocTag OSMallocTag@/link</code> created
115 * with this attribute allocates all blocks in wired memory.
117 #define OSMT_DEFAULT 0x00
121 * @define OSMT_PAGEABLE
124 * Indicates that an <code>@link OSMallocTag OSMallocTag@/link</code>
125 * should allocate pageable memory when possible.
128 * An <code>@link OSMallocTag OSMallocTag@/link</code> created
129 * with this attribute allocates blocks of a full page size or larger
130 * in pageable memory,
131 * and blocks smaller than a full page size in wired memory.
133 #define OSMT_PAGEABLE 0x01
137 * @function OSMalloc_Tagalloc
140 * Creates a tag for use with OSMalloc functions.
142 * @param name The name of the tag to create.
143 * @param flags A bitmask that controls allocation behavior; see description.
146 * An opaque tag to be used with OSMalloc functions for tracking memory usage.
149 * OSMalloc tags can have arbitrary names of a length up to 63 characters.
150 * Calling this function twice with the same name
151 * creates two tags, which share that name.
153 * <code>flags</code> can be the bitwise OR of the following flags:
155 * <li><code>@link OSMT_DEFAULT OSMT_DEFAULT@/link</code> -
156 * allocations are wired. This is the 'zero' bitmask value and
157 * is overridden by any other flag specified.</li>
158 * <li><code>@link OSMT_PAGEABLE OSMT_PAGEABLE@/link</code> -
159 * allocations of a full page size or greater are pageable;
160 * allocations smaller than a page are wired.</li>
163 #if XNU_KERNEL_PRIVATE
166 extern OSMallocTag
OSMalloc_Tagalloc(
172 * @function OSMalloc_Tagfree
175 * Frees a tag used with OSMalloc functions.
177 * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code> to free.
180 * OSMalloc tags must not be freed
181 * while any memory blocks allocated
182 * with them still exist.
183 * Any OSMalloc function called on those blocks
184 * will result in a panic.
186 #if XNU_KERNEL_PRIVATE
189 extern void OSMalloc_Tagfree(OSMallocTag tag
);
196 * Allocates a block of memory associated
197 * with a given <code>@link OSMallocTag OSMallocTag@/link</code>.
199 * @param size The size of the memory block to allocate.
200 * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code>
201 * under which to allocate the memory.
204 * A pointer to the memory on success, <code>NULL</code> on failure.
207 * If <code>tag</code> was created with the
208 * <code>@link OSMT_PAGEABLE OSMT_PAGEABLE@/link</code>
209 * attribute <i>and</i> <code>size</code>
210 * is a full page or larger, the allocated memory is pageable;
211 * otherwise it is wired.
213 #if XNU_KERNEL_PRIVATE
216 extern void * OSMalloc(
218 OSMallocTag tag
) __attribute__((alloc_size(1)));
221 * @function OSMalloc_nowait
224 * Equivalent to <code>@link OSMalloc_noblock OSMalloc_noblock@/link</code>.
226 #if XNU_KERNEL_PRIVATE
229 extern void * OSMalloc_nowait(
231 OSMallocTag tag
) __attribute__((alloc_size(1)));
234 * @function OSMalloc_noblock
237 * Allocates a block of memory associated
238 * with a given <code>@link OSMallocTag OSMallocTag@/link</code>,
239 * returning <code>NULL</code> if it would block.
241 * @param size The size of the memory block to allocate.
242 * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code>
243 * under which to allocate the memory.
246 * A pointer to the memory on success, <code>NULL</code> on failure
247 * or if allocation would block.
250 * If <code>tag</code> was created with the
251 * <code>@link OSMT_PAGEABLE OSMT_PAGEABLE@/link</code>
252 * attribute <i>and</i> <code>size</code>
253 * is a full page or larger, the allocated memory is pageable;
254 * otherwise it is wired.
256 * This function is guaranteed not to block.
258 #if XNU_KERNEL_PRIVATE
261 extern void * OSMalloc_noblock(
263 OSMallocTag tag
) __attribute__((alloc_size(1)));
269 * Frees a block of memory allocated by <code>@link OSMalloc OSMalloc@/link</code>.
271 * @param addr A pointer to the memory block to free.
272 * @param size The size of the memory block to free.
273 * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code>
274 * with which <code>addr</code> was originally allocated.
276 #if XNU_KERNEL_PRIVATE
286 #endif /* LIBKERN_OSMALLOC_h */