xref: /dragonfly/sys/sys/slaballoc.h (revision 36a3d1d6)
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 typedef struct SLZone {
82     __int32_t	z_Magic;	/* magic number for sanity check */
83     int		z_Cpu;		/* which cpu owns this zone? */
84     struct globaldata *z_CpuGd;	/* which cpu owns this zone? */
85     int		z_NFree;	/* total free chunks / ualloc space in zone */
86     struct SLZone *z_Next;	/* ZoneAry[] link if z_NFree non-zero */
87     int		z_NMax;		/* maximum free chunks */
88     char	*z_BasePtr;	/* pointer to start of chunk array */
89     int		z_UIndex;	/* current initial allocation index */
90     int		z_UEndIndex;	/* last (first) allocation index */
91     int		z_ChunkSize;	/* chunk size for validation */
92     int		z_FirstFreePg;	/* chunk list on a page-by-page basis */
93     int		z_ZoneIndex;
94     int		z_Flags;
95     SLChunk	*z_PageAry[ZALLOC_MAX_ZONE_SIZE / PAGE_SIZE];
96 #if defined(INVARIANTS)
97     __uint32_t	z_Bitmap[];	/* bitmap of free chunks for sanity check */
98 #endif
99 } SLZone;
100 
101 #define SLZF_UNOTZEROD		0x0001
102 
103 typedef struct SLGlobalData {
104     SLZone	*ZoneAry[NZONES];	/* linked list of zones NFree > 0 */
105     SLZone	*FreeZones;		/* whole zones that have become free */
106     SLZone	*FreeOvZones;		/* oversized zones */
107     int		NFreeZones;		/* free zone count */
108     int		JunkIndex;
109     struct malloc_type ZoneInfo;	/* stats on meta-zones allocated */
110 } SLGlobalData;
111 
112 #endif	/* _KERNEL */
113 
114 #endif	/* _SYS_SLABALLOC_H_ */
115 
116