|
|
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:
59: #if SLAB_USE_CPU_POOLS
60:
61: /*
62: * Per-processor cache of pre-constructed objects.
63: *
64: * The flags member is a read-only CPU-local copy of the parent cache flags.
65: */
66: struct kmem_cpu_pool {
67: simple_lock_data_t lock;
68: int flags;
69: int size;
70: int transfer_size;
71: int nr_objs;
72: void **array;
73: } __attribute__((aligned(CPU_L1_SIZE)));
74:
75: /*
76: * When a cache is created, its CPU pool type is determined from the buffer
77: * size. For small buffer sizes, many objects can be cached in a CPU pool.
78: * Conversely, for large buffer sizes, this would incur much overhead, so only
79: * a few objects are stored in a CPU pool.
80: */
81: struct kmem_cpu_pool_type {
82: size_t buf_size;
83: int array_size;
84: size_t array_align;
85: struct kmem_cache *array_cache;
86: };
87: #endif /* SLAB_USE_CPU_POOLS */
88:
89: /*
90: * Buffer descriptor.
91: *
92: * For normal caches (i.e. without SLAB_CF_VERIFY), bufctls are located at the
93: * end of (but inside) each buffer. If SLAB_CF_VERIFY is set, bufctls are
94: * located after each buffer.
95: *
96: * When an object is allocated to a client, its bufctl isn't used. This memory
97: * is instead used for redzoning if cache debugging is in effect.
98: */
99: union kmem_bufctl {
100: union kmem_bufctl *next;
101: unsigned long redzone;
102: };
103:
104: /*
105: * Buffer tag.
106: *
107: * This structure is only used for SLAB_CF_VERIFY caches. It is located after
108: * the bufctl and includes information about the state of the buffer it
109: * describes (allocated or not). It should be thought of as a debugging
110: * extension of the bufctl.
111: */
112: struct kmem_buftag {
113: unsigned long state;
114: };
115:
116: /*
117: * Page-aligned collection of unconstructed buffers.
118: */
119: struct kmem_slab {
120: struct list list_node;
121: struct rbtree_node tree_node;
122: unsigned long nr_refs;
123: union kmem_bufctl *first_free;
124: void *addr;
125: };
126:
127: /*
128: * Type for constructor functions.
129: *
130: * The pre-constructed state of an object is supposed to include only
131: * elements such as e.g. linked lists, locks, reference counters. Therefore
132: * constructors are expected to 1) never fail and 2) not need any
133: * user-provided data. The first constraint implies that object construction
134: * never performs dynamic resource allocation, which also means there is no
135: * need for destructors.
136: */
137: typedef void (*kmem_cache_ctor_t)(void *obj);
138:
139: /*
1.1.1.2 root 140: * Cache name buffer size. The size is chosen so that struct
141: * kmem_cache fits into two cache lines. The size of a cache line on
142: * a typical CPU is 64 bytes.
1.1 root 143: */
1.1.1.2 root 144: #define KMEM_CACHE_NAME_SIZE 24
1.1 root 145:
146: /*
147: * Cache of objects.
148: *
149: * Locking order : cpu_pool -> cache. CPU pools locking is ordered by CPU ID.
1.1.1.2 root 150: *
151: * Currently, SLAB_USE_CPU_POOLS is not defined. KMEM_CACHE_NAME_SIZE
152: * is chosen so that the struct fits into two cache lines. The first
153: * cache line contains all hot fields.
1.1 root 154: */
155: struct kmem_cache {
156: #if SLAB_USE_CPU_POOLS
157: /* CPU pool layer */
158: struct kmem_cpu_pool cpu_pools[NCPUS];
159: struct kmem_cpu_pool_type *cpu_pool_type;
160: #endif /* SLAB_USE_CPU_POOLS */
161:
162: /* Slab layer */
163: simple_lock_data_t lock;
164: struct list node; /* Cache list linkage */
165: struct list partial_slabs;
166: struct list free_slabs;
167: struct rbtree active_slabs;
168: int flags;
1.1.1.2 root 169: size_t bufctl_dist; /* Distance from buffer to bufctl */
170: size_t slab_size;
171: unsigned long bufs_per_slab;
172: unsigned long nr_objs; /* Number of allocated objects */
173: unsigned long nr_free_slabs;
174: kmem_cache_ctor_t ctor;
175: /* All fields below are cold */
1.1 root 176: size_t obj_size; /* User-provided size */
1.1.1.2 root 177: /* Assuming ! SLAB_USE_CPU_POOLS, here is the cacheline boundary */
1.1 root 178: size_t align;
179: size_t buf_size; /* Aligned object size */
180: size_t color;
181: size_t color_max;
182: unsigned long nr_bufs; /* Total number of buffers */
183: unsigned long nr_slabs;
184: char name[KMEM_CACHE_NAME_SIZE];
185: size_t buftag_dist; /* Distance from buffer to buftag */
186: size_t redzone_pad; /* Bytes from end of object to redzone word */
1.1.1.2 root 187: } __cacheline_aligned;
1.1 root 188:
189: /*
190: * Mach-style declarations for struct kmem_cache.
191: */
192: typedef struct kmem_cache *kmem_cache_t;
193: #define KMEM_CACHE_NULL ((kmem_cache_t) 0)
194:
195: /*
196: * Cache initialization flags.
197: */
1.1.1.4 ! root 198: #define KMEM_CACHE_NOOFFSLAB 0x1 /* Don't allocate external slab data */
! 199: #define KMEM_CACHE_PHYSMEM 0x2 /* Allocate from physical memory */
! 200: #define KMEM_CACHE_VERIFY 0x4 /* Use debugging facilities */
1.1 root 201:
202: /*
203: * Initialize a cache.
204: */
205: void kmem_cache_init(struct kmem_cache *cache, const char *name,
1.1.1.4 ! root 206: size_t obj_size, size_t align,
! 207: kmem_cache_ctor_t ctor, int flags);
1.1 root 208:
209: /*
210: * Allocate an object from a cache.
211: */
212: vm_offset_t kmem_cache_alloc(struct kmem_cache *cache);
213:
214: /*
215: * Release an object to its cache.
216: */
217: void kmem_cache_free(struct kmem_cache *cache, vm_offset_t obj);
218:
219: /*
220: * Initialize the memory allocator module.
221: */
222: void slab_bootstrap(void);
223: void slab_init(void);
224:
225: /*
226: * Release free slabs to the VM system.
227: */
228: void slab_collect(void);
229:
230: /*
231: * Display a summary of all kernel caches.
232: */
233: void slab_info(void);
234:
1.1.1.3 root 235: #if MACH_KDB
236: void db_show_slab_info(void);
237: #endif /* MACH_KDB */
238:
1.1 root 239: #endif /* _KERN_SLAB_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.