Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | /* * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #ifndef LIBKERN_OSMALLOC_h #define LIBKERN_OSMALLOC_h #include <sys/cdefs.h> __BEGIN_DECLS #include <stdint.h> #ifdef MACH_KERNEL_PRIVATE #include <kern/queue.h> #endif /*! * @header * * @abstract * This header declares the OSMalloc memory-allocation KPI. * * @discussion * Kernel extensions can use these functions to allocate and deallocate * memory blocks that are tracked under named tags. * A kernel extension can create whatever tags it needs, * but typically just creates one with its bundle identifier. * * Tags are required; attempting to use these functions without one * will result in a panic. * * <b>Use Restrictions</b> * * None of the OSMalloc functions are safe to call * in a primary interrupt handler. */ #ifdef MACH_KERNEL_PRIVATE #define OSMT_MAX_NAME (64) typedef struct _OSMallocTag_ { queue_chain_t OSMT_link; uint32_t OSMT_refcnt; uint32_t OSMT_state; uint32_t OSMT_attr; char OSMT_name[OSMT_MAX_NAME]; } * OSMallocTag; #define OSMT_VALID_MASK 0xFFFF0000 #define OSMT_VALID 0xDEAB0000 #define OSMT_RELEASED 0x00000001 /*! @parseOnly */ #define OSMT_ATTR_PAGEABLE 0x01 #else /*! * @typedef OSMallocTag * * @abstract * An opaque type used to track memory allocations. */ typedef struct __OSMallocTag__ * OSMallocTag; /*! * @typedef OSMallocTag_t * * @abstract * See <code>@link OSMallocTag OSMallocTag@/link</code>. */ typedef struct __OSMallocTag__ * OSMallocTag_t; #endif /*! * @define OSMT_DEFAULT * * @abstract * Indicates that an <code>@link OSMallocTag OSMallocTag@/link</code> * be created with default attributes. * * @discussion * An <code>@link OSMallocTag OSMallocTag@/link</code> created * with this attribute allocates all blocks in wired memory. */ #define OSMT_DEFAULT 0x00 /*! * @define OSMT_PAGEABLE * * @abstract * Indicates that an <code>@link OSMallocTag OSMallocTag@/link</code> * should allocate pageable memory when possible. * * @discussion * An <code>@link OSMallocTag OSMallocTag@/link</code> created * with this attribute allocates blocks of a full page size or larger * in pageable memory, * and blocks smaller than a full page size in wired memory. */ #define OSMT_PAGEABLE 0x01 /*! * @function OSMalloc_Tagalloc * * @abstract * Creates a tag for use with OSMalloc functions. * * @param name The name of the tag to create. * @param flags A bitmask that controls allocation behavior; see description. * * @result * An opaque tag to be used with OSMalloc functions for tracking memory usage. * * @discussion * OSMalloc tags can have arbitrary names of a length up to 63 characters. * Calling this function twice with the same name * creates two tags, which share that name. * * <code>flags</code> can be the bitwise OR of the following flags: * <ul> * <li><code>@link OSMT_DEFAULT OSMT_DEFAULT@/link</code> - * allocations are wired. This is the 'zero' bitmask value and * is overridden by any other flag specified.</li> * <li><code>@link OSMT_PAGEABLE OSMT_PAGEABLE@/link</code> - * allocations of a full page size or greater are pageable; * allocations smaller than a page are wired.</li> * </ul> */ extern OSMallocTag OSMalloc_Tagalloc( const char * name, uint32_t flags); /*! * @function OSMalloc_Tagfree * * @abstract * Frees a tag used with OSMalloc functions. * * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code> to free. * * @discussion * OSMalloc tags must not be freed * while any memory blocks allocated * with them still exist. * Any OSMalloc function called on those blocks * will result in a panic. */ extern void OSMalloc_Tagfree(OSMallocTag tag); /*! * @function OSMalloc * * @abstract * Allocates a block of memory associated * with a given <code>@link OSMallocTag OSMallocTag@/link</code>. * * @param size The size of the memory block to allocate. * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code> * under which to allocate the memory. * * @result * A pointer to the memory on success, <code>NULL</code> on failure. * * @discussion * If <code>tag</code> was created with the * <code>@link OSMT_PAGEABLE OSMT_PAGEABLE@/link</code> * attribute <i>and</i> <code>size</code> * is a full page or larger, the allocated memory is pageable; * otherwise it is wired. */ extern void * OSMalloc( uint32_t size, OSMallocTag tag); /*! * @function OSMalloc_nowait * * @abstract * Equivalent to <code>@link OSMalloc_noblock OSMalloc_noblock@/link</code>. */ extern void * OSMalloc_nowait( uint32_t size, OSMallocTag tag); /*! * @function OSMalloc_noblock * * @abstract * Allocates a block of memory associated * with a given <code>@link OSMallocTag OSMallocTag@/link</code>, * returning <code>NULL</code> if it would block. * * @param size The size of the memory block to allocate. * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code> * under which to allocate the memory. * * @result * A pointer to the memory on success, <code>NULL</code> on failure * or if allocation would block. * * @discussion * If <code>tag</code> was created with the * <code>@link OSMT_PAGEABLE OSMT_PAGEABLE@/link</code> * attribute <i>and</i> <code>size</code> * is a full page or larger, the allocated memory is pageable; * otherwise it is wired. * * This function is guaranteed not to block. */ extern void * OSMalloc_noblock( uint32_t size, OSMallocTag tag); /*! * @function OSFree * * @abstract * Frees a block of memory allocated by <code>@link OSMalloc OSMalloc@/link</code>. * * @param addr A pointer to the memory block to free. * @param size The size of the memory block to free. * @param tag The <code>@link OSMallocTag OSMallocTag@/link</code> * with which <code>addr</code> was originally allocated. */ extern void OSFree( void * addr, uint32_t size, OSMallocTag tag); #ifdef XNU_KERNEL_PRIVATE /*! * @function OSMalloc_size * * @abstract * Returns the size of a block of memory allocated by <code>@link OSMalloc OSMalloc@/link</code>. * * @param addr A pointer to the memory block allocated via OSMalloc. */ extern uint32_t OSMalloc_size( void * addr); #endif /* XNU_KERNEL_PRIVATE */ __END_DECLS #endif /* LIBKERN_OSMALLOC_h */ |