Compare commits
No commits in common. "c72bce50ac86c8eb473212a529679a902b686baf" and "ca0b25d110fff8489a6f229fd6ed1aa276352781" have entirely different histories.
c72bce50ac
...
ca0b25d110
5 changed files with 43 additions and 107 deletions
|
|
@ -1,55 +1,37 @@
|
||||||
#include "freelist.h"
|
#include "freelist.h"
|
||||||
|
#include "stddef.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
/* Header block used to keep track of all the free blocks */
|
struct FreeListBlock {
|
||||||
struct __attribute__((packed)) FreeListBlock {
|
struct FreeListBlock *next;
|
||||||
uint16_t next_offset; /* May be 8, 16, 32, 64, whatever */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Align to nearest multiple of align */
|
|
||||||
static inline size_t align_up_to(size_t n, size_t align) {
|
|
||||||
return (n + align - 1) & ~(align - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Convert pointer -> 1-based offset (0 means NULL) */
|
|
||||||
static inline uint32_t ptr_to_offset(FreeList *fl, void *ptr) {
|
|
||||||
if (!ptr)
|
|
||||||
return 0; // NULL maps to 0
|
|
||||||
uintptr_t diff = (uintptr_t)ptr - fl->start;
|
|
||||||
return (uint32_t)(diff / fl->size) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Convert 1-based offset -> pointer (0 means NULL) */
|
|
||||||
static inline void *offset_to_ptr(FreeList *fl, uint32_t offset) {
|
|
||||||
if (offset == 0)
|
|
||||||
return NULL; // 0 = invalid/null
|
|
||||||
return (void *)(fl->start + (uintptr_t)(offset - 1) * fl->size);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Initialize the FreeList */
|
/* Initialize the FreeList */
|
||||||
int fl_init(FreeList *fl, uintptr_t start, size_t size_bytes, size_t itemsize) {
|
int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) {
|
||||||
/* Fiddle around according to your need, (void *) alignment seems to be enough,
|
size_t size = ALIGN(itemsize);
|
||||||
* but MAX_ALIGN_T is also an option. Especially for allocator implementation. */
|
|
||||||
size_t size = align_up_to(itemsize, sizeof(void *));
|
|
||||||
|
|
||||||
if (size < sizeof(FreeListBlock) || !fl)
|
if (!fl || end <= start)
|
||||||
return 0;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
fl->start = start;
|
fl->start = start;
|
||||||
fl->end = start + size_bytes;
|
fl->end = end;
|
||||||
fl->size = size;
|
fl->size = size;
|
||||||
fl->allocated = 0;
|
fl->allocated = 0;
|
||||||
|
|
||||||
FreeListBlock *block = (FreeListBlock *)start;
|
FreeListBlock *block = (FreeListBlock *)start;
|
||||||
|
for (size_t i = 0; i < fl_capacity(fl); i++) {
|
||||||
for (size_t offset = 1; offset < fl_capacity(fl) + 1; offset++) {
|
block->next = (FreeListBlock *)((void *)block + size);
|
||||||
block->next_offset = (int32_t)offset;
|
block = block->next;
|
||||||
block = offset_to_ptr(fl, offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
block->next_offset = 0; /* Last block */
|
block->next = NULL;
|
||||||
fl->free = (FreeListBlock *)start;
|
|
||||||
|
|
||||||
return 1;
|
fl->free = (FreeListBlock *)start;
|
||||||
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocate some memory from the FreeList */
|
/* Allocate some memory from the FreeList */
|
||||||
|
|
@ -59,32 +41,26 @@ void *fl_alloc(FreeList *fl) {
|
||||||
|
|
||||||
FreeListBlock *m = fl->free;
|
FreeListBlock *m = fl->free;
|
||||||
|
|
||||||
fl->free = offset_to_ptr(fl, fl->free->next_offset); /* May be null, which is fine */
|
fl->free = fl->free->next;
|
||||||
fl->allocated++;
|
fl->allocated++;
|
||||||
|
|
||||||
/* Wipe it before sending it out, could use memset
|
memset((void *)m, 0, sizeof(FreeListBlock));
|
||||||
* here, or even wiping the entire block */
|
|
||||||
m->next_offset = 0;
|
|
||||||
|
|
||||||
/* For reference: */
|
|
||||||
// memset((void *)m, 0, fl->size); /* Wipe entire block */
|
|
||||||
// memset((void *)m, 0, sizeof(FreeListBlock)); /* Wipe only header */
|
|
||||||
|
|
||||||
return ((void *)m);
|
return ((void *)m);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return some memory to the FreeList */
|
/* Return some memory to the FreeList */
|
||||||
int fl_free(FreeList *fl, void *ptr) {
|
int fl_free(FreeList *fl, void *ptr) {
|
||||||
if (!fl_is_managed(fl, ptr))
|
if (!fl_is_managed(fl, ptr)) {
|
||||||
return 0; /* We cant free memory we do not own */
|
return EXIT_FAILURE; /* We cant free memory we do not own */
|
||||||
|
}
|
||||||
|
|
||||||
FreeListBlock *block = (FreeListBlock *)ptr;
|
FreeListBlock *block = (FreeListBlock *)(uintptr_t)ptr;
|
||||||
|
|
||||||
block->next_offset = ptr_to_offset(fl, fl->free); /* May be null, which is fine */
|
block->next = fl->free;
|
||||||
fl->free = block;
|
fl->free = block;
|
||||||
fl->allocated--;
|
fl->allocated--;
|
||||||
|
|
||||||
return 1;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns how many slots are occupied */
|
/* Returns how many slots are occupied */
|
||||||
|
|
@ -104,13 +80,7 @@ size_t fl_capacity(FreeList *fl) {
|
||||||
|
|
||||||
/* Check if a piece of memory is managed by the FreeList */
|
/* Check if a piece of memory is managed by the FreeList */
|
||||||
int fl_is_managed(FreeList *fl, void *ptr) {
|
int fl_is_managed(FreeList *fl, void *ptr) {
|
||||||
uintptr_t p = (uintptr_t)ptr;
|
return ((uintptr_t)ptr >= fl->start && (uintptr_t)ptr < fl->end);
|
||||||
|
|
||||||
if (p < fl->start || p >= fl->end) {
|
|
||||||
return 0; // outside pool
|
|
||||||
}
|
|
||||||
|
|
||||||
return ((p - fl->start) % fl->size) == 0; // aligned to block
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns the ratio of metadata versus data as a scalar in range 0..1 */
|
/* Returns the ratio of metadata versus data as a scalar in range 0..1 */
|
||||||
|
|
@ -123,16 +93,9 @@ size_t fl_check(FreeList *fl) {
|
||||||
int avail = 0;
|
int avail = 0;
|
||||||
FreeListBlock *cursor = fl->free;
|
FreeListBlock *cursor = fl->free;
|
||||||
|
|
||||||
while (cursor) {
|
while (cursor->next != NULL) {
|
||||||
avail++;
|
avail++;
|
||||||
|
cursor = cursor->next;
|
||||||
if (!fl_is_managed(fl, cursor) || avail > fl_capacity(fl))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (cursor->next_offset == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
cursor = offset_to_ptr(fl, cursor->next_offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return avail;
|
return avail;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,18 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* Align to nearest multiple of align */
|
||||||
|
static inline size_t align_up_to(size_t n, size_t align) {
|
||||||
|
return (n + align - 1) & ~(align - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Fiddle these around according to your need. Delete or check makefile */
|
||||||
|
#ifdef FREELIST_ALIGN
|
||||||
|
#define ALIGN(x) (align_up_to(x, sizeof(void *)))
|
||||||
|
#else
|
||||||
|
#define ALIGN(x) (x)
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct FreeListBlock FreeListBlock;
|
typedef struct FreeListBlock FreeListBlock;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
@ -15,7 +27,7 @@ typedef struct {
|
||||||
size_t allocated;
|
size_t allocated;
|
||||||
} FreeList;
|
} FreeList;
|
||||||
|
|
||||||
int fl_init(FreeList *fl, uintptr_t start, size_t size_bytes, size_t itemsize);
|
int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize);
|
||||||
int fl_free(FreeList *fl, void *ptr);
|
int fl_free(FreeList *fl, void *ptr);
|
||||||
int fl_is_managed(FreeList *fl, void *ptr);
|
int fl_is_managed(FreeList *fl, void *ptr);
|
||||||
void *fl_alloc(FreeList *fl);
|
void *fl_alloc(FreeList *fl);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
#ifndef STDALIGN_H
|
|
||||||
#define STDALIGN_H
|
|
||||||
|
|
||||||
#define ALIGN_UP(n, align) (((n) + (align) - 1) & ~((align) - 1))
|
|
||||||
#define ALIGN_DOWN(n, align) ((n) & ~((align) - 1))
|
|
||||||
|
|
||||||
#endif // STDALIGN_H
|
|
||||||
|
|
@ -10,15 +10,4 @@
|
||||||
#define size_t uint64_t
|
#define size_t uint64_t
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
#if __riscv_xlen == 64
|
|
||||||
long double ld;
|
|
||||||
#elif __riscv_xlen == 32
|
|
||||||
long long ll;
|
|
||||||
#else
|
|
||||||
double d;
|
|
||||||
#endif
|
|
||||||
void *p;
|
|
||||||
} max_align_t;
|
|
||||||
|
|
||||||
#endif // STDDEF_H
|
#endif // STDDEF_H
|
||||||
|
|
|
||||||
|
|
@ -24,11 +24,6 @@ typedef long int64_t;
|
||||||
typedef uint64_t size_t;
|
typedef uint64_t size_t;
|
||||||
typedef uint64_t uintptr_t;
|
typedef uint64_t uintptr_t;
|
||||||
|
|
||||||
typedef int32_t int_fast16_t;
|
|
||||||
typedef int32_t int_fast32_t;
|
|
||||||
typedef uint32_t uint_fast16_t;
|
|
||||||
typedef uint32_t uint_fast32_t;
|
|
||||||
|
|
||||||
#define INT8_MIN (-128)
|
#define INT8_MIN (-128)
|
||||||
#define INT16_MIN (-32767 - 1)
|
#define INT16_MIN (-32767 - 1)
|
||||||
#define INT32_MIN (-2147483647 - 1)
|
#define INT32_MIN (-2147483647 - 1)
|
||||||
|
|
@ -44,20 +39,4 @@ typedef uint32_t uint_fast32_t;
|
||||||
#define UINT32_MAX (4294967295U)
|
#define UINT32_MAX (4294967295U)
|
||||||
#define UINT64_MAX (__UINT64_C(18446744073709551615))
|
#define UINT64_MAX (__UINT64_C(18446744073709551615))
|
||||||
|
|
||||||
#define INT_FAST16_MIN INT32_MIN
|
|
||||||
#define INT_FAST32_MIN INT32_MIN
|
|
||||||
|
|
||||||
#define INT_FAST16_MAX INT32_MAX
|
|
||||||
#define INT_FAST32_MAX INT32_MAX
|
|
||||||
|
|
||||||
#define UINT_FAST16_MAX UINT32_MAX
|
|
||||||
#define UINT_FAST32_MAX UINT32_MAX
|
|
||||||
|
|
||||||
#define INTPTR_MIN INT64_MIN
|
|
||||||
#define INTPTR_MAX INT64_MAX
|
|
||||||
#define UINTPTR_MAX UINT64_MAX
|
|
||||||
#define PTRDIFF_MIN INT64_MIN
|
|
||||||
#define PTRDIFF_MAX INT64_MAX
|
|
||||||
#define SIZE_MAX UINT64_MAX
|
|
||||||
|
|
||||||
#endif // STDINT_H
|
#endif // STDINT_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue