xref: /dragonfly/sys/sys/slaballoc.h (revision 9d4f17d13bf5d8bcd1e924ee709578e6f59bc958)
1 /*
2  * KERN_SLABALLOC.H - Kernel SLAB memory allocator
3  *
4  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
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
18  *    distribution.
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.
22  *
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
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef _SYS_SLABALLOC_H_
38 #define _SYS_SLABALLOC_H_
39 
40 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
41 
42 #ifndef _SYS__MALLOC_H_
43 #include <sys/_malloc.h>
44 #endif
45 #include <machine/stdint.h>
46 
47 /*
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.
50  */
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
57 
58 
59 #if ZALLOC_ZONE_LIMIT == 16384
60 #define NZONES                          72
61 #elif ZALLOC_ZONE_LIMIT == 32768
62 #define NZONES                          80
63 #else
64 #error "I couldn't figure out NZONES"
65 #endif
66 
67 /*
68  * Chunk structure for free elements
69  */
70 typedef struct SLChunk {
71     struct SLChunk *c_Next;
72 } SLChunk;
73 
74 #if defined(SLAB_DEBUG)
75 /*
76  * Only used for kernels compiled w/SLAB_DEBUG
77  */
78 struct ZSources {
79     const char *file;
80     int line;
81 };
82 
83 #endif
84 
85 /*
86  * The IN-BAND zone header is placed at the beginning of each zone.
87  *
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
90  *         cpus as necessary.
91  */
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 */
97     void  *z_UNused01;
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 */
104     int             z_ZoneIndex;
105     int             z_Flags;
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];
115 #endif
116 #if defined(INVARIANTS)
117     __uint32_t      z_Bitmap[];         /* bitmap of free chunks for sanity check */
118 #endif
119 } SLZone;
120 
121 #define SLZF_UNOTZEROD                  0x0001
122 
123 TAILQ_HEAD(SLZoneList, SLZone);
124 typedef struct SLZoneList SLZoneList;
125 
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 */
131     int             JunkIndex;
132     struct malloc_type ZoneInfo;        /* stats on meta-zones allocated */
133 } SLGlobalData;
134 
135 #endif    /* _KERNEL */
136 
137 #endif    /* _SYS_SLABALLOC_H_ */
138 
139