|
Packit |
4e8bc4 |
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
Packit |
4e8bc4 |
#ifndef CACHE_H
|
|
Packit |
4e8bc4 |
#define CACHE_H
|
|
Packit |
4e8bc4 |
#include <pthread.h>
|
|
Packit |
4e8bc4 |
|
|
Packit |
4e8bc4 |
#ifdef HAVE_UMEM_H
|
|
Packit |
4e8bc4 |
#include <umem.h>
|
|
Packit |
4e8bc4 |
#define cache_t umem_cache_t
|
|
Packit |
4e8bc4 |
#define cache_alloc(a) umem_cache_alloc(a, UMEM_DEFAULT)
|
|
Packit |
4e8bc4 |
#define do_cache_alloc(a) umem_cache_alloc(a, UMEM_DEFAULT)
|
|
Packit |
4e8bc4 |
#define cache_free(a, b) umem_cache_free(a, b)
|
|
Packit |
4e8bc4 |
#define do_cache_free(a, b) umem_cache_free(a, b)
|
|
Packit |
4e8bc4 |
#define cache_create(a,b,c,d,e) umem_cache_create((char*)a, b, c, d, e, NULL, NULL, NULL, 0)
|
|
Packit |
4e8bc4 |
#define cache_destroy(a) umem_cache_destroy(a);
|
|
Packit |
4e8bc4 |
|
|
Packit |
4e8bc4 |
#else
|
|
Packit |
4e8bc4 |
|
|
Packit |
4e8bc4 |
#ifndef NDEBUG
|
|
Packit |
4e8bc4 |
/* may be used for debug purposes */
|
|
Packit |
4e8bc4 |
extern int cache_error;
|
|
Packit |
4e8bc4 |
#endif
|
|
Packit |
4e8bc4 |
|
|
Packit |
4e8bc4 |
/**
|
|
Packit |
4e8bc4 |
* Constructor used to initialize allocated objects
|
|
Packit |
4e8bc4 |
*
|
|
Packit |
4e8bc4 |
* @param obj pointer to the object to initialized.
|
|
Packit |
4e8bc4 |
* @param notused1 This parameter is currently not used.
|
|
Packit |
4e8bc4 |
* @param notused2 This parameter is currently not used.
|
|
Packit |
4e8bc4 |
* @return you should return 0, but currently this is not checked
|
|
Packit |
4e8bc4 |
*/
|
|
Packit |
4e8bc4 |
typedef int cache_constructor_t(void* obj, void* notused1, int notused2);
|
|
Packit |
4e8bc4 |
/**
|
|
Packit |
4e8bc4 |
* Destructor used to clean up allocated objects before they are
|
|
Packit |
4e8bc4 |
* returned to the operating system.
|
|
Packit |
4e8bc4 |
*
|
|
Packit |
4e8bc4 |
* @param obj pointer to the object to clean up.
|
|
Packit |
4e8bc4 |
* @param notused This parameter is currently not used.
|
|
Packit |
4e8bc4 |
* @return you should return 0, but currently this is not checked
|
|
Packit |
4e8bc4 |
*/
|
|
Packit |
4e8bc4 |
typedef void cache_destructor_t(void* obj, void* notused);
|
|
Packit |
4e8bc4 |
|
|
Packit |
4e8bc4 |
/**
|
|
Packit |
4e8bc4 |
* Definition of the structure to keep track of the internal details of
|
|
Packit |
4e8bc4 |
* the cache allocator. Touching any of these variables results in
|
|
Packit |
4e8bc4 |
* undefined behavior.
|
|
Packit |
4e8bc4 |
*/
|
|
Packit |
4e8bc4 |
typedef struct {
|
|
Packit |
4e8bc4 |
/** Mutex to protect access to the structure */
|
|
Packit |
4e8bc4 |
pthread_mutex_t mutex;
|
|
Packit |
4e8bc4 |
/** Name of the cache objects in this cache (provided by the caller) */
|
|
Packit |
4e8bc4 |
char *name;
|
|
Packit |
4e8bc4 |
/** List of pointers to available buffers in this cache */
|
|
Packit |
4e8bc4 |
void **ptr;
|
|
Packit |
4e8bc4 |
/** The size of each element in this cache */
|
|
Packit |
4e8bc4 |
size_t bufsize;
|
|
Packit |
4e8bc4 |
/** The capacity of the list of elements */
|
|
Packit |
4e8bc4 |
int freetotal;
|
|
Packit |
4e8bc4 |
/** The current number of free elements */
|
|
Packit |
4e8bc4 |
int freecurr;
|
|
Packit |
4e8bc4 |
/** The constructor to be called each time we allocate more memory */
|
|
Packit |
4e8bc4 |
cache_constructor_t* constructor;
|
|
Packit |
4e8bc4 |
/** The destructor to be called each time before we release memory */
|
|
Packit |
4e8bc4 |
cache_destructor_t* destructor;
|
|
Packit |
4e8bc4 |
} cache_t;
|
|
Packit |
4e8bc4 |
|
|
Packit |
4e8bc4 |
/**
|
|
Packit |
4e8bc4 |
* Create an object cache.
|
|
Packit |
4e8bc4 |
*
|
|
Packit |
4e8bc4 |
* The object cache will let you allocate objects of the same size. It is fully
|
|
Packit |
4e8bc4 |
* MT safe, so you may allocate objects from multiple threads without having to
|
|
Packit |
4e8bc4 |
* do any synchronization in the application code.
|
|
Packit |
4e8bc4 |
*
|
|
Packit |
4e8bc4 |
* @param name the name of the object cache. This name may be used for debug purposes
|
|
Packit |
4e8bc4 |
* and may help you track down what kind of object you have problems with
|
|
Packit |
4e8bc4 |
* (buffer overruns, leakage etc)
|
|
Packit |
4e8bc4 |
* @param bufsize the size of each object in the cache
|
|
Packit |
4e8bc4 |
* @param align the alignment requirements of the objects in the cache.
|
|
Packit |
4e8bc4 |
* @param constructor the function to be called to initialize memory when we need
|
|
Packit |
4e8bc4 |
* to allocate more memory from the os.
|
|
Packit |
4e8bc4 |
* @param destructor the function to be called before we release the memory back
|
|
Packit |
4e8bc4 |
* to the os.
|
|
Packit |
4e8bc4 |
* @return a handle to an object cache if successful, NULL otherwise.
|
|
Packit |
4e8bc4 |
*/
|
|
Packit |
4e8bc4 |
cache_t* cache_create(const char* name, size_t bufsize, size_t align,
|
|
Packit |
4e8bc4 |
cache_constructor_t* constructor,
|
|
Packit |
4e8bc4 |
cache_destructor_t* destructor);
|
|
Packit |
4e8bc4 |
/**
|
|
Packit |
4e8bc4 |
* Destroy an object cache.
|
|
Packit |
4e8bc4 |
*
|
|
Packit |
4e8bc4 |
* Destroy and invalidate an object cache. You should return all buffers allocated
|
|
Packit |
4e8bc4 |
* with cache_alloc by using cache_free before calling this function. Not doing
|
|
Packit |
4e8bc4 |
* so results in undefined behavior (the buffers may or may not be invalidated)
|
|
Packit |
4e8bc4 |
*
|
|
Packit |
4e8bc4 |
* @param handle the handle to the object cache to destroy.
|
|
Packit |
4e8bc4 |
*/
|
|
Packit |
4e8bc4 |
void cache_destroy(cache_t* handle);
|
|
Packit |
4e8bc4 |
/**
|
|
Packit |
4e8bc4 |
* Allocate an object from the cache.
|
|
Packit |
4e8bc4 |
*
|
|
Packit |
4e8bc4 |
* @param handle the handle to the object cache to allocate from
|
|
Packit |
4e8bc4 |
* @return a pointer to an initialized object from the cache, or NULL if
|
|
Packit |
4e8bc4 |
* the allocation cannot be satisfied.
|
|
Packit |
4e8bc4 |
*/
|
|
Packit |
4e8bc4 |
void* cache_alloc(cache_t* handle);
|
|
Packit |
4e8bc4 |
void* do_cache_alloc(cache_t* handle);
|
|
Packit |
4e8bc4 |
/**
|
|
Packit |
4e8bc4 |
* Return an object back to the cache.
|
|
Packit |
4e8bc4 |
*
|
|
Packit |
4e8bc4 |
* The caller should return the object in an initialized state so that
|
|
Packit |
4e8bc4 |
* the object may be returned in an expected state from cache_alloc.
|
|
Packit |
4e8bc4 |
*
|
|
Packit |
4e8bc4 |
* @param handle handle to the object cache to return the object to
|
|
Packit |
4e8bc4 |
* @param ptr pointer to the object to return.
|
|
Packit |
4e8bc4 |
*/
|
|
Packit |
4e8bc4 |
void cache_free(cache_t* handle, void* ptr);
|
|
Packit |
4e8bc4 |
void do_cache_free(cache_t* handle, void* ptr);
|
|
Packit |
4e8bc4 |
#endif
|
|
Packit |
4e8bc4 |
|
|
Packit |
4e8bc4 |
#endif
|