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@727: /** Internal structure for pooled memory. */ universe@727: struct cx_mempool_memory_s; 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@727: /** The provided allocator. */ universe@727: CxAllocator const *allocator; universe@727: universe@571: /** universe@727: * A destructor that shall be automatically registered for newly allocated memory. universe@727: * This destructor MUST NOT free the memory. universe@571: */ universe@727: cx_destructor_func auto_destr; universe@727: universe@727: /** Array of pooled memory. */ universe@727: struct cx_mempool_memory_s **data; universe@727: universe@727: /** Number of pooled memory items. */ universe@727: size_t size; universe@727: universe@727: /** Memory pool capacity. */ universe@727: size_t capacity; 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@727: * Creates an array-based memory pool with a shared destructor function. universe@727: * universe@727: * This destructor MUST NOT free the memory. universe@727: * universe@727: * @param capacity the initial capacity of the pool universe@727: * @param destr the destructor function to use for allocated memory universe@727: * @return the created memory pool or \c NULL if allocation failed universe@571: */ universe@727: __attribute__((__warn_unused_result__)) universe@727: CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr); universe@571: universe@727: /** universe@727: * Creates a basic array-based memory pool. universe@727: * universe@727: * @param capacity the initial capacity of the pool universe@727: * @return the created memory pool or \c NULL if allocation failed universe@727: */ universe@727: __attribute__((__warn_unused_result__)) universe@727: static inline CxMempool *cxBasicMempoolCreate(size_t capacity) { universe@727: return cxMempoolCreate(capacity, NULL); universe@727: } universe@571: universe@571: /** olaf@742: * Destroys a memory pool and frees the managed memory. universe@571: * universe@571: * @param pool the memory pool to destroy universe@571: */ universe@571: __attribute__((__nonnull__)) universe@727: void cxMempoolDestroy(CxMempool *pool); universe@571: universe@571: /** universe@727: * Sets the destructor function for a specific allocated memory object. universe@571: * universe@727: * If the memory is not managed by a UCX memory pool, the behavior is undefined. universe@727: * The destructor MUST NOT free the memory. universe@571: * universe@727: * @param memory the object allocated in the pool universe@571: * @param fnc the destructor function universe@571: */ universe@571: __attribute__((__nonnull__)) universe@727: void cxMempoolSetDestructor( universe@727: void *memory, universe@727: cx_destructor_func fnc universe@727: ); universe@727: universe@727: /** universe@727: * Registers foreign memory with this pool. universe@727: * universe@727: * The destructor, in contrast to memory allocated by the pool, MUST free the memory. universe@727: * universe@727: * A small portion of memory will be allocated to register the information in the pool. universe@727: * If that allocation fails, this function will return non-zero. universe@727: * universe@727: * @param pool the pool universe@727: * @param memory the object allocated in the pool universe@727: * @param destr the destructor function universe@727: * @return zero on success, non-zero on failure universe@727: */ universe@727: __attribute__((__nonnull__)) universe@727: int cxMempoolRegister( universe@571: CxMempool *pool, universe@571: void *memory, universe@727: cx_destructor_func destr universe@727: ); universe@571: universe@571: #ifdef __cplusplus universe@571: } // extern "C" universe@571: #endif universe@571: universe@571: #endif // UCX_MEMPOOL_H