src/cx/mempool.h

Sun, 21 May 2023 14:40:05 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 May 2023 14:40:05 +0200
changeset 707
87eb4bdb2d0e
parent 681
502105523db7
child 727
d92a59f5d261
permissions
-rw-r--r--

fix rehash not valid for non-hash-maps

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@571 47 /**
universe@571 48 * Memory pool class type.
universe@571 49 */
universe@571 50 typedef struct cx_mempool_class_s cx_mempool_class;
universe@571 51
universe@571 52 /**
universe@571 53 * The basic structure of a memory pool.
universe@571 54 * Should be the first member of an actual memory pool implementation.
universe@571 55 */
universe@571 56 struct cx_mempool_s {
universe@571 57 /**
universe@571 58 * The pool class definition.
universe@571 59 */
universe@571 60 cx_mempool_class *cl;
universe@571 61 /**
universe@571 62 * The provided allocator.
universe@571 63 */
universe@571 64 CxAllocator const *allocator;
universe@571 65 };
universe@571 66
universe@571 67 /**
universe@571 68 * Common type for all memory pool implementations.
universe@571 69 */
universe@571 70 typedef struct cx_mempool_s CxMempool;
universe@571 71
universe@571 72 /**
universe@571 73 * The class definition for a memory pool.
universe@571 74 */
universe@571 75 struct cx_mempool_class_s {
universe@571 76 /** Member function for destroying the pool. */
universe@571 77 __attribute__((__nonnull__))
universe@571 78 void (*destroy)(CxMempool *pool);
universe@571 79
universe@571 80 /** Member function for setting a destructor. */
universe@571 81 __attribute__((__nonnull__))
universe@571 82 void (*set_destructor)(
universe@571 83 CxMempool *pool,
universe@571 84 void *memory,
universe@571 85 cx_destructor_func fnc
universe@571 86 );
universe@571 87 };
universe@571 88
universe@571 89
universe@571 90 /**
universe@571 91 * Destroys a memory pool including their contents.
universe@571 92 *
universe@571 93 * @param pool the memory pool to destroy
universe@571 94 */
universe@571 95 __attribute__((__nonnull__))
universe@571 96 static inline void cxMempoolDestroy(CxMempool *pool) {
universe@571 97 pool->cl->destroy(pool);
universe@571 98 }
universe@571 99
universe@571 100 /**
universe@571 101 * Sets a destructor function for an allocated memory object.
universe@571 102 *
universe@571 103 * If the memory is not managed by the pool, the behavior is undefined.
universe@571 104 *
universe@571 105 * @param pool the pool
universe@571 106 * @param memory the objected allocated in the pool
universe@571 107 * @param fnc the destructor function
universe@571 108 */
universe@571 109 __attribute__((__nonnull__))
universe@571 110 static inline void cxMempoolSetDestructor(
universe@571 111 CxMempool *pool,
universe@571 112 void *memory,
universe@571 113 cx_destructor_func fnc
universe@571 114 ) {
universe@571 115 pool->cl->set_destructor(pool, memory, fnc);
universe@571 116 }
universe@571 117
universe@571 118 #ifdef __cplusplus
universe@571 119 } // extern "C"
universe@571 120 #endif
universe@571 121
universe@571 122 #endif // UCX_MEMPOOL_H

mercurial