Kohi Game Engine
kmemory.h File Reference

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  memory_system_configuration
 The configuration for the memory system. More...
 

Typedefs

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_AUDIO , MEMORY_TAG_MAX_TAGS
}
 Tags to indicate the usage of memory allocations made in this system. More...
 

Functions

b8 memory_system_initialize (memory_system_configuration config)
 Initializes the memory system. More...
 
void memory_system_shutdown (void *state)
 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 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...
 

Detailed Description

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.

Author
Travis Vroman (travi.nosp@m.s@ko.nosp@m.hieng.nosp@m.ine..nosp@m.com)
Note
Note that reliance on this will likely be by core systems only, as items using allocations directly will use allocators as they are added to the system.
Version
1.0
Date
2022-01-10

Typedef Documentation

◆ memory_system_configuration

The configuration for the memory system.

◆ memory_tag

typedef enum memory_tag memory_tag

Tags to indicate the usage of memory allocations made in this system.

Enumeration Type Documentation

◆ memory_tag

enum memory_tag

Tags to indicate the usage of memory allocations made in this system.

Enumerator
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_AUDIO 
MEMORY_TAG_MAX_TAGS 

Function Documentation

◆ get_memory_alloc_count()

KAPI u64 get_memory_alloc_count ( void  )

Obtains the number of times kallocate was called since the memory system was initialized.

Returns
The total count of allocations since the system's initialization.

◆ get_memory_usage_str()

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.

Deprecated:
This function should be discontinued in favour of something more robust in the future.
Returns
A pointer to a character array containing the string representation of memory usage.

◆ kallocate()

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.

Parameters
sizeThe size of the allocation.
tagIndicates the use of the allocated block.
Returns
If successful, a pointer to a block of allocated memory; otherwise 0.

◆ kallocate_aligned()

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.

Parameters
sizeThe size of the allocation.
alignmentThe alignment in bytes.
tagIndicates the use of the allocated block.
Returns
If successful, a pointer to a block of allocated memory; otherwise 0.

◆ kallocate_report()

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.

Parameters
sizeThe size of the allocation.
tagIndicates the use of the allocated block.

◆ kcopy_memory()

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.

Parameters
destA pointer to the destination block of memory to copy to.
sourceA pointer to the source block of memory to copy from.
sizeThe amount of memory in bytes to be copied over.
Returns
A pointer to the block of memory copied to.

◆ kfree()

KAPI void kfree ( void *  block,
u64  size,
memory_tag  tag 
)

Frees the given block, and untracks its size from the given tag.

Parameters
blockA pointer to the block of memory to be freed.
sizeThe size of the block to be freed.
tagThe tag indicating the block's use.

◆ kfree_aligned()

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.

Parameters
blockA pointer to the block of memory to be freed.
sizeThe size of the block to be freed.
tagThe tag indicating the block's use.

◆ kfree_report()

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.

Parameters
sizeThe size in bytes.
tagThe tag indicating the block's use.

◆ kmemory_get_size_alignment()

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.

Parameters
blockThe memory block.
out_sizeA pointer to hold the size of the block.
out_alignmentA pointer to hold the alignment of the block.
Returns
True on success; otherwise false.

◆ kset_memory()

KAPI void* kset_memory ( void *  dest,
i32  value,
u64  size 
)

Sets the bytes of memory located at dest to value over the given size.

Parameters
destA pointer to the destination block of memory to be set.
valueThe value to be set.
sizeThe size in bytes to copy over to.
Returns
A pointer to the destination block of memory.

◆ kzero_memory()

KAPI void* kzero_memory ( void *  block,
u64  size 
)

Zeroes out the provided memory block.

Parameters
blockA pointer to the block of memory to be zeroed out.
sizeThe size in bytes to zero out.
Apointer to the zeroed out block of memory.

◆ memory_system_initialize()

b8 memory_system_initialize ( memory_system_configuration  config)

Initializes the memory system.

Parameters
configThe configuration for this system.

◆ memory_system_shutdown()

void memory_system_shutdown ( void *  state)

Shuts down the memory system.