summaryrefslogtreecommitdiff
path: root/src/core/memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/memory.c')
-rw-r--r--src/core/memory.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/core/memory.c b/src/core/memory.c
new file mode 100644
index 0000000..f19803e
--- /dev/null
+++ b/src/core/memory.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <engine/logging.h>
+
+#include <engine/memory.h>
+
+memory* memory_new(usize max_size) {
+ memory* m = malloc(sizeof(memory));
+ m->data = malloc(max_size);
+ m->size = max_size;
+ m->pos = 0;
+ m->free = max_size;
+
+ memset(m->data, 0, max_size);
+
+ return m;
+}
+
+/* Returns a pointer to the allocated data */
+void* memory_allocate(memory* mem, usize size) {
+ void* data = NULL;
+
+ if (mem->pos + size <= mem->size) {
+ data = (void*)((usize)mem->data + mem->pos);
+ mem->pos += size;
+ mem->free -= size;
+ } else {
+ ERROR("Trying to allocate %lu in a %lu sized memory block", size,
+ mem->size);
+ ERROR("No more room!");
+ exit(EXIT_FAILURE);
+ }
+
+ return data;
+}
+
+memory memory_init(void* data, usize size) {
+ memory m = {0};
+ m.data = data;
+ m.size = size;
+ m.free = 0;
+ return m;
+}
+
+void memory_free(memory* mem, usize size) {
+ if (size > mem->pos) {
+ perror("Freeing too much memory!");
+ exit(EXIT_FAILURE);
+ } else {
+ mem->pos -= size;
+ mem->free += size;
+ }
+}
+
+void memory_clear(memory* mem) {
+ mem->pos = 0;
+ mem->free = mem->size;
+ /* Reset the memory? */
+ memset(mem->data, 0, mem->size);
+}