616909f785226cc88ab19c03f6ba506ac9e96df0
[m6w6/libmemcached] / src / libmemcachedprotocol / cache.h
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include <pthread.h>
19
20 #ifdef HAVE_UMEM_H
21 # include <umem.h>
22 # define cache_t umem_cache_t
23 # define cache_alloc(a) umem_cache_alloc(a, UMEM_DEFAULT)
24 # define cache_free(a, b) umem_cache_free(a, b)
25 # define cache_create(a, b, c, d, e) umem_cache_create((char *) a, b, c, d, e, NULL, NULL, NULL, 0)
26 # define cache_destroy(a) umem_cache_destroy(a);
27 #else
28 # ifndef NDEBUG
29 /* may be used for debug purposes */
30 extern int cache_error;
31 # endif
32
33 /**
34 * Constructor used to initialize allocated objects
35 *
36 * @param obj pointer to the object to initialized.
37 * @param notused1 This parameter is currently not used.
38 * @param notused2 This parameter is currently not used.
39 * @return you should return 0, but currently this is not checked
40 */
41 typedef int cache_constructor_t(void *obj, void *notused1, int notused2);
42 /**
43 * Destructor used to clean up allocated objects before they are
44 * returned to the operating system.
45 *
46 * @param obj pointer to the object to initialized.
47 * @param notused1 This parameter is currently not used.
48 * @param notused2 This parameter is currently not used.
49 * @return you should return 0, but currently this is not checked
50 */
51 typedef void cache_destructor_t(void *obj, void *notused);
52
53 /**
54 * Definition of the structure to keep track of the internal details of
55 * the cache allocator. Touching any of these variables results in
56 * undefined behavior.
57 */
58 typedef struct {
59 /** Mutex to protect access to the structure */
60 pthread_mutex_t mutex;
61 /** Name of the cache objects in this cache (provided by the caller) */
62 char *name;
63 /** List of pointers to available buffers in this cache */
64 void **ptr;
65 /** The size of each element in this cache */
66 size_t bufsize;
67 /** The capacity of the list of elements */
68 size_t freetotal;
69 /** The current number of free elements */
70 size_t freecurr;
71 /** The constructor to be called each time we allocate more memory */
72 cache_constructor_t *constructor;
73 /** The destructor to be called each time before we release memory */
74 cache_destructor_t *destructor;
75 } cache_t;
76
77 /**
78 * Create an object cache.
79 *
80 * The object cache will let you allocate objects of the same size. It is fully
81 * MT safe, so you may allocate objects from multiple threads without having to
82 * do any syncrhonization in the application code.
83 *
84 * @param name the name of the object cache. This name may be used for debug purposes
85 * and may help you track down what kind of object you have problems with
86 * (buffer overruns, leakage etc)
87 * @param bufsize the size of each object in the cache
88 * @param align the alignment requirements of the objects in the cache.
89 * @param constructor the function to be called to initialize memory when we need
90 * to allocate more memory from the os.
91 * @param destructor the function to be called before we release the memory back
92 * to the os.
93 * @return a handle to an object cache if successful, NULL otherwise.
94 */
95 cache_t *cache_create(const char *name, size_t bufsize, size_t align,
96 cache_constructor_t *constructor, cache_destructor_t *destructor);
97 /**
98 * Destroy an object cache.
99 *
100 * Destroy and invalidate an object cache. You should return all buffers allocated
101 * with cache_alloc by using cache_free before calling this function. Not doing
102 * so results in undefined behavior (the buffers may or may not be invalidated)
103 *
104 * @param handle the handle to the object cache to destroy.
105 */
106 void cache_destroy(cache_t *handle);
107 /**
108 * Allocate an object from the cache.
109 *
110 * @param handle the handle to the object cache to allocate from
111 * @return a pointer to an initialized object from the cache, or NULL if
112 * the allocation cannot be satisfied.
113 */
114 void *cache_alloc(cache_t *handle);
115 /**
116 * Return an object back to the cache.
117 *
118 * The caller should return the object in an initialized state so that
119 * the object may be returned in an expected state from cache_alloc.
120 *
121 * @param handle handle to the object cache to return the object to
122 * @param ptr pointer to the object to return.
123 */
124 void cache_free(cache_t *handle, void *ptr);
125 #endif // HAVE_UMEM_H