xref: /freebsd/sys/vm/uma.h (revision 08ecce74bc66514e44ec704a0e1cae97afbc3ce2)
160727d8bSWarner Losh /*-
208ecce74SRobert Watson  * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org>
308ecce74SRobert Watson  * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
408ecce74SRobert Watson  * All rights reserved.
58355f576SJeff Roberson  *
68355f576SJeff Roberson  * Redistribution and use in source and binary forms, with or without
78355f576SJeff Roberson  * modification, are permitted provided that the following conditions
88355f576SJeff Roberson  * are met:
98355f576SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
108355f576SJeff Roberson  *    notice unmodified, this list of conditions, and the following
118355f576SJeff Roberson  *    disclaimer.
128355f576SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
138355f576SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
148355f576SJeff Roberson  *    documentation and/or other materials provided with the distribution.
158355f576SJeff Roberson  *
168355f576SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
178355f576SJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
188355f576SJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198355f576SJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
208355f576SJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
218355f576SJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
228355f576SJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
238355f576SJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248355f576SJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
258355f576SJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268355f576SJeff Roberson  *
278355f576SJeff Roberson  * $FreeBSD$
288355f576SJeff Roberson  *
298355f576SJeff Roberson  */
308355f576SJeff Roberson 
318355f576SJeff Roberson /*
328355f576SJeff Roberson  * uma.h - External definitions for the Universal Memory Allocator
338355f576SJeff Roberson  *
348355f576SJeff Roberson */
358355f576SJeff Roberson 
368355f576SJeff Roberson #ifndef VM_UMA_H
378355f576SJeff Roberson #define VM_UMA_H
388355f576SJeff Roberson 
398355f576SJeff Roberson #include <sys/param.h>		/* For NULL */
408355f576SJeff Roberson #include <sys/malloc.h>		/* For M_* */
418355f576SJeff Roberson 
428355f576SJeff Roberson /* User visable parameters */
438355f576SJeff Roberson #define UMA_SMALLEST_UNIT       (PAGE_SIZE / 256) /* Smallest item allocated */
448355f576SJeff Roberson 
458355f576SJeff Roberson /* Types and type defs */
468355f576SJeff Roberson 
478355f576SJeff Roberson struct uma_zone;
488355f576SJeff Roberson /* Opaque type used as a handle to the zone */
498355f576SJeff Roberson typedef struct uma_zone * uma_zone_t;
508355f576SJeff Roberson 
518355f576SJeff Roberson /*
528355f576SJeff Roberson  * Item constructor
538355f576SJeff Roberson  *
548355f576SJeff Roberson  * Arguments:
558355f576SJeff Roberson  *	item  A pointer to the memory which has been allocated.
568355f576SJeff Roberson  *	arg   The arg field passed to uma_zalloc_arg
578355f576SJeff Roberson  *	size  The size of the allocated item
58b23f72e9SBrian Feldman  *	flags See zalloc flags
598355f576SJeff Roberson  *
608355f576SJeff Roberson  * Returns:
61b23f72e9SBrian Feldman  *	0      on success
62b23f72e9SBrian Feldman  *      errno  on failure
638355f576SJeff Roberson  *
648355f576SJeff Roberson  * Discussion:
658355f576SJeff Roberson  *	The constructor is called just before the memory is returned
6629b4d526SSheldon Hearn  *	to the user. It may block if necessary.
678355f576SJeff Roberson  */
68b23f72e9SBrian Feldman typedef int (*uma_ctor)(void *mem, int size, void *arg, int flags);
698355f576SJeff Roberson 
708355f576SJeff Roberson /*
718355f576SJeff Roberson  * Item destructor
728355f576SJeff Roberson  *
738355f576SJeff Roberson  * Arguments:
748355f576SJeff Roberson  *	item  A pointer to the memory which has been allocated.
758355f576SJeff Roberson  *	size  The size of the item being destructed.
768355f576SJeff Roberson  *	arg   Argument passed through uma_zfree_arg
778355f576SJeff Roberson  *
788355f576SJeff Roberson  * Returns:
798355f576SJeff Roberson  *	Nothing
808355f576SJeff Roberson  *
818355f576SJeff Roberson  * Discussion:
828355f576SJeff Roberson  *	The destructor may perform operations that differ from those performed
838355f576SJeff Roberson  *	by the initializer, but it must leave the object in the same state.
848355f576SJeff Roberson  *	This IS type stable storage.  This is called after EVERY zfree call.
858355f576SJeff Roberson  */
868355f576SJeff Roberson typedef void (*uma_dtor)(void *mem, int size, void *arg);
878355f576SJeff Roberson 
888355f576SJeff Roberson /*
898355f576SJeff Roberson  * Item initializer
908355f576SJeff Roberson  *
918355f576SJeff Roberson  * Arguments:
928355f576SJeff Roberson  *	item  A pointer to the memory which has been allocated.
938355f576SJeff Roberson  *	size  The size of the item being initialized.
94b23f72e9SBrian Feldman  *	flags See zalloc flags
958355f576SJeff Roberson  *
968355f576SJeff Roberson  * Returns:
97b23f72e9SBrian Feldman  *	0      on success
98b23f72e9SBrian Feldman  *      errno  on failure
998355f576SJeff Roberson  *
1008355f576SJeff Roberson  * Discussion:
1018355f576SJeff Roberson  *	The initializer is called when the memory is cached in the uma zone.
1028355f576SJeff Roberson  *	this should be the same state that the destructor leaves the object in.
1038355f576SJeff Roberson  */
104b23f72e9SBrian Feldman typedef int (*uma_init)(void *mem, int size, int flags);
1058355f576SJeff Roberson 
1068355f576SJeff Roberson /*
1078355f576SJeff Roberson  * Item discard function
1088355f576SJeff Roberson  *
1098355f576SJeff Roberson  * Arguments:
1108355f576SJeff Roberson  * 	item  A pointer to memory which has been 'freed' but has not left the
1118355f576SJeff Roberson  *	      zone's cache.
1128355f576SJeff Roberson  *	size  The size of the item being discarded.
1138355f576SJeff Roberson  *
1148355f576SJeff Roberson  * Returns:
1158355f576SJeff Roberson  *	Nothing
1168355f576SJeff Roberson  *
1178355f576SJeff Roberson  * Discussion:
1188355f576SJeff Roberson  *	This routine is called when memory leaves a zone and is returned to the
1198355f576SJeff Roberson  *	system for other uses.  It is the counter part to the init function.
1208355f576SJeff Roberson  */
1218355f576SJeff Roberson typedef void (*uma_fini)(void *mem, int size);
1228355f576SJeff Roberson 
1238355f576SJeff Roberson /*
1248355f576SJeff Roberson  * What's the difference between initializing and constructing?
1258355f576SJeff Roberson  *
1268355f576SJeff Roberson  * The item is initialized when it is cached, and this is the state that the
1278355f576SJeff Roberson  * object should be in when returned to the allocator. The purpose of this is
1288355f576SJeff Roberson  * to remove some code which would otherwise be called on each allocation by
1298355f576SJeff Roberson  * utilizing a known, stable state.  This differs from the constructor which
1308355f576SJeff Roberson  * will be called on EVERY allocation.
1318355f576SJeff Roberson  *
1328355f576SJeff Roberson  * For example, in the initializer you may want to initialize embeded locks,
1338355f576SJeff Roberson  * NULL list pointers, set up initial states, magic numbers, etc.  This way if
13429b4d526SSheldon Hearn  * the object is held in the allocator and re-used it won't be necessary to
1358355f576SJeff Roberson  * re-initialize it.
1368355f576SJeff Roberson  *
1378355f576SJeff Roberson  * The constructor may be used to lock a data structure, link it on to lists,
1388355f576SJeff Roberson  * bump reference counts or total counts of outstanding structures, etc.
1398355f576SJeff Roberson  *
1408355f576SJeff Roberson  */
1418355f576SJeff Roberson 
1428355f576SJeff Roberson 
1438355f576SJeff Roberson /* Function proto types */
1448355f576SJeff Roberson 
1458355f576SJeff Roberson /*
1468355f576SJeff Roberson  * Create a new uma zone
1478355f576SJeff Roberson  *
1488355f576SJeff Roberson  * Arguments:
1498355f576SJeff Roberson  *	name  The text name of the zone for debugging and stats, this memory
1508355f576SJeff Roberson  *		should not be freed until the zone has been deallocated.
1518355f576SJeff Roberson  *	size  The size of the object that is being created.
1528355f576SJeff Roberson  *	ctor  The constructor that is called when the object is allocated
1538355f576SJeff Roberson  *	dtor  The destructor that is called when the object is freed.
1548355f576SJeff Roberson  *	init  An initializer that sets up the initial state of the memory.
1558355f576SJeff Roberson  *	fini  A discard function that undoes initialization done by init.
1568355f576SJeff Roberson  *		ctor/dtor/init/fini may all be null, see notes above.
1578355f576SJeff Roberson  *	align A bitmask that corisponds to the requested alignment
1588355f576SJeff Roberson  *		eg 4 would be 0x3
1598355f576SJeff Roberson  *	flags A set of parameters that control the behavior of the zone
1608355f576SJeff Roberson  *
1618355f576SJeff Roberson  * Returns:
1628355f576SJeff Roberson  *	A pointer to a structure which is intended to be opaque to users of
1638355f576SJeff Roberson  *	the interface.  The value may be null if the wait flag is not set.
1648355f576SJeff Roberson  */
165c3bdc05fSAndrew R. Reiter uma_zone_t uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1668355f576SJeff Roberson 			uma_init uminit, uma_fini fini, int align,
1672018f30cSMike Silbersack 			u_int32_t flags);
1688355f576SJeff Roberson 
169b60f5b79SJeff Roberson /*
170099a0e58SBosko Milekic  * Create a secondary uma zone
171099a0e58SBosko Milekic  *
172099a0e58SBosko Milekic  * Arguments:
173099a0e58SBosko Milekic  *	name  The text name of the zone for debugging and stats, this memory
174099a0e58SBosko Milekic  *		should not be freed until the zone has been deallocated.
175099a0e58SBosko Milekic  *	ctor  The constructor that is called when the object is allocated
176099a0e58SBosko Milekic  *	dtor  The destructor that is called when the object is freed.
177099a0e58SBosko Milekic  *	zinit  An initializer that sets up the initial state of the memory
178099a0e58SBosko Milekic  *		as the object passes from the Keg's slab to the Zone's cache.
179099a0e58SBosko Milekic  *	zfini  A discard function that undoes initialization done by init
180099a0e58SBosko Milekic  *		as the object passes from the Zone's cache to the Keg's slab.
181099a0e58SBosko Milekic  *
182099a0e58SBosko Milekic  *		ctor/dtor/zinit/zfini may all be null, see notes above.
183099a0e58SBosko Milekic  *		Note that the zinit and zfini specified here are NOT
184099a0e58SBosko Milekic  *		exactly the same as the init/fini specified to uma_zcreate()
185099a0e58SBosko Milekic  *		when creating a master zone.  These zinit/zfini are called
186099a0e58SBosko Milekic  *		on the TRANSITION from keg to zone (and vice-versa). Once
187099a0e58SBosko Milekic  *		these are set, the primary zone may alter its init/fini
188099a0e58SBosko Milekic  *		(which are called when the object passes from VM to keg)
189099a0e58SBosko Milekic  *		using uma_zone_set_init/fini()) as well as its own
190099a0e58SBosko Milekic  *		zinit/zfini (unset by default for master zone) with
191099a0e58SBosko Milekic  *		uma_zone_set_zinit/zfini() (note subtle 'z' prefix).
192099a0e58SBosko Milekic  *
193b83e441bSBosko Milekic  *	master  A reference to this zone's Master Zone (Primary Zone),
194b83e441bSBosko Milekic  *		which contains the backing Keg for the Secondary Zone
195b83e441bSBosko Milekic  *		being added.
196099a0e58SBosko Milekic  *
197099a0e58SBosko Milekic  * Returns:
198099a0e58SBosko Milekic  *	A pointer to a structure which is intended to be opaque to users of
199099a0e58SBosko Milekic  *	the interface.  The value may be null if the wait flag is not set.
200099a0e58SBosko Milekic  */
201099a0e58SBosko Milekic uma_zone_t uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
202099a0e58SBosko Milekic 		    uma_init zinit, uma_fini zfini, uma_zone_t master);
203099a0e58SBosko Milekic 
204099a0e58SBosko Milekic /*
205b60f5b79SJeff Roberson  * Definitions for uma_zcreate flags
206b60f5b79SJeff Roberson  *
207b60f5b79SJeff Roberson  * These flags share space with UMA_ZFLAGs in uma_int.h.  Be careful not to
2082018f30cSMike Silbersack  * overlap when adding new features.  0xf0000000 is in use by uma_int.h.
209b60f5b79SJeff Roberson  */
2108355f576SJeff Roberson #define UMA_ZONE_PAGEABLE	0x0001	/* Return items not fully backed by
2118355f576SJeff Roberson 					   physical memory XXX Not yet */
2128355f576SJeff Roberson #define UMA_ZONE_ZINIT		0x0002	/* Initialize with zeros */
2138355f576SJeff Roberson #define UMA_ZONE_STATIC		0x0004	/* Staticly sized zone */
2148355f576SJeff Roberson #define UMA_ZONE_OFFPAGE	0x0008	/* Force the slab structure allocation
2158355f576SJeff Roberson 					   off of the real memory */
2168355f576SJeff Roberson #define UMA_ZONE_MALLOC		0x0010	/* For use by malloc(9) only! */
2178355f576SJeff Roberson #define UMA_ZONE_NOFREE		0x0020	/* Do not free slabs of this type! */
21828bc4419SJeff Roberson #define UMA_ZONE_MTXCLASS	0x0040	/* Create a new lock class */
21999571dc3SJeff Roberson #define	UMA_ZONE_VM		0x0080	/*
22099571dc3SJeff Roberson 					 * Used for internal vm datastructures
22199571dc3SJeff Roberson 					 * only.
22299571dc3SJeff Roberson 					 */
22399571dc3SJeff Roberson #define	UMA_ZONE_HASH		0x0100	/*
22499571dc3SJeff Roberson 					 * Use a hash table instead of caching
22599571dc3SJeff Roberson 					 * information in the vm_page.
22699571dc3SJeff Roberson 					 */
227099a0e58SBosko Milekic #define	UMA_ZONE_SECONDARY	0x0200	/* Zone is a Secondary Zone */
228099a0e58SBosko Milekic #define	UMA_ZONE_REFCNT		0x0400	/* Allocate refcnts in slabs */
229099a0e58SBosko Milekic #define	UMA_ZONE_MAXBUCKET	0x0800	/* Use largest buckets */
2308355f576SJeff Roberson 
2318355f576SJeff Roberson /* Definitions for align */
2328355f576SJeff Roberson #define UMA_ALIGN_PTR	(sizeof(void *) - 1)	/* Alignment fit for ptr */
2338355f576SJeff Roberson #define UMA_ALIGN_LONG	(sizeof(long) - 1)	/* "" long */
2348355f576SJeff Roberson #define UMA_ALIGN_INT	(sizeof(int) - 1)	/* "" int */
2358355f576SJeff Roberson #define UMA_ALIGN_SHORT	(sizeof(short) - 1)	/* "" short */
2368355f576SJeff Roberson #define UMA_ALIGN_CHAR	(sizeof(char) - 1)	/* "" char */
2378355f576SJeff Roberson #define UMA_ALIGN_CACHE	(16 - 1)		/* Cache line size align */
2388355f576SJeff Roberson 
2398355f576SJeff Roberson /*
2409c2cd7e5SJeff Roberson  * Destroys an empty uma zone.  If the zone is not empty uma complains loudly.
2418355f576SJeff Roberson  *
2428355f576SJeff Roberson  * Arguments:
2438355f576SJeff Roberson  *	zone  The zone we want to destroy.
2448355f576SJeff Roberson  *
2458355f576SJeff Roberson  */
2469c2cd7e5SJeff Roberson void uma_zdestroy(uma_zone_t zone);
2478355f576SJeff Roberson 
2488355f576SJeff Roberson /*
2498355f576SJeff Roberson  * Allocates an item out of a zone
2508355f576SJeff Roberson  *
2518355f576SJeff Roberson  * Arguments:
2528355f576SJeff Roberson  *	zone  The zone we are allocating from
2538355f576SJeff Roberson  *	arg   This data is passed to the ctor function
2542cc35ff9SJeff Roberson  *	flags See sys/malloc.h for available flags.
2558355f576SJeff Roberson  *
2568355f576SJeff Roberson  * Returns:
2578355f576SJeff Roberson  *	A non null pointer to an initialized element from the zone is
258a163d034SWarner Losh  *	garanteed if the wait flag is M_WAITOK, otherwise a null pointer may be
2598355f576SJeff Roberson  *	returned if the zone is empty or the ctor failed.
2608355f576SJeff Roberson  */
2618355f576SJeff Roberson 
2622cc35ff9SJeff Roberson void *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags);
2638355f576SJeff Roberson 
2648355f576SJeff Roberson /*
2658355f576SJeff Roberson  * Allocates an item out of a zone without supplying an argument
2668355f576SJeff Roberson  *
2678355f576SJeff Roberson  * This is just a wrapper for uma_zalloc_arg for convenience.
2688355f576SJeff Roberson  *
2698355f576SJeff Roberson  */
2702cc35ff9SJeff Roberson static __inline void *uma_zalloc(uma_zone_t zone, int flags);
2718355f576SJeff Roberson 
2728355f576SJeff Roberson static __inline void *
2732cc35ff9SJeff Roberson uma_zalloc(uma_zone_t zone, int flags)
2748355f576SJeff Roberson {
2752cc35ff9SJeff Roberson 	return uma_zalloc_arg(zone, NULL, flags);
2768355f576SJeff Roberson }
2778355f576SJeff Roberson 
2788355f576SJeff Roberson /*
2798355f576SJeff Roberson  * Frees an item back into the specified zone.
2808355f576SJeff Roberson  *
2818355f576SJeff Roberson  * Arguments:
2828355f576SJeff Roberson  *	zone  The zone the item was originally allocated out of.
2838355f576SJeff Roberson  *	item  The memory to be freed.
2848355f576SJeff Roberson  *	arg   Argument passed to the destructor
2858355f576SJeff Roberson  *
2868355f576SJeff Roberson  * Returns:
2878355f576SJeff Roberson  *	Nothing.
2888355f576SJeff Roberson  */
2898355f576SJeff Roberson 
2908355f576SJeff Roberson void uma_zfree_arg(uma_zone_t zone, void *item, void *arg);
2918355f576SJeff Roberson 
2928355f576SJeff Roberson /*
2938355f576SJeff Roberson  * Frees an item back to a zone without supplying an argument
2948355f576SJeff Roberson  *
2958355f576SJeff Roberson  * This is just a wrapper for uma_zfree_arg for convenience.
2968355f576SJeff Roberson  *
2978355f576SJeff Roberson  */
2988355f576SJeff Roberson static __inline void uma_zfree(uma_zone_t zone, void *item);
2998355f576SJeff Roberson 
3008355f576SJeff Roberson static __inline void
3018355f576SJeff Roberson uma_zfree(uma_zone_t zone, void *item)
3028355f576SJeff Roberson {
303f6e34b82SMark Murray 	uma_zfree_arg(zone, item, NULL);
3048355f576SJeff Roberson }
3058355f576SJeff Roberson 
3068355f576SJeff Roberson /*
3078355f576SJeff Roberson  * XXX The rest of the prototypes in this header are h0h0 magic for the VM.
3088355f576SJeff Roberson  * If you think you need to use it for a normal zone you're probably incorrect.
3098355f576SJeff Roberson  */
3108355f576SJeff Roberson 
3118355f576SJeff Roberson /*
3128355f576SJeff Roberson  * Backend page supplier routines
3138355f576SJeff Roberson  *
3148355f576SJeff Roberson  * Arguments:
3158355f576SJeff Roberson  *	zone  The zone that is requesting pages
3168355f576SJeff Roberson  *	size  The number of bytes being requested
3178355f576SJeff Roberson  *	pflag Flags for these memory pages, see below.
3188355f576SJeff Roberson  *	wait  Indicates our willingness to block.
3198355f576SJeff Roberson  *
3208355f576SJeff Roberson  * Returns:
3218355f576SJeff Roberson  *	A pointer to the alloced memory or NULL on failure.
3228355f576SJeff Roberson  */
3238355f576SJeff Roberson 
3248355f576SJeff Roberson typedef void *(*uma_alloc)(uma_zone_t zone, int size, u_int8_t *pflag, int wait);
3258355f576SJeff Roberson 
3268355f576SJeff Roberson /*
3278355f576SJeff Roberson  * Backend page free routines
3288355f576SJeff Roberson  *
3298355f576SJeff Roberson  * Arguments:
3308355f576SJeff Roberson  *	item  A pointer to the previously allocated pages
3318355f576SJeff Roberson  *	size  The original size of the allocation
3328355f576SJeff Roberson  *	pflag The flags for the slab.  See UMA_SLAB_* below
3338355f576SJeff Roberson  *
3348355f576SJeff Roberson  * Returns:
3358355f576SJeff Roberson  *	None
3368355f576SJeff Roberson  */
3378355f576SJeff Roberson typedef void (*uma_free)(void *item, int size, u_int8_t pflag);
3388355f576SJeff Roberson 
3398355f576SJeff Roberson 
3408355f576SJeff Roberson 
3418355f576SJeff Roberson /*
3428355f576SJeff Roberson  * Sets up the uma allocator. (Called by vm_mem_init)
3438355f576SJeff Roberson  *
3448355f576SJeff Roberson  * Arguments:
3458355f576SJeff Roberson  *	bootmem  A pointer to memory used to bootstrap the system.
3468355f576SJeff Roberson  *
3478355f576SJeff Roberson  * Returns:
3488355f576SJeff Roberson  *	Nothing
3498355f576SJeff Roberson  *
3508355f576SJeff Roberson  * Discussion:
3518355f576SJeff Roberson  *	This memory is used for zones which allocate things before the
3528355f576SJeff Roberson  *	backend page supplier can give us pages.  It should be
3538355f576SJeff Roberson  *	UMA_SLAB_SIZE * UMA_BOOT_PAGES bytes. (see uma_int.h)
3548355f576SJeff Roberson  *
3558355f576SJeff Roberson  */
3568355f576SJeff Roberson 
3578355f576SJeff Roberson void uma_startup(void *bootmem);
3588355f576SJeff Roberson 
3598355f576SJeff Roberson /*
3608355f576SJeff Roberson  * Finishes starting up the allocator.  This should
3618355f576SJeff Roberson  * be called when kva is ready for normal allocs.
3628355f576SJeff Roberson  *
3638355f576SJeff Roberson  * Arguments:
36499571dc3SJeff Roberson  *	None
3658355f576SJeff Roberson  *
3668355f576SJeff Roberson  * Returns:
3678355f576SJeff Roberson  *	Nothing
3688355f576SJeff Roberson  *
3698355f576SJeff Roberson  * Discussion:
37099571dc3SJeff Roberson  *	uma_startup2 is called by kmeminit() to enable us of uma for malloc.
3718355f576SJeff Roberson  */
3728355f576SJeff Roberson 
37399571dc3SJeff Roberson void uma_startup2(void);
3748355f576SJeff Roberson 
3758355f576SJeff Roberson /*
3768355f576SJeff Roberson  * Reclaims unused memory for all zones
3778355f576SJeff Roberson  *
3788355f576SJeff Roberson  * Arguments:
3798355f576SJeff Roberson  *	None
3808355f576SJeff Roberson  * Returns:
3818355f576SJeff Roberson  *	None
3828355f576SJeff Roberson  *
3838355f576SJeff Roberson  * This should only be called by the page out daemon.
3848355f576SJeff Roberson  */
3858355f576SJeff Roberson 
3868355f576SJeff Roberson void uma_reclaim(void);
3878355f576SJeff Roberson 
3888355f576SJeff Roberson /*
3898355f576SJeff Roberson  * Switches the backing object of a zone
3908355f576SJeff Roberson  *
3918355f576SJeff Roberson  * Arguments:
3928355f576SJeff Roberson  *	zone  The zone to update
3938355f576SJeff Roberson  *	obj   The obj to use for future allocations
3948355f576SJeff Roberson  *	size  The size of the object to allocate
3958355f576SJeff Roberson  *
3968355f576SJeff Roberson  * Returns:
3978355f576SJeff Roberson  *	0  if kva space can not be allocated
3988355f576SJeff Roberson  *	1  if successful
3998355f576SJeff Roberson  *
4008355f576SJeff Roberson  * Discussion:
4018355f576SJeff Roberson  *	A NULL object can be used and uma will allocate one for you.  Setting
4028355f576SJeff Roberson  *	the size will limit the amount of memory allocated to this zone.
4038355f576SJeff Roberson  *
4048355f576SJeff Roberson  */
4058355f576SJeff Roberson struct vm_object;
4068355f576SJeff Roberson int uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int size);
4078355f576SJeff Roberson 
408736ee590SJeff Roberson /*
409736ee590SJeff Roberson  * Sets a high limit on the number of items allowed in a zone
410736ee590SJeff Roberson  *
411736ee590SJeff Roberson  * Arguments:
412736ee590SJeff Roberson  *	zone  The zone to limit
413736ee590SJeff Roberson  *
414736ee590SJeff Roberson  * Returns:
415736ee590SJeff Roberson  *	Nothing
416736ee590SJeff Roberson  */
417736ee590SJeff Roberson void uma_zone_set_max(uma_zone_t zone, int nitems);
4188355f576SJeff Roberson 
4198355f576SJeff Roberson /*
420099a0e58SBosko Milekic  * The following two routines (uma_zone_set_init/fini)
421099a0e58SBosko Milekic  * are used to set the backend init/fini pair which acts on an
422099a0e58SBosko Milekic  * object as it becomes allocated and is placed in a slab within
423099a0e58SBosko Milekic  * the specified zone's backing keg.  These should probably not
424099a0e58SBosko Milekic  * be changed once allocations have already begun and only
425099a0e58SBosko Milekic  * immediately upon zone creation.
426099a0e58SBosko Milekic  */
427099a0e58SBosko Milekic void uma_zone_set_init(uma_zone_t zone, uma_init uminit);
428099a0e58SBosko Milekic void uma_zone_set_fini(uma_zone_t zone, uma_fini fini);
429099a0e58SBosko Milekic 
430099a0e58SBosko Milekic /*
431099a0e58SBosko Milekic  * The following two routines (uma_zone_set_zinit/zfini) are
432099a0e58SBosko Milekic  * used to set the zinit/zfini pair which acts on an object as
433099a0e58SBosko Milekic  * it passes from the backing Keg's slab cache to the
434099a0e58SBosko Milekic  * specified Zone's bucket cache.  These should probably not
435099a0e58SBosko Milekic  * be changed once allocations have already begun and
436099a0e58SBosko Milekic  * only immediately upon zone creation.
437099a0e58SBosko Milekic  */
438099a0e58SBosko Milekic void uma_zone_set_zinit(uma_zone_t zone, uma_init zinit);
439099a0e58SBosko Milekic void uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini);
440099a0e58SBosko Milekic 
441099a0e58SBosko Milekic /*
4428355f576SJeff Roberson  * Replaces the standard page_alloc or obj_alloc functions for this zone
4438355f576SJeff Roberson  *
4448355f576SJeff Roberson  * Arguments:
4458355f576SJeff Roberson  *	zone   The zone whos back end allocator is being changed.
4468355f576SJeff Roberson  *	allocf A pointer to the allocation function
4478355f576SJeff Roberson  *
4488355f576SJeff Roberson  * Returns:
4498355f576SJeff Roberson  *	Nothing
4508355f576SJeff Roberson  *
4518355f576SJeff Roberson  * Discussion:
4528355f576SJeff Roberson  *	This could be used to implement pageable allocation, or perhaps
4538355f576SJeff Roberson  *	even DMA allocators if used in conjunction with the OFFPAGE
4548355f576SJeff Roberson  *	zone flag.
4558355f576SJeff Roberson  */
4568355f576SJeff Roberson 
4578355f576SJeff Roberson void uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf);
4588355f576SJeff Roberson 
4598355f576SJeff Roberson /*
4608355f576SJeff Roberson  * Used for freeing memory provided by the allocf above
4618355f576SJeff Roberson  *
4628355f576SJeff Roberson  * Arguments:
4638355f576SJeff Roberson  *	zone  The zone that intends to use this free routine.
4648355f576SJeff Roberson  *	freef The page freeing routine.
4658355f576SJeff Roberson  *
4668355f576SJeff Roberson  * Returns:
4678355f576SJeff Roberson  *	Nothing
4688355f576SJeff Roberson  */
4698355f576SJeff Roberson 
4708355f576SJeff Roberson void uma_zone_set_freef(uma_zone_t zone, uma_free freef);
4718355f576SJeff Roberson 
4728355f576SJeff Roberson /*
4738355f576SJeff Roberson  * These flags are setable in the allocf and visable in the freef.
4748355f576SJeff Roberson  */
4758355f576SJeff Roberson #define UMA_SLAB_BOOT	0x01		/* Slab alloced from boot pages */
4768355f576SJeff Roberson #define UMA_SLAB_KMEM	0x02		/* Slab alloced from kmem_map */
4778355f576SJeff Roberson #define UMA_SLAB_PRIV	0x08		/* Slab alloced from priv allocator */
478c235bfa5SJeff Roberson #define UMA_SLAB_OFFP	0x10		/* Slab is managed separately  */
4798355f576SJeff Roberson #define UMA_SLAB_MALLOC	0x20		/* Slab is a large malloc slab */
4808355f576SJeff Roberson /* 0x40 and 0x80 are available */
4818355f576SJeff Roberson 
4828355f576SJeff Roberson /*
4838355f576SJeff Roberson  * Used to pre-fill a zone with some number of items
4848355f576SJeff Roberson  *
4858355f576SJeff Roberson  * Arguments:
4868355f576SJeff Roberson  *	zone    The zone to fill
4878355f576SJeff Roberson  *	itemcnt The number of items to reserve
4888355f576SJeff Roberson  *
4898355f576SJeff Roberson  * Returns:
4908355f576SJeff Roberson  *	Nothing
4918355f576SJeff Roberson  *
4928355f576SJeff Roberson  * NOTE: This is blocking and should only be done at startup
4938355f576SJeff Roberson  */
4948355f576SJeff Roberson void uma_prealloc(uma_zone_t zone, int itemcnt);
4958355f576SJeff Roberson 
496099a0e58SBosko Milekic /*
497099a0e58SBosko Milekic  * Used to lookup the reference counter allocated for an item
498099a0e58SBosko Milekic  * from a UMA_ZONE_REFCNT zone.  For UMA_ZONE_REFCNT zones,
499099a0e58SBosko Milekic  * reference counters are allocated for items and stored in
500099a0e58SBosko Milekic  * the underlying slab header.
501099a0e58SBosko Milekic  *
502099a0e58SBosko Milekic  * Arguments:
503099a0e58SBosko Milekic  * 	zone  The UMA_ZONE_REFCNT zone to which the item belongs.
504099a0e58SBosko Milekic  *	item  The address of the item for which we want a refcnt.
505099a0e58SBosko Milekic  *
506099a0e58SBosko Milekic  * Returns:
507099a0e58SBosko Milekic  * 	A pointer to a u_int32_t reference counter.
508099a0e58SBosko Milekic  */
509099a0e58SBosko Milekic u_int32_t *uma_find_refcnt(uma_zone_t zone, void *item);
5108355f576SJeff Roberson 
5117a52a97eSRobert Watson /*
5127a52a97eSRobert Watson  * Exported statistics structures to be used by user space monitoring tools.
5137a52a97eSRobert Watson  * Statistics stream consusts of a uma_stream_header, followed by a series of
5147a52a97eSRobert Watson  * alternative uma_type_header and uma_type_stat structures.  Statistics
5157a52a97eSRobert Watson  * structures
5167a52a97eSRobert Watson  */
5177a52a97eSRobert Watson #define	UMA_STREAM_VERSION	0x00000001
5187a52a97eSRobert Watson struct uma_stream_header {
5197a52a97eSRobert Watson 	u_int32_t	ush_version;	/* Stream format version. */
5207a52a97eSRobert Watson 	u_int32_t	ush_maxcpus;	/* Value of MAXCPU for stream. */
5217a52a97eSRobert Watson 	u_int32_t	ush_count;	/* Number of records. */
5227a52a97eSRobert Watson 	u_int32_t	_ush_pad;	/* Pad/reserved field. */
5237a52a97eSRobert Watson };
5247a52a97eSRobert Watson 
5257a52a97eSRobert Watson #define	UMA_MAX_NAME	32
5267a52a97eSRobert Watson struct uma_type_header {
5277a52a97eSRobert Watson 	/*
5287a52a97eSRobert Watson 	 * Static per-zone data, some extracted from the supporting keg.
5297a52a97eSRobert Watson 	 */
5307a52a97eSRobert Watson 	char		uth_name[UMA_MAX_NAME];
5317a52a97eSRobert Watson 	u_int32_t	uth_align;	/* Keg: alignment. */
5327a52a97eSRobert Watson 	u_int32_t	uth_size;	/* Keg: requested size of item. */
5337a52a97eSRobert Watson 	u_int32_t	uth_rsize;	/* Keg: real size of item. */
5347a52a97eSRobert Watson 	u_int32_t	uth_maxpages;	/* Keg: maximum number of pages. */
5357a52a97eSRobert Watson 	u_int32_t	uth_limit;	/* Keg: max items to allocate. */
5367a52a97eSRobert Watson 
5377a52a97eSRobert Watson 	/*
5387a52a97eSRobert Watson 	 * Current dynamic zone/keg-derived statistics.
5397a52a97eSRobert Watson 	 */
5407a52a97eSRobert Watson 	u_int32_t	uth_pages;	/* Keg: pages allocated. */
5417a52a97eSRobert Watson 	u_int32_t	uth_keg_free;	/* Keg: items free. */
5427a52a97eSRobert Watson 	u_int32_t	uth_zone_free;	/* Zone: items free. */
5437a52a97eSRobert Watson 	u_int32_t	uth_bucketsize;	/* Zone: desired bucket size. */
5447a52a97eSRobert Watson 	u_int32_t	_uth_reserved0;	/* Reserved. */
5457a52a97eSRobert Watson 	u_int64_t	uth_allocs;	/* Zone: number of allocations. */
5467a52a97eSRobert Watson 	u_int64_t	uth_frees;	/* Zone: number of frees. */
5472019094aSRobert Watson 	u_int64_t	uth_fails;	/* Zone: number of alloc failures. */
5482019094aSRobert Watson 	u_int64_t	_uth_reserved1[3];	/* Reserved. */
5497a52a97eSRobert Watson 
5507a52a97eSRobert Watson };
5517a52a97eSRobert Watson 
5527a52a97eSRobert Watson struct uma_percpu_stat {
5537a52a97eSRobert Watson 	u_int64_t	ups_allocs;	/* Cache: number of alloctions. */
5547a52a97eSRobert Watson 	u_int64_t	ups_frees;	/* Cache: number of frees. */
5557a52a97eSRobert Watson 	u_int64_t	ups_cache_free;	/* Cache: free items in cache. */
5567a52a97eSRobert Watson 	u_int64_t	_ups_reserved[5];	/* Reserved. */
5577a52a97eSRobert Watson };
5587a52a97eSRobert Watson 
5598355f576SJeff Roberson #endif
560