diff --git a/kern/libkern/freelist.c b/kern/libkern/freelist.c index 35ffa41..f9dc531 100644 --- a/kern/libkern/freelist.c +++ b/kern/libkern/freelist.c @@ -1,55 +1,37 @@ #include "freelist.h" +#include "stddef.h" +#include +#include +#include +#include +#include -/* Header block used to keep track of all the free blocks */ -struct __attribute__((packed)) FreeListBlock { - uint16_t next_offset; /* May be 8, 16, 32, 64, whatever */ +struct FreeListBlock { + struct FreeListBlock *next; }; -/* 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 */ -int fl_init(FreeList *fl, uintptr_t start, size_t size_bytes, size_t itemsize) { - /* Fiddle around according to your need, (void *) alignment seems to be enough, - * but MAX_ALIGN_T is also an option. Especially for allocator implementation. */ - size_t size = align_up_to(itemsize, sizeof(void *)); +int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) { + size_t size = ALIGN(itemsize); - if (size < sizeof(FreeListBlock) || !fl) - return 0; + if (!fl || end <= start) + return EXIT_FAILURE; fl->start = start; - fl->end = start + size_bytes; + fl->end = end; fl->size = size; fl->allocated = 0; FreeListBlock *block = (FreeListBlock *)start; - - for (size_t offset = 1; offset < fl_capacity(fl) + 1; offset++) { - block->next_offset = (int32_t)offset; - block = offset_to_ptr(fl, offset); + for (size_t i = 0; i < fl_capacity(fl); i++) { + block->next = (FreeListBlock *)((void *)block + size); + block = block->next; } - block->next_offset = 0; /* Last block */ - fl->free = (FreeListBlock *)start; + block->next = NULL; - return 1; + fl->free = (FreeListBlock *)start; + return EXIT_SUCCESS; } /* Allocate some memory from the FreeList */ @@ -59,32 +41,26 @@ void *fl_alloc(FreeList *fl) { 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++; - /* Wipe it before sending it out, could use memset - * 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 */ - + memset((void *)m, 0, sizeof(FreeListBlock)); return ((void *)m); } /* Return some memory to the FreeList */ int fl_free(FreeList *fl, void *ptr) { - if (!fl_is_managed(fl, ptr)) - return 0; /* We cant free memory we do not own */ + if (!fl_is_managed(fl, ptr)) { + 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->allocated--; - return 1; + return EXIT_SUCCESS; } /* 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 */ int fl_is_managed(FreeList *fl, void *ptr) { - uintptr_t p = (uintptr_t)ptr; - - if (p < fl->start || p >= fl->end) { - return 0; // outside pool - } - - return ((p - fl->start) % fl->size) == 0; // aligned to block + return ((uintptr_t)ptr >= fl->start && (uintptr_t)ptr < fl->end); } /* 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; FreeListBlock *cursor = fl->free; - while (cursor) { + while (cursor->next != NULL) { avail++; - - 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); + cursor = cursor->next; } return avail; diff --git a/kern/libkern/freelist.h b/kern/libkern/freelist.h index ed7949c..015ee33 100644 --- a/kern/libkern/freelist.h +++ b/kern/libkern/freelist.h @@ -5,6 +5,18 @@ #include #include +/* 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 { @@ -15,7 +27,7 @@ typedef struct { size_t allocated; } 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_is_managed(FreeList *fl, void *ptr); void *fl_alloc(FreeList *fl); diff --git a/kern/libkern/stdalign.h b/kern/libkern/stdalign.h deleted file mode 100644 index 6d64eb1..0000000 --- a/kern/libkern/stdalign.h +++ /dev/null @@ -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 diff --git a/kern/libkern/stddef.h b/kern/libkern/stddef.h index c3d195b..fa35dd3 100644 --- a/kern/libkern/stddef.h +++ b/kern/libkern/stddef.h @@ -10,15 +10,4 @@ #define size_t uint64_t #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 diff --git a/kern/libkern/stdint.h b/kern/libkern/stdint.h index c88222e..aaced78 100644 --- a/kern/libkern/stdint.h +++ b/kern/libkern/stdint.h @@ -24,11 +24,6 @@ typedef long int64_t; typedef uint64_t size_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 INT16_MIN (-32767 - 1) #define INT32_MIN (-2147483647 - 1) @@ -44,20 +39,4 @@ typedef uint32_t uint_fast32_t; #define UINT32_MAX (4294967295U) #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