cache.h (memcached-1.6.10) | : | cache.h (memcached-1.6.11) | ||
---|---|---|---|---|
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ | /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ | |||
#ifndef CACHE_H | #ifndef CACHE_H | |||
#define CACHE_H | #define CACHE_H | |||
#include <pthread.h> | #include <pthread.h> | |||
#include "queue.h" | ||||
#ifndef NDEBUG | #ifndef NDEBUG | |||
/* may be used for debug purposes */ | /* may be used for debug purposes */ | |||
extern int cache_error; | extern int cache_error; | |||
#endif | #endif | |||
/** | struct cache_free_s { | |||
* Constructor used to initialize allocated objects | STAILQ_ENTRY(cache_free_s) c_next; | |||
* | }; | |||
* @param obj pointer to the object to initialized. | ||||
* @param notused1 This parameter is currently not used. | ||||
* @param notused2 This parameter is currently not used. | ||||
* @return you should return 0, but currently this is not checked | ||||
*/ | ||||
typedef int cache_constructor_t(void* obj, void* notused1, int notused2); | ||||
/** | ||||
* Destructor used to clean up allocated objects before they are | ||||
* returned to the operating system. | ||||
* | ||||
* @param obj pointer to the object to clean up. | ||||
* @param notused This parameter is currently not used. | ||||
* @return you should return 0, but currently this is not checked | ||||
*/ | ||||
typedef void cache_destructor_t(void* obj, void* notused); | ||||
//typedef STAILQ_HEAD(cache_head_s, cache_free_s) cache_head_t; | ||||
/** | /** | |||
* Definition of the structure to keep track of the internal details of | * Definition of the structure to keep track of the internal details of | |||
* the cache allocator. Touching any of these variables results in | * the cache allocator. Touching any of these variables results in | |||
* undefined behavior. | * undefined behavior. | |||
*/ | */ | |||
typedef struct { | typedef struct { | |||
/** Mutex to protect access to the structure */ | /** Mutex to protect access to the structure */ | |||
pthread_mutex_t mutex; | pthread_mutex_t mutex; | |||
/** Name of the cache objects in this cache (provided by the caller) */ | /** Name of the cache objects in this cache (provided by the caller) */ | |||
char *name; | char *name; | |||
/** List of pointers to available buffers in this cache */ | /** freelist of available buffers */ | |||
void **ptr; | STAILQ_HEAD(cache_head, cache_free_s) head; | |||
/** The size of each element in this cache */ | /** The size of each element in this cache */ | |||
size_t bufsize; | size_t bufsize; | |||
/** The capacity of the list of elements */ | /** The capacity of the list of elements */ | |||
int freetotal; | int freetotal; | |||
/** Total malloc'ed objects */ | /** Total malloc'ed objects */ | |||
int total; | int total; | |||
/** The current number of free elements */ | /** The current number of free elements */ | |||
int freecurr; | int freecurr; | |||
/** A limit on the total number of elements */ | /** A limit on the total number of elements */ | |||
int limit; | int limit; | |||
/** The constructor to be called each time we allocate more memory */ | ||||
cache_constructor_t* constructor; | ||||
/** The destructor to be called each time before we release memory */ | ||||
cache_destructor_t* destructor; | ||||
} cache_t; | } cache_t; | |||
/** | /** | |||
* Create an object cache. | * Create an object cache. | |||
* | * | |||
* The object cache will let you allocate objects of the same size. It is fully | * The object cache will let you allocate objects of the same size. It is fully | |||
* MT safe, so you may allocate objects from multiple threads without having to | * MT safe, so you may allocate objects from multiple threads without having to | |||
* do any synchronization in the application code. | * do any synchronization in the application code. | |||
* | * | |||
* @param name the name of the object cache. This name may be used for debug pur poses | * @param name the name of the object cache. This name may be used for debug pur poses | |||
* and may help you track down what kind of object you have problems with | * and may help you track down what kind of object you have problems with | |||
* (buffer overruns, leakage etc) | * (buffer overruns, leakage etc) | |||
* @param bufsize the size of each object in the cache | * @param bufsize the size of each object in the cache | |||
* @param align the alignment requirements of the objects in the cache. | * @param align the alignment requirements of the objects in the cache. | |||
* @param constructor the function to be called to initialize memory when we nee d | * @param constructor the function to be called to initialize memory when we nee d | |||
* to allocate more memory from the os. | * to allocate more memory from the os. | |||
* @param destructor the function to be called before we release the memory back | * @param destructor the function to be called before we release the memory back | |||
* to the os. | * to the os. | |||
* @return a handle to an object cache if successful, NULL otherwise. | * @return a handle to an object cache if successful, NULL otherwise. | |||
*/ | */ | |||
cache_t* cache_create(const char* name, size_t bufsize, size_t align, | cache_t* cache_create(const char* name, size_t bufsize, size_t align); | |||
cache_constructor_t* constructor, | ||||
cache_destructor_t* destructor); | ||||
/** | /** | |||
* Destroy an object cache. | * Destroy an object cache. | |||
* | * | |||
* Destroy and invalidate an object cache. You should return all buffers allocat ed | * Destroy and invalidate an object cache. You should return all buffers allocat ed | |||
* with cache_alloc by using cache_free before calling this function. Not doing | * with cache_alloc by using cache_free before calling this function. Not doing | |||
* so results in undefined behavior (the buffers may or may not be invalidated) | * so results in undefined behavior (the buffers may or may not be invalidated) | |||
* | * | |||
* @param handle the handle to the object cache to destroy. | * @param handle the handle to the object cache to destroy. | |||
*/ | */ | |||
void cache_destroy(cache_t* handle); | void cache_destroy(cache_t* handle); | |||
End of changes. 6 change blocks. | ||||
27 lines changed or deleted | 9 lines changed or added |