mem_allocator.h (5645B)
1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr) 2 * 3 * The RSys library is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published 5 * by the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * The RSys library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #ifndef MEM_ALLOCATOR_H 17 #define MEM_ALLOCATOR_H 18 19 #include "rsys.h" 20 #include <stddef.h> 21 22 /******************************************************************************* 23 * Memory allocator interface 24 ******************************************************************************/ 25 struct mem_allocator { 26 void* (*alloc) 27 (void* data, 28 const size_t size, 29 const char* filename, 30 const unsigned int fileline); 31 32 void* (*calloc) 33 (void* data, 34 const size_t nbelmts, 35 const size_t size, 36 const char* filename, 37 const unsigned int fileline); 38 39 void* (*realloc) 40 (void* data, 41 void* mem, 42 const size_t size, 43 const char* filename, 44 const unsigned int fileline); 45 46 void* (*alloc_aligned) 47 (void* data, 48 const size_t size, 49 const size_t alignment, 50 const char* filename, 51 const unsigned int fileline); 52 53 void (*rm) 54 (void* data, 55 void* mem); 56 57 size_t (*mem_size) 58 (void* data, 59 void* mem); 60 61 size_t (*allocated_size) 62 (const void* data); 63 64 size_t (*dump) /* Return the real dump len (without the null char) */ 65 (const void* data, 66 char* dump, 67 const size_t max_dump_len); /* Include the null char */ 68 69 void* data; 70 }; 71 72 /******************************************************************************* 73 * Helper macros 74 ******************************************************************************/ 75 #define MEM_ALLOC(Allocator, Size) \ 76 ((Allocator)->alloc((Allocator)->data, (Size), __FILE__, __LINE__)) 77 78 #define MEM_CALLOC(Allocator, Nb, Size) \ 79 ((Allocator)->calloc((Allocator)->data, (Nb), (Size), __FILE__, __LINE__)) 80 81 #define MEM_REALLOC(Allocator, Mem, Size) \ 82 ((Allocator)->realloc((Allocator)->data, (Mem), (Size), __FILE__, __LINE__)) 83 84 #define MEM_ALLOC_ALIGNED(Allocator, Size, Alignment) \ 85 ((Allocator)->alloc_aligned \ 86 ((Allocator)->data, (Size), (Alignment), __FILE__, __LINE__)) 87 88 #define MEM_RM(Allocator, Mem) \ 89 ((Allocator)->rm((Allocator)->data, (void*)(Mem))) 90 91 #define MEM_SIZE(Allocator, Mem) \ 92 ((Allocator)->mem_size((Allocator)->data, (Mem))) 93 94 #define MEM_ALLOCATED_SIZE(Allocator) \ 95 ((Allocator)->allocated_size((Allocator)->data)) 96 97 #define MEM_DUMP(Allocator, Msg, MaxLen) \ 98 ((Allocator)->dump((Allocator)->data, (Msg), (MaxLen))) 99 100 101 BEGIN_DECLS 102 103 /* Default allocator. */ 104 RSYS_API struct mem_allocator mem_default_allocator; 105 106 /******************************************************************************* 107 * Regular allocation functions. 108 ******************************************************************************/ 109 RSYS_API void* mem_alloc(const size_t size); 110 RSYS_API void* mem_calloc(const size_t nelmts, const size_t size); 111 RSYS_API void* mem_realloc(void* ptr, const size_t size); 112 RSYS_API void* mem_alloc_aligned(const size_t size, const size_t alignment); 113 RSYS_API void mem_rm(void* ptr); 114 RSYS_API size_t mem_size(void* ptr); 115 RSYS_API size_t mem_allocated_size(void); 116 117 /******************************************************************************* 118 * Proxy allocator - Register the filename and the fileline of the allocation. 119 ******************************************************************************/ 120 RSYS_API res_T 121 mem_init_proxy_allocator 122 (struct mem_allocator* proxy, 123 struct mem_allocator* allocator); 124 125 RSYS_API void 126 mem_shutdown_proxy_allocator 127 (struct mem_allocator* proxy_allocator); 128 129 /******************************************************************************* 130 * Regular allocator - Wrap the regular allocation functions. 131 ******************************************************************************/ 132 RSYS_API res_T 133 mem_init_regular_allocator 134 (struct mem_allocator* allocator); 135 136 RSYS_API void 137 mem_shutdown_regular_allocator 138 (struct mem_allocator* allocator); 139 140 /******************************************************************************* 141 * LIFO allocator - Allocate the memory in a preallocated memory chunk wrt to a 142 * LIFO pattern; the last allocated entry is the first that can be deallocated. 143 * If the entry to delete is not on top of the LIFO stack, it is marked as 144 * freed and will be effectively removed when it will be on top of the LIFO 145 * stack. 146 ******************************************************************************/ 147 RSYS_API res_T 148 mem_init_lifo_allocator 149 (struct mem_allocator* lifo_allocator, 150 struct mem_allocator* allocator, 151 const size_t size); /* Overall size that can be allocated by the allocator */ 152 153 RSYS_API void 154 mem_shutdown_lifo_allocator 155 (struct mem_allocator* lifo_allocator); 156 157 END_DECLS 158 159 #endif /* MEM_ALLOCATOR_H */