xref: /dragonfly/sys/sys/slaballoc.h (revision 2d8a3be7)
1 /*
2  * KERN_SLABALLOC.H	- Kernel SLAB memory allocator
3  *
4  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $DragonFly: src/sys/sys/slaballoc.h,v 1.4 2003/10/02 22:27:00 dillon Exp $
29  */
30 
31 #ifndef _SYS_SLABALLOC_H_
32 #define _SYS_SLABALLOC_H_
33 
34 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
35 
36 #ifndef _SYS_MALLOC_H_
37 #include <sys/malloc.h>
38 #endif
39 
40 /*
41  * Note that any allocations which are exact multiples of PAGE_SIZE, or
42  * which are >= ZALLOC_ZONE_LIMIT, will fall through to the kmem subsystem.
43  */
44 #define ZALLOC_ZONE_LIMIT	(16 * 1024)	/* max slab-managed alloc */
45 #define ZALLOC_MIN_ZONE_SIZE	(32 * 1024)	/* minimum zone size */
46 #define ZALLOC_MAX_ZONE_SIZE	(128 * 1024)	/* maximum zone size */
47 #define ZALLOC_SLAB_MAGIC	0x736c6162	/* magic sanity */
48 #define ZALLOC_OVSZ_MAGIC	0x736c6163	/* magic sanity */
49 #define ZALLOC_SLAB_SLIDE	20
50 
51 
52 #if ZALLOC_ZONE_LIMIT == 16384
53 #define NZONES			72
54 #elif ZALLOC_ZONE_LIMIT == 32768
55 #define NZONES			80
56 #else
57 #error "I couldn't figure out NZONES"
58 #endif
59 
60 /*
61  * Chunk structure for free elements
62  */
63 typedef struct SLChunk {
64     struct SLChunk *c_Next;
65 } SLChunk;
66 
67 /*
68  * The IN-BAND zone header is placed at the beginning of each zone.
69  */
70 typedef struct SLZone {
71     int32_t	z_Magic;	/* magic number for sanity check */
72     int		z_Cpu;		/* which cpu owns this zone? */
73     int		z_NFree;	/* total free chunks / ualloc space in zone */
74     struct SLZone *z_Next;	/* ZoneAry[] link if z_NFree non-zero */
75     int		z_NMax;		/* maximum free chunks */
76     char	*z_BasePtr;	/* pointer to start of chunk array */
77     int		z_UIndex;	/* current initial allocation index */
78     int		z_UEndIndex;	/* last (first) allocation index */
79     int		z_ChunkSize;	/* chunk size for validation */
80     int		z_FirstFreePg;	/* chunk list on a page-by-page basis */
81     int		z_ZoneIndex;
82     int		z_Flags;
83     SLChunk	*z_PageAry[ZALLOC_MAX_ZONE_SIZE / PAGE_SIZE];
84 } SLZone;
85 
86 #define SLZF_UNOTZEROD		0x0001
87 
88 typedef struct SLGlobalData {
89     SLZone	*ZoneAry[NZONES];	/* linked list of zones NFree > 0 */
90     SLZone	*FreeZones;		/* whole zones that have become free */
91     SLZone	*FreeOvZones;		/* oversized zones */
92     int		NFreeZones;		/* free zone count */
93     int		JunkIndex;
94     struct malloc_type ZoneInfo;	/* stats on meta-zones allocated */
95 } SLGlobalData;
96 
97 #endif
98 
99 #ifdef _KERNEL
100 
101 void slab_init(void);
102 void *slab_alloc(struct malloc_type *info, uintptr_t bytes, int flags);
103 void slab_free(void *ptr, struct malloc_type *info);
104 
105 #endif	/* _KERNEL */
106 
107 #endif
108 
109