Fix and Free #6

Merged
wesley merged 1 commits from zone/fix-and-free into main 2024-02-08 17:53:01 -05:00
6 changed files with 71 additions and 31 deletions
Showing only changes of commit a13b6fcac5 - Show all commits

3
.gitignore vendored
View File

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

View File

@@ -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)

View File

@@ -1,7 +1,7 @@
#pragma once
#include <inttypes.h>
#include <assert.h>
#include <inttypes.h>
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, "");

View File

@@ -1,5 +1,6 @@
#include <stdio.h> // printf()
#include <stdlib.h> // malloc() free()
#include <stdio.h> // printf()
#include <string.h>
#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) {

View File

@@ -1,13 +1,13 @@
#pragma once
#include <stddef.h> // size_t definition
#include <stddef.h> // 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);

View File

@@ -3,15 +3,15 @@
#include <stdio.h>
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;
}