Compare commits

...

2 Commits

Author SHA1 Message Date
Nils O. Selåsdal d58e24f6a9 Minor cleanup 2026-04-21 23:01:22 +02:00
Nils O. Selåsdal d1a45cf8e0 Comments 2026-04-21 22:53:48 +02:00
2 changed files with 11 additions and 10 deletions
+10 -6
View File
@@ -4,10 +4,11 @@
#include <stdint.h> #include <stdint.h>
#include <assert.h> #include <assert.h>
// Repurpose free memory to eep a linked list of free slots //Free memory is reused to build a linked list of slots
typedef struct UCSlot UCSlot; typedef union UCSlot UCSlot;
struct UCSlot { union UCSlot {
UCSlot *next; UCSlot *next; // next free block (when not allocated)
uint8_t data[0]; // data when allocated
}; };
typedef struct UCSlotArena UCSlotArena; typedef struct UCSlotArena UCSlotArena;
@@ -66,9 +67,12 @@ static inline void *uc_slot_alloc(UCSlotArena *arena)
if (slot != NULL) { if (slot != NULL) {
arena->free_list = slot->next; arena->free_list = slot->next;
arena->used_slots++; arena->used_slots++;
#ifdef DEBUG
memset(slot, 0xCB, arena->slot_size);
#endif
} }
return slot; return &slot->data[0];
} }
/** /**
* Free a slot. * Free a slot.
@@ -81,7 +85,7 @@ static inline void uc_slot_free(UCSlotArena *arena, void *memory)
#ifdef DEBUG #ifdef DEBUG
for (const UCSlot *slot = arena->free_list ; slot = slot->next) { for (const UCSlot *slot = arena->free_list ; slot = slot->next) {
if (slot == mem) { if (slot == mem) {
assert("Double free detected"); assert(0 && "Double free detected");
} }
} }
#endif #endif
+1 -4
View File
@@ -41,7 +41,7 @@ void uc_pool_init(UCPool *pool, void *buffer, size_t buffer_size, size_t block_s
p->next = next; p->next = next;
p = next; p = next;
} }
((UCPoolBlock *)p)->next = NULL; // last element p->next = NULL; // last element
pool->free_list = (UCPoolBlock *)first_addr; pool->free_list = (UCPoolBlock *)first_addr;
} }
@@ -65,8 +65,6 @@ void *uc_pool_alloc(UCPool *pool)
#endif #endif
return block->data; return block->data;
} }
#include <stdio.h>
void uc_pool_free(UCPool *pool, void *p) void uc_pool_free(UCPool *pool, void *p)
{ {
if (p == NULL) { if (p == NULL) {
@@ -92,7 +90,6 @@ void uc_pool_free(UCPool *pool, void *p)
pool->free_list = p; pool->free_list = p;
} }
void uc_pool_reset(UCPool *pool) void uc_pool_reset(UCPool *pool)
{ {
if (pool->capacity == 0) if (pool->capacity == 0)