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 +--------------------------------------------------------------------+
21 typedef void *pthread_mutex_t
;
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);
33 /* may be used for debug purposes */
34 extern int cache_error
;
38 * Constructor used to initialize allocated objects
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
45 typedef int cache_constructor_t(void *obj
, void *notused1
, int notused2
);
47 * Destructor used to clean up allocated objects before they are
48 * returned to the operating system.
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
55 typedef void cache_destructor_t(void *obj
, void *notused
);
58 * Definition of the structure to keep track of the internal details of
59 * the cache allocator. Touching any of these variables results in
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) */
67 /** List of pointers to available buffers in this cache */
69 /** The size of each element in this cache */
71 /** The capacity of the list of elements */
73 /** The current number of free elements */
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
;
82 * Create an object cache.
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.
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
97 * @return a handle to an object cache if successful, NULL otherwise.
99 cache_t
*cache_create(const char *name
, size_t bufsize
, size_t align
,
100 cache_constructor_t
*constructor
, cache_destructor_t
*destructor
);
102 * Destroy an object cache.
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)
108 * @param handle the handle to the object cache to destroy.
110 void cache_destroy(cache_t
*handle
);
112 * Allocate an object from the cache.
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.
118 void *cache_alloc(cache_t
*handle
);
120 * Return an object back to the cache.
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.
125 * @param handle handle to the object cache to return the object to
126 * @param ptr pointer to the object to return.
128 void cache_free(cache_t
*handle
, void *ptr
);
129 #endif // HAVE_UMEM_H