|
|
1.1 root 1: /*
2: * Copyright (c) 2011 Free Software Foundation.
3: *
4: * This program is free software; you can redistribute it and/or modify
5: * it under the terms of the GNU General Public License as published by
6: * the Free Software Foundation; either version 2 of the License, or
7: * (at your option) any later version.
8: *
9: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License along
15: * with this program; if not, write to the Free Software Foundation, Inc.,
16: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17: */
18:
19: /*
20: * Copyright (c) 2010, 2011 Richard Braun.
21: * All rights reserved.
22: *
23: * Redistribution and use in source and binary forms, with or without
24: * modification, are permitted provided that the following conditions
25: * are met:
26: * 1. Redistributions of source code must retain the above copyright
27: * notice, this list of conditions and the following disclaimer.
28: * 2. Redistributions in binary form must reproduce the above copyright
29: * notice, this list of conditions and the following disclaimer in the
30: * documentation and/or other materials provided with the distribution.
31: *
32: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42: *
43: *
44: * Object caching memory allocator.
45: */
46:
47: #ifndef _KERN_SLAB_H
48: #define _KERN_SLAB_H
49:
1.1.1.2 root 50: #include <cache.h>
1.1.1.4 root 51: #include <kern/cpu_number.h>
1.1 root 52: #include <kern/lock.h>
53: #include <kern/list.h>
54: #include <kern/rbtree.h>
55: #include <mach/machine/vm_types.h>
56: #include <sys/types.h>
57: #include <vm/vm_types.h>
58:
1.1.1.5 ! root 59: struct kmem_cache;
! 60:
1.1 root 61: #if SLAB_USE_CPU_POOLS
62:
63: /*
64: * Per-processor cache of pre-constructed objects.
65: *
66: * The flags member is a read-only CPU-local copy of the parent cache flags.
67: */
68: struct kmem_cpu_pool {
69: simple_lock_data_t lock;
70: int flags;
71: int size;
72: int transfer_size;
73: int nr_objs;
74: void **array;
75: } __attribute__((aligned(CPU_L1_SIZE)));
76:
77: /*
78: * When a cache is created, its CPU pool type is determined from the buffer
79: * size. For small buffer sizes, many objects can be cached in a CPU pool.
80: * Conversely, for large buffer sizes, this would incur much overhead, so only
81: * a few objects are stored in a CPU pool.
82: */
83: struct kmem_cpu_pool_type {
84: size_t buf_size;
85: int array_size;
86: size_t array_align;
87: struct kmem_cache *array_cache;
88: };
89: #endif /* SLAB_USE_CPU_POOLS */
90:
91: /*
92: * Buffer descriptor.
93: *
94: * For normal caches (i.e. without SLAB_CF_VERIFY), bufctls are located at the
95: * end of (but inside) each buffer. If SLAB_CF_VERIFY is set, bufctls are
96: * located after each buffer.
97: *
98: * When an object is allocated to a client, its bufctl isn't used. This memory
99: * is instead used for redzoning if cache debugging is in effect.
100: */
101: union kmem_bufctl {
102: union kmem_bufctl *next;
103: unsigned long redzone;
104: };
105:
106: /*
107: * Buffer tag.
108: *
109: * This structure is only used for SLAB_CF_VERIFY caches. It is located after
110: * the bufctl and includes information about the state of the buffer it
111: * describes (allocated or not). It should be thought of as a debugging
112: * extension of the bufctl.
113: */
114: struct kmem_buftag {
115: unsigned long state;
116: };
117:
118: /*
119: * Page-aligned collection of unconstructed buffers.
120: */
121: struct kmem_slab {
1.1.1.5 ! root 122: struct kmem_cache *cache;
1.1 root 123: struct list list_node;
124: struct rbtree_node tree_node;
125: unsigned long nr_refs;
126: union kmem_bufctl *first_free;
127: void *addr;
128: };
129:
130: /*
131: * Type for constructor functions.
132: *
133: * The pre-constructed state of an object is supposed to include only
134: * elements such as e.g. linked lists, locks, reference counters. Therefore
135: * constructors are expected to 1) never fail and 2) not need any
136: * user-provided data. The first constraint implies that object construction
137: * never performs dynamic resource allocation, which also means there is no
138: * need for destructors.
139: */
140: typedef void (*kmem_cache_ctor_t)(void *obj);
141:
142: /*
1.1.1.2 root 143: * Cache name buffer size. The size is chosen so that struct
144: * kmem_cache fits into two cache lines. The size of a cache line on
145: * a typical CPU is 64 bytes.
1.1 root 146: */
1.1.1.2 root 147: #define KMEM_CACHE_NAME_SIZE 24
1.1 root 148:
149: /*
150: * Cache of objects.
151: *
152: * Locking order : cpu_pool -> cache. CPU pools locking is ordered by CPU ID.
1.1.1.2 root 153: *
154: * Currently, SLAB_USE_CPU_POOLS is not defined. KMEM_CACHE_NAME_SIZE
155: * is chosen so that the struct fits into two cache lines. The first
156: * cache line contains all hot fields.
1.1 root 157: */
158: struct kmem_cache {
159: #if SLAB_USE_CPU_POOLS
160: /* CPU pool layer */
161: struct kmem_cpu_pool cpu_pools[NCPUS];
162: struct kmem_cpu_pool_type *cpu_pool_type;
163: #endif /* SLAB_USE_CPU_POOLS */
164:
165: /* Slab layer */
166: simple_lock_data_t lock;
167: struct list node; /* Cache list linkage */
168: struct list partial_slabs;
169: struct list free_slabs;
170: struct rbtree active_slabs;
171: int flags;
1.1.1.2 root 172: size_t bufctl_dist; /* Distance from buffer to bufctl */
173: size_t slab_size;
174: unsigned long bufs_per_slab;
175: unsigned long nr_objs; /* Number of allocated objects */
176: unsigned long nr_free_slabs;
177: kmem_cache_ctor_t ctor;
178: /* All fields below are cold */
1.1 root 179: size_t obj_size; /* User-provided size */
1.1.1.2 root 180: /* Assuming ! SLAB_USE_CPU_POOLS, here is the cacheline boundary */
1.1 root 181: size_t align;
182: size_t buf_size; /* Aligned object size */
183: size_t color;
184: size_t color_max;
185: unsigned long nr_bufs; /* Total number of buffers */
186: unsigned long nr_slabs;
187: char name[KMEM_CACHE_NAME_SIZE];
188: size_t buftag_dist; /* Distance from buffer to buftag */
189: size_t redzone_pad; /* Bytes from end of object to redzone word */
1.1.1.2 root 190: } __cacheline_aligned;
1.1 root 191:
192: /*
193: * Mach-style declarations for struct kmem_cache.
194: */
195: typedef struct kmem_cache *kmem_cache_t;
196: #define KMEM_CACHE_NULL ((kmem_cache_t) 0)
197:
198: /*
199: * Cache initialization flags.
200: */
1.1.1.4 root 201: #define KMEM_CACHE_NOOFFSLAB 0x1 /* Don't allocate external slab data */
202: #define KMEM_CACHE_PHYSMEM 0x2 /* Allocate from physical memory */
203: #define KMEM_CACHE_VERIFY 0x4 /* Use debugging facilities */
1.1 root 204:
205: /*
206: * Initialize a cache.
207: */
208: void kmem_cache_init(struct kmem_cache *cache, const char *name,
1.1.1.4 root 209: size_t obj_size, size_t align,
210: kmem_cache_ctor_t ctor, int flags);
1.1 root 211:
212: /*
213: * Allocate an object from a cache.
214: */
215: vm_offset_t kmem_cache_alloc(struct kmem_cache *cache);
216:
217: /*
218: * Release an object to its cache.
219: */
220: void kmem_cache_free(struct kmem_cache *cache, vm_offset_t obj);
221:
222: /*
223: * Initialize the memory allocator module.
224: */
225: void slab_bootstrap(void);
226: void slab_init(void);
227:
228: /*
229: * Release free slabs to the VM system.
230: */
231: void slab_collect(void);
232:
233: /*
234: * Display a summary of all kernel caches.
235: */
236: void slab_info(void);
237:
1.1.1.3 root 238: #if MACH_KDB
239: void db_show_slab_info(void);
240: #endif /* MACH_KDB */
241:
1.1 root 242: #endif /* _KERN_SLAB_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.