Compare commits
4 commits
39c331cd09
...
a8b7fc75e3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a8b7fc75e3 | ||
|
|
13a0d42514 | ||
|
|
8f41b0873d | ||
|
|
be3d902be5 |
4 changed files with 31 additions and 22 deletions
|
|
@ -1,6 +1,8 @@
|
|||
CC = gcc
|
||||
CFLAGS = -Wall -O2
|
||||
|
||||
CFLAGS += -DFREELIST_NOALIGN
|
||||
|
||||
TARGET = main.elf
|
||||
SRC = main.c freelist.c
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue