universe@571: /* universe@571: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@571: * universe@571: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@571: * universe@571: * Redistribution and use in source and binary forms, with or without universe@571: * modification, are permitted provided that the following conditions are met: universe@571: * universe@571: * 1. Redistributions of source code must retain the above copyright universe@571: * notice, this list of conditions and the following disclaimer. universe@571: * universe@571: * 2. Redistributions in binary form must reproduce the above copyright universe@571: * notice, this list of conditions and the following disclaimer in the universe@571: * documentation and/or other materials provided with the distribution. universe@571: * universe@571: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@571: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@571: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@571: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@571: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@571: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@571: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@571: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@571: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@571: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@571: * POSSIBILITY OF SUCH DAMAGE. universe@571: */ universe@571: /** universe@571: * \file mempool.h universe@571: * \brief Interface for memory pool implementations. universe@571: * \author Mike Becker universe@571: * \author Olaf Wintermann universe@571: * \version 3.0 universe@571: * \copyright 2-Clause BSD License universe@571: */ universe@571: universe@571: #ifndef UCX_MEMPOOL_H universe@571: #define UCX_MEMPOOL_H universe@571: universe@681: #include "common.h" universe@571: #include "allocator.h" universe@571: universe@571: #ifdef __cplusplus universe@571: extern "C" { universe@571: #endif universe@571: universe@571: /** universe@571: * Memory pool class type. universe@571: */ universe@571: typedef struct cx_mempool_class_s cx_mempool_class; universe@571: universe@571: /** universe@571: * The basic structure of a memory pool. universe@571: * Should be the first member of an actual memory pool implementation. universe@571: */ universe@571: struct cx_mempool_s { universe@571: /** universe@571: * The pool class definition. universe@571: */ universe@571: cx_mempool_class *cl; universe@571: /** universe@571: * The provided allocator. universe@571: */ universe@571: CxAllocator const *allocator; universe@571: }; universe@571: universe@571: /** universe@571: * Common type for all memory pool implementations. universe@571: */ universe@571: typedef struct cx_mempool_s CxMempool; universe@571: universe@571: /** universe@571: * The class definition for a memory pool. universe@571: */ universe@571: struct cx_mempool_class_s { universe@571: /** Member function for destroying the pool. */ universe@571: __attribute__((__nonnull__)) universe@571: void (*destroy)(CxMempool *pool); universe@571: universe@571: /** Member function for setting a destructor. */ universe@571: __attribute__((__nonnull__)) universe@571: void (*set_destructor)( universe@571: CxMempool *pool, universe@571: void *memory, universe@571: cx_destructor_func fnc universe@571: ); universe@571: }; universe@571: universe@571: universe@571: /** universe@571: * Destroys a memory pool including their contents. universe@571: * universe@571: * @param pool the memory pool to destroy universe@571: */ universe@571: __attribute__((__nonnull__)) universe@571: static inline void cxMempoolDestroy(CxMempool *pool) { universe@571: pool->cl->destroy(pool); universe@571: } universe@571: universe@571: /** universe@571: * Sets a destructor function for an allocated memory object. universe@571: * universe@571: * If the memory is not managed by the pool, the behavior is undefined. universe@571: * universe@571: * @param pool the pool universe@571: * @param memory the objected allocated in the pool universe@571: * @param fnc the destructor function universe@571: */ universe@571: __attribute__((__nonnull__)) universe@571: static inline void cxMempoolSetDestructor( universe@571: CxMempool *pool, universe@571: void *memory, universe@571: cx_destructor_func fnc universe@571: ) { universe@571: pool->cl->set_destructor(pool, memory, fnc); universe@571: } universe@571: universe@571: #ifdef __cplusplus universe@571: } // extern "C" universe@571: #endif universe@571: universe@571: #endif // UCX_MEMPOOL_H