Remove tracking of free state in freelist header since being in the list means it is also free

This commit is contained in:
Imbus 2025-09-08 07:38:22 +02:00
parent 69d4683cc5
commit 39c331cd09
3 changed files with 1 additions and 11 deletions

View file

@ -33,7 +33,6 @@ int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) {
FreeListBlock *block = (FreeListBlock *)start; FreeListBlock *block = (FreeListBlock *)start;
for (size_t i = 0; i < fl_capacity(fl); i++) { for (size_t i = 0; i < fl_capacity(fl); i++) {
block->next = (FreeListBlock *)((void *)block + size); block->next = (FreeListBlock *)((void *)block + size);
block->block_state = FL_FREE;
block = block->next; 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 */ /* Allocate some memory from the FreeList */
void *fl_alloc(FreeList *fl) { void *fl_alloc(FreeList *fl) {
if (!fl->free || fl->free->block_state != FL_FREE) if (!fl->free)
return NULL; return NULL;
FreeListBlock *m = fl->free; FreeListBlock *m = fl->free;
m->block_state = FL_USED;
fl->free = fl->free->next; fl->free = fl->free->next;
fl->allocated++; fl->allocated++;
@ -65,11 +63,6 @@ int fl_free(FreeList *fl, void *ptr) {
FreeListBlock *block = (FreeListBlock *)((uintptr_t)ptr - sizeof(FreeListBlock)); 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; block->next = fl->free;
fl->free = block; fl->free = block;
fl->allocated--; fl->allocated--;
@ -109,7 +102,6 @@ size_t fl_check(FreeList *fl) {
while (cursor->next != NULL) { while (cursor->next != NULL) {
avail++; avail++;
assert(cursor->block_state == FL_FREE);
cursor = cursor->next; cursor = cursor->next;
} }

View file

@ -10,7 +10,6 @@
typedef struct FreeListBlock { typedef struct FreeListBlock {
struct FreeListBlock *next; struct FreeListBlock *next;
uint8_t block_state;
} FreeListBlock; } FreeListBlock;
typedef struct { typedef struct {

View file

@ -51,7 +51,6 @@ int main() {
assert(fl_free(&fl, a) == EXIT_SUCCESS); assert(fl_free(&fl, a) == EXIT_SUCCESS);
assert(fl_free(&fl, b) == EXIT_SUCCESS); assert(fl_free(&fl, b) == EXIT_SUCCESS);
assert(fl_free(&fl, c) == 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)); printf("Available: %zu of %zu\n", fl_available(&fl), fl_capacity(&fl));