From 1448d3336b9cc07cdefb4d615d014cfaf29f6cc6 Mon Sep 17 00:00:00 2001 From: Wesley Irvin Date: Wed, 7 Feb 2024 18:25:13 -0500 Subject: [PATCH] Memory Zones First draft of memory zone functionality. Can create a zone and allocate you memory from the zone. Also has the ability to clear zones and to free. There is no way yet to free anything that has been allocated on the zones. --- CMakeLists.txt | 2 +- quantum/src/defines.h | 23 ++++++++++++++ quantum/src/memory/zone.c | 66 +++++++++++++++++++++++++++++++++++++++ quantum/src/memory/zone.h | 13 ++++++++ quantum/src/test.c | 5 --- quantum/src/test.h | 1 - test_stuff/src/main.c | 27 ++++++++++++++-- 7 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 quantum/src/defines.h create mode 100644 quantum/src/memory/zone.c create mode 100644 quantum/src/memory/zone.h delete mode 100644 quantum/src/test.c delete mode 100644 quantum/src/test.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d3946ea..8ee7fb1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(quantum-utils) set(quantum-src "./quantum/src/") set(test_stuff-src "./test_stuff/src/") -add_library(quantum SHARED ${quantum-src}test.c) +add_library(quantum SHARED ${quantum-src}memory/zone.c) add_executable(test_stuff ${test_stuff-src}main.c) target_link_libraries(test_stuff quantum) diff --git a/quantum/src/defines.h b/quantum/src/defines.h new file mode 100644 index 0000000..fa54c59 --- /dev/null +++ b/quantum/src/defines.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +typedef int16_t i16; +typedef uint16_t u16; +typedef int32_t i32; +typedef uint32_t u32; +typedef int64_t i64; +typedef uint64_t u64; +typedef float f32; +typedef double f64; + +static_assert(sizeof(i16) == 2); +static_assert(sizeof(u16) == 2); +static_assert(sizeof(i32) == 4); +static_assert(sizeof(u32) == 4); +static_assert(sizeof(i64) == 8); +static_assert(sizeof(u64) == 8); +static_assert(sizeof(f32) == 4); +static_assert(sizeof(f64) == 8); + diff --git a/quantum/src/memory/zone.c b/quantum/src/memory/zone.c new file mode 100644 index 0000000..0ef5b27 --- /dev/null +++ b/quantum/src/memory/zone.c @@ -0,0 +1,66 @@ +#include // malloc() free() +#include // printf() + +#include "../defines.h" +#include "zone.h" + +typedef struct ZoneHeader { + u64 capacity; + u64 cur_size; +} ZoneHeader; + +Zone * zoneCreate(size_t sizeBytes) { + // First we need to get a block of memory from the OS + // This block needs to include the size of what we want to store + // plus the size of our ZoneHeader + void *mem_block = malloc(sizeBytes + sizeof(ZoneHeader)); + + // Now that we have the block let's get the addresses + // for where the header is, and the offset where the data starts + ZoneHeader *zone_header = (ZoneHeader *)mem_block; + Zone *zone_addr = (Zone *)(zone_header + sizeof(ZoneHeader)); + + // Initialize the values of the header + zone_header->capacity = sizeBytes; + zone_header->cur_size = 0; + + return zone_addr; +} + +void * zoneAlloc(Zone *zone, size_t sizeBytes) { + ZoneHeader *zone_header = (ZoneHeader *)zone - sizeof(ZoneHeader); + + if (zone_header->cur_size + sizeBytes > zone_header->capacity) { + printf("Could not allocate, not enough space."); + return NULL; + } + + void *new_mem = (char *)zone + zone_header->cur_size; + + zone_header->cur_size += sizeBytes; + + printf("Zone Header Information:\n"); + printf("Current Size: %u\n", zone_header->cur_size); + printf("Total Capacity: %u\n", zone_header->capacity); + + return new_mem; +} + +void zoneClear(Zone *zone) { + ZoneHeader *zone_header = (ZoneHeader *)zone - sizeof(ZoneHeader); + + zone_header->cur_size = 0; + + printf("Zone Header Information:\n"); + printf("Current Size: %u\n", zone_header->cur_size); + printf("Total Capacity: %u\n", zone_header->capacity); +} + +void zoneDestroy(Zone *zone) { + // First we need to go back to the beginning of the header as we returned + // an offset for the user to use + void *del_mem = (ZoneHeader *)zone - sizeof(ZoneHeader); + + // Free the memory + free(del_mem); +} diff --git a/quantum/src/memory/zone.h b/quantum/src/memory/zone.h new file mode 100644 index 0000000..aa52044 --- /dev/null +++ b/quantum/src/memory/zone.h @@ -0,0 +1,13 @@ +#pragma once + +#include // size_t definition + +#include "../defines.h" + +typedef struct _Zone Zone; + +Zone * zoneCreate(size_t sizeBytes); +void * zoneAlloc(Zone *zone, size_t sizeBytes); +void zoneClear(Zone *zone); +void zoneDestroy(Zone *zone); + diff --git a/quantum/src/test.c b/quantum/src/test.c deleted file mode 100644 index aa57d7b..0000000 --- a/quantum/src/test.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -#include "test.h" - -void hello_quantum() { printf("Hello from Quantum!\n"); } diff --git a/quantum/src/test.h b/quantum/src/test.h deleted file mode 100644 index db254d7..0000000 --- a/quantum/src/test.h +++ /dev/null @@ -1 +0,0 @@ -void hello_quantum(); diff --git a/test_stuff/src/main.c b/test_stuff/src/main.c index c3564fa..b402818 100644 --- a/test_stuff/src/main.c +++ b/test_stuff/src/main.c @@ -1,7 +1,30 @@ -#include +#include +#include +#include int main() { - hello_quantum(); + printf("Size of i16: %i\n", sizeof(i16)); + printf("Size of u16: %i\n", sizeof(u16)); + printf("Size of i32: %i\n", sizeof(i32)); + printf("Size of u32: %i\n", sizeof(u32)); + printf("Size of i64: %i\n", sizeof(i64)); + printf("Size of u64: %i\n", sizeof(u64)); + printf("Size of f32: %i\n", sizeof(f32)); + printf("Size of f64: %i\n", sizeof(f64)); + + printf("\nCreating new zone 4K in size...\n"); + Zone *test_zone = zoneCreate(4096); + + void *test_alloc = zoneAlloc(test_zone, 4); + void *test_alloc2 = zoneAlloc(test_zone, 4); + + printf("Address of test_zone: %p\n", (void *)test_zone); + printf("Address of test_alloc: %p\n", test_alloc); + printf("Address of test_alloc2: %p\n", test_alloc2); + + zoneClear(test_zone); + + zoneDestroy(test_zone); return 0; }