Update freelist

This commit is contained in:
Imbus 2025-09-08 09:16:40 +02:00
parent d583dbaced
commit ca0b25d110
2 changed files with 22 additions and 31 deletions

View file

@ -4,23 +4,15 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Fiddle these around according to your need. */
#ifdef FREELIST_NOALIGN
#define ALIGN(x) (x)
#else // FREELIST_NOALIGN
/* Align to nearest multiple of sizeof(void*) */
static inline size_t align_up(size_t n) {
return (n + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
}
#define ALIGN(x) (align_up(x))
#endif // FREELIST_NOALIGN
struct FreeListBlock {
struct FreeListBlock *next;
};
/* Initialize the FreeList */
int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) {
size_t size = ALIGN(itemsize + sizeof(FreeListBlock));
size_t size = ALIGN(itemsize);
if (!fl || end <= start)
return EXIT_FAILURE;
@ -33,7 +25,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,16 +36,16 @@ 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++;
return ((void *)m) + sizeof(FreeListBlock);
memset((void *)m, 0, sizeof(FreeListBlock));
return ((void *)m);
}
/* Return some memory to the FreeList */
@ -63,13 +54,8 @@ int fl_free(FreeList *fl, void *ptr) {
return EXIT_FAILURE; /* We cant free memory we do not own */
}
FreeListBlock *block = (FreeListBlock *)((uintptr_t)ptr - sizeof(FreeListBlock));
FreeListBlock *block = (FreeListBlock *)(uintptr_t)ptr;
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 +95,6 @@ size_t fl_check(FreeList *fl) {
while (cursor->next != NULL) {
avail++;
assert(cursor->block_state == FL_FREE);
cursor = cursor->next;
}

View file

@ -5,18 +5,24 @@
#include <stdint.h>
#include <stdlib.h>
#define FL_FREE ((uint8_t)0x00)
#define FL_USED ((uint8_t)0x01)
/* Align to nearest multiple of align */
static inline size_t align_up_to(size_t n, size_t align) {
return (n + align - 1) & ~(align - 1);
}
typedef struct FreeListBlock {
struct FreeListBlock *next;
uint8_t block_state;
} FreeListBlock;
/* Fiddle these around according to your need. Delete or check makefile */
#ifdef FREELIST_ALIGN
#define ALIGN(x) (align_up_to(x, sizeof(void *)))
#else
#define ALIGN(x) (x)
#endif
typedef struct FreeListBlock FreeListBlock;
typedef struct {
FreeListBlock *free;
uintptr_t start;
uintptr_t end;
FreeListBlock *free;
size_t size;
size_t allocated;
} FreeList;