#include <pthread.h>
Go to the source code of this file.
Classes | |
struct | cache_t |
Definition of the structure to keep track of the internal details of the cache allocator. More... | |
Typedefs | |
typedef int | cache_constructor_t (void *obj, void *notused1, int notused2) |
Constructor used to initialize allocated objects. | |
typedef void | cache_destructor_t (void *obj, void *notused) |
Destructor used to clean up allocated objects before they are returned to the operating system. | |
Functions | |
cache_t * | cache_create (const char *name, size_t bufsize, size_t align, cache_constructor_t *constructor, cache_destructor_t *destructor) |
Create an object cache. | |
void | cache_destroy (cache_t *handle) |
Destroy an object cache. | |
void * | cache_alloc (cache_t *handle) |
Allocate an object from the cache. | |
void | cache_free (cache_t *handle, void *ptr) |
Return an object back to the cache. | |
Variables | |
int | cache_error |
typedef int cache_constructor_t(void *obj, void *notused1, int notused2) |
Constructor used to initialize allocated objects.
obj | pointer to the object to initialized. | |
notused1 | This parameter is currently not used. | |
notused2 | This parameter is currently not used. |
typedef void cache_destructor_t(void *obj, void *notused) |
Destructor used to clean up allocated objects before they are returned to the operating system.
obj | pointer to the object to initialized. | |
notused1 | This parameter is currently not used. | |
notused2 | This parameter is currently not used. |
void* cache_alloc | ( | cache_t * | handle | ) |
Allocate an object from the cache.
handle | the handle to the object cache to allocate from |
cache_t* cache_create | ( | const char * | name, | |
size_t | bufsize, | |||
size_t | align, | |||
cache_constructor_t * | constructor, | |||
cache_destructor_t * | destructor | |||
) |
Create an object cache.
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 do any syncrhonization in the application code.
name | the name of the object cache. This name may be used for debug purposes and may help you track down what kind of object you have problems with (buffer overruns, leakage etc) | |
bufsize | the size of each object in the cache | |
align | the alignment requirements of the objects in the cache. | |
constructor | the function to be called to initialize memory when we need to allocate more memory from the os. | |
destructor | the function to be called before we release the memory back to the os. |
void cache_destroy | ( | cache_t * | handle | ) |
Destroy an object cache.
Destroy and invalidate an object cache. You should return all buffers allocated 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)
handle | the handle to the object cache to destroy. |
void cache_free | ( | cache_t * | handle, | |
void * | ptr | |||
) |
Return an object back to the cache.
The caller should return the object in an initialized state so that the object may be returned in an expected state from cache_alloc.
handle | handle to the object cache to return the object to | |
ptr | pointer to the object to return. |
int cache_error |