xref: /6.0.3/moxi/src/cache.h (revision e3ff3a18)
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #ifndef CACHE_H
3 #define CACHE_H
4 #include "src/config.h"
5 
6 #ifdef HAVE_UMEM_H
7 #include <umem.h>
8 #define cache_t umem_cache_t
9 #define cache_alloc(a) umem_cache_alloc(a, UMEM_DEFAULT)
10 #define cache_free(a, b) umem_cache_free(a, b)
11 #define cache_create(a,b,c,d,e) umem_cache_create((char*)a, b, c, d, e, NULL, NULL, NULL, 0)
12 #define cache_destroy(a) umem_cache_destroy(a);
13 
14 #else
15 #include <platform/platform.h>
16 
17 #ifndef NDEBUG
18 /* may be used for debug purposes */
19 extern int cache_error;
20 #endif
21 
22 /**
23  * Constructor used to initialize allocated objects
24  *
25  * @param obj pointer to the object to initialized.
26  * @param notused1 This parameter is currently not used.
27  * @param notused2 This parameter is currently not used.
28  * @return you should return 0, but currently this is not checked
29  */
30 typedef int cache_constructor_t(void* obj, void* notused1, int notused2);
31 /**
32  * Destructor used to clean up allocated objects before they are
33  * returned to the operating system.
34  *
35  * @param obj pointer to the object to initialized.
36  * @param notused1 This parameter is currently not used.
37  * @param notused2 This parameter is currently not used.
38  * @return you should return 0, but currently this is not checked
39  */
40 typedef void cache_destructor_t(void* obj, void* notused);
41 
42 /**
43  * Definition of the structure to keep track of the internal details of
44  * the cache allocator. Touching any of these variables results in
45  * undefined behavior.
46  */
47 typedef struct {
48     /** Mutex to protect access to the structure */
49     cb_mutex_t mutex;
50     /** Name of the cache objects in this cache (provided by the caller) */
51     char *name;
52     /** List of pointers to available buffers in this cache */
53     void **ptr;
54     /** The size of each element in this cache */
55     size_t bufsize;
56     /** The capacity of the list of elements */
57     int freetotal;
58     /** The current number of free elements */
59     int freecurr;
60     /** The constructor to be called each time we allocate more memory */
61     cache_constructor_t* constructor;
62     /** The destructor to be called each time before we release memory */
63     cache_destructor_t* destructor;
64 } cache_t;
65 
66 /**
67  * Create an object cache.
68  *
69  * The object cache will let you allocate objects of the same size. It is fully
70  * MT safe, so you may allocate objects from multiple threads without having to
71  * do any syncrhonization in the application code.
72  *
73  * @param name the name of the object cache. This name may be used for debug purposes
74  *             and may help you track down what kind of object you have problems with
75  *             (buffer overruns, leakage etc)
76  * @param bufsize the size of each object in the cache
77  * @param align the alignment requirements of the objects in the cache.
78  * @param constructor the function to be called to initialize memory when we need
79  *                    to allocate more memory from the os.
80  * @param destructor the function to be called before we release the memory back
81  *                   to the os.
82  * @return a handle to an object cache if successful, NULL otherwise.
83  */
84 cache_t* cache_create(const char* name, size_t bufsize, size_t align,
85                       cache_constructor_t* constructor,
86                       cache_destructor_t* destructor);
87 /**
88  * Destroy an object cache.
89  *
90  * Destroy and invalidate an object cache. You should return all buffers allocated
91  * with cache_alloc by using cache_free before calling this function. Not doing
92  * so results in undefined behavior (the buffers may or may not be invalidated)
93  *
94  * @param handle the handle to the object cache to destroy.
95  */
96 void cache_destroy(cache_t* handle);
97 /**
98  * Allocate an object from the cache.
99  *
100  * @param handle the handle to the object cache to allocate from
101  * @return a pointer to an initialized object from the cache, or NULL if
102  *         the allocation cannot be satisfied.
103  */
104 void* cache_alloc(cache_t* handle);
105 /**
106  * Return an object back to the cache.
107  *
108  * The caller should return the object in an initialized state so that
109  * the object may be returned in an expected state from cache_alloc.
110  *
111  * @param handle handle to the object cache to return the object to
112  * @param ptr pointer to the object to return.
113  */
114 void cache_free(cache_t* handle, void* ptr);
115 #endif
116 
117 #endif
118