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 +--------------------------------------------------------------------+
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);
29 /* may be used for debug purposes */
30 extern int cache_error
;
34 * Constructor used to initialize allocated objects
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
41 typedef int cache_constructor_t(void *obj
, void *notused1
, int notused2
);
43 * Destructor used to clean up allocated objects before they are
44 * returned to the operating system.
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
51 typedef void cache_destructor_t(void *obj
, void *notused
);
54 * Definition of the structure to keep track of the internal details of
55 * the cache allocator. Touching any of these variables results in
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) */
63 /** List of pointers to available buffers in this cache */
65 /** The size of each element in this cache */
67 /** The capacity of the list of elements */
69 /** The current number of free elements */
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
;
78 * Create an object cache.
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.
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
93 * @return a handle to an object cache if successful, NULL otherwise.
95 cache_t
*cache_create(const char *name
, size_t bufsize
, size_t align
,
96 cache_constructor_t
*constructor
, cache_destructor_t
*destructor
);
98 * Destroy an object cache.
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)
104 * @param handle the handle to the object cache to destroy.
106 void cache_destroy(cache_t
*handle
);
108 * Allocate an object from the cache.
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.
114 void *cache_alloc(cache_t
*handle
);
116 * Return an object back to the cache.
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.
121 * @param handle handle to the object cache to return the object to
122 * @param ptr pointer to the object to return.
124 void cache_free(cache_t
*handle
, void *ptr
);
125 #endif // HAVE_UMEM_H