Update slot_allocator
This commit is contained in:
@@ -16,10 +16,11 @@ struct UCSlotArena {
|
|||||||
uint8_t *memory_start;
|
uint8_t *memory_start;
|
||||||
size_t memory_len;
|
size_t memory_len;
|
||||||
size_t n_slots;
|
size_t n_slots;
|
||||||
|
size_t used_slots;
|
||||||
size_t slot_size;
|
size_t slot_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize a memory arena for dynamically allocating slots of memory.
|
* Initialize a memory arena for dynamically allocating slots of memory.
|
||||||
* Each allocation returns memory slots of the same size
|
* Each allocation returns memory slots of the same size
|
||||||
* Note: For max speed, align @memory and @slot_size to the size of a pointer
|
* Note: For max speed, align @memory and @slot_size to the size of a pointer
|
||||||
@@ -53,26 +54,44 @@ static inline void uc_slot_arena_init(UCSlotArena *arena, void *memory, size_t m
|
|||||||
arena->slot_size = slot_size;
|
arena->slot_size = slot_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate a slot.
|
||||||
|
*
|
||||||
|
* @return pointer to a slot, NULL if all slots are already allocated
|
||||||
|
*/
|
||||||
static inline void *uc_slot_alloc(UCSlotArena *arena)
|
static inline void *uc_slot_alloc(UCSlotArena *arena)
|
||||||
{
|
{
|
||||||
UCSlot *slot = arena->free_list;
|
UCSlot *slot = arena->free_list;
|
||||||
|
|
||||||
if (slot != NULL) {
|
if (slot != NULL) {
|
||||||
arena->free_list = slot->next;
|
arena->free_list = slot->next;
|
||||||
|
arena->used_slots++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return slot;
|
return slot;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
static inline void uc_slot_free(UCSlotArena *arena, void *mem)
|
* Free a slot.
|
||||||
|
*
|
||||||
|
* @param mem pointer to a slot that previously was allocated with uc_slot_alloc
|
||||||
|
*/
|
||||||
|
static inline void uc_slot_free(UCSlotArena *arena, void *memory)
|
||||||
{
|
{
|
||||||
if (mem != NULL) {
|
if (memory != NULL) {
|
||||||
assert((uint8_t *)mem >= arena->memory_start);
|
#ifdef DEBUG
|
||||||
assert((uint8_t *)mem < arena->memory_start + arena->memory_len);
|
for (const UCSlot *slot = arena->free_list ; slot = slot->next) {
|
||||||
|
if (slot == mem) {
|
||||||
UCSlot *slot = (UCSlot *)mem;
|
assert("Double free detected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
assert((uint8_t *)memory >= arena->memory_start);
|
||||||
|
assert((uint8_t *)memory < arena->memory_start + arena->memory_len);
|
||||||
|
assert(arena->used_slots > 0);
|
||||||
|
UCSlot *slot = (UCSlot *)memory;
|
||||||
slot->next = arena->free_list;
|
slot->next = arena->free_list;
|
||||||
arena->free_list = slot;
|
arena->free_list = slot;
|
||||||
|
arena->used_slots--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user