uma.h revision 120223
1224110Sjchandra/*
2224110Sjchandra * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
3224110Sjchandra * All rights reserved.
4224110Sjchandra *
5224110Sjchandra * Redistribution and use in source and binary forms, with or without
6224110Sjchandra * modification, are permitted provided that the following conditions
7224110Sjchandra * are met:
8224110Sjchandra * 1. Redistributions of source code must retain the above copyright
9224110Sjchandra *    notice unmodified, this list of conditions, and the following
10224110Sjchandra *    disclaimer.
11224110Sjchandra * 2. Redistributions in binary form must reproduce the above copyright
12224110Sjchandra *    notice, this list of conditions and the following disclaimer in the
13224110Sjchandra *    documentation and/or other materials provided with the distribution.
14224110Sjchandra *
15224110Sjchandra * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16224110Sjchandra * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17224110Sjchandra * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18224110Sjchandra * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19224110Sjchandra * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20224110Sjchandra * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21224110Sjchandra * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22224110Sjchandra * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23224110Sjchandra * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24224110Sjchandra * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25224110Sjchandra *
26224110Sjchandra * $FreeBSD: head/sys/vm/uma.h 120223 2003-09-19 08:37:44Z jeff $
27224110Sjchandra *
28224110Sjchandra */
29224110Sjchandra
30224110Sjchandra/*
31224110Sjchandra * uma.h - External definitions for the Universal Memory Allocator
32224110Sjchandra *
33224110Sjchandra*/
34224110Sjchandra
35224110Sjchandra#ifndef VM_UMA_H
36224110Sjchandra#define VM_UMA_H
37224110Sjchandra
38224110Sjchandra#include <sys/param.h>		/* For NULL */
39224110Sjchandra#include <sys/malloc.h>		/* For M_* */
40224110Sjchandra
41224110Sjchandra/* User visable parameters */
42224110Sjchandra#define UMA_SMALLEST_UNIT       (PAGE_SIZE / 256) /* Smallest item allocated */
43224110Sjchandra
44224110Sjchandra/* Types and type defs */
45224110Sjchandra
46224110Sjchandrastruct uma_zone;
47224110Sjchandra/* Opaque type used as a handle to the zone */
48224110Sjchandratypedef struct uma_zone * uma_zone_t;
49224110Sjchandra
50224110Sjchandra/*
51224110Sjchandra * Item constructor
52224110Sjchandra *
53224110Sjchandra * Arguments:
54224110Sjchandra *	item  A pointer to the memory which has been allocated.
55224110Sjchandra *	arg   The arg field passed to uma_zalloc_arg
56224110Sjchandra *	size  The size of the allocated item
57224110Sjchandra *
58224110Sjchandra * Returns:
59224110Sjchandra *	Nothing
60224110Sjchandra *
61224110Sjchandra * Discussion:
62224110Sjchandra *	The constructor is called just before the memory is returned
63224110Sjchandra *	to the user. It may block if necessary.
64224110Sjchandra */
65224110Sjchandratypedef void (*uma_ctor)(void *mem, int size, void *arg);
66224110Sjchandra
67224110Sjchandra/*
68224110Sjchandra * Item destructor
69224110Sjchandra *
70224110Sjchandra * Arguments:
71224110Sjchandra *	item  A pointer to the memory which has been allocated.
72224110Sjchandra *	size  The size of the item being destructed.
73224110Sjchandra *	arg   Argument passed through uma_zfree_arg
74224110Sjchandra *
75224110Sjchandra * Returns:
76224110Sjchandra *	Nothing
77224110Sjchandra *
78224110Sjchandra * Discussion:
79224110Sjchandra *	The destructor may perform operations that differ from those performed
80224110Sjchandra *	by the initializer, but it must leave the object in the same state.
81224110Sjchandra *	This IS type stable storage.  This is called after EVERY zfree call.
82224110Sjchandra */
83224110Sjchandratypedef void (*uma_dtor)(void *mem, int size, void *arg);
84224110Sjchandra
85224110Sjchandra/*
86224110Sjchandra * Item initializer
87224110Sjchandra *
88224110Sjchandra * Arguments:
89224110Sjchandra *	item  A pointer to the memory which has been allocated.
90224110Sjchandra *	size  The size of the item being initialized.
91224110Sjchandra *
92224110Sjchandra * Returns:
93224110Sjchandra *	Nothing
94224110Sjchandra *
95224110Sjchandra * Discussion:
96224110Sjchandra *	The initializer is called when the memory is cached in the uma zone.
97224110Sjchandra *	this should be the same state that the destructor leaves the object in.
98224110Sjchandra */
99224110Sjchandratypedef void (*uma_init)(void *mem, int size);
100224110Sjchandra
101224110Sjchandra/*
102224110Sjchandra * Item discard function
103224110Sjchandra *
104224110Sjchandra * Arguments:
105224110Sjchandra * 	item  A pointer to memory which has been 'freed' but has not left the
106224110Sjchandra *	      zone's cache.
107224110Sjchandra *	size  The size of the item being discarded.
108224110Sjchandra *
109224110Sjchandra * Returns:
110224110Sjchandra *	Nothing
111224110Sjchandra *
112224110Sjchandra * Discussion:
113224110Sjchandra *	This routine is called when memory leaves a zone and is returned to the
114224110Sjchandra *	system for other uses.  It is the counter part to the init function.
115224110Sjchandra */
116224110Sjchandratypedef void (*uma_fini)(void *mem, int size);
117224110Sjchandra
118224110Sjchandra/*
119224110Sjchandra * What's the difference between initializing and constructing?
120224110Sjchandra *
121224110Sjchandra * The item is initialized when it is cached, and this is the state that the
122224110Sjchandra * object should be in when returned to the allocator. The purpose of this is
123224110Sjchandra * to remove some code which would otherwise be called on each allocation by
124224110Sjchandra * utilizing a known, stable state.  This differs from the constructor which
125224110Sjchandra * will be called on EVERY allocation.
126 *
127 * For example, in the initializer you may want to initialize embeded locks,
128 * NULL list pointers, set up initial states, magic numbers, etc.  This way if
129 * the object is held in the allocator and re-used it won't be necessary to
130 * re-initialize it.
131 *
132 * The constructor may be used to lock a data structure, link it on to lists,
133 * bump reference counts or total counts of outstanding structures, etc.
134 *
135 */
136
137
138/* Function proto types */
139
140/*
141 * Create a new uma zone
142 *
143 * Arguments:
144 *	name  The text name of the zone for debugging and stats, this memory
145 *		should not be freed until the zone has been deallocated.
146 *	size  The size of the object that is being created.
147 *	ctor  The constructor that is called when the object is allocated
148 *	dtor  The destructor that is called when the object is freed.
149 *	init  An initializer that sets up the initial state of the memory.
150 *	fini  A discard function that undoes initialization done by init.
151 *		ctor/dtor/init/fini may all be null, see notes above.
152 *	align A bitmask that corisponds to the requested alignment
153 *		eg 4 would be 0x3
154 *	flags A set of parameters that control the behavior of the zone
155 *
156 * Returns:
157 *	A pointer to a structure which is intended to be opaque to users of
158 *	the interface.  The value may be null if the wait flag is not set.
159 */
160
161uma_zone_t uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
162			uma_init uminit, uma_fini fini, int align,
163			u_int16_t flags);
164
165/*
166 * Definitions for uma_zcreate flags
167 *
168 * These flags share space with UMA_ZFLAGs in uma_int.h.  Be careful not to
169 * overlap when adding new features.  0xf000 is in use by uma_int.h.
170 */
171#define UMA_ZONE_PAGEABLE	0x0001	/* Return items not fully backed by
172					   physical memory XXX Not yet */
173#define UMA_ZONE_ZINIT		0x0002	/* Initialize with zeros */
174#define UMA_ZONE_STATIC		0x0004	/* Staticly sized zone */
175#define UMA_ZONE_OFFPAGE	0x0008	/* Force the slab structure allocation
176					   off of the real memory */
177#define UMA_ZONE_MALLOC		0x0010	/* For use by malloc(9) only! */
178#define UMA_ZONE_NOFREE		0x0020	/* Do not free slabs of this type! */
179#define UMA_ZONE_MTXCLASS	0x0040	/* Create a new lock class */
180#define	UMA_ZONE_VM		0x0080	/*
181					 * Used for internal vm datastructures
182					 * only.
183					 */
184#define	UMA_ZONE_HASH		0x0100	/*
185					 * Use a hash table instead of caching
186					 * information in the vm_page.
187					 */
188
189/* Definitions for align */
190#define UMA_ALIGN_PTR	(sizeof(void *) - 1)	/* Alignment fit for ptr */
191#define UMA_ALIGN_LONG	(sizeof(long) - 1)	/* "" long */
192#define UMA_ALIGN_INT	(sizeof(int) - 1)	/* "" int */
193#define UMA_ALIGN_SHORT	(sizeof(short) - 1)	/* "" short */
194#define UMA_ALIGN_CHAR	(sizeof(char) - 1)	/* "" char */
195#define UMA_ALIGN_CACHE	(16 - 1)		/* Cache line size align */
196
197/*
198 * Destroys an empty uma zone.  If the zone is not empty uma complains loudly.
199 *
200 * Arguments:
201 *	zone  The zone we want to destroy.
202 *
203 */
204
205void uma_zdestroy(uma_zone_t zone);
206
207/*
208 * Allocates an item out of a zone
209 *
210 * Arguments:
211 *	zone  The zone we are allocating from
212 *	arg   This data is passed to the ctor function
213 *	flags See sys/malloc.h for available flags.
214 *
215 * Returns:
216 *	A non null pointer to an initialized element from the zone is
217 *	garanteed if the wait flag is M_WAITOK, otherwise a null pointer may be
218 *	returned if the zone is empty or the ctor failed.
219 */
220
221void *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags);
222
223/*
224 * Allocates an item out of a zone without supplying an argument
225 *
226 * This is just a wrapper for uma_zalloc_arg for convenience.
227 *
228 */
229static __inline void *uma_zalloc(uma_zone_t zone, int flags);
230
231static __inline void *
232uma_zalloc(uma_zone_t zone, int flags)
233{
234	return uma_zalloc_arg(zone, NULL, flags);
235}
236
237/*
238 * Frees an item back into the specified zone.
239 *
240 * Arguments:
241 *	zone  The zone the item was originally allocated out of.
242 *	item  The memory to be freed.
243 *	arg   Argument passed to the destructor
244 *
245 * Returns:
246 *	Nothing.
247 */
248
249void uma_zfree_arg(uma_zone_t zone, void *item, void *arg);
250
251/*
252 * Frees an item back to a zone without supplying an argument
253 *
254 * This is just a wrapper for uma_zfree_arg for convenience.
255 *
256 */
257static __inline void uma_zfree(uma_zone_t zone, void *item);
258
259static __inline void
260uma_zfree(uma_zone_t zone, void *item)
261{
262	uma_zfree_arg(zone, item, NULL);
263}
264
265/*
266 * XXX The rest of the prototypes in this header are h0h0 magic for the VM.
267 * If you think you need to use it for a normal zone you're probably incorrect.
268 */
269
270/*
271 * Backend page supplier routines
272 *
273 * Arguments:
274 *	zone  The zone that is requesting pages
275 *	size  The number of bytes being requested
276 *	pflag Flags for these memory pages, see below.
277 *	wait  Indicates our willingness to block.
278 *
279 * Returns:
280 *	A pointer to the alloced memory or NULL on failure.
281 */
282
283typedef void *(*uma_alloc)(uma_zone_t zone, int size, u_int8_t *pflag, int wait);
284
285/*
286 * Backend page free routines
287 *
288 * Arguments:
289 *	item  A pointer to the previously allocated pages
290 *	size  The original size of the allocation
291 *	pflag The flags for the slab.  See UMA_SLAB_* below
292 *
293 * Returns:
294 *	None
295 */
296typedef void (*uma_free)(void *item, int size, u_int8_t pflag);
297
298
299
300/*
301 * Sets up the uma allocator. (Called by vm_mem_init)
302 *
303 * Arguments:
304 *	bootmem  A pointer to memory used to bootstrap the system.
305 *
306 * Returns:
307 *	Nothing
308 *
309 * Discussion:
310 *	This memory is used for zones which allocate things before the
311 *	backend page supplier can give us pages.  It should be
312 *	UMA_SLAB_SIZE * UMA_BOOT_PAGES bytes. (see uma_int.h)
313 *
314 */
315
316void uma_startup(void *bootmem);
317
318/*
319 * Finishes starting up the allocator.  This should
320 * be called when kva is ready for normal allocs.
321 *
322 * Arguments:
323 *	None
324 *
325 * Returns:
326 *	Nothing
327 *
328 * Discussion:
329 *	uma_startup2 is called by kmeminit() to enable us of uma for malloc.
330 */
331
332void uma_startup2(void);
333
334/*
335 * Reclaims unused memory for all zones
336 *
337 * Arguments:
338 *	None
339 * Returns:
340 *	None
341 *
342 * This should only be called by the page out daemon.
343 */
344
345void uma_reclaim(void);
346
347/*
348 * Switches the backing object of a zone
349 *
350 * Arguments:
351 *	zone  The zone to update
352 *	obj   The obj to use for future allocations
353 *	size  The size of the object to allocate
354 *
355 * Returns:
356 *	0  if kva space can not be allocated
357 *	1  if successful
358 *
359 * Discussion:
360 *	A NULL object can be used and uma will allocate one for you.  Setting
361 *	the size will limit the amount of memory allocated to this zone.
362 *
363 */
364struct vm_object;
365int uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int size);
366
367/*
368 * Sets a high limit on the number of items allowed in a zone
369 *
370 * Arguments:
371 *	zone  The zone to limit
372 *
373 * Returns:
374 *	Nothing
375 */
376void uma_zone_set_max(uma_zone_t zone, int nitems);
377
378/*
379 * Replaces the standard page_alloc or obj_alloc functions for this zone
380 *
381 * Arguments:
382 *	zone   The zone whos back end allocator is being changed.
383 *	allocf A pointer to the allocation function
384 *
385 * Returns:
386 *	Nothing
387 *
388 * Discussion:
389 *	This could be used to implement pageable allocation, or perhaps
390 *	even DMA allocators if used in conjunction with the OFFPAGE
391 *	zone flag.
392 */
393
394void uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf);
395
396/*
397 * Used for freeing memory provided by the allocf above
398 *
399 * Arguments:
400 *	zone  The zone that intends to use this free routine.
401 *	freef The page freeing routine.
402 *
403 * Returns:
404 *	Nothing
405 */
406
407void uma_zone_set_freef(uma_zone_t zone, uma_free freef);
408
409/*
410 * These flags are setable in the allocf and visable in the freef.
411 */
412#define UMA_SLAB_BOOT	0x01		/* Slab alloced from boot pages */
413#define UMA_SLAB_KMEM	0x02		/* Slab alloced from kmem_map */
414#define UMA_SLAB_PRIV	0x08		/* Slab alloced from priv allocator */
415#define UMA_SLAB_OFFP	0x10		/* Slab is managed separately  */
416#define UMA_SLAB_MALLOC	0x20		/* Slab is a large malloc slab */
417/* 0x40 and 0x80 are available */
418
419/*
420 * Used to pre-fill a zone with some number of items
421 *
422 * Arguments:
423 *	zone    The zone to fill
424 *	itemcnt The number of items to reserve
425 *
426 * Returns:
427 *	Nothing
428 *
429 * NOTE: This is blocking and should only be done at startup
430 */
431void uma_prealloc(uma_zone_t zone, int itemcnt);
432
433
434#endif
435