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.
This commit is contained in:
23
quantum/src/defines.h
Normal file
23
quantum/src/defines.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <assert.h>
|
||||
|
||||
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);
|
||||
|
||||
66
quantum/src/memory/zone.c
Normal file
66
quantum/src/memory/zone.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <stdlib.h> // malloc() free()
|
||||
#include <stdio.h> // 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);
|
||||
}
|
||||
13
quantum/src/memory/zone.h
Normal file
13
quantum/src/memory/zone.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#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);
|
||||
void zoneClear(Zone *zone);
|
||||
void zoneDestroy(Zone *zone);
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "test.h"
|
||||
|
||||
void hello_quantum() { printf("Hello from Quantum!\n"); }
|
||||
@@ -1 +0,0 @@
|
||||
void hello_quantum();
|
||||
Reference in New Issue
Block a user