Kohi Game Engine
freelist.h File Reference

This file contains a free list, used for custom memory allocation tracking. More...

#include "defines.h"

Go to the source code of this file.

Data Structures

struct  freelist
 A data structure to be used alongside an allocator for dynamic memory allocation. Tracks free ranges of memory. More...
 

Typedefs

typedef struct freelist freelist
 A data structure to be used alongside an allocator for dynamic memory allocation. Tracks free ranges of memory. More...
 

Functions

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...
 

Detailed Description

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

Typedef Documentation

◆ freelist

typedef struct freelist freelist

A data structure to be used alongside an allocator for dynamic memory allocation. Tracks free ranges of memory.

Function Documentation

◆ freelist_allocate_block()

KAPI b8 freelist_allocate_block ( freelist list,
u64  size,
u64 out_offset 
)

Attempts to find a free block of memory of the given size.

Parameters
listA pointer to the list to search.
sizeThe size to allocate.
out_offsetA 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()

KAPI void freelist_clear ( freelist list)

Clears the free list.

Parameters
listThe 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_sizeThe total size in bytes that the free list should track.
memory_requirementA pointer to hold memory requirement for the free list itself.
memory0, or a pre-allocated block of memory for the free list to use.
out_listA pointer to hold the created free list.

◆ freelist_destroy()

KAPI void freelist_destroy ( freelist list)

Destroys the provided list.

Parameters
listThe list to be destroyed.

◆ freelist_free_block()

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.

Parameters
listA pointer to the list to free from.
sizeThe size to be freed.
offsetThe offset to free at.
Returns
b8 True if successful; otherwise false. False should be treated as an error.

◆ freelist_free_space()

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.

Parameters
listA 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
listA pointer to the list to be resized.
memory_requirementA pointer to hold the amount of memory required for the resize.
new_memoryThe new block of state memory. Set to 0 if only querying memory_requirement.
new_sizeThe new size. Must be greater than the size of the provided list.
out_old_memoryA pointer to hold the old block of memory so that it may be freed after this call.
Returns
True on success; otherwise false.