Update freelist
This commit is contained in:
parent
d583dbaced
commit
ca0b25d110
2 changed files with 22 additions and 31 deletions
|
@ -4,23 +4,15 @@
|
||||||
#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. */
|
struct FreeListBlock {
|
||||||
#ifdef FREELIST_NOALIGN
|
struct FreeListBlock *next;
|
||||||
#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 + sizeof(FreeListBlock));
|
size_t size = ALIGN(itemsize);
|
||||||
|
|
||||||
if (!fl || end <= start)
|
if (!fl || end <= start)
|
||||||
return EXIT_FAILURE;
|
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;
|
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,16 +36,16 @@ 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++;
|
||||||
|
|
||||||
return ((void *)m) + sizeof(FreeListBlock);
|
memset((void *)m, 0, sizeof(FreeListBlock));
|
||||||
|
return ((void *)m);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return some memory to the FreeList */
|
/* 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 */
|
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;
|
block->next = fl->free;
|
||||||
fl->free = block;
|
fl->free = block;
|
||||||
fl->allocated--;
|
fl->allocated--;
|
||||||
|
@ -109,7 +95,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,18 +5,24 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define FL_FREE ((uint8_t)0x00)
|
/* Align to nearest multiple of align */
|
||||||
#define FL_USED ((uint8_t)0x01)
|
static inline size_t align_up_to(size_t n, size_t align) {
|
||||||
|
return (n + align - 1) & ~(align - 1);
|
||||||
|
}
|
||||||
|
|
||||||
typedef struct FreeListBlock {
|
/* Fiddle these around according to your need. Delete or check makefile */
|
||||||
struct FreeListBlock *next;
|
#ifdef FREELIST_ALIGN
|
||||||
uint8_t block_state;
|
#define ALIGN(x) (align_up_to(x, sizeof(void *)))
|
||||||
} FreeListBlock;
|
#else
|
||||||
|
#define ALIGN(x) (x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct FreeListBlock FreeListBlock;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
FreeListBlock *free;
|
||||||
uintptr_t start;
|
uintptr_t start;
|
||||||
uintptr_t end;
|
uintptr_t end;
|
||||||
FreeListBlock *free;
|
|
||||||
size_t size;
|
size_t size;
|
||||||
size_t allocated;
|
size_t allocated;
|
||||||
} FreeList;
|
} FreeList;
|
||||||
|
|
Loading…
Add table
Reference in a new issue