Kohi Game Engine
|
This file contains the structures and functions of the memory system. This is responsible for memory interaction with the platform layer, such as allocations/frees and tagging of memory allocations. More...
#include "defines.h"
Go to the source code of this file.
Data Structures | |
struct | frame_allocator_int |
struct | memory_system_configuration |
The configuration for the memory system. More... | |
Macros | |
#define | KALLOC_TYPE(type, mem_tag) (type*)kallocate(sizeof(type), mem_tag) |
Dynamically allocates memory for the given type. Also casts to type*. More... | |
#define | KFREE_TYPE(block, type, mem_tag) kfree(block, sizeof(type), mem_tag) |
Frees the given dynamically-allocated memory of the provided type, using the given tag. More... | |
#define | KALLOC_TYPE_CARRAY(type, count) (type*)kallocate(sizeof(type) * count, MEMORY_TAG_ARRAY) |
Dynamically allocates memory for a standard C array of the given type. Also casts to type*. Memory is tagged as MEMORY_TAG_ARRAY. More... | |
#define | KFREE_TYPE_CARRAY(block, type, count) kfree(block, sizeof(type) * count, MEMORY_TAG_ARRAY) |
Frees the given dynamically-allocated array of the provided type, using MEMORY_TAG_ARRAY. More... | |
#define | KRESIZE_ARRAY(array, type, old_count, new_count) |
Resizes the given array of the provided type, also copying the contents of the old array to the new. Performs a new allocation, so the array address will be different. NOTE: new_count must be greater than old_count. More... | |
#define | KREALLOC_TYPE_CARRAY(block, type, old_count, new_count) (type*)kreallocate(block, sizeof(type) * old_count, sizeof(type) * new_count, MEMORY_TAG_ARRAY) |
Dynamically reallocates memory for a standard C array of the given type. Also casts to type*. Memory is tagged as MEMORY_TAG_ARRAY. More... | |
#define | KCOPY_TYPE(dest, source, type) kcopy_memory(dest, source, sizeof(type)) |
#define | KCOPY_TYPE_CARRAY(dest, source, type, count) kcopy_memory(dest, source, sizeof(type) * count) |
Typedefs | |
typedef struct frame_allocator_int | frame_allocator_int |
typedef enum memory_tag | memory_tag |
Tags to indicate the usage of memory allocations made in this system. More... | |
typedef struct memory_system_configuration | memory_system_configuration |
The configuration for the memory system. More... | |
Enumerations | |
enum | memory_tag { MEMORY_TAG_UNKNOWN , MEMORY_TAG_ARRAY , MEMORY_TAG_LINEAR_ALLOCATOR , MEMORY_TAG_DARRAY , MEMORY_TAG_DICT , MEMORY_TAG_RING_QUEUE , MEMORY_TAG_BST , MEMORY_TAG_STRING , MEMORY_TAG_ENGINE , MEMORY_TAG_JOB , MEMORY_TAG_TEXTURE , MEMORY_TAG_MATERIAL_INSTANCE , MEMORY_TAG_RENDERER , MEMORY_TAG_GAME , MEMORY_TAG_TRANSFORM , MEMORY_TAG_ENTITY , MEMORY_TAG_ENTITY_NODE , MEMORY_TAG_SCENE , MEMORY_TAG_RESOURCE , MEMORY_TAG_VULKAN , MEMORY_TAG_VULKAN_EXT , MEMORY_TAG_DIRECT3D , MEMORY_TAG_OPENGL , MEMORY_TAG_GPU_LOCAL , MEMORY_TAG_BITMAP_FONT , MEMORY_TAG_SYSTEM_FONT , MEMORY_TAG_KEYMAP , MEMORY_TAG_HASHTABLE , MEMORY_TAG_UI , MEMORY_TAG_AUDIO , MEMORY_TAG_REGISTRY , MEMORY_TAG_PLUGIN , MEMORY_TAG_PLATFORM , MEMORY_TAG_SERIALIZER , MEMORY_TAG_ASSET , MEMORY_TAG_MAX_TAGS } |
Tags to indicate the usage of memory allocations made in this system. More... | |
Functions | |
KAPI b8 | memory_system_initialize (memory_system_configuration config) |
Initializes the memory system. More... | |
KAPI void | memory_system_shutdown (void) |
Shuts down the memory system. More... | |
KAPI void * | kallocate (u64 size, memory_tag tag) |
Performs a memory allocation from the host of the given size. The allocation is tracked for the provided tag. More... | |
KAPI void * | kallocate_aligned (u64 size, u16 alignment, memory_tag tag) |
Performs an aligned memory allocation from the host of the given size and alignment. The allocation is tracked for the provided tag. NOTE: Memory allocated this way must be freed using kfree_aligned. More... | |
KAPI void | kallocate_report (u64 size, memory_tag tag) |
Reports an allocation associated with the application, but made externally. This can be done for items allocated within 3rd party libraries, for example, to track allocations but not perform them. More... | |
KAPI void * | kreallocate (void *block, u64 old_size, u64 new_size, memory_tag tag) |
Performs a memory reallocation from the host of the given size, and also frees the block of memory given. The reallocation is tracked for the provided tag. More... | |
KAPI void * | kreallocate_aligned (void *block, u64 old_size, u64 new_size, u16 alignment, memory_tag tag) |
Performs a memory reallocation from the host of the given size and alignment, and also frees the block of memory given. The reallocation is tracked for the provided tag. NOTE: Memory allocated this way must be freed using kfree_aligned. More... | |
KAPI void | kreallocate_report (u64 old_size, u64 new_size, memory_tag tag) |
Reports an allocation associated with the application, but made externally. This can be done for items allocated within 3rd party libraries, for example, to track allocations but not perform them. More... | |
KAPI void | kfree (void *block, u64 size, memory_tag tag) |
Frees the given block, and untracks its size from the given tag. More... | |
KAPI void | kfree_aligned (void *block, u64 size, u16 alignment, memory_tag tag) |
Frees the given block, and untracks its size from the given tag. More... | |
KAPI void | kfree_report (u64 size, memory_tag tag) |
Reports a free associated with the application, but made externally. This can be done for items allocated within 3rd party libraries, for example, to track frees but not perform them. More... | |
KAPI b8 | kmemory_get_size_alignment (void *block, u64 *out_size, u16 *out_alignment) |
Returns the size and alignment of the given block of memory. NOTE: A failure result from this method most likely indicates heap corruption. More... | |
KAPI void * | kzero_memory (void *block, u64 size) |
Zeroes out the provided memory block. More... | |
KAPI void * | kcopy_memory (void *dest, const void *source, u64 size) |
Performs a copy of the memory at source to dest of the given size. More... | |
KAPI void * | kset_memory (void *dest, i32 value, u64 size) |
Sets the bytes of memory located at dest to value over the given size. More... | |
KAPI char * | get_memory_usage_str (void) |
Obtains a string containing a "printout" of memory usage, categorized by memory tag. The memory should be freed by the caller. More... | |
KAPI u64 | get_memory_alloc_count (void) |
Obtains the number of times kallocate was called since the memory system was initialized. More... | |
KAPI u32 | pack_u8_into_u32 (u8 x, u8 y, u8 z, u8 w) |
Packs the values of 4 u8s into a single u32. More... | |
KAPI b8 | unpack_u8_from_u32 (u32 n, u8 *x, u8 *y, u8 *z, u8 *w) |
Attempts to unpack 4 u8s from a u32. More... | |
This file contains the structures and functions of the memory system. This is responsible for memory interaction with the platform layer, such as allocations/frees and tagging of memory allocations.
#define KALLOC_TYPE | ( | type, | |
mem_tag | |||
) | (type*)kallocate(sizeof(type), mem_tag) |
Dynamically allocates memory for the given type. Also casts to type*.
type | The type to be used when determining allocation size. |
mem_tag | The memory tag to be used for the allocation. |
#define KALLOC_TYPE_CARRAY | ( | type, | |
count | |||
) | (type*)kallocate(sizeof(type) * count, MEMORY_TAG_ARRAY) |
Dynamically allocates memory for a standard C array of the given type. Also casts to type*. Memory is tagged as MEMORY_TAG_ARRAY.
type | The type to be used when determining allocation size. |
count | The number of elements existing in the array. |
#define KCOPY_TYPE | ( | dest, | |
source, | |||
type | |||
) | kcopy_memory(dest, source, sizeof(type)) |
#define KCOPY_TYPE_CARRAY | ( | dest, | |
source, | |||
type, | |||
count | |||
) | kcopy_memory(dest, source, sizeof(type) * count) |
#define KFREE_TYPE | ( | block, | |
type, | |||
mem_tag | |||
) | kfree(block, sizeof(type), mem_tag) |
Frees the given dynamically-allocated memory of the provided type, using the given tag.
block | The block of memory to be freed. |
type | The type to be used when determining allocation size. |
mem_tag | The memory tag to be used for the deallocation. |
#define KFREE_TYPE_CARRAY | ( | block, | |
type, | |||
count | |||
) | kfree(block, sizeof(type) * count, MEMORY_TAG_ARRAY) |
Frees the given dynamically-allocated array of the provided type, using MEMORY_TAG_ARRAY.
block | The block of memory to be freed. |
type | The type to be used when determining allocation size. |
count | The number of elements in the array to be freed. |
#define KREALLOC_TYPE_CARRAY | ( | block, | |
type, | |||
old_count, | |||
new_count | |||
) | (type*)kreallocate(block, sizeof(type) * old_count, sizeof(type) * new_count, MEMORY_TAG_ARRAY) |
Dynamically reallocates memory for a standard C array of the given type. Also casts to type*. Memory is tagged as MEMORY_TAG_ARRAY.
block | The block of memory to reallocate. |
type | The type to be used when determining allocation size. |
old_count | The number of elements existing in the array. |
new_count | The number of elements in the resized array. |
#define KRESIZE_ARRAY | ( | array, | |
type, | |||
old_count, | |||
new_count | |||
) |
Resizes the given array of the provided type, also copying the contents of the old array to the new. Performs a new allocation, so the array address will be different. NOTE: new_count must be greater than old_count.
typedef struct frame_allocator_int frame_allocator_int |
typedef struct memory_system_configuration memory_system_configuration |
The configuration for the memory system.
typedef enum memory_tag memory_tag |
Tags to indicate the usage of memory allocations made in this system.
enum memory_tag |
Tags to indicate the usage of memory allocations made in this system.
Obtains the number of times kallocate was called since the memory system was initialized.
KAPI char* get_memory_usage_str | ( | void | ) |
Obtains a string containing a "printout" of memory usage, categorized by memory tag. The memory should be freed by the caller.
KAPI void* kallocate | ( | u64 | size, |
memory_tag | tag | ||
) |
Performs a memory allocation from the host of the given size. The allocation is tracked for the provided tag.
size | The size of the allocation. |
tag | Indicates the use of the allocated block. |
KAPI void* kallocate_aligned | ( | u64 | size, |
u16 | alignment, | ||
memory_tag | tag | ||
) |
Performs an aligned memory allocation from the host of the given size and alignment. The allocation is tracked for the provided tag. NOTE: Memory allocated this way must be freed using kfree_aligned.
size | The size of the allocation. |
alignment | The alignment in bytes. |
tag | Indicates the use of the allocated block. |
KAPI void kallocate_report | ( | u64 | size, |
memory_tag | tag | ||
) |
Reports an allocation associated with the application, but made externally. This can be done for items allocated within 3rd party libraries, for example, to track allocations but not perform them.
size | The size of the allocation. |
tag | Indicates the use of the allocated block. |
Performs a copy of the memory at source to dest of the given size.
dest | A pointer to the destination block of memory to copy to. |
source | A pointer to the source block of memory to copy from. |
size | The amount of memory in bytes to be copied over. |
KAPI void kfree | ( | void * | block, |
u64 | size, | ||
memory_tag | tag | ||
) |
Frees the given block, and untracks its size from the given tag.
block | A pointer to the block of memory to be freed. |
size | The size of the block to be freed. |
tag | The tag indicating the block's use. |
KAPI void kfree_aligned | ( | void * | block, |
u64 | size, | ||
u16 | alignment, | ||
memory_tag | tag | ||
) |
Frees the given block, and untracks its size from the given tag.
block | A pointer to the block of memory to be freed. |
size | The size of the block to be freed. |
tag | The tag indicating the block's use. |
KAPI void kfree_report | ( | u64 | size, |
memory_tag | tag | ||
) |
Reports a free associated with the application, but made externally. This can be done for items allocated within 3rd party libraries, for example, to track frees but not perform them.
size | The size in bytes. |
tag | The tag indicating the block's use. |
Returns the size and alignment of the given block of memory. NOTE: A failure result from this method most likely indicates heap corruption.
block | The memory block. |
out_size | A pointer to hold the size of the block. |
out_alignment | A pointer to hold the alignment of the block. |
KAPI void* kreallocate | ( | void * | block, |
u64 | old_size, | ||
u64 | new_size, | ||
memory_tag | tag | ||
) |
Performs a memory reallocation from the host of the given size, and also frees the block of memory given. The reallocation is tracked for the provided tag.
block | The block of memory to reallocate. |
old_size | The size of the old allocation (that gets freed). |
new_size | The size of the new allocation (that get allocated). |
tag | Indicates the use of the allocated block. |
KAPI void* kreallocate_aligned | ( | void * | block, |
u64 | old_size, | ||
u64 | new_size, | ||
u16 | alignment, | ||
memory_tag | tag | ||
) |
Performs a memory reallocation from the host of the given size and alignment, and also frees the block of memory given. The reallocation is tracked for the provided tag. NOTE: Memory allocated this way must be freed using kfree_aligned.
block | The block of memory to reallocate. |
old_size | The size of the old allocation (that gets freed). |
new_size | The size of the new allocation (that get allocated). |
alignment | The byte alignment to be used for the reallocation. |
tag | Indicates the use of the allocated block. |
KAPI void kreallocate_report | ( | u64 | old_size, |
u64 | new_size, | ||
memory_tag | tag | ||
) |
Reports an allocation associated with the application, but made externally. This can be done for items allocated within 3rd party libraries, for example, to track allocations but not perform them.
old_size | The size of the old allocation. |
new_size | The size of the new allocation. |
tag | Indicates the use of the allocated block. |
Sets the bytes of memory located at dest to value over the given size.
dest | A pointer to the destination block of memory to be set. |
value | The value to be set. |
size | The size in bytes to copy over to. |
Zeroes out the provided memory block.
block | A pointer to the block of memory to be zeroed out. |
size | The size in bytes to zero out. |
A | pointer to the zeroed out block of memory. |
KAPI b8 memory_system_initialize | ( | memory_system_configuration | config | ) |
Initializes the memory system.
config | The configuration for this system. |
KAPI void memory_system_shutdown | ( | void | ) |
Shuts down the memory system.
Packs the values of 4 u8s into a single u32.
x | The first u8 to pack. |
y | The second u8 to pack. |
z | The third u8 to pack. |
w | The fourth u8 to pack. |
Attempts to unpack 4 u8s from a u32.
n | The u32 to extract from. |
x | The first u8 to extract to. Required. |
y | The second u8 to extract to. Required. |
z | The third u8 to extract to. Required. |
w | The fourth u8 to extract to. Required. |