More minor IPI work.
[dragonfly/vkernel-mp.git] / lib / libcaps / slaballoc.h
blobc5008b85a8df15bc3b4c65be428099a03d58c745
1 /*
2 * SLABALLOC.H - Userland SLAB memory allocator
4 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5 * All rights reserved.
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.
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.
28 * $DragonFly: src/lib/libcaps/slaballoc.h,v 1.3 2004/03/06 20:48:22 dillon Exp $
31 #ifndef _LIBCAPS_SLABALLOC_H_
32 #define _LIBCAPS_SLABALLOC_H_
34 #ifndef _SYS_STDINT_H_
35 #include <sys/stdint.h>
36 #endif
37 #ifndef _SYS_MALLOC_H_
38 #include <sys/malloc.h>
39 #endif
42 * Note that any allocations which are exact multiples of PAGE_SIZE, or
43 * which are >= ZALLOC_ZONE_LIMIT, will fall through to the kmem subsystem.
45 #define ZALLOC_ZONE_LIMIT (16 * 1024) /* max slab-managed alloc */
46 #define ZALLOC_MIN_ZONE_SIZE (32 * 1024) /* minimum zone size */
47 #define ZALLOC_MAX_ZONE_SIZE (128 * 1024) /* maximum zone size */
48 #define ZALLOC_SLAB_MAGIC 0x736c6162 /* magic sanity */
49 #define ZALLOC_OVSZ_MAGIC 0x736c6163 /* magic sanity */
50 #define ZALLOC_SLAB_SLIDE 20
53 #if ZALLOC_ZONE_LIMIT == 16384
54 #define NZONES 72
55 #elif ZALLOC_ZONE_LIMIT == 32768
56 #define NZONES 80
57 #else
58 #error "I couldn't figure out NZONES"
59 #endif
62 * Chunk structure for free elements
64 typedef struct SLChunk {
65 struct SLChunk *c_Next;
66 } SLChunk;
69 * The IN-BAND zone header is placed at the beginning of each zone.
71 typedef struct SLZone {
72 __int32_t z_Magic; /* magic number for sanity check */
73 int z_Cpu; /* which cpu owns this zone? */
74 struct globaldata *z_CpuGd;
75 int z_NFree; /* total free chunks / ualloc space in zone */
76 struct SLZone *z_Next; /* ZoneAry[] link if z_NFree non-zero */
77 int z_NMax; /* maximum free chunks */
78 char *z_BasePtr; /* pointer to start of chunk array */
79 int z_UIndex; /* current initial allocation index */
80 int z_UEndIndex; /* last (first) allocation index */
81 int z_ChunkSize; /* chunk size for validation */
82 int z_FirstFreePg; /* chunk list on a page-by-page basis */
83 int z_ZoneIndex;
84 int z_Flags;
85 SLChunk *z_PageAry[ZALLOC_MAX_ZONE_SIZE / PAGE_SIZE];
86 } SLZone;
88 #define SLZF_UNOTZEROD 0x0001
90 typedef struct SLGlobalData {
91 SLZone *ZoneAry[NZONES]; /* linked list of zones NFree > 0 */
92 SLZone *FreeZones; /* whole zones that have become free */
93 SLZone *FreeOvZones; /* oversized zones */
94 int NFreeZones; /* free zone count */
95 int JunkIndex;
96 struct malloc_type ZoneInfo; /* stats on meta-zones allocated */
97 } SLGlobalData;
99 typedef struct SLOversized {
100 struct SLOversized *ov_Next;
101 void *ov_Ptr;
102 __uintptr_t ov_Bytes;
103 } SLOversized;
105 void slab_init(void);
106 void slab_malloc_init(void *data);
107 void *slab_malloc(unsigned long size, struct malloc_type *type, int flags);
108 void slab_free(void *ptr, struct malloc_type *info);
110 #endif