uma.h revision 95925
1284990Scy/* 2284990Scy * Copyright (c) 2002, Jeffrey Roberson <jroberson@chesapeake.net> 3284990Scy * All rights reserved. 4284990Scy * 5284990Scy * Redistribution and use in source and binary forms, with or without 6284990Scy * modification, are permitted provided that the following conditions 7284990Scy * are met: 8284990Scy * 1. Redistributions of source code must retain the above copyright 9284990Scy * notice unmodified, this list of conditions, and the following 10284990Scy * disclaimer. 11284990Scy * 2. Redistributions in binary form must reproduce the above copyright 12284990Scy * notice, this list of conditions and the following disclaimer in the 13284990Scy * documentation and/or other materials provided with the distribution. 14284990Scy * 15284990Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16284990Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17284990Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18284990Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19284990Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20284990Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21284990Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22284990Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23284990Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24284990Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25284990Scy * 26284990Scy * $FreeBSD: head/sys/vm/uma.h 95925 2002-05-02 07:36:30Z arr $ 27284990Scy * 28284990Scy */ 29284990Scy 30284990Scy/* 31284990Scy * uma.h - External definitions for the Universal Memory Allocator 32284990Scy * 33284990Scy * Jeff Roberson <jroberson@chesapeake.net> 34284990Scy*/ 35284990Scy 36284990Scy#ifndef VM_UMA_H 37284990Scy#define VM_UMA_H 38284990Scy 39284990Scy#include <sys/param.h> /* For NULL */ 40284990Scy#include <sys/malloc.h> /* For M_* */ 41284990Scy 42284990Scy/* User visable parameters */ 43284990Scy#define UMA_SMALLEST_UNIT (PAGE_SIZE / 256) /* Smallest item allocated */ 44284990Scy 45284990Scy/* Types and type defs */ 46284990Scy 47284990Scystruct uma_zone; 48284990Scy/* Opaque type used as a handle to the zone */ 49284990Scytypedef struct uma_zone * uma_zone_t; 50284990Scy 51284990Scy/* 52284990Scy * Item constructor 53284990Scy * 54284990Scy * Arguments: 55284990Scy * item A pointer to the memory which has been allocated. 56284990Scy * arg The arg field passed to uma_zalloc_arg 57284990Scy * size The size of the allocated item 58284990Scy * 59284990Scy * Returns: 60284990Scy * Nothing 61284990Scy * 62284990Scy * Discussion: 63284990Scy * The constructor is called just before the memory is returned 64284990Scy * to the user. It may block if neccisary. 65284990Scy */ 66284990Scytypedef void (*uma_ctor)(void *mem, int size, void *arg); 67284990Scy 68284990Scy/* 69284990Scy * Item destructor 70284990Scy * 71284990Scy * Arguments: 72284990Scy * item A pointer to the memory which has been allocated. 73284990Scy * size The size of the item being destructed. 74284990Scy * arg Argument passed through uma_zfree_arg 75284990Scy * 76284990Scy * Returns: 77284990Scy * Nothing 78284990Scy * 79284990Scy * Discussion: 80284990Scy * The destructor may perform operations that differ from those performed 81284990Scy * by the initializer, but it must leave the object in the same state. 82284990Scy * This IS type stable storage. This is called after EVERY zfree call. 83284990Scy */ 84284990Scytypedef void (*uma_dtor)(void *mem, int size, void *arg); 85284990Scy 86284990Scy/* 87284990Scy * Item initializer 88284990Scy * 89284990Scy * Arguments: 90284990Scy * item A pointer to the memory which has been allocated. 91284990Scy * size The size of the item being initialized. 92284990Scy * 93284990Scy * Returns: 94284990Scy * Nothing 95284990Scy * 96284990Scy * Discussion: 97284990Scy * The initializer is called when the memory is cached in the uma zone. 98284990Scy * this should be the same state that the destructor leaves the object in. 99284990Scy */ 100284990Scytypedef void (*uma_init)(void *mem, int size); 101284990Scy 102284990Scy/* 103284990Scy * Item discard function 104284990Scy * 105284990Scy * Arguments: 106284990Scy * item A pointer to memory which has been 'freed' but has not left the 107284990Scy * zone's cache. 108284990Scy * size The size of the item being discarded. 109284990Scy * 110284990Scy * Returns: 111284990Scy * Nothing 112284990Scy * 113284990Scy * Discussion: 114284990Scy * This routine is called when memory leaves a zone and is returned to the 115284990Scy * system for other uses. It is the counter part to the init function. 116284990Scy */ 117284990Scytypedef void (*uma_fini)(void *mem, int size); 118284990Scy 119284990Scy/* 120284990Scy * What's the difference between initializing and constructing? 121284990Scy * 122284990Scy * The item is initialized when it is cached, and this is the state that the 123284990Scy * object should be in when returned to the allocator. The purpose of this is 124284990Scy * to remove some code which would otherwise be called on each allocation by 125284990Scy * utilizing a known, stable state. This differs from the constructor which 126284990Scy * will be called on EVERY allocation. 127284990Scy * 128284990Scy * For example, in the initializer you may want to initialize embeded locks, 129284990Scy * NULL list pointers, set up initial states, magic numbers, etc. This way if 130284990Scy * the object is held in the allocator and re-used it won't be neccisary to 131284990Scy * re-initialize it. 132284990Scy * 133284990Scy * The constructor may be used to lock a data structure, link it on to lists, 134284990Scy * bump reference counts or total counts of outstanding structures, etc. 135284990Scy * 136284990Scy */ 137284990Scy 138284990Scy 139284990Scy/* Function proto types */ 140284990Scy 141284990Scy/* 142284990Scy * Create a new uma zone 143284990Scy * 144284990Scy * Arguments: 145284990Scy * name The text name of the zone for debugging and stats, this memory 146284990Scy * should not be freed until the zone has been deallocated. 147284990Scy * size The size of the object that is being created. 148284990Scy * ctor The constructor that is called when the object is allocated 149284990Scy * dtor The destructor that is called when the object is freed. 150284990Scy * init An initializer that sets up the initial state of the memory. 151284990Scy * fini A discard function that undoes initialization done by init. 152284990Scy * ctor/dtor/init/fini may all be null, see notes above. 153284990Scy * align A bitmask that corisponds to the requested alignment 154284990Scy * eg 4 would be 0x3 155284990Scy * flags A set of parameters that control the behavior of the zone 156284990Scy * 157284990Scy * Returns: 158284990Scy * A pointer to a structure which is intended to be opaque to users of 159284990Scy * the interface. The value may be null if the wait flag is not set. 160284990Scy */ 161284990Scy 162284990Scyuma_zone_t uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 163284990Scy uma_init uminit, uma_fini fini, int align, 164284990Scy u_int16_t flags); 165284990Scy 166284990Scy/* Definitions for uma_zcreate flags */ 167284990Scy#define UMA_ZONE_PAGEABLE 0x0001 /* Return items not fully backed by 168284990Scy physical memory XXX Not yet */ 169284990Scy#define UMA_ZONE_ZINIT 0x0002 /* Initialize with zeros */ 170284990Scy#define UMA_ZONE_STATIC 0x0004 /* Staticly sized zone */ 171284990Scy#define UMA_ZONE_OFFPAGE 0x0008 /* Force the slab structure allocation 172284990Scy off of the real memory */ 173284990Scy#define UMA_ZONE_MALLOC 0x0010 /* For use by malloc(9) only! */ 174284990Scy#define UMA_ZONE_NOFREE 0x0020 /* Do not free slabs of this type! */ 175284990Scy#define UMA_ZONE_MTXCLASS 0x0040 /* Create a new lock class */ 176284990Scy 177284990Scy/* Definitions for align */ 178284990Scy#define UMA_ALIGN_PTR (sizeof(void *) - 1) /* Alignment fit for ptr */ 179284990Scy#define UMA_ALIGN_LONG (sizeof(long) - 1) /* "" long */ 180284990Scy#define UMA_ALIGN_INT (sizeof(int) - 1) /* "" int */ 181284990Scy#define UMA_ALIGN_SHORT (sizeof(short) - 1) /* "" short */ 182284990Scy#define UMA_ALIGN_CHAR (sizeof(char) - 1) /* "" char */ 183284990Scy#define UMA_ALIGN_CACHE (16 - 1) /* Cache line size align */ 184284990Scy 185284990Scy/* 186284990Scy * Destroys an empty uma zone. If the zone is not empty uma complains loudly. 187284990Scy * 188284990Scy * Arguments: 189284990Scy * zone The zone we want to destroy. 190284990Scy * 191284990Scy */ 192284990Scy 193284990Scyvoid uma_zdestroy(uma_zone_t zone); 194284990Scy 195284990Scy/* 196284990Scy * Allocates an item out of a zone 197284990Scy * 198284990Scy * Arguments: 199284990Scy * zone The zone we are allocating from 200284990Scy * arg This data is passed to the ctor function 201284990Scy * flags See sys/malloc.h for available flags. 202284990Scy * 203284990Scy * Returns: 204284990Scy * A non null pointer to an initialized element from the zone is 205284990Scy * garanteed if the wait flag is M_WAITOK, otherwise a null pointer may be 206284990Scy * returned if the zone is empty or the ctor failed. 207284990Scy */ 208284990Scy 209284990Scyvoid *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags); 210284990Scy 211284990Scy/* 212284990Scy * Allocates an item out of a zone without supplying an argument 213284990Scy * 214284990Scy * This is just a wrapper for uma_zalloc_arg for convenience. 215284990Scy * 216284990Scy */ 217284990Scystatic __inline void *uma_zalloc(uma_zone_t zone, int flags); 218284990Scy 219284990Scystatic __inline void * 220284990Scyuma_zalloc(uma_zone_t zone, int flags) 221284990Scy{ 222284990Scy return uma_zalloc_arg(zone, NULL, flags); 223284990Scy} 224284990Scy 225284990Scy/* 226284990Scy * Frees an item back into the specified zone. 227284990Scy * 228284990Scy * Arguments: 229284990Scy * zone The zone the item was originally allocated out of. 230284990Scy * item The memory to be freed. 231284990Scy * arg Argument passed to the destructor 232284990Scy * 233284990Scy * Returns: 234284990Scy * Nothing. 235284990Scy */ 236284990Scy 237284990Scyvoid uma_zfree_arg(uma_zone_t zone, void *item, void *arg); 238284990Scy 239284990Scy/* 240284990Scy * Frees an item back to a zone without supplying an argument 241284990Scy * 242284990Scy * This is just a wrapper for uma_zfree_arg for convenience. 243284990Scy * 244284990Scy */ 245284990Scystatic __inline void uma_zfree(uma_zone_t zone, void *item); 246284990Scy 247284990Scystatic __inline void 248284990Scyuma_zfree(uma_zone_t zone, void *item) 249284990Scy{ 250284990Scy return uma_zfree_arg(zone, item, NULL); 251284990Scy} 252284990Scy 253284990Scy/* 254284990Scy * XXX The rest of the prototypes in this header are h0h0 magic for the VM. 255284990Scy * If you think you need to use it for a normal zone you're probably incorrect. 256284990Scy */ 257284990Scy 258284990Scy/* 259284990Scy * Backend page supplier routines 260284990Scy * 261284990Scy * Arguments: 262284990Scy * zone The zone that is requesting pages 263284990Scy * size The number of bytes being requested 264284990Scy * pflag Flags for these memory pages, see below. 265284990Scy * wait Indicates our willingness to block. 266284990Scy * 267284990Scy * Returns: 268284990Scy * A pointer to the alloced memory or NULL on failure. 269284990Scy */ 270284990Scy 271284990Scytypedef void *(*uma_alloc)(uma_zone_t zone, int size, u_int8_t *pflag, int wait); 272284990Scy 273284990Scy/* 274284990Scy * Backend page free routines 275284990Scy * 276284990Scy * Arguments: 277284990Scy * item A pointer to the previously allocated pages 278284990Scy * size The original size of the allocation 279284990Scy * pflag The flags for the slab. See UMA_SLAB_* below 280284990Scy * 281284990Scy * Returns: 282284990Scy * None 283284990Scy */ 284284990Scytypedef void (*uma_free)(void *item, int size, u_int8_t pflag); 285284990Scy 286284990Scy 287284990Scy 288284990Scy/* 289284990Scy * Sets up the uma allocator. (Called by vm_mem_init) 290284990Scy * 291284990Scy * Arguments: 292284990Scy * bootmem A pointer to memory used to bootstrap the system. 293284990Scy * 294284990Scy * Returns: 295284990Scy * Nothing 296284990Scy * 297284990Scy * Discussion: 298284990Scy * This memory is used for zones which allocate things before the 299284990Scy * backend page supplier can give us pages. It should be 300284990Scy * UMA_SLAB_SIZE * UMA_BOOT_PAGES bytes. (see uma_int.h) 301284990Scy * 302284990Scy */ 303284990Scy 304284990Scyvoid uma_startup(void *bootmem); 305284990Scy 306284990Scy/* 307284990Scy * Finishes starting up the allocator. This should 308284990Scy * be called when kva is ready for normal allocs. 309284990Scy * 310284990Scy * Arguments: 311284990Scy * hash An area of memory that will become the malloc hash 312284990Scy * elems The number of elements in this array 313284990Scy * 314284990Scy * Returns: 315284990Scy * Nothing 316284990Scy * 317284990Scy * Discussion: 318284990Scy * uma_startup2 is called by kmeminit() to prepare the malloc 319284990Scy * hash bucket, and enable use of uma for malloc ops. 320284990Scy */ 321284990Scy 322284990Scyvoid uma_startup2(void *hash, u_long elems); 323284990Scy 324284990Scy/* 325284990Scy * Reclaims unused memory for all zones 326284990Scy * 327284990Scy * Arguments: 328284990Scy * None 329284990Scy * Returns: 330284990Scy * None 331284990Scy * 332284990Scy * This should only be called by the page out daemon. 333284990Scy */ 334284990Scy 335284990Scyvoid uma_reclaim(void); 336284990Scy 337284990Scy/* 338284990Scy * Switches the backing object of a zone 339284990Scy * 340284990Scy * Arguments: 341284990Scy * zone The zone to update 342284990Scy * obj The obj to use for future allocations 343284990Scy * size The size of the object to allocate 344284990Scy * 345284990Scy * Returns: 346284990Scy * 0 if kva space can not be allocated 347284990Scy * 1 if successful 348284990Scy * 349284990Scy * Discussion: 350284990Scy * A NULL object can be used and uma will allocate one for you. Setting 351284990Scy * the size will limit the amount of memory allocated to this zone. 352284990Scy * 353284990Scy */ 354284990Scystruct vm_object; 355284990Scyint uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int size); 356284990Scy 357284990Scy/* 358284990Scy * Sets a high limit on the number of items allowed in a zone 359284990Scy * 360284990Scy * Arguments: 361284990Scy * zone The zone to limit 362284990Scy * 363284990Scy * Returns: 364284990Scy * Nothing 365284990Scy */ 366284990Scyvoid uma_zone_set_max(uma_zone_t zone, int nitems); 367284990Scy 368284990Scy/* 369284990Scy * Replaces the standard page_alloc or obj_alloc functions for this zone 370284990Scy * 371284990Scy * Arguments: 372284990Scy * zone The zone whos back end allocator is being changed. 373284990Scy * allocf A pointer to the allocation function 374284990Scy * 375284990Scy * Returns: 376284990Scy * Nothing 377284990Scy * 378284990Scy * Discussion: 379284990Scy * This could be used to implement pageable allocation, or perhaps 380284990Scy * even DMA allocators if used in conjunction with the OFFPAGE 381284990Scy * zone flag. 382284990Scy */ 383284990Scy 384284990Scyvoid uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf); 385284990Scy 386284990Scy/* 387284990Scy * Used for freeing memory provided by the allocf above 388284990Scy * 389284990Scy * Arguments: 390284990Scy * zone The zone that intends to use this free routine. 391284990Scy * freef The page freeing routine. 392284990Scy * 393284990Scy * Returns: 394284990Scy * Nothing 395284990Scy */ 396284990Scy 397284990Scyvoid uma_zone_set_freef(uma_zone_t zone, uma_free freef); 398284990Scy 399284990Scy/* 400284990Scy * These flags are setable in the allocf and visable in the freef. 401284990Scy */ 402284990Scy#define UMA_SLAB_BOOT 0x01 /* Slab alloced from boot pages */ 403284990Scy#define UMA_SLAB_KMEM 0x02 /* Slab alloced from kmem_map */ 404284990Scy#define UMA_SLAB_KMAP 0x04 /* Slab alloced from kernel_map */ 405284990Scy#define UMA_SLAB_PRIV 0x08 /* Slab alloced from priv allocator */ 406284990Scy#define UMA_SLAB_OFFP 0x10 /* Slab is managed separately */ 407284990Scy#define UMA_SLAB_MALLOC 0x20 /* Slab is a large malloc slab */ 408284990Scy/* 0x40 and 0x80 are available */ 409284990Scy 410284990Scy/* 411284990Scy * Used to pre-fill a zone with some number of items 412284990Scy * 413284990Scy * Arguments: 414284990Scy * zone The zone to fill 415284990Scy * itemcnt The number of items to reserve 416284990Scy * 417284990Scy * Returns: 418284990Scy * Nothing 419284990Scy * 420284990Scy * NOTE: This is blocking and should only be done at startup 421284990Scy */ 422284990Scyvoid uma_prealloc(uma_zone_t zone, int itemcnt); 423284990Scy 424284990Scy 425284990Scy#endif 426284990Scy