| 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
| 27 */ |
27 */ |
| 28 /** |
28 /** |
| 29 * \file mempool.h |
29 * @file mempool.h |
| 30 * \brief Interface for memory pool implementations. |
30 * @brief Interface for memory pool implementations. |
| 31 * \author Mike Becker |
31 * @author Mike Becker |
| 32 * \author Olaf Wintermann |
32 * @author Olaf Wintermann |
| 33 * \copyright 2-Clause BSD License |
33 * @copyright 2-Clause BSD License |
| 34 */ |
34 */ |
| 35 |
35 |
| 36 #ifndef UCX_MEMPOOL_H |
36 #ifndef UCX_MEMPOOL_H |
| 37 #define UCX_MEMPOOL_H |
37 #define UCX_MEMPOOL_H |
| 38 |
38 |
| 78 /** |
78 /** |
| 79 * Deallocates a memory pool and frees the managed memory. |
79 * Deallocates a memory pool and frees the managed memory. |
| 80 * |
80 * |
| 81 * @param pool the memory pool to free |
81 * @param pool the memory pool to free |
| 82 */ |
82 */ |
| |
83 cx_attr_export |
| 83 void cxMempoolFree(CxMempool *pool); |
84 void cxMempoolFree(CxMempool *pool); |
| 84 |
85 |
| 85 /** |
86 /** |
| 86 * Creates an array-based memory pool with a shared destructor function. |
87 * Creates an array-based memory pool with a shared destructor function. |
| 87 * |
88 * |
| 88 * This destructor MUST NOT free the memory. |
89 * This destructor MUST NOT free the memory. |
| 89 * |
90 * |
| 90 * @param capacity the initial capacity of the pool |
91 * @param capacity the initial capacity of the pool |
| 91 * @param destr optional destructor function to use for allocated memory |
92 * @param destr optional destructor function to use for allocated memory |
| 92 * @return the created memory pool or \c NULL if allocation failed |
93 * @return the created memory pool or @c NULL if allocation failed |
| 93 */ |
94 */ |
| 94 cx_attr_nodiscard |
95 cx_attr_nodiscard |
| 95 cx_attr_malloc |
96 cx_attr_malloc |
| 96 cx_attr_dealloc(cxMempoolFree, 1) |
97 cx_attr_dealloc(cxMempoolFree, 1) |
| |
98 cx_attr_export |
| 97 CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr); |
99 CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr); |
| 98 |
100 |
| 99 /** |
101 /** |
| 100 * Creates a basic array-based memory pool. |
102 * Creates a basic array-based memory pool. |
| 101 * |
103 * |
| 102 * @param capacity the initial capacity of the pool |
104 * @param capacity (@c size_t) the initial capacity of the pool |
| 103 * @return the created memory pool or \c NULL if allocation failed |
105 * @return (@c CxMempool*) the created memory pool or @c NULL if allocation failed |
| 104 */ |
106 */ |
| 105 #define cxBasicMempoolCreate(capacity) cxMempoolCreate(capacity, NULL) |
107 #define cxMempoolCreateSimple(capacity) cxMempoolCreate(capacity, NULL) |
| 106 |
108 |
| 107 /** |
109 /** |
| 108 * Sets the destructor function for a specific allocated memory object. |
110 * Sets the destructor function for a specific allocated memory object. |
| 109 * |
111 * |
| 110 * If the memory is not managed by a UCX memory pool, the behavior is undefined. |
112 * If the memory is not managed by a UCX memory pool, the behavior is undefined. |
| 139 * If that allocation fails, this function will return non-zero. |
143 * If that allocation fails, this function will return non-zero. |
| 140 * |
144 * |
| 141 * @param pool the pool |
145 * @param pool the pool |
| 142 * @param memory the object to register (MUST NOT be already allocated in the pool) |
146 * @param memory the object to register (MUST NOT be already allocated in the pool) |
| 143 * @param destr the destructor function |
147 * @param destr the destructor function |
| 144 * @return zero on success, non-zero on failure |
148 * @retval zero success |
| |
149 * @retval non-zero failure |
| 145 */ |
150 */ |
| 146 cx_attr_nonnull |
151 cx_attr_nonnull |
| |
152 cx_attr_export |
| 147 int cxMempoolRegister( |
153 int cxMempoolRegister( |
| 148 CxMempool *pool, |
154 CxMempool *pool, |
| 149 void *memory, |
155 void *memory, |
| 150 cx_destructor_func destr |
156 cx_destructor_func destr |
| 151 ); |
157 ); |