Start implementing the allocator

This commit is contained in:
Abdelrahman Said 2023-05-16 00:10:05 +01:00
parent f099d810a3
commit 583fc634e4
2 changed files with 78 additions and 0 deletions

65
allocator.c Normal file
View File

@ -0,0 +1,65 @@
#include "allocator.h"
#include "aliases.h"
#include <stdbool.h>
#include <stdlib.h>
#define HEADER_MAGIC_01 76329872
#define HEADER_MAGIC_02 120830238
#define FOOTER_MAGIC_01 2783946
#define FOOTER_MAGIC_02 878512284
struct allocator {
u8 *buf;
u64 size;
u64 used;
};
struct block_boundary {
u64 size;
bool free;
u64 magic_01;
u64 magic_02;
};
typedef struct block_boundary block_header;
typedef struct block_boundary block_footer;
typedef struct {
block_header header;
u8 *data;
block_footer footer;
} block_t;
allocator_t *init_allocator(u64 size) {
allocator_t *allocator = (allocator_t *)malloc(sizeof(allocator_t));
if (!allocator) {
return NULL;
}
allocator->buf = (u8 *)malloc(size);
if (!(allocator->buf)) {
free(allocator);
return NULL;
}
allocator->size = size;
allocator->used = 0;
return allocator;
}
void deinit_allocator(allocator_t **allocator) {
if (*allocator != NULL) {
if ((*allocator)->buf != NULL) {
free((*allocator)->buf);
(*allocator)->buf = NULL;
}
free(*allocator);
*allocator = NULL;
}
}
void *alloc(allocator_t *allocator, u64 size) {}

13
allocator.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef ALLOCATOR_H
#define ALLOCATOR_H
#include "aliases.h"
#include <stdbool.h>
typedef struct allocator allocator_t;
allocator_t *init_allocator(u64 size);
void deinit_allocator(allocator_t **allocator);
void *alloc(allocator_t *allocator, u64 size);
#endif // !ALLOCATOR_H