2 * KERN_SLABALLOC.H - Kernel SLAB memory allocator
4 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #ifndef _SYS_SLABALLOC_H_
38 #define _SYS_SLABALLOC_H_
40 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
42 #ifndef _SYS__MALLOC_H_
43 #include <sys/_malloc.h>
45 #include <machine/stdint.h>
48 * Note that any allocations which are exact multiples of PAGE_SIZE, or
49 * which are >= ZALLOC_ZONE_LIMIT, will fall through to the kmem subsystem.
51 #define ZALLOC_ZONE_LIMIT (16 * 1024) /* max slab-managed alloc */
52 #define ZALLOC_MIN_ZONE_SIZE (32 * 1024) /* minimum zone size */
53 #define ZALLOC_MAX_ZONE_SIZE (128 * 1024) /* maximum zone size */
54 #define ZALLOC_SLAB_MAGIC 0x736c6162 /* magic sanity */
55 #define ZALLOC_OVSZ_MAGIC 0x736c6163 /* magic sanity */
56 #define ZALLOC_SLAB_SLIDE 20
59 #if ZALLOC_ZONE_LIMIT == 16384
61 #elif ZALLOC_ZONE_LIMIT == 32768
64 #error "I couldn't figure out NZONES"
68 * Chunk structure for free elements
70 typedef struct SLChunk {
71 struct SLChunk *c_Next;
74 #if defined(SLAB_DEBUG)
76 * Only used for kernels compiled w/SLAB_DEBUG
86 * The IN-BAND zone header is placed at the beginning of each zone.
88 * NOTE! All fields are cpu-local except z_RChunks. Remote cpus free
89 * chunks using atomic ops to z_RChunks and then signal local
92 typedef struct SLZone {
93 __int32_t z_Magic; /* magic number for sanity check */
94 int z_Cpu; /* which cpu owns this zone? */
95 struct globaldata *z_CpuGd; /* which cpu owns this zone? */
96 TAILQ_ENTRY(SLZone) z_Entry;/* ZoneAry[] if z_NFree!=0, else Free*Zones */
98 int z_NFree; /* total free chunks / ualloc space in zone */
99 int z_NMax; /* maximum free chunks */
100 char *z_BasePtr; /* pointer to start of chunk array */
101 int z_UIndex; /* current initial allocation index */
102 int z_UEndIndex; /* last (first) allocation index */
103 int z_ChunkSize; /* chunk size for validation */
106 SLChunk *z_LChunks; /* linked list of chunks current cpu */
107 SLChunk **z_LChunksp; /* tailp */
108 SLChunk *z_RChunks; /* linked list of chunks remote cpu */
109 int z_RSignal; /* signal interlock */
110 int z_RCount; /* prevent local destruction w/inflight ipis */
111 #if defined(SLAB_DEBUG)
112 #define SLAB_DEBUG_ENTRIES 32 /* must be power of 2 */
113 struct ZSources z_Sources[SLAB_DEBUG_ENTRIES];
114 struct ZSources z_AltSources[SLAB_DEBUG_ENTRIES];
116 #if defined(INVARIANTS)
117 __uint32_t z_Bitmap[]; /* bitmap of free chunks for sanity check */
121 #define SLZF_UNOTZEROD 0x0001
123 TAILQ_HEAD(SLZoneList, SLZone);
124 typedef struct SLZoneList SLZoneList;
126 typedef struct SLGlobalData {
127 SLZoneList ZoneAry[NZONES]; /* linked list of zones NFree > 0 */
128 SLZoneList FreeZones; /* whole zones that have become free */
129 SLZoneList FreeOvZones; /* oversized zones */
130 int NFreeZones; /* free zone count */
132 struct malloc_type ZoneInfo; /* stats on meta-zones allocated */
137 #endif /* _SYS_SLABALLOC_H_ */