Compare commits
No commits in common. "a8b7fc75e3a68e6cef133a1f4c371f15e2b5a689" and "39c331cd09b9d954e4bf2d109262165963f49779" have entirely different histories.
a8b7fc75e3
...
39c331cd09
4 changed files with 22 additions and 31 deletions
|
|
@ -1,8 +1,6 @@
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -Wall -O2
|
CFLAGS = -Wall -O2
|
||||||
|
|
||||||
CFLAGS += -DFREELIST_NOALIGN
|
|
||||||
|
|
||||||
TARGET = main.elf
|
TARGET = main.elf
|
||||||
SRC = main.c freelist.c
|
SRC = main.c freelist.c
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,23 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.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
|
||||||
|
|
||||||
/* Initialize the FreeList */
|
/* Initialize the FreeList */
|
||||||
int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) {
|
int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) {
|
||||||
size_t size = ALIGN(itemsize);
|
size_t size = ALIGN(itemsize + sizeof(FreeListBlock));
|
||||||
|
|
||||||
if (!fl || end <= start)
|
if (!fl || end <= start)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|
@ -40,8 +52,7 @@ void *fl_alloc(FreeList *fl) {
|
||||||
fl->free = fl->free->next;
|
fl->free = fl->free->next;
|
||||||
fl->allocated++;
|
fl->allocated++;
|
||||||
|
|
||||||
memset((void *)m, 0, sizeof(FreeListBlock));
|
return ((void *)m) + sizeof(FreeListBlock);
|
||||||
return ((void *)m);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return some memory to the FreeList */
|
/* Return some memory to the FreeList */
|
||||||
|
|
@ -50,7 +61,7 @@ int fl_free(FreeList *fl, void *ptr) {
|
||||||
return EXIT_FAILURE; /* We cant free memory we do not own */
|
return EXIT_FAILURE; /* We cant free memory we do not own */
|
||||||
}
|
}
|
||||||
|
|
||||||
FreeListBlock *block = (FreeListBlock *)(uintptr_t)ptr;
|
FreeListBlock *block = (FreeListBlock *)((uintptr_t)ptr - sizeof(FreeListBlock));
|
||||||
|
|
||||||
block->next = fl->free;
|
block->next = fl->free;
|
||||||
fl->free = block;
|
fl->free = block;
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,6 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.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_FREE ((uint8_t)0x00)
|
||||||
#define FL_USED ((uint8_t)0x01)
|
#define FL_USED ((uint8_t)0x01)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,21 +17,16 @@ void printvec(const Vec3 *v) {
|
||||||
printf("Vec3: (%d, %d, %d)\n", v->x, v->y, v->z);
|
printf("Vec3: (%d, %d, %d)\n", v->x, v->y, v->z);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BUFFER_SIZE (4096)
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
FreeList fl;
|
FreeList fl;
|
||||||
const char *mem = malloc(BUFFER_SIZE);
|
char *mem = malloc(4096);
|
||||||
|
|
||||||
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);
|
assert(fl_available(&fl) == cap);
|
||||||
|
|
||||||
|
printf("Space utilization: %.2f%%\n", 100.0 * fl_utilization(&fl, sizeof(Vec3)));
|
||||||
|
|
||||||
Vec3 *a = fl_alloc(&fl);
|
Vec3 *a = fl_alloc(&fl);
|
||||||
Vec3 *b = fl_alloc(&fl);
|
Vec3 *b = fl_alloc(&fl);
|
||||||
Vec3 *c = fl_alloc(&fl);
|
Vec3 *c = fl_alloc(&fl);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue