kiba-engine
memory.c
Go to the documentation of this file.
1 
7 #include <kiba/core/memory.h>
8 
9 #include <kiba/platform/memory.h>
10 
11 #include <kiba/core/log.h>
12 
13 typedef struct memory_page {
14  usize size_class;
15  struct memory_page *next;
16 } memory_page;
17 
18 #define KB_PAGE_SIZE KB_MEGABYTE(8)
19 #define KB_MEMORY_MAX_SIZE_CLASS 4
20 
21 memory_page *page_free_list[KB_MEMORY_MAX_SIZE_CLASS + 1] = {0}; // could be threadlocal in the future
22 
23 static inline usize memory_page_size_class(usize num_bytes) {
24  usize target = num_bytes + sizeof(memory_page);
25  return target / KB_PAGE_SIZE + 1;
26 }
27 
28 b8 memory_initialize(void) { return true; }
29 
30 void *memory_page_allocate(usize target_num_bytes, usize *res_num_bytes) {
31  memory_page *res = KB_NULL;
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;
36  res->next = KB_NULL;
37  } else {
38  res = platform_memory_allocate(size_class * KB_PAGE_SIZE);
39  if (res == KB_NULL) {
40  return res;
41  }
42  res->next = KB_NULL;
43  res->size_class = size_class;
44  }
45  if (res_num_bytes) {
46  *res_num_bytes = size_class * KB_PAGE_SIZE - sizeof(memory_page);
47  }
48  return ++res; // skip header info
49 }
50 
51 void memory_page_free(void *page_data) {
52  memory_page *page = page_data;
53  --page; // recover header info
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;
57  } else {
58  platform_memory_free(page, page->size_class * KB_PAGE_SIZE);
59  }
60 }
61 
62 void memory_shutdown(void) {
63  for (usize i = 0; i < KB_MEMORY_MAX_SIZE_CLASS; ++i) {
64  while (page_free_list[i]) {
65  memory_page *page = page_free_list[i];
66  page_free_list[i] = page->next;
67  platform_memory_free(page, page->size_class * KB_PAGE_SIZE);
68  }
69  }
70 }
71 
72 void *memory_allocate(usize size) {
73  void *mem = platform_memory_allocate(size);
74  return memory_zero(mem, size);
75 }
76 
77 void memory_free(void *mem, usize size) { platform_memory_free(mem, size); }
78 
79 void *memory_zero(void *mem, usize size) { return platform_memory_set(mem, 0, size); }
80 
81 void *memory_set(void *mem, u8 byte, usize size) { return platform_memory_set(mem, byte, size); }
82 
83 void *memory_copy(void *dst, const void *src, usize size) { return platform_memory_copy(dst, src, size); }
84 
85 void *memory_aligned_address(void *addr, usize alignment) {
86  return (void *) (((uptr) addr + alignment - 1) & (uptr) ~(alignment - 1)); // TODO clean that up
87 }
void * memory_allocate(usize size)
Allocate memory.
Definition: memory.c:72
void * memory_copy(void *dst, const void *src, usize size)
Copy memory.
Definition: memory.c:83
void * memory_aligned_address(void *addr, usize alignment)
Calculate properly aligned memory block address.
Definition: memory.c:85
void * memory_zero(void *mem, usize size)
Zero out memory.
Definition: memory.c:79
void memory_free(void *mem, usize size)
Free memory.
Definition: memory.c:77
void * memory_set(void *mem, u8 byte, usize size)
Set content of memory block.
Definition: memory.c:81
Lightweight layer between platform and other engine components to enable tracing/monitoring.
#define KB_NULL
Value of an invalid ptr (nullptr).
Definition: defines.h:18
Logging system.
void * platform_memory_set(void *mem, u8 byte, usize size)
Set memory block to a specific byte value.
Definition: memory.c:18
void * platform_memory_allocate(usize size)
Allocate memory.
Definition: memory.c:11
void platform_memory_free(void *mem, usize size)
Free memory.
Definition: memory.c:13
void * platform_memory_copy(void *dst, const void *src, usize size)
Copy memory.
Definition: memory.c:20
Interface to access platform specific memory functionality.