Compare commits

..

4 commits

Author SHA1 Message Date
Imbus
c72bce50ac max_align_t 2025-09-09 11:20:27 +02:00
Imbus
700828676b Some more stdint types and defines 2025-09-09 11:20:13 +02:00
Imbus
6b12606d82 Freelist that seems to behave 2025-09-09 11:11:28 +02:00
Imbus
6cb84f5081 stdalign 2025-09-08 09:24:50 +02:00
5 changed files with 108 additions and 44 deletions

View file

@ -1,37 +1,55 @@
#include "freelist.h" #include "freelist.h"
#include "stddef.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct FreeListBlock { /* Header block used to keep track of all the free blocks */
struct FreeListBlock *next; struct __attribute__((packed)) FreeListBlock {
uint16_t next_offset; /* May be 8, 16, 32, 64, whatever */
}; };
/* Initialize the FreeList */ /* Align to nearest multiple of align */
int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) { static inline size_t align_up_to(size_t n, size_t align) {
size_t size = ALIGN(itemsize); return (n + align - 1) & ~(align - 1);
}
if (!fl || end <= start) /* Convert pointer -> 1-based offset (0 means NULL) */
return EXIT_FAILURE; 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 *));
if (size < sizeof(FreeListBlock) || !fl)
return 0;
fl->start = start; fl->start = start;
fl->end = end; fl->end = start + size_bytes;
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++) {
block->next = (FreeListBlock *)((void *)block + size); for (size_t offset = 1; offset < fl_capacity(fl) + 1; offset++) {
block = block->next; block->next_offset = (int32_t)offset;
block = offset_to_ptr(fl, offset);
} }
block->next = NULL; block->next_offset = 0; /* Last block */
fl->free = (FreeListBlock *)start; fl->free = (FreeListBlock *)start;
return EXIT_SUCCESS;
return 1;
} }
/* Allocate some memory from the FreeList */ /* Allocate some memory from the FreeList */
@ -41,26 +59,32 @@ void *fl_alloc(FreeList *fl) {
FreeListBlock *m = fl->free; FreeListBlock *m = fl->free;
fl->free = fl->free->next; fl->free = offset_to_ptr(fl, fl->free->next_offset); /* May be null, which is fine */
fl->allocated++; fl->allocated++;
memset((void *)m, 0, sizeof(FreeListBlock)); /* 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 */
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 EXIT_FAILURE; /* We cant free memory we do not own */ return 0; /* We cant free memory we do not own */
}
FreeListBlock *block = (FreeListBlock *)(uintptr_t)ptr; FreeListBlock *block = (FreeListBlock *)ptr;
block->next = fl->free; block->next_offset = ptr_to_offset(fl, fl->free); /* May be null, which is fine */
fl->free = block; fl->free = block;
fl->allocated--; fl->allocated--;
return EXIT_SUCCESS; return 1;
} }
/* Returns how many slots are occupied */ /* Returns how many slots are occupied */
@ -80,7 +104,13 @@ 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) {
return ((uintptr_t)ptr >= fl->start && (uintptr_t)ptr < fl->end); 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
} }
/* 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 */
@ -93,9 +123,16 @@ size_t fl_check(FreeList *fl) {
int avail = 0; int avail = 0;
FreeListBlock *cursor = fl->free; FreeListBlock *cursor = fl->free;
while (cursor->next != NULL) { while (cursor) {
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;

View file

@ -5,18 +5,6 @@
#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 {
@ -27,7 +15,7 @@ typedef struct {
size_t allocated; size_t allocated;
} FreeList; } FreeList;
int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize); int fl_init(FreeList *fl, uintptr_t start, size_t size_bytes, 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);

7
kern/libkern/stdalign.h Normal file
View file

@ -0,0 +1,7 @@
#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

View file

@ -10,4 +10,15 @@
#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

View file

@ -24,6 +24,11 @@ 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)
@ -39,4 +44,20 @@ typedef uint64_t uintptr_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