Kohi Game Engine
dynamic_allocator.h File Reference

Contains the implementation of the dynamic allocator. More...

#include "defines.h"

Go to the source code of this file.

Data Structures

struct  dynamic_allocator
 The dynamic allocator structure. More...
 

Typedefs

typedef struct dynamic_allocator dynamic_allocator
 The dynamic allocator structure. More...
 

Functions

KAPI b8 dynamic_allocator_create (u64 total_size, u64 *memory_requirement, void *memory, dynamic_allocator *out_allocator)
 Creates a new dynamic allocator. Should be called twice; once to obtain the memory amount required (passing memory=0), and a second time with memory being set to an allocated block. More...
 
KAPI b8 dynamic_allocator_destroy (dynamic_allocator *allocator)
 Destroys the given allocator. More...
 
KAPI void * dynamic_allocator_allocate (dynamic_allocator *allocator, u64 size)
 Allocates the given amount of memory from the provided allocator. More...
 
KAPI void * dynamic_allocator_allocate_aligned (dynamic_allocator *allocator, u64 size, u16 alignment)
 Allocates the given amount of aligned memory from the provided allocator. More...
 
KAPI b8 dynamic_allocator_free (dynamic_allocator *allocator, void *block, u64 size)
 Frees the given block of memory. More...
 
KAPI b8 dynamic_allocator_free_aligned (dynamic_allocator *allocator, void *block)
 Frees the given block of aligned memory. Technically the same as calling dynamic_allocator_free, but here for API consistency. No size is required. More...
 
KAPI b8 dynamic_allocator_get_size_alignment (void *block, u64 *out_size, u16 *out_alignment)
 Obtains the size and alignment of the given block of memory. Can fail if invalid data is passed. More...
 
KAPI u64 dynamic_allocator_free_space (dynamic_allocator *allocator)
 Obtains the amount of free space left in the provided allocator. More...
 
KAPI u64 dynamic_allocator_total_space (dynamic_allocator *allocator)
 Obtains the amount of total space originally available in the provided allocator. More...
 
KAPI u64 dynamic_allocator_header_size (void)
 

Detailed Description

Contains the implementation of the dynamic allocator.

Author
Travis Vroman (travi.nosp@m.s@ko.nosp@m.hieng.nosp@m.ine..nosp@m.com)
Version
1.0
Date
2022-01-25

Typedef Documentation

◆ dynamic_allocator

The dynamic allocator structure.

Function Documentation

◆ dynamic_allocator_allocate()

KAPI void* dynamic_allocator_allocate ( dynamic_allocator allocator,
u64  size 
)

Allocates the given amount of memory from the provided allocator.

Parameters
allocatorA pointer to the allocator to allocate from.
sizeThe amount in bytes to be allocated.
Returns
The allocated block of memory unless this operation fails, then 0.

◆ dynamic_allocator_allocate_aligned()

KAPI void* dynamic_allocator_allocate_aligned ( dynamic_allocator allocator,
u64  size,
u16  alignment 
)

Allocates the given amount of aligned memory from the provided allocator.

Parameters
allocatorA pointer to the allocator to allocate from.
sizeThe amount in bytes to be allocated.
alignmentThe alignment in bytes.
Returns
The aligned, allocated block of memory unless this operation fails, then 0.

◆ dynamic_allocator_create()

KAPI b8 dynamic_allocator_create ( u64  total_size,
u64 memory_requirement,
void *  memory,
dynamic_allocator out_allocator 
)

Creates a new dynamic allocator. Should be called twice; once to obtain the memory amount required (passing memory=0), and a second time with memory being set to an allocated block.

Parameters
total_sizeThe total size in bytes the allocator should hold. Note this size does not include the size of the internal state.
memory_requirementA pointer to hold the required memory for the internal state plus total_size.
memoryAn allocated block of memory, or 0 if just obtaining the requirement.
out_allocatorA pointer to hold the allocator.
Returns
True on success; otherwise false.

◆ dynamic_allocator_destroy()

KAPI b8 dynamic_allocator_destroy ( dynamic_allocator allocator)

Destroys the given allocator.

Parameters
allocatorA pointer to the allocator to be destroyed.
Returns
True on success; otherwise false.

◆ dynamic_allocator_free()

KAPI b8 dynamic_allocator_free ( dynamic_allocator allocator,
void *  block,
u64  size 
)

Frees the given block of memory.

Parameters
allocatorA pointer to the allocator to free from.
blockThe block to be freed. Must have been allocated by the provided allocator.
sizeThe size of the block.
Returns
True on success; otherwise false.

◆ dynamic_allocator_free_aligned()

KAPI b8 dynamic_allocator_free_aligned ( dynamic_allocator allocator,
void *  block 
)

Frees the given block of aligned memory. Technically the same as calling dynamic_allocator_free, but here for API consistency. No size is required.

Parameters
allocatorA pointer to the allocator to free from.
blockThe block to be freed. Must have been allocated by the provided allocator.
Returns
True on success; otherwise false.

◆ dynamic_allocator_free_space()

KAPI u64 dynamic_allocator_free_space ( dynamic_allocator allocator)

Obtains the amount of free space left in the provided allocator.

Parameters
allocatorA pointer to the allocator to be examined.
Returns
The amount of free space in bytes.

◆ dynamic_allocator_get_size_alignment()

KAPI b8 dynamic_allocator_get_size_alignment ( void *  block,
u64 out_size,
u16 out_alignment 
)

Obtains the size and alignment of the given block of memory. Can fail if invalid data is passed.

Parameters
blockThe block of memory.
out_sizeA pointer to hold the size.
out_alignmentA pointer to hold the alignment.
Returns
True on success; otherwise false.

◆ dynamic_allocator_header_size()

KAPI u64 dynamic_allocator_header_size ( void  )

Obtains the size of the internal allocation header. This is really only used for unit testing purposes.

◆ dynamic_allocator_total_space()

KAPI u64 dynamic_allocator_total_space ( dynamic_allocator allocator)

Obtains the amount of total space originally available in the provided allocator.

Parameters
allocatorA pointer to the allocator to be examined.
Returns
The total amount of space originally available in bytes.