Kohi Game Engine
kmemory.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include "defines.h"
19 
20 // Turn on for per-allocation verification of memory. Has both a performance and memory impact,
21 // so only leave on when troubleshooting.
22 #define MEM_DEBUG_TRACE 1
23 
24 // Interface for a frame allocator.
25 typedef struct frame_allocator_int {
26  void* (*allocate)(u64 size);
27  void (*free)(void* block, u64 size);
28  void (*free_all)(void);
29  u64 (*total_space)(void);
30  u64 (*allocated)(void);
32 
34 typedef enum memory_tag {
35  // For temporary use. Should be assigned one of the below or have a new tag created.
57  // "External" vulkan allocations, for reporting purposes only.
61  // Representation of GPU-local/vram
75 
78 
84 
90 
95 
103 KAPI void* _kallocate(u64 size, memory_tag tag, const char* file, u32 line);
104 
105 #define kallocate(size, tag) _kallocate(size, tag, __FILE__, __LINE__)
106 
113 #define KALLOC_TYPE(type, mem_tag) (type*)kallocate(sizeof(type), mem_tag)
114 
123 #define KFREE_TYPE(block, type, mem_tag) kfree(block, sizeof(type), mem_tag)
124 
132 #define KALLOC_TYPE_CARRAY(type, count) (type*)kallocate(sizeof(type) * count, MEMORY_TAG_ARRAY)
133 
142 #define KFREE_TYPE_CARRAY(block, type, count) \
143  if (block) { \
144  kfree(block, sizeof(type) * count, MEMORY_TAG_ARRAY); \
145  }
146 
152 #define KRESIZE_ARRAY(array, type, old_count, new_count) \
153  { \
154  type* temp = KALLOC_TYPE_CARRAY(type, new_count); \
155  if (old_count && array) { \
156  KCOPY_TYPE_CARRAY(temp, array, type, old_count); \
157  KFREE_TYPE_CARRAY(array, type, old_count); \
158  } \
159  array = temp; \
160  }
161 
171 KAPI void* _kallocate_aligned(u64 size, u16 alignment, memory_tag tag, const char* filename, u32 line);
172 
173 #define kallocate_aligned(size, alignment, tag) _kallocate_aligned(size, alignment, tag, __FILE__, __LINE__)
174 
184 
194 KAPI void* _kreallocate(void* block, u64 old_size, u64 new_size, memory_tag tag, const char* filename, u32 line);
195 
196 #define kreallocate(block, old_size, new_size, tag) _kreallocate(block, old_size, new_size, tag, __FILE__, __LINE__)
197 
207 #define KREALLOC_TYPE_CARRAY(block, type, old_count, new_count) (type*)kreallocate(block, sizeof(type) * (old_count), sizeof(type) * (new_count), MEMORY_TAG_ARRAY)
208 
221 KAPI void* _kreallocate_aligned(void* block, u64 old_size, u64 new_size, u16 alignment, memory_tag tag, const char* filename, u32 line);
222 
223 #define kreallocate_aligned(block, old_size, new_size, alignment, tag) _kreallocate_aligned(block, old_size, new_size, alignment, tag, __FILE__, __LINE__)
224 
234 KAPI void kreallocate_report(u64 old_size, u64 new_size, memory_tag tag);
235 
242 KAPI void kfree(void* block, u64 size, memory_tag tag);
243 
250 KAPI void kfree_aligned(void* block, u64 size, u16 alignment, memory_tag tag);
251 
260 KAPI void kfree_report(u64 size, memory_tag tag);
261 
271 KAPI b8 kmemory_get_size_alignment(void* block, u64* out_size, u16* out_alignment, memory_tag* out_tag);
272 
279 KAPI void* kzero_memory(void* block, u64 size);
280 
281 #define KZERO_TYPE(block, type) kzero_memory(block, sizeof(type));
282 #define KZERO_TYPE_CARRAY(block, type, count) kzero_memory(block, sizeof(type) * count);
283 
291 KAPI void* kcopy_memory(void* dest, const void* source, u64 size);
292 
293 #define KCOPY_TYPE(dest, source, type) kcopy_memory(dest, source, sizeof(type))
294 #define KCOPY_TYPE_CARRAY(dest, source, type, count) kcopy_memory(dest, source, sizeof(type) * count)
295 
296 #define KDUPLICATE_TYPE_CARRAY(dest, source, type, count) \
297  dest = KALLOC_TYPE_CARRAY(type, count); \
298  KCOPY_TYPE_CARRAY(dest, source, type, count);
299 
307 KAPI void* kset_memory(void* dest, i32 value, u64 size);
308 
309 KAPI const char* get_unit_for_size(u64 size_bytes, f32* out_amount);
310 
318 
324 
328 
339 
350 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:209
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:27
_Bool b8
8-bit boolean type
Definition: defines.h:60
float f32
32-bit floating point number
Definition: defines.h:49
signed int i32
Signed 32-bit integer.
Definition: defines.h:41
unsigned short u16
Unsigned 16-bit integer.
Definition: defines.h:24
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:30
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:21
KAPI void * _kreallocate(void *block, u64 old_size, u64 new_size, memory_tag tag, const char *filename, u32 line)
Performs a memory reallocation from the host of the given size, and also frees the block of memory gi...
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 * 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:34
@ MEMORY_TAG_BINARY_DATA
Definition: kmemory.h:53
@ MEMORY_TAG_POOL_ALLOCATOR
Definition: kmemory.h:39
@ MEMORY_TAG_EDITOR
Definition: kmemory.h:74
@ MEMORY_TAG_ARRAY
Definition: kmemory.h:37
@ MEMORY_TAG_DIRECT3D
Definition: kmemory.h:59
@ MEMORY_TAG_OPENGL
Definition: kmemory.h:60
@ MEMORY_TAG_ASSET
Definition: kmemory.h:73
@ MEMORY_TAG_SERIALIZER
Definition: kmemory.h:72
@ MEMORY_TAG_REGISTRY
Definition: kmemory.h:69
@ MEMORY_TAG_UI
Definition: kmemory.h:67
@ MEMORY_TAG_HASHTABLE
Definition: kmemory.h:66
@ MEMORY_TAG_DICT
Definition: kmemory.h:41
@ MEMORY_TAG_VULKAN_EXT
Definition: kmemory.h:58
@ MEMORY_TAG_BITMAP_FONT
Definition: kmemory.h:63
@ MEMORY_TAG_SCENE
Definition: kmemory.h:54
@ MEMORY_TAG_GPU_LOCAL
Definition: kmemory.h:62
@ MEMORY_TAG_RING_QUEUE
Definition: kmemory.h:42
@ MEMORY_TAG_BST
Definition: kmemory.h:43
@ MEMORY_TAG_MATERIAL_INSTANCE
Definition: kmemory.h:48
@ MEMORY_TAG_DARRAY
Definition: kmemory.h:40
@ MEMORY_TAG_RENDERER
Definition: kmemory.h:49
@ MEMORY_TAG_KEYMAP
Definition: kmemory.h:65
@ MEMORY_TAG_UNKNOWN
Definition: kmemory.h:36
@ MEMORY_TAG_VULKAN
Definition: kmemory.h:56
@ MEMORY_TAG_PLUGIN
Definition: kmemory.h:70
@ MEMORY_TAG_PACKAGE
Definition: kmemory.h:55
@ MEMORY_TAG_PLATFORM
Definition: kmemory.h:71
@ MEMORY_TAG_AUDIO
Definition: kmemory.h:68
@ MEMORY_TAG_JOB
Definition: kmemory.h:46
@ MEMORY_TAG_STRING
Definition: kmemory.h:44
@ MEMORY_TAG_ENGINE
Definition: kmemory.h:45
@ MEMORY_TAG_LINEAR_ALLOCATOR
Definition: kmemory.h:38
@ MEMORY_TAG_SYSTEM_FONT
Definition: kmemory.h:64
@ MEMORY_TAG_TRANSFORM
Definition: kmemory.h:51
@ MEMORY_TAG_MAX_TAGS
Definition: kmemory.h:76
@ MEMORY_TAG_GAME
Definition: kmemory.h:50
@ MEMORY_TAG_BINARY_STRING_TABLE
Definition: kmemory.h:52
@ MEMORY_TAG_TEXTURE
Definition: kmemory.h:47
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 u64 get_free_memory_space(void)
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.
KAPI b8 kmemory_get_size_alignment(void *block, u64 *out_size, u16 *out_alignment, memory_tag *out_tag)
Returns the size and alignment of the given block of memory. NOTE: A failure result from this method ...
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 u64 get_used_memory_space(void)
KAPI void * _kallocate(u64 size, memory_tag tag, const char *file, u32 line)
Performs a memory allocation from the host of the given size. The allocation is tracked for the provi...
KAPI u64 get_total_memory_space(void)
KAPI const char * get_unit_for_size(u64 size_bytes, f32 *out_amount)
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 * _kallocate_aligned(u64 size, u16 alignment, memory_tag tag, const char *filename, u32 line)
Performs an aligned memory allocation from the host of the given size and alignment....
KAPI void * kzero_memory(void *block, u64 size)
Zeroes out the provided memory block.
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 void * _kreallocate_aligned(void *block, u64 old_size, u64 new_size, u16 alignment, memory_tag tag, const char *filename, u32 line)
Performs a memory reallocation from the host of the given size and alignment, and also frees the bloc...
KAPI char * get_memory_usage_str(void)
Obtains a string containing a "printout" of memory usage, categorized by memory tag....
Definition: kmemory.h:25
void(* free)(void *block, u64 size)
Definition: kmemory.h:27
u64(* allocated)(void)
Definition: kmemory.h:30
void(* free_all)(void)
Definition: kmemory.h:28
u64(* total_space)(void)
Definition: kmemory.h:29
The configuration for the memory system.
Definition: kmemory.h:80
u64 total_alloc_size
The total memory size in byes used by the internal allocator for this system.
Definition: kmemory.h:82