kiba-engine
Data Structures | Typedefs | Enumerations | Functions
allocator.h File Reference

Central header providing allocator functionality. More...

#include <kiba/defines.h>
Include dependency graph for allocator.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  allocator_impl
 Structure holding a specific allocator type's implementation. More...
 
struct  allocator
 Central allocator structure. More...
 

Typedefs

typedef enum allocator_type allocator_type
 Available allocator types. More...
 
typedef b8(* allocator_impl_create_fn) (struct allocator *alloc, usize size)
 Create a specific allocator. More...
 
typedef void(* allocator_impl_destroy_fn) (struct allocator *alloc)
 Destroy the allocator. More...
 
typedef void *(* allocator_impl_allocate_fn) (struct allocator *alloc, usize size)
 Allocate memory. More...
 
typedef void(* allocator_impl_free_fn) (struct allocator *alloc, void *mem, usize size)
 Give memory back to the allocator. More...
 
typedef void(* allocator_impl_free_all_fn) (struct allocator *alloc)
 Give all allocated memory back to the allocator. More...
 
typedef struct allocator_impl allocator_impl
 Structure holding a specific allocator type's implementation.
 
typedef struct allocator allocator
 Central allocator structure. More...
 

Enumerations

enum  allocator_type { ALLOCATOR_LINEAR , ALLOCATOR_FREE_LIST , ALLOCATOR_SYSTEM }
 Available allocator types. More...
 

Functions

KB_API b8 allocator_create (allocator *alloc, allocator_type type, usize size)
 Create an allocator of a specific type. More...
 
KB_API b8 allocator_create_custom (allocator *alloc, allocator_impl_create_fn create_fn, usize size)
 Create an allocator based on a custom create function. More...
 
KB_API void allocator_destroy (allocator *alloc)
 Destroy an allocator. More...
 
KB_API void * allocator_allocate (allocator *alloc, usize size)
 Allocate unaligned memory. More...
 
KB_API void * allocator_allocate_aligned (allocator *alloc, usize size, usize alignment)
 Allocate aligned memory. More...
 
KB_API void * allocator_reallocate (allocator *alloc, void *original, usize new_size)
 Reallocate memory to unaligned block. More...
 
KB_API void * allocator_reallocate_aligned (allocator *alloc, void *original, usize new_size, usize alignment)
 Rellocate aligned memory. More...
 
KB_API void allocator_free (allocator *alloc, void *mem)
 Give back memory to the allocator. More...
 
KB_API void allocator_free_all (allocator *alloc)
 Reset the allocator. More...
 
KB_API void allocator_implementation (allocator *alloc, allocator_impl_destroy_fn destroy, allocator_impl_allocate_fn allocate, allocator_impl_free_fn free, allocator_impl_free_all_fn free_all)
 Set allocator implementation. More...
 

Detailed Description

Central header providing allocator functionality.

Provides general interface to create and interact with allocators.

Definition in file allocator.h.

Typedef Documentation

◆ allocator

typedef struct allocator allocator

Central allocator structure.

This type is used to interact with the allocator subsystem.

◆ allocator_impl_allocate_fn

typedef void*(* allocator_impl_allocate_fn) (struct allocator *alloc, usize size)

Allocate memory.

Parameters
allocthe allocator to allocate from
sizesize of the memory block to allocate
Returns
pointer to allocated memory block or KB_NULL if unsuccessful

Definition at line 54 of file allocator.h.

◆ allocator_impl_create_fn

typedef b8(* allocator_impl_create_fn) (struct allocator *alloc, usize size)

Create a specific allocator.

See also
allocator_create
allocator_implementation

Must initialize the state and provide implementations for all functions.

Parameters
allocallocator to populate with the allocator implementation
sizeminimum size of the initial memory block to manage
Returns
true if succesful, false otherwise

Definition at line 38 of file allocator.h.

◆ allocator_impl_destroy_fn

typedef void(* allocator_impl_destroy_fn) (struct allocator *alloc)

Destroy the allocator.

Cleans up the state. Also returns the managed memory to the memory system.

Parameters
allocthe allocator to destroy

Definition at line 46 of file allocator.h.

◆ allocator_impl_free_all_fn

typedef void(* allocator_impl_free_all_fn) (struct allocator *alloc)

Give all allocated memory back to the allocator.

Parameters
allocthe allocator to free all allocations from

Definition at line 68 of file allocator.h.

◆ allocator_impl_free_fn

typedef void(* allocator_impl_free_fn) (struct allocator *alloc, void *mem, usize size)

Give memory back to the allocator.

Parameters
allocthe allocator the memory was initially requested from
mempointer to the block of memory to give back
sizenumber of bytes that should be given back to the allocator

Definition at line 62 of file allocator.h.

◆ allocator_type

Available allocator types.

This type is used to determine the specific implementation of an allocator.

Enumeration Type Documentation

◆ allocator_type

