| 1 |
/* File: universal_memory_arena.inl; Copyright and License: see below */ |
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
|
| 5 |
static inline void universal_memory_arena_init ( universal_memory_arena_t *this_, |
| 6 |
void* mem_buf_start, |
| 7 |
size_t mem_buf_size ) |
| 8 |
{ |
| 9 |
assert( mem_buf_start != NULL ); |
| 10 |
(*this_).int_buf_start = mem_buf_start; |
| 11 |
(*this_).int_buf_size = mem_buf_size / sizeof(int); |
| 12 |
(*this_).int_buf_used = 0; |
| 13 |
} |
| 14 |
|
| 15 |
static inline void universal_memory_arena_destroy ( universal_memory_arena_t *this_ ) |
| 16 |
{ |
| 17 |
assert( (*this_).int_buf_start != NULL ); |
| 18 |
(*this_).int_buf_start = NULL; |
| 19 |
} |
| 20 |
|
| 21 |
static inline void universal_memory_arena_reset ( universal_memory_arena_t *this_ ) |
| 22 |
{ |
| 23 |
(*this_).int_buf_used = 0; |
| 24 |
} |
| 25 |
|
| 26 |
static inline u8_error_t universal_memory_arena_get_block ( universal_memory_arena_t *this_, |
| 27 |
size_t block_size, |
| 28 |
void **out_block ) |
| 29 |
{ |
| 30 |
assert( out_block != NULL ); |
| 31 |
u8_error_t err = U8_ERROR_NONE; |
| 32 |
|
| 33 |
const size_t requested_ints = ( block_size + sizeof(int) - 1 ) / sizeof(int); |
| 34 |
|
| 35 |
if (( (*this_).int_buf_used + requested_ints ) > (*this_).int_buf_size ) |
| 36 |
{ |
| 37 |
err = U8_ERROR_ARRAY_BUFFER_EXCEEDED; |
| 38 |
*out_block = NULL; |
| 39 |
} |
| 40 |
else |
| 41 |
{ |
| 42 |
err = U8_ERROR_NONE; |
| 43 |
*out_block = &( (*this_).int_buf_start[(*this_).int_buf_used] ); |
| 44 |
(*this_).int_buf_used += requested_ints; |
| 45 |
} |
| 46 |
|
| 47 |
return err; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/* |
| 52 |
Copyright 2021-2026 Andreas Warnke |
| 53 |
|
| 54 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 55 |
you may not use this file except in compliance with the License. |
| 56 |
You may obtain a copy of the License at |
| 57 |
|
| 58 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 59 |
|
| 60 |
Unless required by applicable law or agreed to in writing, software |
| 61 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 62 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 63 |
See the License for the specific language governing permissions and |
| 64 |
limitations under the License. |
| 65 |
*/ |