Fix and Free

Added the ability to free data from a zone. Also made some fixes to make
sure that our numbers format properly in print statements.
This commit is contained in:
2024-02-08 17:50:46 -05:00
parent 9f9bcf215f
commit a13b6fcac5
6 changed files with 71 additions and 31 deletions

3
.gitignore vendored
View File

@@ -54,3 +54,6 @@ dkms.conf
# Ignore /build directory from cmake # Ignore /build directory from cmake
build/ build/
# Ignore the .cache/ directory that clangd gernates
.cache/

View File

@@ -4,6 +4,7 @@ project(quantum-utils)
set(quantum-src "./quantum/src/") set(quantum-src "./quantum/src/")
set(test_stuff-src "./test_stuff/src/") set(test_stuff-src "./test_stuff/src/")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_library(quantum SHARED ${quantum-src}memory/zone.c) add_library(quantum SHARED ${quantum-src}memory/zone.c)
add_executable(test_stuff ${test_stuff-src}main.c) add_executable(test_stuff ${test_stuff-src}main.c)

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <inttypes.h>
#include <assert.h> #include <assert.h>
#include <inttypes.h>
typedef int16_t i16; typedef int16_t i16;
typedef uint16_t u16; typedef uint16_t u16;
@@ -12,12 +12,11 @@ typedef uint64_t u64;
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
static_assert(sizeof(i16) == 2); static_assert(sizeof(i16) == 2, "");
static_assert(sizeof(u16) == 2); static_assert(sizeof(u16) == 2, "");
static_assert(sizeof(i32) == 4); static_assert(sizeof(i32) == 4, "");
static_assert(sizeof(u32) == 4); static_assert(sizeof(u32) == 4, "");
static_assert(sizeof(i64) == 8); static_assert(sizeof(i64) == 8, "");
static_assert(sizeof(u64) == 8); static_assert(sizeof(u64) == 8, "");
static_assert(sizeof(f32) == 4); static_assert(sizeof(f32) == 4, "");
static_assert(sizeof(f64) == 8); static_assert(sizeof(f64) == 8, "");

View File

@@ -1,5 +1,6 @@
#include <stdio.h> // printf()
#include <stdlib.h> // malloc() free() #include <stdlib.h> // malloc() free()
#include <stdio.h> // printf() #include <string.h>
#include "../defines.h" #include "../defines.h"
#include "zone.h" #include "zone.h"
@@ -9,7 +10,7 @@ typedef struct ZoneHeader {
u64 cur_size; u64 cur_size;
} ZoneHeader; } ZoneHeader;
Zone * zoneCreate(size_t sizeBytes) { Zone *zoneCreate(size_t sizeBytes) {
// First we need to get a block of memory from the OS // 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 // This block needs to include the size of what we want to store
// plus the size of our ZoneHeader // plus the size of our ZoneHeader
@@ -27,7 +28,7 @@ Zone * zoneCreate(size_t sizeBytes) {
return zone_addr; return zone_addr;
} }
void * zoneAlloc(Zone *zone, size_t sizeBytes) { void *zoneAlloc(Zone *zone, size_t sizeBytes) {
ZoneHeader *zone_header = (ZoneHeader *)zone - sizeof(ZoneHeader); ZoneHeader *zone_header = (ZoneHeader *)zone - sizeof(ZoneHeader);
if (zone_header->cur_size + sizeBytes > zone_header->capacity) { 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; zone_header->cur_size += sizeBytes;
printf("Zone Header Information:\n"); printf("Zone Header Information:\n");
printf("Current Size: %u\n", zone_header->cur_size); printf("Current Size: %" PRIu64 "\n", zone_header->cur_size);
printf("Total Capacity: %u\n", zone_header->capacity); printf("Total Capacity: %" PRIu64 "\n", zone_header->capacity);
return new_mem; 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) { void zoneClear(Zone *zone) {
ZoneHeader *zone_header = (ZoneHeader *)zone - sizeof(ZoneHeader); ZoneHeader *zone_header = (ZoneHeader *)zone - sizeof(ZoneHeader);
zone_header->cur_size = 0; zone_header->cur_size = 0;
printf("Zone Header Information:\n"); printf("Zone Header Information:\n");
printf("Current Size: %u\n", zone_header->cur_size); printf("Current Size: %" PRIu64 "\n", zone_header->cur_size);
printf("Total Capacity: %u\n", zone_header->capacity); printf("Total Capacity: %" PRIu64 "\n", zone_header->capacity);
} }
void zoneDestroy(Zone *zone) { void zoneDestroy(Zone *zone) {

View File

@@ -1,13 +1,13 @@
#pragma once #pragma once
#include <stddef.h> // size_t definition #include <stddef.h> // size_t definition
#include "../defines.h" #include "../defines.h"
typedef struct _Zone Zone; typedef struct _Zone Zone;
Zone * zoneCreate(size_t sizeBytes); Zone *zoneCreate(size_t sizeBytes);
void * zoneAlloc(Zone *zone, size_t sizeBytes); void *zoneAlloc(Zone *zone, size_t sizeBytes);
void zoneFree(void *data, size_t dataSize);
void zoneClear(Zone *zone); void zoneClear(Zone *zone);
void zoneDestroy(Zone *zone); void zoneDestroy(Zone *zone);

View File

@@ -3,14 +3,14 @@
#include <stdio.h> #include <stdio.h>
int main() { int main() {
printf("Size of i16: %i\n", sizeof(i16)); printf("Size of i16: %" PRId16 "\n", sizeof(i16));
printf("Size of u16: %i\n", sizeof(u16)); printf("Size of u16: %" PRIu16 "\n", sizeof(u16));
printf("Size of i32: %i\n", sizeof(i32)); printf("Size of i32: %" PRId32 "\n", sizeof(i32));
printf("Size of u32: %i\n", sizeof(u32)); printf("Size of u32: %" PRIu32 "\n", sizeof(u32));
printf("Size of i64: %i\n", sizeof(i64)); printf("Size of i64: %" PRId64 "\n", sizeof(i64));
printf("Size of u64: %i\n", sizeof(u64)); printf("Size of u64: %" PRIu64 "\n", sizeof(u64));
printf("Size of f32: %i\n", sizeof(f32)); printf("Size of f32: %lu\n", sizeof(f32));
printf("Size of f64: %i\n", sizeof(f64)); printf("Size of f64: %lu\n", sizeof(f64));
printf("\nCreating new zone 4K in size...\n"); printf("\nCreating new zone 4K in size...\n");
Zone *test_zone = zoneCreate(4096); 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_zone: %p\n", (void *)test_zone);
printf("Address of test_alloc: %p\n", test_alloc); 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); 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); 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; return 0;
} }