From 96224870082a0fd33010fb736528a66f5e85e6a8 Mon Sep 17 00:00:00 2001 From: jackjohn7 <70782491+jackjohn7@users.noreply.github.com> Date: Tue, 16 Jun 2026 01:05:21 -0500 Subject: [PATCH] feat: add gdb, add some free logic --- nix-modules/shell.nix | 1 + src/jalloc.c | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/nix-modules/shell.nix b/nix-modules/shell.nix index 64ad929..47bd045 100644 --- a/nix-modules/shell.nix +++ b/nix-modules/shell.nix @@ -9,6 +9,7 @@ glibc gcc gnumake + gdb clang clang-tools nixfmt diff --git a/src/jalloc.c b/src/jalloc.c index 7614256..30308a0 100644 --- a/src/jalloc.c +++ b/src/jalloc.c @@ -35,17 +35,38 @@ void *jalloc(size_t size) { void *ptr = heap_ptr + HEADER_SIZE; struct Header *hdr = (struct Header *)heap_ptr; struct Footer *ftr = (struct Footer *)(ptr + aligned_size); - if (ftr + FOOTER_SIZE > heap + sizeof(heap)) return NULL; - hdr->size = size; + if ((uint8_t *)ftr + FOOTER_SIZE > (uint8_t *)heap + sizeof(heap)) return NULL; + 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->is_free = false; ftr->header = hdr; - heap_ptr = (void *) ftr + FOOTER_SIZE; // set next ptr return ptr; } void jfree(void *ptr) { 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; + 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; + } + } }