]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSMalloc.h
04a5ba6f267fa4d1bce4e9e893b177a680a619b3
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>
45 * This header declares the OSMalloc memory-allocation KPI.
48 * Kernel extensions can use these functions to allocate and deallocate
49 * memory blocks that are tracked under named tags.
50 * A kernel extension can create whatever tags it needs,
51 * but typically just creates one with its bundle identifier.
53 * Tags are required; attempting to use these functions without one
54 * will result in a panic.
56 * <b>Use Restrictions</b>
58 * None of the OSMalloc functions are safe to call
59 * in a primary interrupt handler.
62 #ifdef MACH_KERNEL_PRIVATE
64 #define OSMT_MAX_NAME (64)
66 typedef struct _OSMallocTag_
{
67 queue_chain_t OSMT_link
;
71 char OSMT_name
[OSMT_MAX_NAME
];
74 #define OSMT_VALID_MASK 0xFFFF0000
75 #define OSMT_VALID 0xDEAB0000
76 #define OSMT_RELEASED 0x00000001
79 #define OSMT_ATTR_PAGEABLE 0x01
83 * @typedef OSMallocTag
86 * An opaque type used to track memory allocations.
88 typedef struct __OSMallocTag__
* OSMallocTag
;
92 * @typedef OSMallocTag_t
95 * See <code>@link OSMallocTag OSMallocTag@/link</code>.
97 typedef struct __OSMallocTag__
* OSMallocTag_t
;
101 * @define OSMT_DEFAULT
104 * Indicates that an <code>@link OSMallocTag OSMallocTag@/link</code>
105 * be created with default attributes.
108 * An <code>@link OSMallocTag OSMallocTag@/link</code> created
109 * with this attribute allocates all blocks in wired memory.
111 #define OSMT_DEFAULT 0x00
115 * @define OSMT_PAGEABLE
118 * Indicates that an <code>@link OSMallocTag OSMallocTag@/link</code>
119 * should allocate pageable memory when possible.
122 * An <code>@link OSMallocTag OSMallocTag@/link</code> created
123 * with this attribute allocates blocks of a full page size or larger
124 * in pageable memory,
125 * and blocks smaller than a full page size in wired memory.
127 #define OSMT_PAGEABLE 0x01
131 * @function OSMalloc_Tagalloc
134 * Creates a tag for use with OSMalloc functions.
136 * @param name The name of the tag to create.
137 * @param flags A bitmask that controls allocation behavior; see description.
140 * An opaque tag to be used with OSMalloc functions for tracking memory usage.
143 * OSMalloc tags can have arbitrary names of a length up to 63 characters.
144 * Calling this function twice with the same name
145 * creates two tags, which share that name.
147 * <code>flags</code> can be the bitwise OR of the following flags:
149 * <li><code>@link OSMT_DEFAULT OSMT_DEFAULT@/link</code> -
150 * allocations are wired. This is the 'zero' bitmask value and
151 * is overridden by any other flag specified.</li>
152 * <li><code>@link OSMT_PAGEABLE OSMT_PAGEABLE@/link</code> -
153 * allocations of a full page size or greater are pageable;
154 * allocations smaller than a page are wired.</li>
157 extern OSMallocTag
OSMalloc_Tagalloc(
163 * @function OSMalloc_Tagfree
166 * Frees a tag used with OSMalloc functions.
168 * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code> to free.
171 * OSMalloc tags must not be freed
172 * while any memory blocks allocated
173 * with them still exist.
174 * Any OSMalloc function called on those blocks
175 * will result in a panic.
177 extern void OSMalloc_Tagfree(OSMallocTag tag
);
184 * Allocates a block of memory associated
185 * with a given <code>@link OSMallocTag OSMallocTag@/link</code>.
187 * @param size The size of the memory block to allocate.
188 * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code>
189 * under which to allocate the memory.
192 * A pointer to the memory on success, <code>NULL</code> on failure.
195 * If <code>tag</code> was created with the
196 * <code>@link OSMT_PAGEABLE OSMT_PAGEABLE@/link</code>
197 * attribute <i>and</i> <code>size</code>
198 * is a full page or larger, the allocated memory is pageable;
199 * otherwise it is wired.
201 extern void * OSMalloc(
207 * @function OSMalloc_nowait
210 * Equivalent to <code>@link OSMalloc_noblock OSMalloc_noblock@/link</code>.
212 extern void * OSMalloc_nowait(
218 * @function OSMalloc_noblock
221 * Allocates a block of memory associated
222 * with a given <code>@link OSMallocTag OSMallocTag@/link</code>,
223 * returning <code>NULL</code> if it would block.
225 * @param size The size of the memory block to allocate.
226 * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code>
227 * under which to allocate the memory.
230 * A pointer to the memory on success, <code>NULL</code> on failure
231 * or if allocation would block.
234 * If <code>tag</code> was created with the
235 * <code>@link OSMT_PAGEABLE OSMT_PAGEABLE@/link</code>
236 * attribute <i>and</i> <code>size</code>
237 * is a full page or larger, the allocated memory is pageable;
238 * otherwise it is wired.
240 * This function is guaranteed not to block.
242 extern void * OSMalloc_noblock(
251 * Frees a block of memory allocated by <code>@link OSMalloc OSMalloc@/link</code>.
253 * @param addr A pointer to the memory block to free.
254 * @param size The size of the memory block to free.
255 * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code>
256 * with which <code>addr</code> was originally allocated.
263 #ifdef XNU_KERNEL_PRIVATE
265 * @function OSMalloc_size
268 * Returns the size of a block of memory allocated by <code>@link OSMalloc OSMalloc@/link</code>.
270 * @param addr A pointer to the memory block allocated via OSMalloc.
272 extern uint32_t OSMalloc_size(
274 #endif /* XNU_KERNEL_PRIVATE */
278 #endif /* LIBKERN_OSMALLOC_h */