Kohi Game Engine
linear_allocator.h File Reference

This file contains the linear allocator implementation. More...

#include "defines.h"

Go to the source code of this file.

Data Structures

struct  linear_allocator
 The data structure for a linear allocator. More...
 

Typedefs

typedef struct linear_allocator linear_allocator
 The data structure for a linear allocator. More...
 

Functions

KAPI void linear_allocator_create (u64 total_size, void *memory, linear_allocator *out_allocator)
 Creates a linear allocator of the given size. More...
 
KAPI void linear_allocator_destroy (linear_allocator *allocator)
 Destroys the given allocator. If the allocator owns its memory, it is freed at this time. More...
 
KAPI void * linear_allocator_allocate (linear_allocator *allocator, u64 size)
 Allocates the given amount from the allocator. More...
 
KAPI void linear_allocator_free_all (linear_allocator *allocator, b8 clear)
 Frees everything in the allocator, effectively moving its pointer back to the beginning. Does not free internal memory, if owned. Only resets the pointer. More...
 

Detailed Description

This file contains the linear allocator implementation.

Author
Travis Vroman (travi.nosp@m.s@ko.nosp@m.hieng.nosp@m.ine..nosp@m.com)

A linear allocator allocates memory from its internal block of memory in a linear fashion. That is, one after another, moving a pointer along as it goes. A linear allocator can perform allocations of any size, but allocation sizes are not stored, and thus allocations made in this way are not individually freeable. Only the entire thing can be freed. This comes with the benefit of speed at a cost of flexibility.

Version
1.0
Date
2022-01-10

Typedef Documentation

◆ linear_allocator

The data structure for a linear allocator.

Function Documentation

◆ linear_allocator_allocate()

KAPI void* linear_allocator_allocate ( linear_allocator allocator,
u64  size 
)

Allocates the given amount from the allocator.

Parameters
allocatorA pointer to the allocator to allocate from.
sizeThe size to be allocated.
Returns
A pointer to a block of memory as allocated. If this fails, 0 is returned.

◆ linear_allocator_create()

KAPI void linear_allocator_create ( u64  total_size,
void *  memory,
linear_allocator out_allocator 
)

Creates a linear allocator of the given size.

Parameters
total_sizeThe total amount in bytes the allocator will hold.
memoryAllocated block of memory matching size above, or 0. If 0, a dynamic allocation is performed and this allocator is considered to own that memory.
out_allocatorA pointer to hold the new allocator.

◆ linear_allocator_destroy()

KAPI void linear_allocator_destroy ( linear_allocator allocator)

Destroys the given allocator. If the allocator owns its memory, it is freed at this time.

Parameters
allocatorA pointer to the allocator to be destroyed.

◆ linear_allocator_free_all()

KAPI void linear_allocator_free_all ( linear_allocator allocator,
b8  clear 
)

Frees everything in the allocator, effectively moving its pointer back to the beginning. Does not free internal memory, if owned. Only resets the pointer.

Parameters
allocatorA pointer to the allocator to free.
clearIndicates whether or not to clear/zero the memory. Enabling this obviously takes more processing power.