Blame cache.h

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