diff --git a/.gitignore b/.gitignore index 8504bb4..afcd3b2 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,6 @@ dkms.conf # Ignore /build directory from cmake build/ + +# Ignore the .cache/ directory that clangd gernates +.cache/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ee7fb1..53b354e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(quantum-utils) set(quantum-src "./quantum/src/") set(test_stuff-src "./test_stuff/src/") +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) add_library(quantum SHARED ${quantum-src}memory/zone.c) add_executable(test_stuff ${test_stuff-src}main.c) diff --git a/quantum/src/defines.h b/quantum/src/defines.h index fa54c59..92640e4 100644 --- a/quantum/src/defines.h +++ b/quantum/src/defines.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include typedef int16_t i16; typedef uint16_t u16; @@ -12,12 +12,11 @@ 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); - +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 index 0ef5b27..cb1d067 100644 --- a/quantum/src/memory/zone.c +++ b/quantum/src/memory/zone.c @@ -1,5 +1,6 @@ +#include // printf() #include // malloc() free() -#include // printf() +#include #include "../defines.h" #include "zone.h" @@ -9,7 +10,7 @@ typedef struct ZoneHeader { u64 cur_size; } ZoneHeader; -Zone * zoneCreate(size_t sizeBytes) { +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 @@ -27,7 +28,7 @@ Zone * zoneCreate(size_t sizeBytes) { return zone_addr; } -void * zoneAlloc(Zone *zone, size_t sizeBytes) { +void *zoneAlloc(Zone *zone, size_t sizeBytes) { ZoneHeader *zone_header = (ZoneHeader *)zone - sizeof(ZoneHeader); if (zone_header->cur_size + sizeBytes > zone_header->capacity) { @@ -40,20 +41,38 @@ void * zoneAlloc(Zone *zone, size_t sizeBytes) { 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); + printf("Current Size: %" PRIu64 "\n", zone_header->cur_size); + printf("Total Capacity: %" PRIu64 "\n", zone_header->capacity); return new_mem; } +void zoneFree(void *data, size_t dataSize) { + // TODO: We want to eventually pass in a pointer to the zone so we can track + // our free blocks + + // Lets first check to make sure we weren't given a NULL pointer + if (data == NULL) + return; + + // First we need to set the memory starting at `data` and going to `dataSize` + // to 0 + memset(data, 0, dataSize); + + // Now we need to null the pointer + data = NULL; + + // TODO: Add logic to alert that this space can now be used +} + 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); + printf("Current Size: %" PRIu64 "\n", zone_header->cur_size); + printf("Total Capacity: %" PRIu64 "\n", zone_header->capacity); } void zoneDestroy(Zone *zone) { diff --git a/quantum/src/memory/zone.h b/quantum/src/memory/zone.h index aa52044..ff4684f 100644 --- a/quantum/src/memory/zone.h +++ b/quantum/src/memory/zone.h @@ -1,13 +1,13 @@ #pragma once -#include // size_t definition +#include // size_t definition #include "../defines.h" typedef struct _Zone Zone; -Zone * zoneCreate(size_t sizeBytes); -void * zoneAlloc(Zone *zone, size_t sizeBytes); +Zone *zoneCreate(size_t sizeBytes); +void *zoneAlloc(Zone *zone, size_t sizeBytes); +void zoneFree(void *data, size_t dataSize); void zoneClear(Zone *zone); void zoneDestroy(Zone *zone); - diff --git a/test_stuff/src/main.c b/test_stuff/src/main.c index b402818..39054d7 100644 --- a/test_stuff/src/main.c +++ b/test_stuff/src/main.c @@ -3,15 +3,15 @@ #include int main() { - 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("Size of i16: %" PRId16 "\n", sizeof(i16)); + printf("Size of u16: %" PRIu16 "\n", sizeof(u16)); + printf("Size of i32: %" PRId32 "\n", sizeof(i32)); + printf("Size of u32: %" PRIu32 "\n", sizeof(u32)); + printf("Size of i64: %" PRId64 "\n", sizeof(i64)); + printf("Size of u64: %" PRIu64 "\n", sizeof(u64)); + printf("Size of f32: %lu\n", sizeof(f32)); + printf("Size of f64: %lu\n", sizeof(f64)); + printf("\nCreating new zone 4K in size...\n"); Zone *test_zone = zoneCreate(4096); @@ -20,11 +20,29 @@ int main() { 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); + printf("Address of test_alloc2: %p\n\n", test_alloc2); zoneClear(test_zone); + u64 *test_free = zoneAlloc(test_zone, sizeof(u64)); + + printf("Address of test_free: %p\n", (void *)test_free); + printf("Freeing test_free...\n"); + + zoneFree(&test_free, sizeof(u64)); + + printf("Is test_free NULL: %s\n\n", test_free == NULL ? "true" : "false"); + zoneDestroy(test_zone); + const u64 FOUR_GIGS = (u64)4 * 1024 * 1024 * 1024; + + Zone *break_zone = zoneCreate(FOUR_GIGS); + + while (zoneAlloc(break_zone, 1024 * 1024 * 1024) != NULL) { + } + + zoneDestroy(break_zone); + return 0; }