fix: using typedefs

This commit is contained in:
jackjohn7 2026-06-17 02:38:36 -05:00
parent 01f057afb9
commit 4e2e2cd69f

View file

@ -7,37 +7,38 @@
#define HEAP_SIZE (1024 * 1024 * 2) #define HEAP_SIZE (1024 * 1024 * 2)
#define ALIGNMENT 16 #define ALIGNMENT 16
#define ALIGN(size) (((size) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1)) #define ALIGN(size) (((size) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1))
#define HEADER_SIZE (ALIGN(sizeof(struct Header)))
#define FOOTER_SIZE (ALIGN(sizeof(struct Footer)))
struct Header { typedef struct Header {
size_t size; size_t size;
bool is_free; bool is_free;
struct Header *next; struct Header *next;
struct Header *next_free; struct Header *next_free;
}; } Header;
struct Footer { typedef struct Footer {
struct Header *header; struct Header *header;
}; } Footer ;
#define HEADER_SIZE (ALIGN(sizeof(Header)))
#define FOOTER_SIZE (ALIGN(sizeof(Footer)))
static uint8_t heap[HEAP_SIZE]; static uint8_t heap[HEAP_SIZE];
static struct Header *free_list; static Header *free_list;
static bool initialized = false; static bool initialized = false;
struct Footer *footer(struct Header *hdr) { Footer *footer(Header *hdr) {
return (struct Footer *)((uint8_t *)hdr + HEADER_SIZE + hdr->size); return (Footer *)((uint8_t *)hdr + HEADER_SIZE + hdr->size);
} }
struct Header *pop_first_fit(size_t size) { Header *pop_first_fit(size_t size) {
if (free_list == NULL) return NULL; if (free_list == NULL) return NULL;
struct Header *head = free_list; Header *head = free_list;
if (head->size >= size) { if (head->size >= size) {
free_list = head->next_free; // remove from free list free_list = head->next_free; // remove from free list
return head; return head;
} }
while (head != NULL) { while (head != NULL) {
struct Header *next = head->next_free; Header *next = head->next_free;
if (next == NULL) return NULL; if (next == NULL) return NULL;
if (next->size >= size) { if (next->size >= size) {
head->next_free = next->next_free; // remove from free list head->next_free = next->next_free; // remove from free list
@ -48,15 +49,15 @@ struct Header *pop_first_fit(size_t size) {
return NULL; return NULL;
} }
struct Header *pop(struct Header *ptr) { Header *pop(Header *ptr) {
if (free_list == NULL) return NULL; if (free_list == NULL) return NULL;
struct Header *head = free_list; Header *head = free_list;
if (head == ptr) { if (head == ptr) {
free_list = head->next_free; free_list = head->next_free;
return head; return head;
} }
while (head != NULL) { while (head != NULL) {
struct Header *next = head->next_free; Header *next = head->next_free;
if (next == NULL) break; // if next is NULL, deref below will fault if (next == NULL) break; // if next is NULL, deref below will fault
if (next == ptr) { if (next == ptr) {
head->next_free = next->next_free; head->next_free = next->next_free;
@ -67,7 +68,7 @@ struct Header *pop(struct Header *ptr) {
return NULL; return NULL;
} }
void push_header(struct Header *hdr) { void push_header(Header *hdr) {
hdr->next_free = free_list; hdr->next_free = free_list;
free_list = hdr; free_list = hdr;
} }
@ -76,19 +77,19 @@ void initialize() {
// initialize the free list to be a single element containing the entire block // initialize the free list to be a single element containing the entire block
size_t initial_block_size = HEAP_SIZE - (HEADER_SIZE + FOOTER_SIZE); size_t initial_block_size = HEAP_SIZE - (HEADER_SIZE + FOOTER_SIZE);
// write a header // write a header
struct Header *hdr = (struct Header *) heap; // first two alignment slots taken by initial header Header *hdr = (Header *) heap; // first two alignment slots taken by initial header
hdr->is_free = true; hdr->is_free = true;
hdr->size = initial_block_size; hdr->size = initial_block_size;
hdr->next = NULL; hdr->next = NULL;
hdr->next_free = NULL; hdr->next_free = NULL;
push_header(hdr); push_header(hdr);
// write a footer to heap // write a footer to heap
struct Footer *ftr = footer(hdr); Footer *ftr = footer(hdr);
ftr->header = hdr; ftr->header = hdr;
initialized = true; initialized = true;
} }
struct Header *shrink(struct Header *hdr, size_t size) { Header *shrink(Header *hdr, size_t size) {
// given a size, shrink a block down to the appropriate alignment. // given a size, shrink a block down to the appropriate alignment.
// if align(size) + ALIGNMENT*3 is equal to current size, just return initial ptr with noop // if align(size) + ALIGNMENT*3 is equal to current size, just return initial ptr with noop
// otherwise, shrink it down so that we have two blocks. // otherwise, shrink it down so that we have two blocks.
@ -101,16 +102,16 @@ struct Header *shrink(struct Header *hdr, size_t size) {
if (other_size >= ALIGNMENT) { if (other_size >= ALIGNMENT) {
hdr->size = size; // resize left block hdr->size = size; // resize left block
// create our footer // create our footer
struct Footer *ftr = footer(hdr); Footer *ftr = footer(hdr);
ftr->header = hdr; ftr->header = hdr;
// create next header // create next header
struct Header *next_hdr = (struct Header *)((uint8_t *)ftr + FOOTER_SIZE); Header *next_hdr = (Header *)((uint8_t *)ftr + FOOTER_SIZE);
next_hdr->size = other_size; next_hdr->size = other_size;
next_hdr->is_free = true; next_hdr->is_free = true;
next_hdr->next = hdr->next; next_hdr->next = hdr->next;
hdr->next = next_hdr; hdr->next = next_hdr;
// create next footer // create next footer
struct Footer *next_ftr = footer(next_hdr); Footer *next_ftr = footer(next_hdr);
next_ftr->header = next_hdr; next_ftr->header = next_hdr;
// push new block to list // push new block to list
@ -127,15 +128,15 @@ void *jalloc(size_t size) {
// Total size of allocation = HEADER(padded to ALIGNMENT) + size(padded to ALIGNMENT) + FOOTER(padded to ALIGNMENT) // Total size of allocation = HEADER(padded to ALIGNMENT) + size(padded to ALIGNMENT) + FOOTER(padded to ALIGNMENT)
size_t aligned_size = ALIGN(size); size_t aligned_size = ALIGN(size);
struct Header *hdr = pop_first_fit(aligned_size); Header *hdr = pop_first_fit(aligned_size);
if (hdr == NULL) return NULL; if (hdr == NULL) return NULL;
hdr = shrink(hdr, aligned_size); hdr = shrink(hdr, aligned_size);
struct Footer *ftr = footer(hdr); Footer *ftr = footer(hdr);
uint8_t* next_ptr = (uint8_t *) ftr + FOOTER_SIZE; // set next adjacent ptr uint8_t* next_ptr = (uint8_t *) ftr + FOOTER_SIZE; // set next adjacent ptr
if ((uint8_t *)next_ptr >= (heap + HEAP_SIZE)) next_ptr = NULL; if ((uint8_t *)next_ptr >= (heap + HEAP_SIZE)) next_ptr = NULL;
hdr->next = (struct Header *)next_ptr; hdr->next = (Header *)next_ptr;
hdr->is_free = false; hdr->is_free = false;
ftr->header = hdr; ftr->header = hdr;
return (uint8_t*)hdr + HEADER_SIZE; return (uint8_t*)hdr + HEADER_SIZE;
@ -143,12 +144,12 @@ void *jalloc(size_t size) {
void jfree(void *ptr) { void jfree(void *ptr) {
if (ptr == NULL) return; if (ptr == NULL) return;
struct Header *hdr = (struct Header *)((uint8_t *)ptr-HEADER_SIZE); Header *hdr = (Header *)((uint8_t *)ptr-HEADER_SIZE);
struct Footer *ftr = footer(hdr); Footer *ftr = footer(hdr);
hdr->is_free = true; hdr->is_free = true;
if ((uint8_t *)hdr > heap) { // if this is greater, then there ought to be a foot to the left if ((uint8_t *)hdr > heap) { // if this is greater, then there ought to be a foot to the left
struct Footer *prev_ftr = (struct Footer *)((uint8_t *)hdr-FOOTER_SIZE); Footer *prev_ftr = (struct Footer *)((uint8_t *)hdr-FOOTER_SIZE);
struct Header *prev_hdr = prev_ftr->header; Header *prev_hdr = prev_ftr->header;
if (prev_hdr->is_free) { if (prev_hdr->is_free) {
pop(prev_hdr); pop(prev_hdr);
prev_hdr->size += hdr->size + HEADER_SIZE + FOOTER_SIZE; prev_hdr->size += hdr->size + HEADER_SIZE + FOOTER_SIZE;
@ -158,8 +159,8 @@ void jfree(void *ptr) {
} }
} }
if (hdr->next != NULL) { if (hdr->next != NULL) {
struct Header *next_hdr = hdr->next; Header *next_hdr = hdr->next;
struct Footer *next_ftr = footer(next_hdr); Footer *next_ftr = footer(next_hdr);
if (next_hdr->is_free) { if (next_hdr->is_free) {
pop(next_hdr); pop(next_hdr);
hdr->size += next_hdr->size + HEADER_SIZE + FOOTER_SIZE; hdr->size += next_hdr->size + HEADER_SIZE + FOOTER_SIZE;