18 #define KB_PAGE_SIZE KB_MEGABYTE(8)
19 #define KB_MEMORY_MAX_SIZE_CLASS 4
21 memory_page *page_free_list[KB_MEMORY_MAX_SIZE_CLASS + 1] = {0};
23 static inline usize memory_page_size_class(usize num_bytes) {
25 return target / KB_PAGE_SIZE + 1;
28 b8 memory_initialize(
void) {
return true; }
30 void *memory_page_allocate(usize target_num_bytes, usize *res_num_bytes) {
32 usize size_class = memory_page_size_class(target_num_bytes);
33 if (size_class <= KB_MEMORY_MAX_SIZE_CLASS && page_free_list[size_class]) {
34 res = page_free_list[size_class];
35 page_free_list[size_class] = res->next;
43 res->size_class = size_class;
46 *res_num_bytes = size_class * KB_PAGE_SIZE -
sizeof(
memory_page);
51 void memory_page_free(
void *page_data) {
54 if (page->size_class <= KB_MEMORY_MAX_SIZE_CLASS) {
55 page->next = page_free_list[page->size_class];
56 page_free_list[page->size_class] = page;
62 void memory_shutdown(
void) {
63 for (usize i = 0; i < KB_MEMORY_MAX_SIZE_CLASS; ++i) {
64 while (page_free_list[i]) {
66 page_free_list[i] = page->next;
86 return (
void *) (((uptr) addr + alignment - 1) & (uptr) ~(alignment - 1));
void * memory_allocate(usize size)
Allocate memory.
void * memory_copy(void *dst, const void *src, usize size)
Copy memory.
void * memory_aligned_address(void *addr, usize alignment)
Calculate properly aligned memory block address.
void * memory_zero(void *mem, usize size)
Zero out memory.
void memory_free(void *mem, usize size)
Free memory.
void * memory_set(void *mem, u8 byte, usize size)
Set content of memory block.
Lightweight layer between platform and other engine components to enable tracing/monitoring.
#define KB_NULL
Value of an invalid ptr (nullptr).