xref: /dragonfly/sys/sys/objcache.h (revision b272101a)
18a268428SJeffrey Hsu /*
28a268428SJeffrey Hsu  * Copyright (c) 2005 Jeffrey M. Hsu.  All rights reserved.
38a268428SJeffrey Hsu  *
48a268428SJeffrey Hsu  * This code is derived from software contributed to The DragonFly Project
58a268428SJeffrey Hsu  * by Jeffrey M. Hsu.
68a268428SJeffrey Hsu  *
78a268428SJeffrey Hsu  * Redistribution and use in source and binary forms, with or without
88a268428SJeffrey Hsu  * modification, are permitted provided that the following conditions
98a268428SJeffrey Hsu  * are met:
108a268428SJeffrey Hsu  * 1. Redistributions of source code must retain the above copyright
118a268428SJeffrey Hsu  *    notice, this list of conditions and the following disclaimer.
128a268428SJeffrey Hsu  * 2. Redistributions in binary form must reproduce the above copyright
138a268428SJeffrey Hsu  *    notice, this list of conditions and the following disclaimer in the
148a268428SJeffrey Hsu  *    documentation and/or other materials provided with the distribution.
158a268428SJeffrey Hsu  * 3. Neither the name of The DragonFly Project nor the names of its
168a268428SJeffrey Hsu  *    contributors may be used to endorse or promote products derived
178a268428SJeffrey Hsu  *    from this software without specific, prior written permission.
188a268428SJeffrey Hsu  *
198a268428SJeffrey Hsu  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
208a268428SJeffrey Hsu  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
218a268428SJeffrey Hsu  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
228a268428SJeffrey Hsu  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
238a268428SJeffrey Hsu  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
248a268428SJeffrey Hsu  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
258a268428SJeffrey Hsu  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
268a268428SJeffrey Hsu  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
278a268428SJeffrey Hsu  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
288a268428SJeffrey Hsu  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
298a268428SJeffrey Hsu  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
308a268428SJeffrey Hsu  * SUCH DAMAGE.
318a268428SJeffrey Hsu  */
328a268428SJeffrey Hsu 
33210a0869SSascha Wildner #ifndef _SYS_OBJCACHE_H_
34210a0869SSascha Wildner #define _SYS_OBJCACHE_H_
358a268428SJeffrey Hsu 
361bd40720SMatthew Dillon #ifndef _SYS_TYPES_H_
371bd40720SMatthew Dillon #include <sys/types.h>
381bd40720SMatthew Dillon #endif
396354fc0fSzrj 
406354fc0fSzrj #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
416354fc0fSzrj 
426354fc0fSzrj #ifndef _SYS__MALLOC_H_
436354fc0fSzrj #include <sys/_malloc.h>
441bd40720SMatthew Dillon #endif
451bd40720SMatthew Dillon 
468a268428SJeffrey Hsu #define OC_MFLAGS	0x0000ffff	/* same as malloc flags */
478a268428SJeffrey Hsu 
48698331b0SMatthew Dillon typedef __boolean_t (objcache_ctor_fn)(void *obj, void *privdata, int ocflags);
49698331b0SMatthew Dillon typedef void (objcache_dtor_fn)(void *obj, void *privdata);
508a268428SJeffrey Hsu 
518a268428SJeffrey Hsu /*
528a268428SJeffrey Hsu  * Underlying allocator.
538a268428SJeffrey Hsu  */
548a268428SJeffrey Hsu typedef void *(objcache_alloc_fn)(void *allocator_args, int ocflags);
558a268428SJeffrey Hsu typedef void (objcache_free_fn)(void *obj, void *allocator_args);
568a268428SJeffrey Hsu 
571bd40720SMatthew Dillon #ifdef	_KERNEL
581bd40720SMatthew Dillon 
598a268428SJeffrey Hsu struct objcache;
608a268428SJeffrey Hsu 
618a268428SJeffrey Hsu struct objcache
622fce2579SSepherosa Ziehau 	*objcache_create(const char *name, int cluster_limit, int nom_cache,
638a268428SJeffrey Hsu 			 objcache_ctor_fn *ctor, objcache_dtor_fn *dtor,
64698331b0SMatthew Dillon 			 void *privdata,
658a268428SJeffrey Hsu 			 objcache_alloc_fn *alloc, objcache_free_fn *free,
668a268428SJeffrey Hsu 			 void *allocator_args);
6770aac194SMatthew Dillon struct objcache
6870aac194SMatthew Dillon 	*objcache_create_simple(malloc_type_t mtype, size_t objsize);
69b1641984SMatthew Dillon struct objcache
70b1641984SMatthew Dillon 	*objcache_create_mbacked(malloc_type_t mtype, size_t objsize,
712fce2579SSepherosa Ziehau 			  int cluster_limit, int nom_cache,
72b1641984SMatthew Dillon 			  objcache_ctor_fn *ctor, objcache_dtor_fn *dtor,
73698331b0SMatthew Dillon 			  void *privdata);
74e98a16b6SMatthew Dillon void	 objcache_set_cluster_limit(struct objcache *oc, int cluster_limit);
758a268428SJeffrey Hsu void	*objcache_get(struct objcache *oc, int ocflags);
768a268428SJeffrey Hsu void	 objcache_put(struct objcache *oc, void *obj);
7777e294a1SMatthew Dillon void	 objcache_dtor(struct objcache *oc, void *obj);
788a268428SJeffrey Hsu void	 objcache_populate_linear(struct objcache *oc, void *elts, int nelts,
798a268428SJeffrey Hsu 				  int size);
80*8a7a7510SAaron LI __boolean_t objcache_reclaimlist(struct objcache *oc[], int nlist);
818a268428SJeffrey Hsu void	 objcache_destroy(struct objcache *oc);
828a268428SJeffrey Hsu 
836354fc0fSzrj #endif	/* _KERNEL */
841bd40720SMatthew Dillon 
858a268428SJeffrey Hsu /*
868a268428SJeffrey Hsu  * Common underlying allocators.
878a268428SJeffrey Hsu  */
888a268428SJeffrey Hsu struct objcache_malloc_args {
898a268428SJeffrey Hsu 	size_t		objsize;
908a268428SJeffrey Hsu 	malloc_type_t	mtype;
918a268428SJeffrey Hsu };
921bd40720SMatthew Dillon 
931bd40720SMatthew Dillon #ifdef	_KERNEL
941bd40720SMatthew Dillon 
9572ab76a8SSascha Wildner void	*objcache_malloc_alloc(void *allocator_args, int ocflags) __malloclike;
9672ab76a8SSascha Wildner void	*objcache_malloc_alloc_zero(void *allocator_args, int ocflags)
9772ab76a8SSascha Wildner 	    __malloclike;
988a268428SJeffrey Hsu void	 objcache_malloc_free(void *obj, void *allocator_args);
998a268428SJeffrey Hsu 
1008a268428SJeffrey Hsu void	*objcache_nop_alloc(void *allocator_args, int ocflags);
1018a268428SJeffrey Hsu void	 objcache_nop_free(void *obj, void *allocator_args);
1028a268428SJeffrey Hsu 
103210a0869SSascha Wildner #endif	/* _KERNEL */
10474c5c87eSHiten Pandya 
10503d6a592SMatthew Dillon #endif	/* _KERNEL || _KERNEL_STRUCTURES */
106c6bb9a90SSepherosa Ziehau 
107c6bb9a90SSepherosa Ziehau #define OBJCACHE_UNLIMITED	0x40000000
108c6bb9a90SSepherosa Ziehau #define OBJCACHE_NAMELEN	32
109c6bb9a90SSepherosa Ziehau 
110c6bb9a90SSepherosa Ziehau struct objcache_stats {
111c6bb9a90SSepherosa Ziehau 	char		oc_name[OBJCACHE_NAMELEN];	/* \0 term. */
112c6bb9a90SSepherosa Ziehau 
113c6bb9a90SSepherosa Ziehau 	/*
114c6bb9a90SSepherosa Ziehau 	 * >= OBJCACHE_UNLIMITED, if unlimited.
115c6bb9a90SSepherosa Ziehau 	 */
116c6bb9a90SSepherosa Ziehau 	u_long		oc_limit;
117c6bb9a90SSepherosa Ziehau 
118c6bb9a90SSepherosa Ziehau 	u_long		oc_requested;
119c6bb9a90SSepherosa Ziehau 	u_long		oc_used;
120c6bb9a90SSepherosa Ziehau 	u_long		oc_cached;
121c6bb9a90SSepherosa Ziehau 	u_long		oc_exhausted;
122c6bb9a90SSepherosa Ziehau 	u_long		oc_failed;
123c6bb9a90SSepherosa Ziehau 	u_long		oc_allocated;
124c6bb9a90SSepherosa Ziehau 	u_long		oc_reserved[5];			/* reserved 0. */
125c6bb9a90SSepherosa Ziehau };
126c6bb9a90SSepherosa Ziehau 
127210a0869SSascha Wildner #endif	/* !_SYS_OBJCACHE_H_ */
128