xref: /haiku/headers/private/kernel/slab/Slab.h (revision 81423c91c784578220954adf9ed003575f8db8a0)
1*81423c91SHugo Santos /*
2*81423c91SHugo Santos  * Copyright 2007, Hugo Santos. All Rights Reserved.
3*81423c91SHugo Santos  * Distributed under the terms of the MIT License.
4*81423c91SHugo Santos  *
5*81423c91SHugo Santos  * Authors:
6*81423c91SHugo Santos  *      Hugo Santos, hugosantos@gmail.com
7*81423c91SHugo Santos  */
8*81423c91SHugo Santos 
9*81423c91SHugo Santos #ifndef _SLAB_SLAB_H_
10*81423c91SHugo Santos #define _SLAB_SLAB_H_
11*81423c91SHugo Santos 
12*81423c91SHugo Santos #ifdef __cplusplus
13*81423c91SHugo Santos 
14*81423c91SHugo Santos #include <slab/Backend.h>
15*81423c91SHugo Santos #include <slab/Base.h>
16*81423c91SHugo Santos #include <slab/Depot.h>
17*81423c91SHugo Santos #include <slab/MergedStrategy.h>
18*81423c91SHugo Santos #include <slab/HashStrategy.h>
19*81423c91SHugo Santos #include <slab/Utilities.h>
20*81423c91SHugo Santos 
21*81423c91SHugo Santos 
22*81423c91SHugo Santos extern "C" {
23*81423c91SHugo Santos #endif
24*81423c91SHugo Santos 
25*81423c91SHugo Santos enum {
26*81423c91SHugo Santos 	CACHE_DONT_SLEEP		= 1 << 0,
27*81423c91SHugo Santos 	CACHE_ALIGN_TO_TOTAL	= 1 << 16,
28*81423c91SHugo Santos };
29*81423c91SHugo Santos 
30*81423c91SHugo Santos typedef void *object_cache_t;
31*81423c91SHugo Santos 
32*81423c91SHugo Santos 
33*81423c91SHugo Santos object_cache_t
34*81423c91SHugo Santos object_cache_create(const char *name, size_t object_size, size_t alignment,
35*81423c91SHugo Santos 	void (*_constructor)(void *, void *), void (*_destructor)(void *, void *),
36*81423c91SHugo Santos 	void *cookie);
37*81423c91SHugo Santos void *object_cache_alloc(object_cache_t cache);
38*81423c91SHugo Santos void *object_cache_alloc_etc(object_cache_t cache, uint32_t flags);
39*81423c91SHugo Santos void object_cache_free(object_cache_t cache, void *object);
40*81423c91SHugo Santos void object_cache_destroy(object_cache_t cache);
41*81423c91SHugo Santos 
42*81423c91SHugo Santos #ifdef __cplusplus
43*81423c91SHugo Santos }
44*81423c91SHugo Santos 
45*81423c91SHugo Santos 
46*81423c91SHugo Santos template<typename CacheType>
47*81423c91SHugo Santos class LocalCache : public CacheType, protected base_depot {
48*81423c91SHugo Santos public:
49*81423c91SHugo Santos 	typedef LocalCache<CacheType> ThisType;
50*81423c91SHugo Santos 	typedef typename CacheType::Constructor Constructor;
51*81423c91SHugo Santos 	typedef typename CacheType::Destructor Destructor;
52*81423c91SHugo Santos 
53*81423c91SHugo Santos 	LocalCache(const char *name, size_t objectSize, size_t alignment,
54*81423c91SHugo Santos 		Constructor _constructor, Destructor _destructor, void *_cookie)
55*81423c91SHugo Santos 		: CacheType(name, objectSize, alignment, _constructor, _destructor,
56*81423c91SHugo Santos 			_cookie)
57*81423c91SHugo Santos 	{
58*81423c91SHugo Santos 		fStatus = base_depot_init(this, _ReturnObject);
59*81423c91SHugo Santos 	}
60*81423c91SHugo Santos 
61*81423c91SHugo Santos 	~LocalCache()
62*81423c91SHugo Santos 	{
63*81423c91SHugo Santos 		base_depot_destroy(this);
64*81423c91SHugo Santos 	}
65*81423c91SHugo Santos 
66*81423c91SHugo Santos 	status_t InitCheck() const { return fStatus; }
67*81423c91SHugo Santos 
68*81423c91SHugo Santos 	void *Alloc(uint32_t flags)
69*81423c91SHugo Santos 	{
70*81423c91SHugo Santos 		void *object = base_depot_obtain_from_store(this, base_depot_cpu(this));
71*81423c91SHugo Santos 		if (object == NULL)
72*81423c91SHugo Santos 			object = CacheType::AllocateObject(flags);
73*81423c91SHugo Santos 		return object;
74*81423c91SHugo Santos 	}
75*81423c91SHugo Santos 
76*81423c91SHugo Santos 	void Free(void *object)
77*81423c91SHugo Santos 	{
78*81423c91SHugo Santos 		if (!base_depot_return_to_store(this, base_depot_cpu(this), object))
79*81423c91SHugo Santos 			CacheType::ReturnObject(object);
80*81423c91SHugo Santos 	}
81*81423c91SHugo Santos 
82*81423c91SHugo Santos 	void Destroy()
83*81423c91SHugo Santos 	{
84*81423c91SHugo Santos 		base_depot_make_empty(this);
85*81423c91SHugo Santos 	}
86*81423c91SHugo Santos 
87*81423c91SHugo Santos private:
88*81423c91SHugo Santos 	void ReturnObject(void *object)
89*81423c91SHugo Santos 	{
90*81423c91SHugo Santos 		CacheType::ReturnObject(object);
91*81423c91SHugo Santos 	}
92*81423c91SHugo Santos 
93*81423c91SHugo Santos 	static void _ReturnObject(base_depot *self, void *object)
94*81423c91SHugo Santos 	{
95*81423c91SHugo Santos 		static_cast<ThisType *>(self)->ReturnObject(object);
96*81423c91SHugo Santos 	}
97*81423c91SHugo Santos 
98*81423c91SHugo Santos 	status_t fStatus;
99*81423c91SHugo Santos };
100*81423c91SHugo Santos 
101*81423c91SHugo Santos #endif /* __cplusplus */
102*81423c91SHugo Santos 
103*81423c91SHugo Santos #endif
104