Available allocator types.

This type is used to determine the specific implementation of an allocator.

Definition at line 18 of file allocator.h.

Function Documentation

◆ allocator_allocate()

KB_API void* allocator_allocate ( allocator alloc,
usize  size 
)

Allocate unaligned memory.

The returned memory block will not be aligned. (aligned to 1 byte boundaries)

See also
allocator_allocate_aligned
Parameters
allocallocator to use
sizenumber of bytes to allocate
Returns
pointer to allocated memory or KB_NULL if not successful

Definition at line 92 of file allocator.c.

Here is the call graph for this function:

◆ allocator_allocate_aligned()

KB_API void* allocator_allocate_aligned ( allocator alloc,
usize  size,
usize  alignment 
)

Allocate aligned memory.

Tries to find the necessary space in its managed memory block(s). If there is not enough space left and the allocator cannot get more then KB_NULL will be returned.

Alignment means the returned address will be a multiple of the specified alignment.

Parameters
allocallocator to use
sizenumber of bytes to allocate
alignmentthe number of bytes (powers of 2) to align to
Returns
pointer to allocated memory or KB_NULL if not successful

Definition at line 94 of file allocator.c.

Here is the call graph for this function:

◆ allocator_create()

KB_API b8 allocator_create ( allocator alloc,
allocator_type  type,
usize  size 
)

Create an allocator of a specific type.

Parameters
allocallocator structure to be filled
typekind of allocator implementation to be used
sizeminimum size in bytes the allocator should be able to provide
Returns
true if successful, false otherwise

Definition at line 66 of file allocator.c.

◆ allocator_create_custom()

KB_API b8 allocator_create_custom ( allocator alloc,
allocator_impl_create_fn  create_fn,
usize  size 
)

Create an allocator based on a custom create function.

See also
allocator_impl_create_fn
Parameters
allocallocator structure to be filled
create_fncustom allocator creation procedure
sizeminimum size in bytes the allocator should be able to provide
Returns
true if successful, false otherwise

Definition at line 79 of file allocator.c.

◆ allocator_destroy()

KB_API void allocator_destroy ( allocator alloc)

Destroy an allocator.

Release all structures created by the allocator.

Parameters
allocallocator to destroy

Definition at line 84 of file allocator.c.

Here is the call graph for this function:

◆ allocator_free()

KB_API void allocator_free ( allocator alloc,
void *  mem 
)

Give back memory to the allocator.

Does not free the memory for the OS. Instead the allocator may reuse the returned block of memory for future calls to allocator_allocate.

Parameters
allocallocator which initially provided the memory
mempointer to memory to give back

Definition at line 134 of file allocator.c.

Here is the call graph for this function:

◆ allocator_free_all()

KB_API void allocator_free_all ( allocator alloc)

Reset the allocator.

Clears the allocators state besides its managed memory blocks. Does not free the memory for the OS. Instead the allocator may reuse the memory for future calls to allocator_allocate.

Parameters
allocallocator to reset

Definition at line 143 of file allocator.c.

◆ allocator_implementation()

KB_API void allocator_implementation ( allocator alloc,
allocator_impl_destroy_fn  destroy,
allocator_impl_allocate_fn  allocate,
allocator_impl_free_fn  free,
allocator_impl_free_all_fn  free_all 
)

Set allocator implementation.

Parameters
allocallocator to set impl for
destroyimplementation's destroy function
allocateimplementation's allocate function
freeimplementation's free function
free_allimplementation's free_all function

Definition at line 149 of file allocator.c.

◆ allocator_reallocate()

KB_API void* allocator_reallocate ( allocator alloc,
void *  original,
usize  new_size 
)

Reallocate memory to unaligned block.

The returned memory block will retain the same alignment as the oiginal block.

See also
allocator_reallocate_aligned
Parameters
allocallocator to use
originalpointer to the original memory block
new_sizenumber of bytes the reallocated space should contain
Returns
pointer to the reallocated memory or KB_NULL if not successful

Definition at line 107 of file allocator.c.

Here is the call graph for this function:

◆ allocator_reallocate_aligned()

KB_API void* allocator_reallocate_aligned ( allocator alloc,
void *  original,
usize  new_size,
usize  alignment 
)

Rellocate aligned memory.

New memory block contains min(size, <previous block's size>) bytes of original block.

Tries to find the necessary space in its managed memory block(s). If there is not enough space left and the allocator cannot get more then KB_NULL will be returned. The original memory block will remain valid in this case.

Note
Setting the alignment to 0 will cause the new block to retain the alignment of the original block.
Parameters
allocallocator to use
originalpointer to the original memory block
new_sizenumber of bytes the reallocated space should contain
alignmentthe number of bytes (powers of 2) the new block should be aligned to
Returns
pointer to the reallocated memory or KB_NULL if not successful

Definition at line 111 of file allocator.c.

Here is the call graph for this function: