src/cx/mempool.h

Fri, 07 Jul 2023 18:41:44 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 07 Jul 2023 18:41:44 +0200
changeset 742
bcf788f3f6f5
parent 727
d92a59f5d261
child 759
475335643af4
permissions
-rw-r--r--

change cxMempoolDestroy doc

universe@571 1 /*
universe@571 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@571 3 *
universe@571 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@571 5 *
universe@571 6 * Redistribution and use in source and binary forms, with or without
universe@571 7 * modification, are permitted provided that the following conditions are met:
universe@571 8 *
universe@571 9 * 1. Redistributions of source code must retain the above copyright
universe@571 10 * notice, this list of conditions and the following disclaimer.
universe@571 11 *
universe@571 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@571 13 * notice, this list of conditions and the following disclaimer in the
universe@571 14 * documentation and/or other materials provided with the distribution.
universe@571 15 *
universe@571 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@571 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@571 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@571 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@571 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@571 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@571 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@571 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@571 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@571 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@571 26 * POSSIBILITY OF SUCH DAMAGE.
universe@571 27 */
universe@571 28 /**
universe@571 29 * \file mempool.h
universe@571 30 * \brief Interface for memory pool implementations.
universe@571 31 * \author Mike Becker
universe@571 32 * \author Olaf Wintermann
universe@571 33 * \version 3.0
universe@571 34 * \copyright 2-Clause BSD License
universe@571 35 */
universe@571 36
universe@571 37 #ifndef UCX_MEMPOOL_H
universe@571 38 #define UCX_MEMPOOL_H
universe@571 39
universe@681 40 #include "common.h"
universe@571 41 #include "allocator.h"
universe@571 42
universe@571 43 #ifdef __cplusplus
universe@571 44 extern "C" {
universe@571 45 #endif
universe@571 46
universe@727 47 /** Internal structure for pooled memory. */
universe@727 48 struct cx_mempool_memory_s;
universe@571 49
universe@571 50 /**
universe@571 51 * The basic structure of a memory pool.
universe@571 52 * Should be the first member of an actual memory pool implementation.
universe@571 53 */
universe@571 54 struct cx_mempool_s {
universe@727 55 /** The provided allocator. */
universe@727 56 CxAllocator const *allocator;
universe@727 57
universe@571 58 /**
universe@727 59 * A destructor that shall be automatically registered for newly allocated memory.
universe@727 60 * This destructor MUST NOT free the memory.
universe@571 61 */
universe@727 62 cx_destructor_func auto_destr;
universe@727 63
universe@727 64 /** Array of pooled memory. */
universe@727 65 struct cx_mempool_memory_s **data;
universe@727 66
universe@727 67 /** Number of pooled memory items. */
universe@727 68 size_t size;
universe@727 69
universe@727 70 /** Memory pool capacity. */
universe@727 71 size_t capacity;
universe@571 72 };
universe@571 73
universe@571 74 /**
universe@571 75 * Common type for all memory pool implementations.
universe@571 76 */
universe@571 77 typedef struct cx_mempool_s CxMempool;
universe@571 78
universe@571 79 /**
universe@727 80 * Creates an array-based memory pool with a shared destructor function.
universe@727 81 *
universe@727 82 * This destructor MUST NOT free the memory.
universe@727 83 *
universe@727 84 * @param capacity the initial capacity of the pool
universe@727 85 * @param destr the destructor function to use for allocated memory
universe@727 86 * @return the created memory pool or \c NULL if allocation failed
universe@571 87 */
universe@727 88 __attribute__((__warn_unused_result__))
universe@727 89 CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr);
universe@571 90
universe@727 91 /**
universe@727 92 * Creates a basic array-based memory pool.
universe@727 93 *
universe@727 94 * @param capacity the initial capacity of the pool
universe@727 95 * @return the created memory pool or \c NULL if allocation failed
universe@727 96 */
universe@727 97 __attribute__((__warn_unused_result__))
universe@727 98 static inline CxMempool *cxBasicMempoolCreate(size_t capacity) {
universe@727 99 return cxMempoolCreate(capacity, NULL);
universe@727 100 }
universe@571 101
universe@571 102 /**
olaf@742 103 * Destroys a memory pool and frees the managed memory.
universe@571 104 *
universe@571 105 * @param pool the memory pool to destroy
universe@571 106 */
universe@571 107 __attribute__((__nonnull__))
universe@727 108 void cxMempoolDestroy(CxMempool *pool);
universe@571 109
universe@571 110 /**
universe@727 111 * Sets the destructor function for a specific allocated memory object.
universe@571 112 *
universe@727 113 * If the memory is not managed by a UCX memory pool, the behavior is undefined.
universe@727 114 * The destructor MUST NOT free the memory.
universe@571 115 *
universe@727 116 * @param memory the object allocated in the pool
universe@571 117 * @param fnc the destructor function
universe@571 118 */
universe@571 119 __attribute__((__nonnull__))
universe@727 120 void cxMempoolSetDestructor(
universe@727 121 void *memory,
universe@727 122 cx_destructor_func fnc
universe@727 123 );
universe@727 124
universe@727 125 /**
universe@727 126 * Registers foreign memory with this pool.
universe@727 127 *
universe@727 128 * The destructor, in contrast to memory allocated by the pool, MUST free the memory.
universe@727 129 *
universe@727 130 * A small portion of memory will be allocated to register the information in the pool.
universe@727 131 * If that allocation fails, this function will return non-zero.
universe@727 132 *
universe@727 133 * @param pool the pool
universe@727 134 * @param memory the object allocated in the pool
universe@727 135 * @param destr the destructor function
universe@727 136 * @return zero on success, non-zero on failure
universe@727 137 */
universe@727 138 __attribute__((__nonnull__))
universe@727 139 int cxMempoolRegister(
universe@571 140 CxMempool *pool,
universe@571 141 void *memory,
universe@727 142 cx_destructor_func destr
universe@727 143 );
universe@571 144
universe@571 145 #ifdef __cplusplus
universe@571 146 } // extern "C"
universe@571 147 #endif
universe@571 148
universe@571 149 #endif // UCX_MEMPOOL_H

mercurial