Compare commits

...

4 commits

Author SHA1 Message Date
Imbus
a8b7fc75e3 Some more info in demo main 2025-09-08 07:52:54 +02:00
Imbus
13a0d42514 Remove block header from allocated blocks entirely 2025-09-08 07:52:41 +02:00
Imbus
8f41b0873d Disable align in makefile 2025-09-08 07:43:22 +02:00
Imbus
be3d902be5 freelist: Move align macro fiddling into header 2025-09-08 07:43:15 +02:00
4 changed files with 31 additions and 22 deletions

View file

@ -1,6 +1,8 @@
CC = gcc
CFLAGS = -Wall -O2
CFLAGS += -DFREELIST_NOALIGN
TARGET = main.elf
SRC = main.c freelist.c

View file

@ -4,23 +4,11 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.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
#include <string.h>
/* 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;
@ -52,7 +40,8 @@ void *fl_alloc(FreeList *fl) {
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 */
@ -61,7 +50,7 @@ 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;
block->next = fl->free;
fl->free = block;

View file

@ -5,6 +5,19 @@
#include <stdint.h>
#include <stdlib.h>
/* Fiddle these around according to your need. Delete or check makefile */
#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
#define FL_FREE ((uint8_t)0x00)
#define FL_USED ((uint8_t)0x01)

View file

@ -17,16 +17,21 @@ void printvec(const Vec3 *v) {
printf("Vec3: (%d, %d, %d)\n", v->x, v->y, v->z);
}
#define BUFFER_SIZE (4096)
int main() {
FreeList fl;
char *mem = malloc(4096);
FreeList fl;
const char *mem = malloc(BUFFER_SIZE);
fl_init(&fl, (uintptr_t)mem, (uintptr_t)mem + BUFFER_SIZE, sizeof(Vec3));
const size_t cap = fl_capacity(&fl);
printf("Item size: %lu\n", sizeof(Vec3));
printf("Buffer size: %d\n", BUFFER_SIZE);
printf("Space utilization internal: %.2f%%\n", 100.0 * fl_utilization(&fl, sizeof(Vec3)));
fl_init(&fl, (uintptr_t)mem, (uintptr_t)mem + 4096, sizeof(Vec3));
size_t cap = fl_capacity(&fl);
assert(fl_available(&fl) == cap);
printf("Space utilization: %.2f%%\n", 100.0 * fl_utilization(&fl, sizeof(Vec3)));
Vec3 *a = fl_alloc(&fl);
Vec3 *b = fl_alloc(&fl);
Vec3 *c = fl_alloc(&fl);