From 39c331cd09b9d954e4bf2d109262165963f49779 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 8 Sep 2025 07:38:22 +0200 Subject: [PATCH] Remove tracking of free state in freelist header since being in the list means it is also free --- freelist/freelist.c | 10 +--------- freelist/freelist.h | 1 - freelist/main.c | 1 - 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/freelist/freelist.c b/freelist/freelist.c index cba0b1f..9b47a8d 100644 --- a/freelist/freelist.c +++ b/freelist/freelist.c @@ -33,7 +33,6 @@ int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) { FreeListBlock *block = (FreeListBlock *)start; for (size_t i = 0; i < fl_capacity(fl); i++) { block->next = (FreeListBlock *)((void *)block + size); - block->block_state = FL_FREE; block = block->next; } @@ -45,11 +44,10 @@ int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) { /* Allocate some memory from the FreeList */ void *fl_alloc(FreeList *fl) { - if (!fl->free || fl->free->block_state != FL_FREE) + if (!fl->free) return NULL; FreeListBlock *m = fl->free; - m->block_state = FL_USED; fl->free = fl->free->next; fl->allocated++; @@ -65,11 +63,6 @@ int fl_free(FreeList *fl, void *ptr) { FreeListBlock *block = (FreeListBlock *)((uintptr_t)ptr - sizeof(FreeListBlock)); - if (block->block_state != FL_USED) { - return EXIT_FAILURE; /* Block must be used */ - } - - block->block_state = FL_FREE; block->next = fl->free; fl->free = block; fl->allocated--; @@ -109,7 +102,6 @@ size_t fl_check(FreeList *fl) { while (cursor->next != NULL) { avail++; - assert(cursor->block_state == FL_FREE); cursor = cursor->next; } diff --git a/freelist/freelist.h b/freelist/freelist.h index 885b58a..de12d5d 100644 --- a/freelist/freelist.h +++ b/freelist/freelist.h @@ -10,7 +10,6 @@ typedef struct FreeListBlock { struct FreeListBlock *next; - uint8_t block_state; } FreeListBlock; typedef struct { diff --git a/freelist/main.c b/freelist/main.c index 9d18438..0841d2e 100644 --- a/freelist/main.c +++ b/freelist/main.c @@ -51,7 +51,6 @@ int main() { assert(fl_free(&fl, a) == EXIT_SUCCESS); assert(fl_free(&fl, b) == EXIT_SUCCESS); assert(fl_free(&fl, c) == EXIT_SUCCESS); - assert(fl_free(&fl, a) == EXIT_FAILURE); // Double free printf("Available: %zu of %zu\n", fl_available(&fl), fl_capacity(&fl));