Compare commits
4 Commits
1966ddf800
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 4567b1bfd5 | |||
| 957ba0880d | |||
| 757d795749 | |||
| 181ed5f732 |
@@ -4,13 +4,20 @@
|
|||||||
|
|
||||||
#include "../defines.h"
|
#include "../defines.h"
|
||||||
#include "../logger/logger.h"
|
#include "../logger/logger.h"
|
||||||
|
#include "../types/linked_list.h"
|
||||||
#include "zone.h"
|
#include "zone.h"
|
||||||
|
|
||||||
typedef struct ZoneHeader {
|
typedef struct ZoneHeader {
|
||||||
u64 capacity;
|
u64 capacity;
|
||||||
u64 cur_size;
|
u64 cur_size;
|
||||||
|
List *free_zones;
|
||||||
} ZoneHeader;
|
} ZoneHeader;
|
||||||
|
|
||||||
|
typedef struct FreeZone {
|
||||||
|
void *start_address;
|
||||||
|
u64 capacity;
|
||||||
|
} FreeZone;
|
||||||
|
|
||||||
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
|
||||||
@@ -25,6 +32,7 @@ Zone *zoneCreate(size_t sizeBytes) {
|
|||||||
// Initialize the values of the header
|
// Initialize the values of the header
|
||||||
zone_header->capacity = sizeBytes;
|
zone_header->capacity = sizeBytes;
|
||||||
zone_header->cur_size = 0;
|
zone_header->cur_size = 0;
|
||||||
|
zone_header->free_zones = listCreate();
|
||||||
|
|
||||||
return zone_addr;
|
return zone_addr;
|
||||||
}
|
}
|
||||||
@@ -32,6 +40,29 @@ Zone *zoneCreate(size_t sizeBytes) {
|
|||||||
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);
|
||||||
|
|
||||||
|
u64 cur_free_zones = listSize(zone_header->free_zones);
|
||||||
|
|
||||||
|
if (cur_free_zones > 0) {
|
||||||
|
for (u64 i = 1; i <= cur_free_zones; i++) {
|
||||||
|
FreeZone *curFreeZone =
|
||||||
|
(FreeZone *)listDataAt(zone_header->free_zones, i);
|
||||||
|
|
||||||
|
u64 freeZoneSize = curFreeZone->capacity;
|
||||||
|
if (freeZoneSize >= sizeBytes) {
|
||||||
|
void *ret_addr = curFreeZone->start_address;
|
||||||
|
curFreeZone->start_address += sizeBytes;
|
||||||
|
curFreeZone->capacity -= sizeBytes;
|
||||||
|
|
||||||
|
if (curFreeZone->capacity <= 0) {
|
||||||
|
listRemoveAt(zone_header->free_zones, i);
|
||||||
|
free(curFreeZone);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret_addr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (zone_header->cur_size + sizeBytes > zone_header->capacity) {
|
if (zone_header->cur_size + sizeBytes > zone_header->capacity) {
|
||||||
QERROR("Could not allocate, not enough space.");
|
QERROR("Could not allocate, not enough space.");
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -41,17 +72,10 @@ void *zoneAlloc(Zone *zone, size_t sizeBytes) {
|
|||||||
|
|
||||||
zone_header->cur_size += sizeBytes;
|
zone_header->cur_size += sizeBytes;
|
||||||
|
|
||||||
printf("Zone Header Information:\n");
|
|
||||||
printf("Current Size: %" PRIu64 "\n", zone_header->cur_size);
|
|
||||||
printf("Total Capacity: %" PRIu64 "\n", zone_header->capacity);
|
|
||||||
|
|
||||||
return new_mem;
|
return new_mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
void zoneFree(void *data, size_t dataSize) {
|
void zoneFree(void *data, Zone *zone, 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
|
// Lets first check to make sure we weren't given a NULL pointer
|
||||||
if (data == NULL)
|
if (data == NULL)
|
||||||
return;
|
return;
|
||||||
@@ -60,26 +84,31 @@ void zoneFree(void *data, size_t dataSize) {
|
|||||||
// to 0
|
// to 0
|
||||||
memset(data, 0, dataSize);
|
memset(data, 0, dataSize);
|
||||||
|
|
||||||
|
FreeZone *newFreeZone = malloc(sizeof(FreeZone));
|
||||||
|
ZoneHeader *zone_header = (ZoneHeader *)zone - sizeof(ZoneHeader);
|
||||||
|
|
||||||
|
newFreeZone->start_address = data;
|
||||||
|
newFreeZone->capacity = dataSize;
|
||||||
|
|
||||||
|
listPush(zone_header->free_zones, newFreeZone);
|
||||||
|
|
||||||
// Now we need to null the pointer
|
// Now we need to null the pointer
|
||||||
data = NULL;
|
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("Current Size: %" PRIu64 "\n", zone_header->cur_size);
|
|
||||||
printf("Total Capacity: %" PRIu64 "\n", zone_header->capacity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void zoneDestroy(Zone *zone) {
|
void zoneDestroy(Zone *zone) {
|
||||||
// First we need to go back to the beginning of the header as we returned
|
// First we need to go back to the beginning of the header as we returned
|
||||||
// an offset for the user to use
|
// an offset for the user to use
|
||||||
void *del_mem = (ZoneHeader *)zone - sizeof(ZoneHeader);
|
ZoneHeader *del_mem = (ZoneHeader *)zone - sizeof(ZoneHeader);
|
||||||
|
|
||||||
|
// Next we need to destory our free_zones list
|
||||||
|
listDestroy(del_mem->free_zones);
|
||||||
|
|
||||||
// Free the memory
|
// Free the memory
|
||||||
free(del_mem);
|
free(del_mem);
|
||||||
|
|||||||
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
#include <stddef.h> // size_t definition
|
#include <stddef.h> // size_t definition
|
||||||
|
|
||||||
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 zoneFree(void *data, Zone *zone, size_t dataSize);
|
||||||
void zoneClear(Zone *zone);
|
void zoneClear(Zone *zone);
|
||||||
void zoneDestroy(Zone *zone);
|
void zoneDestroy(Zone *zone);
|
||||||
|
|||||||
@@ -12,12 +12,18 @@ typedef struct Node {
|
|||||||
void *data;
|
void *data;
|
||||||
} Node;
|
} Node;
|
||||||
|
|
||||||
List listCreate() {
|
typedef struct List {
|
||||||
List new_list;
|
Node *cur_head;
|
||||||
|
Node *cur_tail;
|
||||||
|
u64 cur_size;
|
||||||
|
} List;
|
||||||
|
|
||||||
new_list.cur_size = 0;
|
List *listCreate() {
|
||||||
new_list.cur_head = NULL;
|
List *new_list = malloc(sizeof(List));
|
||||||
new_list.cur_tail = NULL;
|
|
||||||
|
new_list->cur_size = 0;
|
||||||
|
new_list->cur_head = NULL;
|
||||||
|
new_list->cur_tail = NULL;
|
||||||
|
|
||||||
return new_list;
|
return new_list;
|
||||||
}
|
}
|
||||||
@@ -29,34 +35,26 @@ void listDestroy(List *list) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 list_size = list->cur_size;
|
|
||||||
Node *del_node = list->cur_tail;
|
Node *del_node = list->cur_tail;
|
||||||
Node *next_node = list->cur_tail->prev;
|
Node *next_node;
|
||||||
|
|
||||||
while (list_size > 0) {
|
while (del_node != NULL) {
|
||||||
// First we want to free del_node
|
// We want to set del_node to the next_node to free
|
||||||
|
next_node = del_node->prev;
|
||||||
|
|
||||||
|
// Then we want to free del_node
|
||||||
free(del_node);
|
free(del_node);
|
||||||
|
|
||||||
// Now we want to set del_node to the next_node to free
|
// Finally we want to set del_node to the next_node to free
|
||||||
del_node = next_node;
|
del_node = next_node;
|
||||||
|
|
||||||
// Now we set next_node equal to the del_node's previous node
|
|
||||||
if (next_node->prev != NULL) {
|
|
||||||
next_node = next_node->prev;
|
|
||||||
} else if (next_node->prev == NULL) {
|
|
||||||
next_node->next = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now we decrement list_size
|
|
||||||
list_size--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we null out the cur_head and cur_tail of our list
|
// Now we null out the cur_head and cur_tail of our list
|
||||||
list->cur_tail = NULL;
|
list->cur_tail = NULL;
|
||||||
list->cur_head = NULL;
|
list->cur_head = NULL;
|
||||||
|
|
||||||
// Now we null out the list and make it point to nothing
|
// Now we free out the list and make it point to nothing
|
||||||
list = NULL;
|
free(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 listSize(const List *list) {
|
u64 listSize(const List *list) {
|
||||||
@@ -244,6 +242,16 @@ void listInsertAt(List *list, u64 pos, void *data) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure we passed in a valid position
|
||||||
|
if (pos < 1 || pos > list->cur_size) {
|
||||||
|
// If we have elements in the list
|
||||||
|
if (list->cur_size != 0) {
|
||||||
|
// Then the position is actually out of bounds, so return.
|
||||||
|
QERROR("Position given is out of range: %" PRId64, pos);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
u64 counter = 1;
|
u64 counter = 1;
|
||||||
Node *insert_pos;
|
Node *insert_pos;
|
||||||
Node *prev_node;
|
Node *prev_node;
|
||||||
|
|||||||
@@ -2,15 +2,9 @@
|
|||||||
|
|
||||||
#include "../defines.h"
|
#include "../defines.h"
|
||||||
|
|
||||||
typedef struct Node Node;
|
typedef struct List List;
|
||||||
|
|
||||||
typedef struct List {
|
List *listCreate();
|
||||||
Node *cur_head;
|
|
||||||
Node *cur_tail;
|
|
||||||
u64 cur_size;
|
|
||||||
} List;
|
|
||||||
|
|
||||||
List listCreate();
|
|
||||||
void listDestroy(List *list);
|
void listDestroy(List *list);
|
||||||
u64 listSize(const List *list);
|
u64 listSize(const List *list);
|
||||||
void *listPeek(List *list);
|
void *listPeek(List *list);
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
#include "types/linked_list.h"
|
|
||||||
#include <defines.h>
|
#include <defines.h>
|
||||||
#include <logger/logger.h>
|
#include <logger/logger.h>
|
||||||
#include <memory/zone.h>
|
#include <memory/zone.h>
|
||||||
@@ -13,48 +12,24 @@ int main() {
|
|||||||
QINFO("Size of f32: %lu", sizeof(f32));
|
QINFO("Size of f32: %lu", sizeof(f32));
|
||||||
QINFO("Size of f64: %lu", sizeof(f64));
|
QINFO("Size of f64: %lu", sizeof(f64));
|
||||||
|
|
||||||
QINFO("Creating a List...");
|
Zone *test_zone = zoneCreate(4096);
|
||||||
List my_list = listCreate();
|
|
||||||
QINFO("Pushing to my_list...")
|
|
||||||
i32 pushVal = 42;
|
|
||||||
listPush(&my_list, &pushVal);
|
|
||||||
i32 peekVal = *(i32 *)listPeek(&my_list);
|
|
||||||
|
|
||||||
QDEBUG("Peeking value off of my_list: %" PRId32, peekVal);
|
u64 val1 = 420;
|
||||||
|
u32 val2 = 169;
|
||||||
|
u32 val3 = 80085;
|
||||||
|
|
||||||
QINFO("Pushing another number to my_list...");
|
u64 *zVal1 = (u64 *)zoneAlloc(test_zone, sizeof(val1));
|
||||||
i32 pushVal2 = 169420;
|
*zVal1 = val1;
|
||||||
listPush(&my_list, &pushVal2);
|
|
||||||
peekVal = *(i32 *)listPeek(&my_list);
|
|
||||||
|
|
||||||
QDEBUG("Peeking value off of my_list: %" PRId32, peekVal);
|
zoneFree(zVal1, test_zone, sizeof(zVal1));
|
||||||
|
|
||||||
i32 pushVal3 = 69;
|
u32 *zVal2 = (u32 *)zoneAlloc(test_zone, sizeof(val2));
|
||||||
listPush(&my_list, &pushVal3);
|
u32 *zVal3 = (u32 *)zoneAlloc(test_zone, sizeof(val3));
|
||||||
QDEBUG("Peeking a 3rd value off of my_list: %" PRId32, peekVal);
|
|
||||||
|
|
||||||
i32 popVal = *(i32 *)listPop(&my_list);
|
*zVal2 = val2;
|
||||||
QDEBUG("Popped a value off of my_list: %" PRId32, popVal);
|
*zVal3 = val3;
|
||||||
|
|
||||||
peekVal = *(i32 *)listPeek(&my_list);
|
void *dont_care = zoneAlloc(test_zone, 64);
|
||||||
|
|
||||||
QDEBUG("Peeking value off of my_list: %" PRId32, peekVal);
|
|
||||||
|
|
||||||
i32 insertValue = 80085;
|
|
||||||
|
|
||||||
QINFO("Inserting a value at position 2.");
|
|
||||||
listInsertAt(&my_list, 2, &insertValue);
|
|
||||||
|
|
||||||
QDEBUG("Data at position 2 is: %" PRId32, *(i32 *)listDataAt(&my_list, 2));
|
|
||||||
|
|
||||||
QINFO("Removing a value at position 2.");
|
|
||||||
listRemoveAt(&my_list, 2);
|
|
||||||
|
|
||||||
QDEBUG("Data at position 2 is now: %" PRId32,
|
|
||||||
*(i32 *)listDataAt(&my_list, 2));
|
|
||||||
|
|
||||||
QINFO("Destorying List...");
|
|
||||||
listDestroy(&my_list);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user