Kohi Game Engine
kmemory.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include "defines.h"
19 
20 // Interface for a frame allocator.
21 typedef struct frame_allocator_int {
22  void* (*allocate)(u64 size);
23  void (*free)(void* block, u64 size);
24  void (*free_all)(void);
26 
28 typedef enum memory_tag {
29  // For temporary use. Should be assigned one of the below or have a new tag created.
50  // "External" vulkan allocations, for reporting purposes only.
54  // Representation of GPU-local/vram
67 
70 
76 
82 
87 
95 KAPI void* kallocate(u64 size, memory_tag tag);
96 
103 #define KALLOC_TYPE(type, mem_tag) (type*)kallocate(sizeof(type), mem_tag)
104 
113 #define KFREE_TYPE(block, type, mem_tag) kfree(block, sizeof(type), mem_tag)
114 
122 #define KALLOC_TYPE_CARRAY(type, count) (type*)kallocate(sizeof(type) * count, MEMORY_TAG_ARRAY)
123 
132 #define KFREE_TYPE_CARRAY(block, type, count) kfree(block, sizeof(type) * count, MEMORY_TAG_ARRAY)
133 
139 #define KRESIZE_ARRAY(array, type, old_count, new_count) \
140  { \
141  type* temp = KALLOC_TYPE_CARRAY(type, new_count); \
142  if (old_count && array) { \
143  KCOPY_TYPE_CARRAY(temp, array, type, old_count); \
144  KFREE_TYPE_CARRAY(array, type, old_count); \
145  } \
146  array = temp; \
147  }
148 
158 KAPI void* kallocate_aligned(u64 size, u16 alignment, memory_tag tag);
159 
169 
179 KAPI void* kreallocate(void* block, u64 old_size, u64 new_size, memory_tag tag);
180 
190 #define KREALLOC_TYPE_CARRAY(block, type, old_count, new_count) (type*)kreallocate(block, sizeof(type) * old_count, sizeof(type) * new_count, MEMORY_TAG_ARRAY)
191 
204 KAPI void* kreallocate_aligned(void* block, u64 old_size, u64 new_size, u16 alignment, memory_tag tag);
205 
215 KAPI void kreallocate_report(u64 old_size, u64 new_size, memory_tag tag);
216 
223 KAPI void kfree(void* block, u64 size, memory_tag tag);
224 
231 KAPI void kfree_aligned(void* block, u64 size, u16 alignment, memory_tag tag);
232 
241 KAPI void kfree_report(u64 size, memory_tag tag);
242 
252 KAPI b8 kmemory_get_size_alignment(void* block, u64* out_size, u16* out_alignment);
253 
260 KAPI void* kzero_memory(void* block, u64 size);
261 
269 KAPI void* kcopy_memory(void* dest, const void* source, u64 size);
270 
271 #define KCOPY_TYPE(dest, source, type) kcopy_memory(dest, source, sizeof(type))
272 #define KCOPY_TYPE_CARRAY(dest, source, type, count) kcopy_memory(dest, source, sizeof(type) * count)
273 
281 KAPI void* kset_memory(void* dest, i32 value, u64 size);
282 
290 
296 
307 
318 KAPI b8 unpack_u8_from_u32(u32 n, u8* x, u8* y, u8* z, u8* w);
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:205
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:25
_Bool b8
8-bit boolean type
Definition: defines.h:58
signed int i32
Signed 32-bit integer.
Definition: defines.h:39
unsigned short u16
Unsigned 16-bit integer.
Definition: defines.h:22
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:19
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 alloc...
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....
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.
memory_tag
Tags to indicate the usage of memory allocations made in this system.
Definition: kmemory.h:28
@ MEMORY_TAG_ARRAY
Definition: kmemory.h:31
@ MEMORY_TAG_DIRECT3D
Definition: kmemory.h:52
@ MEMORY_TAG_OPENGL
Definition: kmemory.h:53
@ MEMORY_TAG_ASSET
Definition: kmemory.h:66
@ MEMORY_TAG_SERIALIZER
Definition: kmemory.h:65
@ MEMORY_TAG_REGISTRY
Definition: kmemory.h:62
@ MEMORY_TAG_UI
Definition: kmemory.h:60
@ MEMORY_TAG_HASHTABLE
Definition: kmemory.h:59
@ MEMORY_TAG_DICT
Definition: kmemory.h:34
@ MEMORY_TAG_VULKAN_EXT
Definition: kmemory.h:51
@ MEMORY_TAG_BITMAP_FONT
Definition: kmemory.h:56
@ MEMORY_TAG_SCENE
Definition: kmemory.h:47
@ MEMORY_TAG_GPU_LOCAL
Definition: kmemory.h:55
@ MEMORY_TAG_RING_QUEUE
Definition: kmemory.h:35
@ MEMORY_TAG_BST
Definition: kmemory.h:36
@ MEMORY_TAG_MATERIAL_INSTANCE
Definition: kmemory.h:41
@ MEMORY_TAG_DARRAY
Definition: kmemory.h:33
@ MEMORY_TAG_RENDERER
Definition: kmemory.h:42
@ MEMORY_TAG_KEYMAP
Definition: kmemory.h:58
@ MEMORY_TAG_UNKNOWN
Definition: kmemory.h:30
@ MEMORY_TAG_VULKAN
Definition: kmemory.h:49
@ MEMORY_TAG_PLUGIN
Definition: kmemory.h:63
@ MEMORY_TAG_PLATFORM
Definition: kmemory.h:64
@ MEMORY_TAG_AUDIO
Definition: kmemory.h:61
@ MEMORY_TAG_JOB
Definition: kmemory.h:39
@ MEMORY_TAG_STRING
Definition: kmemory.h:37
@ MEMORY_TAG_ENGINE
Definition: kmemory.h:38
@ MEMORY_TAG_LINEAR_ALLOCATOR
Definition: kmemory.h:32
@ MEMORY_TAG_RESOURCE
Definition: kmemory.h:48
@ MEMORY_TAG_ENTITY_NODE
Definition: kmemory.h:46
@ MEMORY_TAG_SYSTEM_FONT
Definition: kmemory.h:57
@ MEMORY_TAG_TRANSFORM
Definition: kmemory.h:44
@ MEMORY_TAG_MAX_TAGS
Definition: kmemory.h:68
@ MEMORY_TAG_ENTITY
Definition: kmemory.h:45
@ MEMORY_TAG_GAME
Definition: kmemory.h:43
@ MEMORY_TAG_TEXTURE
Definition: kmemory.h:40
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.
KAPI void * kset_memory(void *dest, i32 value, u64 size)
Sets the bytes of memory located at dest to value over the given size.
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 item...
KAPI b8 memory_system_initialize(memory_system_configuration config)
Initializes the memory system.
struct memory_system_configuration memory_system_configuration
The configuration for the memory system.
KAPI u64 get_memory_alloc_count(void)
Obtains the number of times kallocate was called since the memory system was initialized.
KAPI void kfree(void *block, u64 size, memory_tag tag)
Frees the given block, and untracks its size from the given tag.
struct frame_allocator_int frame_allocator_int
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 ...
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 gi...
KAPI u32 pack_u8_into_u32(u8 x, u8 y, u8 z, u8 w)
Packs the values of 4 u8s into a single u32.
KAPI void memory_system_shutdown(void)
Shuts down the memory system.
KAPI void * kzero_memory(void *block, u64 size)
Zeroes out the provided memory block.
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 provi...
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 bloc...
KAPI b8 unpack_u8_from_u32(u32 n, u8 *x, u8 *y, u8 *z, u8 *w)
Attempts to unpack 4 u8s from a u32.
KAPI void kallocate_report(u64 size, memory_tag tag)
Reports an allocation associated with the application, but made externally. This can be done for item...
KAPI char * get_memory_usage_str(void)
Obtains a string containing a "printout" of memory usage, categorized by memory tag....
Definition: kmemory.h:21
void(* free)(void *block, u64 size)
Definition: kmemory.h:23
void(* free_all)(void)
Definition: kmemory.h:24
The configuration for the memory system.
Definition: kmemory.h:72
u64 total_alloc_size
The total memory size in byes used by the internal allocator for this system.
Definition: kmemory.h:74