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

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