This file contains a free list, used for custom memory allocation tracking.
More...
Go to the source code of this file.
|
struct | freelist |
| A data structure to be used alongside an allocator for dynamic memory allocation. Tracks free ranges of memory. More...
|
|
|
typedef struct freelist | freelist |
| A data structure to be used alongside an allocator for dynamic memory allocation. Tracks free ranges of memory. More...
|
|
|
KAPI void | freelist_create (u64 total_size, u64 *memory_requirement, void *memory, freelist *out_list) |
| Creates a new freelist or obtains the memory requirement for one. Call twice; once passing 0 to memory to obtain memory requirement, and a second time passing an allocated block to memory. More...
|
|
KAPI void | freelist_destroy (freelist *list) |
| Destroys the provided list. More...
|
|
KAPI b8 | freelist_allocate_block (freelist *list, u64 size, u64 *out_offset) |
| Attempts to find a free block of memory of the given size. More...
|
|
KAPI b8 | freelist_free_block (freelist *list, u64 size, u64 offset) |
| Attempts to free a block of memory at the given offset, and of the given size. Can fail if invalid data is passed. More...
|
|
KAPI b8 | freelist_resize (freelist *list, u64 *memory_requirement, void *new_memory, u64 new_size, void **out_old_memory) |
| Attempts to resize the provided freelist to the given size. Internal data is copied to the new block of memory. The old block must be freed after this call. NOTE: New size must be greater than the existing size of the given list. NOTE: Should be called twice; once to query the memory requirement (passing new_memory=0), and a second time to actually resize the list. More...
|
|
KAPI void | freelist_clear (freelist *list) |
| Clears the free list. More...
|
|
KAPI u64 | freelist_free_space (freelist *list) |
| Returns the amount of free space in this list. NOTE: Since this has to iterate the entire internal list, this can be an expensive operation. Use sparingly. More...
|
|
This file contains a free list, used for custom memory allocation tracking.
- Author
- your name (you@d.nosp@m.omai.nosp@m.n.com)
- Version
- 0.1
- Date
- 2022-01-12
- Copyright
- Copyright (c) 2022
◆ freelist
A data structure to be used alongside an allocator for dynamic memory allocation. Tracks free ranges of memory.
◆ freelist_allocate_block()
Attempts to find a free block of memory of the given size.
- Parameters
-
list | A pointer to the list to search. |
size | The size to allocate. |
out_offset | A pointer to hold the offset to the allocated memory. |
- Returns
- b8 True if a block of memory was found and allocated; otherwise false.
◆ freelist_clear()
Clears the free list.
- Parameters
-
list | The list to be cleared. |
◆ freelist_create()
KAPI void freelist_create |
( |
u64 |
total_size, |
|
|
u64 * |
memory_requirement, |
|
|
void * |
memory, |
|
|
freelist * |
out_list |
|
) |
| |
Creates a new freelist or obtains the memory requirement for one. Call twice; once passing 0 to memory to obtain memory requirement, and a second time passing an allocated block to memory.
- Parameters
-
total_size | The total size in bytes that the free list should track. |
memory_requirement | A pointer to hold memory requirement for the free list itself. |
memory | 0, or a pre-allocated block of memory for the free list to use. |
out_list | A pointer to hold the created free list. |
◆ freelist_destroy()
Destroys the provided list.
- Parameters
-
list | The list to be destroyed. |
◆ freelist_free_block()
Attempts to free a block of memory at the given offset, and of the given size. Can fail if invalid data is passed.
- Parameters
-
list | A pointer to the list to free from. |
size | The size to be freed. |
offset | The offset to free at. |
- Returns
- b8 True if successful; otherwise false. False should be treated as an error.
◆ freelist_free_space()
Returns the amount of free space in this list. NOTE: Since this has to iterate the entire internal list, this can be an expensive operation. Use sparingly.
- Parameters
-
list | A pointer to the list to obtain from. |
- Returns
- The amount of free space in bytes.
◆ freelist_resize()
KAPI b8 freelist_resize |
( |
freelist * |
list, |
|
|
u64 * |
memory_requirement, |
|
|
void * |
new_memory, |
|
|
u64 |
new_size, |
|
|
void ** |
out_old_memory |
|
) |
| |
Attempts to resize the provided freelist to the given size. Internal data is copied to the new block of memory. The old block must be freed after this call. NOTE: New size must be greater than the existing size of the given list. NOTE: Should be called twice; once to query the memory requirement (passing new_memory=0), and a second time to actually resize the list.
- Parameters
-
list | A pointer to the list to be resized. |
memory_requirement | A pointer to hold the amount of memory required for the resize. |
new_memory | The new block of state memory. Set to 0 if only querying memory_requirement. |
new_size | The new size. Must be greater than the size of the provided list. |
out_old_memory | A pointer to hold the old block of memory so that it may be freed after this call. |
- Returns
- True on success; otherwise false.