| 1 | /* File: universal_memory_arena.h; Copyright and License: see below */ |
| 2 | |
| 3 | #ifndef UNIVERSAL_MEMORY_ARENA_H |
| 4 | #define UNIVERSAL_MEMORY_ARENA_H |
| 5 | |
| 6 | /* public file for the doxygen documentation: */ |
| 7 | /*! |
| 8 | * \file |
| 9 | * \brief implements a memory region from which memory blocks can be allocated dynamically. |
| 10 | * |
| 11 | * The memory region itself may be statically allocated. |
| 12 | * These functions are not multi-thread safe. |
| 13 | */ |
| 14 | |
| 15 | #include "u8/u8_error.h" |
| 16 | |
| 17 | /*! |
| 18 | * \brief attributes of the universal_memory_arena |
| 19 | */ |
| 20 | struct universal_memory_arena_struct { |
| 21 | int* int_buf_start; /*!< memory buffer start */ |
| 22 | size_t int_buf_size; /*!< memory buffer size in sizeof(int) */ |
| 23 | size_t int_buf_used; /*!< part of the memory buffer that is already in use, unit: sizeof(int) */ |
| 24 | }; |
| 25 | |
| 26 | typedef struct universal_memory_arena_struct universal_memory_arena_t; |
| 27 | |
| 28 | /*! |
| 29 | * \brief initializes the universal_memory_arena_t |
| 30 | * |
| 31 | * \param this_ pointer to own object attributes |
| 32 | * \param mem_buf_start address of memory buffer. |
| 33 | * To optimize performance, this shall be int-aligned. |
| 34 | * \param mem_buf_size size of the memory buffer |
| 35 | */ |
| 36 | static inline void universal_memory_arena_init ( universal_memory_arena_t *this_, |
| 37 | void* mem_buf_start, |
| 38 | size_t mem_buf_size |
| 39 | ); |
| 40 | |
| 41 | /*! |
| 42 | * \brief destroys the universal_memory_arena_t |
| 43 | * |
| 44 | * \param this_ pointer to own object attributes |
| 45 | */ |
| 46 | static inline void universal_memory_arena_destroy ( universal_memory_arena_t *this_ ); |
| 47 | |
| 48 | /*! |
| 49 | * \brief resets the memory buffer, all memory is treated as unused |
| 50 | * |
| 51 | * \param this_ pointer to own object attributes |
| 52 | */ |
| 53 | static inline void universal_memory_arena_reset ( universal_memory_arena_t *this_ ); |
| 54 | |
| 55 | /*! |
| 56 | * \brief fetches a memory block from a memory region |
| 57 | * |
| 58 | * \param this_ pointer to own object attributes |
| 59 | * \param block_size size of the buffer to allocate |
| 60 | * \param out_block start address of the allocated block, NULL if none available |
| 61 | * \return U8_ERROR_ARRAY_BUFFER_EXCEEDED in case there is not sufficient memory available, U8_ERROR_NONE in case of success |
| 62 | */ |
| 63 | static inline u8_error_t universal_memory_arena_get_block ( universal_memory_arena_t *this_, |
| 64 | size_t block_size, |
| 65 | void **out_block |
| 66 | ); |
| 67 | |
| 68 | #include "u8arena/universal_memory_arena.inl" |
| 69 | |
| 70 | #endif /* UNIVERSAL_MEMORY_ARENA_H */ |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | Copyright 2021-2026 Andreas Warnke |
| 75 | |
| 76 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 77 | you may not use this file except in compliance with the License. |
| 78 | You may obtain a copy of the License at |
| 79 | |
| 80 | http://www.apache.org/licenses/LICENSE-2.0 |
| 81 | |
| 82 | Unless required by applicable law or agreed to in writing, software |
| 83 | distributed under the License is distributed on an "AS IS" BASIS, |
| 84 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 85 | See the License for the specific language governing permissions and |
| 86 | limitations under the License. |
| 87 | */ |