From 4e2e2cd69f8ddcb20b84dc112e8d826e359eb8ae Mon Sep 17 00:00:00 2001 From: jackjohn7 <70782491+jackjohn7@users.noreply.github.com> Date: Wed, 17 Jun 2026 02:38:36 -0500 Subject: [PATCH] fix: using typedefs --- src/jalloc.c | 63 ++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/jalloc.c b/src/jalloc.c index f5e8163..b233f33 100644 --- a/src/jalloc.c +++ b/src/jalloc.c @@ -7,37 +7,38 @@ #define HEAP_SIZE (1024 * 1024 * 2) #define ALIGNMENT 16 #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; bool is_free; struct Header *next; struct Header *next_free; -}; +} Header; -struct Footer { +typedef struct Footer { struct Header *header; -}; +} Footer ; + +#define HEADER_SIZE (ALIGN(sizeof(Header))) +#define FOOTER_SIZE (ALIGN(sizeof(Footer))) static uint8_t heap[HEAP_SIZE]; -static struct Header *free_list; +static Header *free_list; static bool initialized = false; -struct Footer *footer(struct Header *hdr) { - return (struct Footer *)((uint8_t *)hdr + HEADER_SIZE + hdr->size); +Footer *footer(Header *hdr) { + 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; - struct Header *head = free_list; + Header *head = free_list; if (head->size >= size) { free_list = head->next_free; // remove from free list return head; } while (head != NULL) { - struct Header *next = head->next_free; + Header *next = head->next_free; if (next == NULL) return NULL; if (next->size >= size) { head->next_free = next->next_free; // remove from free list @@ -48,15 +49,15 @@ struct Header *pop_first_fit(size_t size) { return NULL; } -struct Header *pop(struct Header *ptr) { +Header *pop(Header *ptr) { if (free_list == NULL) return NULL; - struct Header *head = free_list; + Header *head = free_list; if (head == ptr) { free_list = head->next_free; return head; } 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 == ptr) { head->next_free = next->next_free; @@ -67,7 +68,7 @@ struct Header *pop(struct Header *ptr) { return NULL; } -void push_header(struct Header *hdr) { +void push_header(Header *hdr) { hdr->next_free = free_list; free_list = hdr; } @@ -76,19 +77,19 @@ void initialize() { // initialize the free list to be a single element containing the entire block size_t initial_block_size = HEAP_SIZE - (HEADER_SIZE + FOOTER_SIZE); // 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->size = initial_block_size; hdr->next = NULL; hdr->next_free = NULL; push_header(hdr); // write a footer to heap - struct Footer *ftr = footer(hdr); + Footer *ftr = footer(hdr); ftr->header = hdr; 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. // 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. @@ -101,16 +102,16 @@ struct Header *shrink(struct Header *hdr, size_t size) { if (other_size >= ALIGNMENT) { hdr->size = size; // resize left block // create our footer - struct Footer *ftr = footer(hdr); + Footer *ftr = footer(hdr); ftr->header = hdr; // 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->is_free = true; next_hdr->next = hdr->next; hdr->next = next_hdr; // create next footer - struct Footer *next_ftr = footer(next_hdr); + Footer *next_ftr = footer(next_hdr); next_ftr->header = next_hdr; // 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) 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; 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 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; ftr->header = hdr; return (uint8_t*)hdr + HEADER_SIZE; @@ -143,12 +144,12 @@ void *jalloc(size_t size) { void jfree(void *ptr) { if (ptr == NULL) return; - struct Header *hdr = (struct Header *)((uint8_t *)ptr-HEADER_SIZE); - struct Footer *ftr = footer(hdr); + Header *hdr = (Header *)((uint8_t *)ptr-HEADER_SIZE); + Footer *ftr = footer(hdr); hdr->is_free = true; 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); - struct Header *prev_hdr = prev_ftr->header; + Footer *prev_ftr = (struct Footer *)((uint8_t *)hdr-FOOTER_SIZE); + Header *prev_hdr = prev_ftr->header; if (prev_hdr->is_free) { pop(prev_hdr); prev_hdr->size += hdr->size + HEADER_SIZE + FOOTER_SIZE; @@ -158,8 +159,8 @@ void jfree(void *ptr) { } } if (hdr->next != NULL) { - struct Header *next_hdr = hdr->next; - struct Footer *next_ftr = footer(next_hdr); + Header *next_hdr = hdr->next; + Footer *next_ftr = footer(next_hdr); if (next_hdr->is_free) { pop(next_hdr); hdr->size += next_hdr->size + HEADER_SIZE + FOOTER_SIZE;