Remove tracking of free state in freelist header since being in the list means it is also free
This commit is contained in:
parent
69d4683cc5
commit
39c331cd09
3 changed files with 1 additions and 11 deletions
|
|
@ -33,7 +33,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,11 +44,10 @@ 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++;
|
||||
|
|
@ -65,11 +63,6 @@ int fl_free(FreeList *fl, void *ptr) {
|
|||
|
||||
FreeListBlock *block = (FreeListBlock *)((uintptr_t)ptr - sizeof(FreeListBlock));
|
||||
|
||||
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 +102,6 @@ size_t fl_check(FreeList *fl) {
|
|||
|
||||
while (cursor->next != NULL) {
|
||||
avail++;
|
||||
assert(cursor->block_state == FL_FREE);
|
||||
cursor = cursor->next;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@
|
|||
|
||||
typedef struct FreeListBlock {
|
||||
struct FreeListBlock *next;
|
||||
uint8_t block_state;
|
||||
} FreeListBlock;
|
||||
|
||||
typedef struct {
|
||||
|
|
|
|||
|
|
@ -51,7 +51,6 @@ int main() {
|
|||
assert(fl_free(&fl, a) == EXIT_SUCCESS);
|
||||
assert(fl_free(&fl, b) == EXIT_SUCCESS);
|
||||
assert(fl_free(&fl, c) == EXIT_SUCCESS);
|
||||
assert(fl_free(&fl, a) == EXIT_FAILURE); // Double free
|
||||
|
||||
printf("Available: %zu of %zu\n", fl_available(&fl), fl_capacity(&fl));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue