FreeBSD/Linux Kernel Cross Reference
sys/vm/uma.h
1 /*
2 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: releng/5.3/sys/vm/uma.h 132987 2004-08-02 00:18:36Z green $
27 *
28 */
29
30 /*
31 * uma.h - External definitions for the Universal Memory Allocator
32 *
33 */
34
35 #ifndef VM_UMA_H
36 #define VM_UMA_H
37
38 #include <sys/param.h> /* For NULL */
39 #include <sys/malloc.h> /* For M_* */
40
41 /* User visable parameters */
42 #define UMA_SMALLEST_UNIT (PAGE_SIZE / 256) /* Smallest item allocated */
43
44 /* Types and type defs */
45
46 struct uma_zone;
47 /* Opaque type used as a handle to the zone */
48 typedef struct uma_zone * uma_zone_t;
49
50 /*
51 * Item constructor
52 *
53 * Arguments:
54 * item A pointer to the memory which has been allocated.
55 * arg The arg field passed to uma_zalloc_arg
56 * size The size of the allocated item
57 * flags See zalloc flags
58 *
59 * Returns:
60 * 0 on success
61 * errno on failure
62 *
63 * Discussion:
64 * The constructor is called just before the memory is returned
65 * to the user. It may block if necessary.
66 */
67 typedef int (*uma_ctor)(void *mem, int size, void *arg, int flags);
68
69 /*
70 * Item destructor
71 *
72 * Arguments:
73 * item A pointer to the memory which has been allocated.
74 * size The size of the item being destructed.
75 * arg Argument passed through uma_zfree_arg
76 *
77 * Returns:
78 * Nothing
79 *
80 * Discussion:
81 * The destructor may perform operations that differ from those performed
82 * by the initializer, but it must leave the object in the same state.
83 * This IS type stable storage. This is called after EVERY zfree call.
84 */
85 typedef void (*uma_dtor)(void *mem, int size, void *arg);
86
87 /*
88 * Item initializer
89 *
90 * Arguments:
91 * item A pointer to the memory which has been allocated.
92 * size The size of the item being initialized.
93 * flags See zalloc flags
94 *
95 * Returns:
96 * 0 on success
97 * errno on failure
98 *
99 * Discussion:
100 * The initializer is called when the memory is cached in the uma zone.
101 * this should be the same state that the destructor leaves the object in.
102 */
103 typedef int (*uma_init)(void *mem, int size, int flags);
104
105 /*
106 * Item discard function
107 *
108 * Arguments:
109 * item A pointer to memory which has been 'freed' but has not left the
110 * zone's cache.
111 * size The size of the item being discarded.
112 *
113 * Returns:
114 * Nothing
115 *
116 * Discussion:
117 * This routine is called when memory leaves a zone and is returned to the
118 * system for other uses. It is the counter part to the init function.
119 */
120 typedef void (*uma_fini)(void *mem, int size);
121
122 /*
123 * What's the difference between initializing and constructing?
124 *
125 * The item is initialized when it is cached, and this is the state that the
126 * object should be in when returned to the allocator. The purpose of this is
127 * to remove some code which would otherwise be called on each allocation by
128 * utilizing a known, stable state. This differs from the constructor which
129 * will be called on EVERY allocation.
130 *
131 * For example, in the initializer you may want to initialize embeded locks,
132 * NULL list pointers, set up initial states, magic numbers, etc. This way if
133 * the object is held in the allocator and re-used it won't be necessary to
134 * re-initialize it.
135 *
136 * The constructor may be used to lock a data structure, link it on to lists,
137 * bump reference counts or total counts of outstanding structures, etc.
138 *
139 */
140
141
142 /* Function proto types */
143
144 /*
145 * Create a new uma zone
146 *
147 * Arguments:
148 * name The text name of the zone for debugging and stats, this memory
149 * should not be freed until the zone has been deallocated.
150 * size The size of the object that is being created.
151 * ctor The constructor that is called when the object is allocated
152 * dtor The destructor that is called when the object is freed.
153 * init An initializer that sets up the initial state of the memory.
154 * fini A discard function that undoes initialization done by init.
155 * ctor/dtor/init/fini may all be null, see notes above.
156 * align A bitmask that corisponds to the requested alignment
157 * eg 4 would be 0x3
158 * flags A set of parameters that control the behavior of the zone
159 *
160 * Returns:
161 * A pointer to a structure which is intended to be opaque to users of
162 * the interface. The value may be null if the wait flag is not set.
163 */
164 uma_zone_t uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
165 uma_init uminit, uma_fini fini, int align,
166 u_int16_t flags);
167
168 /*
169 * Create a secondary uma zone
170 *
171 * Arguments:
172 * name The text name of the zone for debugging and stats, this memory
173 * should not be freed until the zone has been deallocated.
174 * ctor The constructor that is called when the object is allocated
175 * dtor The destructor that is called when the object is freed.
176 * zinit An initializer that sets up the initial state of the memory
177 * as the object passes from the Keg's slab to the Zone's cache.
178 * zfini A discard function that undoes initialization done by init
179 * as the object passes from the Zone's cache to the Keg's slab.
180 *
181 * ctor/dtor/zinit/zfini may all be null, see notes above.
182 * Note that the zinit and zfini specified here are NOT
183 * exactly the same as the init/fini specified to uma_zcreate()
184 * when creating a master zone. These zinit/zfini are called
185 * on the TRANSITION from keg to zone (and vice-versa). Once
186 * these are set, the primary zone may alter its init/fini
187 * (which are called when the object passes from VM to keg)
188 * using uma_zone_set_init/fini()) as well as its own
189 * zinit/zfini (unset by default for master zone) with
190 * uma_zone_set_zinit/zfini() (note subtle 'z' prefix).
191 *
192 * master A reference to this zone's Master Zone (Primary Zone),
193 * which contains the backing Keg for the Secondary Zone
194 * being added.
195 *
196 * Returns:
197 * A pointer to a structure which is intended to be opaque to users of
198 * the interface. The value may be null if the wait flag is not set.
199 */
200 uma_zone_t uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
201 uma_init zinit, uma_fini zfini, uma_zone_t master);
202
203 /*
204 * Definitions for uma_zcreate flags
205 *
206 * These flags share space with UMA_ZFLAGs in uma_int.h. Be careful not to
207 * overlap when adding new features. 0xf000 is in use by uma_int.h.
208 */
209 #define UMA_ZONE_PAGEABLE 0x0001 /* Return items not fully backed by
210 physical memory XXX Not yet */
211 #define UMA_ZONE_ZINIT 0x0002 /* Initialize with zeros */
212 #define UMA_ZONE_STATIC 0x0004 /* Staticly sized zone */
213 #define UMA_ZONE_OFFPAGE 0x0008 /* Force the slab structure allocation
214 off of the real memory */
215 #define UMA_ZONE_MALLOC 0x0010 /* For use by malloc(9) only! */
216 #define UMA_ZONE_NOFREE 0x0020 /* Do not free slabs of this type! */
217 #define UMA_ZONE_MTXCLASS 0x0040 /* Create a new lock class */
218 #define UMA_ZONE_VM 0x0080 /*
219 * Used for internal vm datastructures
220 * only.
221 */
222 #define UMA_ZONE_HASH 0x0100 /*
223 * Use a hash table instead of caching
224 * information in the vm_page.
225 */
226 #define UMA_ZONE_SECONDARY 0x0200 /* Zone is a Secondary Zone */
227 #define UMA_ZONE_REFCNT 0x0400 /* Allocate refcnts in slabs */
228 #define UMA_ZONE_MAXBUCKET 0x0800 /* Use largest buckets */
229
230 /* Definitions for align */
231 #define UMA_ALIGN_PTR (sizeof(void *) - 1) /* Alignment fit for ptr */
232 #define UMA_ALIGN_LONG (sizeof(long) - 1) /* "" long */
233 #define UMA_ALIGN_INT (sizeof(int) - 1) /* "" int */
234 #define UMA_ALIGN_SHORT (sizeof(short) - 1) /* "" short */
235 #define UMA_ALIGN_CHAR (sizeof(char) - 1) /* "" char */
236 #define UMA_ALIGN_CACHE (16 - 1) /* Cache line size align */
237
238 /*
239 * Destroys an empty uma zone. If the zone is not empty uma complains loudly.
240 *
241 * Arguments:
242 * zone The zone we want to destroy.
243 *
244 */
245 void uma_zdestroy(uma_zone_t zone);
246
247 /*
248 * Allocates an item out of a zone
249 *
250 * Arguments:
251 * zone The zone we are allocating from
252 * arg This data is passed to the ctor function
253 * flags See sys/malloc.h for available flags.
254 *
255 * Returns:
256 * A non null pointer to an initialized element from the zone is
257 * garanteed if the wait flag is M_WAITOK, otherwise a null pointer may be
258 * returned if the zone is empty or the ctor failed.
259 */
260
261 void *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags);
262
263 /*
264 * Allocates an item out of a zone without supplying an argument
265 *
266 * This is just a wrapper for uma_zalloc_arg for convenience.
267 *
268 */
269 static __inline void *uma_zalloc(uma_zone_t zone, int flags);
270
271 static __inline void *
272 uma_zalloc(uma_zone_t zone, int flags)
273 {
274 return uma_zalloc_arg(zone, NULL, flags);
275 }
276
277 /*
278 * Frees an item back into the specified zone.
279 *
280 * Arguments:
281 * zone The zone the item was originally allocated out of.
282 * item The memory to be freed.
283 * arg Argument passed to the destructor
284 *
285 * Returns:
286 * Nothing.
287 */
288
289 void uma_zfree_arg(uma_zone_t zone, void *item, void *arg);
290
291 /*
292 * Frees an item back to a zone without supplying an argument
293 *
294 * This is just a wrapper for uma_zfree_arg for convenience.
295 *
296 */
297 static __inline void uma_zfree(uma_zone_t zone, void *item);
298
299 static __inline void
300 uma_zfree(uma_zone_t zone, void *item)
301 {
302 uma_zfree_arg(zone, item, NULL);
303 }
304
305 /*
306 * XXX The rest of the prototypes in this header are h0h0 magic for the VM.
307 * If you think you need to use it for a normal zone you're probably incorrect.
308 */
309
310 /*
311 * Backend page supplier routines
312 *
313 * Arguments:
314 * zone The zone that is requesting pages
315 * size The number of bytes being requested
316 * pflag Flags for these memory pages, see below.
317 * wait Indicates our willingness to block.
318 *
319 * Returns:
320 * A pointer to the alloced memory or NULL on failure.
321 */
322
323 typedef void *(*uma_alloc)(uma_zone_t zone, int size, u_int8_t *pflag, int wait);
324
325 /*
326 * Backend page free routines
327 *
328 * Arguments:
329 * item A pointer to the previously allocated pages
330 * size The original size of the allocation
331 * pflag The flags for the slab. See UMA_SLAB_* below
332 *
333 * Returns:
334 * None
335 */
336 typedef void (*uma_free)(void *item, int size, u_int8_t pflag);
337
338
339
340 /*
341 * Sets up the uma allocator. (Called by vm_mem_init)
342 *
343 * Arguments:
344 * bootmem A pointer to memory used to bootstrap the system.
345 *
346 * Returns:
347 * Nothing
348 *
349 * Discussion:
350 * This memory is used for zones which allocate things before the
351 * backend page supplier can give us pages. It should be
352 * UMA_SLAB_SIZE * UMA_BOOT_PAGES bytes. (see uma_int.h)
353 *
354 */
355
356 void uma_startup(void *bootmem);
357
358 /*
359 * Finishes starting up the allocator. This should
360 * be called when kva is ready for normal allocs.
361 *
362 * Arguments:
363 * None
364 *
365 * Returns:
366 * Nothing
367 *
368 * Discussion:
369 * uma_startup2 is called by kmeminit() to enable us of uma for malloc.
370 */
371
372 void uma_startup2(void);
373
374 /*
375 * Reclaims unused memory for all zones
376 *
377 * Arguments:
378 * None
379 * Returns:
380 * None
381 *
382 * This should only be called by the page out daemon.
383 */
384
385 void uma_reclaim(void);
386
387 /*
388 * Switches the backing object of a zone
389 *
390 * Arguments:
391 * zone The zone to update
392 * obj The obj to use for future allocations
393 * size The size of the object to allocate
394 *
395 * Returns:
396 * 0 if kva space can not be allocated
397 * 1 if successful
398 *
399 * Discussion:
400 * A NULL object can be used and uma will allocate one for you. Setting
401 * the size will limit the amount of memory allocated to this zone.
402 *
403 */
404 struct vm_object;
405 int uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int size);
406
407 /*
408 * Sets a high limit on the number of items allowed in a zone
409 *
410 * Arguments:
411 * zone The zone to limit
412 *
413 * Returns:
414 * Nothing
415 */
416 void uma_zone_set_max(uma_zone_t zone, int nitems);
417
418 /*
419 * The following two routines (uma_zone_set_init/fini)
420 * are used to set the backend init/fini pair which acts on an
421 * object as it becomes allocated and is placed in a slab within
422 * the specified zone's backing keg. These should probably not
423 * be changed once allocations have already begun and only
424 * immediately upon zone creation.
425 */
426 void uma_zone_set_init(uma_zone_t zone, uma_init uminit);
427 void uma_zone_set_fini(uma_zone_t zone, uma_fini fini);
428
429 /*
430 * The following two routines (uma_zone_set_zinit/zfini) are
431 * used to set the zinit/zfini pair which acts on an object as
432 * it passes from the backing Keg's slab cache to the
433 * specified Zone's bucket cache. These should probably not
434 * be changed once allocations have already begun and
435 * only immediately upon zone creation.
436 */
437 void uma_zone_set_zinit(uma_zone_t zone, uma_init zinit);
438 void uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini);
439
440 /*
441 * Replaces the standard page_alloc or obj_alloc functions for this zone
442 *
443 * Arguments:
444 * zone The zone whos back end allocator is being changed.
445 * allocf A pointer to the allocation function
446 *
447 * Returns:
448 * Nothing
449 *
450 * Discussion:
451 * This could be used to implement pageable allocation, or perhaps
452 * even DMA allocators if used in conjunction with the OFFPAGE
453 * zone flag.
454 */
455
456 void uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf);
457
458 /*
459 * Used for freeing memory provided by the allocf above
460 *
461 * Arguments:
462 * zone The zone that intends to use this free routine.
463 * freef The page freeing routine.
464 *
465 * Returns:
466 * Nothing
467 */
468
469 void uma_zone_set_freef(uma_zone_t zone, uma_free freef);
470
471 /*
472 * These flags are setable in the allocf and visable in the freef.
473 */
474 #define UMA_SLAB_BOOT 0x01 /* Slab alloced from boot pages */
475 #define UMA_SLAB_KMEM 0x02 /* Slab alloced from kmem_map */
476 #define UMA_SLAB_PRIV 0x08 /* Slab alloced from priv allocator */
477 #define UMA_SLAB_OFFP 0x10 /* Slab is managed separately */
478 #define UMA_SLAB_MALLOC 0x20 /* Slab is a large malloc slab */
479 /* 0x40 and 0x80 are available */
480
481 /*
482 * Used to pre-fill a zone with some number of items
483 *
484 * Arguments:
485 * zone The zone to fill
486 * itemcnt The number of items to reserve
487 *
488 * Returns:
489 * Nothing
490 *
491 * NOTE: This is blocking and should only be done at startup
492 */
493 void uma_prealloc(uma_zone_t zone, int itemcnt);
494
495 /*
496 * Used to lookup the reference counter allocated for an item
497 * from a UMA_ZONE_REFCNT zone. For UMA_ZONE_REFCNT zones,
498 * reference counters are allocated for items and stored in
499 * the underlying slab header.
500 *
501 * Arguments:
502 * zone The UMA_ZONE_REFCNT zone to which the item belongs.
503 * item The address of the item for which we want a refcnt.
504 *
505 * Returns:
506 * A pointer to a u_int32_t reference counter.
507 */
508 u_int32_t *uma_find_refcnt(uma_zone_t zone, void *item);
509
510 #endif
Cache object: 708dd3f8d17cc0ac1afa445438bb1cbc
|