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