xref: /dragonfly/sys/sys/slaballoc.h (revision bcb3e04d)
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  * $DragonFly: src/sys/sys/slaballoc.h,v 1.8 2005/06/20 20:49:12 dillon Exp $
37  */
38 
39 #ifndef _SYS_SLABALLOC_H_
40 #define _SYS_SLABALLOC_H_
41 
42 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
43 
44 #ifndef _SYS_STDINT_H_
45 #include <sys/stdint.h>
46 #endif
47 #ifndef _SYS_MALLOC_H_
48 #include <sys/malloc.h>
49 #endif
50 
51 /*
52  * Note that any allocations which are exact multiples of PAGE_SIZE, or
53  * which are >= ZALLOC_ZONE_LIMIT, will fall through to the kmem subsystem.
54  */
55 #define ZALLOC_ZONE_LIMIT	(16 * 1024)	/* max slab-managed alloc */
56 #define ZALLOC_MIN_ZONE_SIZE	(32 * 1024)	/* minimum zone size */
57 #define ZALLOC_MAX_ZONE_SIZE	(128 * 1024)	/* maximum zone size */
58 #define ZALLOC_SLAB_MAGIC	0x736c6162	/* magic sanity */
59 #define ZALLOC_OVSZ_MAGIC	0x736c6163	/* magic sanity */
60 #define ZALLOC_SLAB_SLIDE	20
61 
62 
63 #if ZALLOC_ZONE_LIMIT == 16384
64 #define NZONES			72
65 #elif ZALLOC_ZONE_LIMIT == 32768
66 #define NZONES			80
67 #else
68 #error "I couldn't figure out NZONES"
69 #endif
70 
71 /*
72  * Chunk structure for free elements
73  */
74 typedef struct SLChunk {
75     struct SLChunk *c_Next;
76 } SLChunk;
77 
78 /*
79  * The IN-BAND zone header is placed at the beginning of each zone.
80  *
81  * NOTE! All fields are cpu-local except z_RChunks.  Remote cpus free
82  *	 chunks using atomic ops to z_RChunks and then signal local
83  *	 cpus as necessary.
84  */
85 typedef struct SLZone {
86     __int32_t	z_Magic;	/* magic number for sanity check */
87     int		z_Cpu;		/* which cpu owns this zone? */
88     struct globaldata *z_CpuGd;	/* which cpu owns this zone? */
89     struct SLZone *z_Next;	/* ZoneAry[] link if z_NFree non-zero */
90     int		z_NFree;	/* total free chunks / ualloc space in zone */
91     int		z_NMax;		/* maximum free chunks */
92     char	*z_BasePtr;	/* pointer to start of chunk array */
93     int		z_UIndex;	/* current initial allocation index */
94     int		z_UEndIndex;	/* last (first) allocation index */
95     int		z_ChunkSize;	/* chunk size for validation */
96     int		z_ZoneIndex;
97     int		z_Flags;
98     SLChunk	*z_LChunks;	/* linked list of chunks current cpu */
99     SLChunk	**z_LChunksp;	/* tailp */
100     SLChunk	*z_RChunks;	/* linked list of chunks remote cpu */
101     int		z_RSignal;	/* signal interlock */
102     int		z_RCount;	/* prevent local destruction w/inflight ipis */
103 #if defined(INVARIANTS)
104     __uint32_t	z_Bitmap[];	/* bitmap of free chunks for sanity check */
105 #endif
106 } SLZone;
107 
108 #define SLZF_UNOTZEROD		0x0001
109 
110 typedef struct SLGlobalData {
111     SLZone	*ZoneAry[NZONES];	/* linked list of zones NFree > 0 */
112     SLZone	*FreeZones;		/* whole zones that have become free */
113     SLZone	*FreeOvZones;		/* oversized zones */
114     int		NFreeZones;		/* free zone count */
115     int		JunkIndex;
116     struct malloc_type ZoneInfo;	/* stats on meta-zones allocated */
117 } SLGlobalData;
118 
119 #endif	/* _KERNEL */
120 
121 #endif	/* _SYS_SLABALLOC_H_ */
122 
123