feat: add gdb, add some free logic

This commit is contained in:
jackjohn7 2026-06-16 01:05:21 -05:00
parent 134e27d38b
commit 9622487008
2 changed files with 26 additions and 4 deletions

View file

@ -9,6 +9,7 @@
glibc glibc
gcc gcc
gnumake gnumake
gdb
clang clang
clang-tools clang-tools
nixfmt nixfmt

View file

@ -35,17 +35,38 @@ void *jalloc(size_t size) {
void *ptr = heap_ptr + HEADER_SIZE; void *ptr = heap_ptr + HEADER_SIZE;
struct Header *hdr = (struct Header *)heap_ptr; struct Header *hdr = (struct Header *)heap_ptr;
struct Footer *ftr = (struct Footer *)(ptr + aligned_size); struct Footer *ftr = (struct Footer *)(ptr + aligned_size);
if (ftr + FOOTER_SIZE > heap + sizeof(heap)) return NULL; if ((uint8_t *)ftr + FOOTER_SIZE > (uint8_t *)heap + sizeof(heap)) return NULL;
hdr->size = size; heap_ptr = (void *) ftr + FOOTER_SIZE; // set next ptr
if ((uint8_t *)heap_ptr > (heap + HEAP_SIZE)) heap_ptr = NULL;
hdr->size = aligned_size;
hdr->next = (struct Header *)heap_ptr; hdr->next = (struct Header *)heap_ptr;
hdr->is_free = false; hdr->is_free = false;
ftr->header = hdr; ftr->header = hdr;
heap_ptr = (void *) ftr + FOOTER_SIZE; // set next ptr
return ptr; return ptr;
} }
void jfree(void *ptr) { void jfree(void *ptr) {
struct Header *hdr = (struct Header *)(ptr-HEADER_SIZE); struct Header *hdr = (struct Header *)(ptr-HEADER_SIZE);
struct Footer *ftr = (struct Footer *)(hdr->next-FOOTER_SIZE); struct Footer *ftr = (struct Footer *)((uint8_t *)hdr+hdr->size);
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
struct Footer *prev_ftr = (struct Footer *)((uint8_t *)hdr-FOOTER_SIZE);
struct Header *prev_hdr = prev_ftr->header;
if (prev_hdr->is_free) {
prev_hdr->size += hdr->size + HEADER_SIZE + FOOTER_SIZE;
prev_hdr->next=hdr->next;
ftr->header=prev_hdr;
hdr = prev_hdr;
}
}
if (hdr->next != NULL) {
struct Header *next_hdr = hdr->next;
struct Footer *next_ftr = (struct Footer *) ((uint8_t *)next_hdr + next_hdr->size);
if (next_hdr->is_free) {
hdr->size += next_hdr->size + HEADER_SIZE + FOOTER_SIZE;
hdr->next = (struct Header *) ((uint8_t*)next_ftr + FOOTER_SIZE);
next_ftr->header = hdr;
ftr = next_ftr;
}
}
} }