Arena allocator.
More...
#include <assert.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "align.h"
Go to the source code of this file.
|
|
#define | FUNCTION_DEFINITIONS |
| | Define the functions.
|
| |
|
#define | TYPE_DEFINITIONS |
| | Define the types.
|
| |
|
#define | FUNCTION_LINKAGE |
| | Specify function linkage e.g. static inline.
|
| |
| #define | PASTE(a, b) |
| | Paste two tokens together.
|
| |
| #define | XPASTE(a, b) |
| | First expand tokens, then paste them together.
|
| |
| #define | JOIN(a, b) |
| | First expand tokens, then paste them together with a _ in between.
|
| |
| #define | IS_POW2(X) |
| | Macro to check if a number is a power of two.
|
| |
|
| arena_state_type | arena_state_save (arena_type *arena_ptr) |
| | Save the arena state temporarily.
|
| |
| void | arena_state_restore (arena_state_type prev_state) |
| | Restore the arena state.
|
| |
| void | arena_init (arena_type *self, const size_t len, unsigned char *backing_buf) |
| | Initialize the arena.
|
| |
| void | arena_deallocate_all (arena_type *self) |
| | Deallocate all allocations in the arena.
|
| |
| void * | arena_allocate_aligned (arena_type *self, const size_t alignment, const size_t size) |
| | Get the pointer to a chunk of the arena. With specific alignment.
|
| |
| void * | arena_allocate (arena_type *self, const size_t size) |
| | Get the pointer to a chunk of the arena.
|
| |
| void * | arena_reallocate_aligned (arena_type *self, void *old_ptr_, const size_t alignment, const size_t old_size, const size_t new_size) |
| | Reallocate a previously allocated chunk in the arena. With specific aligment.
|
| |
| void * | arena_reallocate (arena_type *self, void *old_ptr, const size_t old_size, const size_t new_size) |
| | Reallocate a previously allocated chunk in the arena.
|
| |
Arena allocator.
For a comprehensive source, read:
◆ IS_POW2
Value:((X) != 0 && ((X) & ((X) - 1)) == 0)
Macro to check if a number is a power of two.
- Parameters
-
- Returns
- A boolean value indicating whether the number is a power of two.
◆ JOIN
Value:
#define XPASTE(a, b)
First expand tokens, then paste them together.
Definition fstack_template.h:42
First expand tokens, then paste them together with a _ in between.
◆ PASTE
Value:
Paste two tokens together.
◆ XPASTE
Value:
#define PASTE(a, b)
Paste two tokens together.
Definition fstack_template.h:34
First expand tokens, then paste them together.
◆ arena_allocate()
| void * arena_allocate |
( |
arena_type * | self, |
|
|
const size_t | size ) |
Get the pointer to a chunk of the arena.
- Parameters
-
| [in] | self | The arena pointer. |
| [in] | size | The section size in bytes. |
- Returns
- A pointer to a zeroed-out memory chunk.
- Return values
-
| NULL | If the arena doesn't have enough memory for the allocation. |
- Examples
- arena_example.c.
◆ arena_allocate_aligned()
| void * arena_allocate_aligned |
( |
arena_type * | self, |
|
|
const size_t | alignment, |
|
|
const size_t | size ) |
Get the pointer to a chunk of the arena. With specific alignment.
- Parameters
-
| [in] | self | arena pointer. |
| [in] | alignment | alignment size |
| [in] | size | chunk size |
- Returns
- A pointer to a zeroed-out memory chunk.
- Return values
-
| NULL | If the arena doesn't have enough memory for the allocation. |
- Examples
- arena_example.c.
◆ arena_deallocate_all()
| void arena_deallocate_all |
( |
arena_type * | self | ) |
|
Deallocate all allocations in the arena.
- Parameters
-
- Examples
- arena_example.c.
◆ arena_init()
| void arena_init |
( |
arena_type * | self, |
|
|
const size_t | len, |
|
|
unsigned char * | backing_buf ) |
Initialize the arena.
- Parameters
-
| [in] | self | Arena pointer. |
| [in] | len | Backing buffer length. |
| [in] | backing_buf | Backing buffer. |
- Examples
- arena_example.c.
◆ arena_reallocate()
| void * arena_reallocate |
( |
arena_type * | self, |
|
|
void * | old_ptr, |
|
|
const size_t | old_size, |
|
|
const size_t | new_size ) |
Reallocate a previously allocated chunk in the arena.
- Parameters
-
| [in] | self | The arena pointer. |
| [in] | old_ptr | Pointer to the buffer to reallocate |
| [in] | old_size | Old size. |
| [in] | new_size | New size to grow/shrink to. |
- Returns
- A pointer to the reallocated memory chunk.
- Return values
-
| NULL | If arena doesn't have enough memory for the reallocation or invalid parameters are given. |
- Examples
- arena_example.c.
◆ arena_reallocate_aligned()
| void * arena_reallocate_aligned |
( |
arena_type * | self, |
|
|
void * | old_ptr_, |
|
|
const size_t | alignment, |
|
|
const size_t | old_size, |
|
|
const size_t | new_size ) |
Reallocate a previously allocated chunk in the arena. With specific aligment.
- Parameters
-
| [in] | self | Arena pointer. |
| [in] | old_ptr_ | Pointer to the buffer to reallocate |
| [in] | alignment | Alignment size. |
| [in] | old_size | Old size. |
| [in] | new_size | New size to grow/shrink to. |
- Returns
- A pointer to the reallocated memory chunk.
- Return values
-
| NULL | If arena doesn't have enough memory for the reallocation or invalid parameters are given. |
- Examples
- arena_example.c.
◆ arena_state_restore()
| void arena_state_restore |
( |
arena_state_type | prev_state | ) |
|
Restore the arena state.
- Parameters
-
| [in] | prev_state | Stored arena state. |
- Examples
- arena_example.c.
◆ arena_state_save()
| arena_state_type arena_state_save |
( |
arena_type * | arena_ptr | ) |
|
Save the arena state temporarily.
- Parameters
-
| [in] | arena_ptr | The arena whose state to save. |
- Examples
- arena_example.c.