memory/mempool: pass arena pointer to acquire() instead of storing in struct
This commit is contained in:
parent
b81f338d2d
commit
f9c2748fae
4 changed files with 8 additions and 27 deletions
|
@ -8,16 +8,7 @@
|
|||
|
||||
#include "mempool.h"
|
||||
|
||||
void mempool_generic_init(
|
||||
MemPool *pool,
|
||||
MemArena *arena
|
||||
) {
|
||||
*pool = (MemPool) {
|
||||
.arena = arena,
|
||||
};
|
||||
}
|
||||
|
||||
void *mempool_generic_acquire(MemPool *pool, size_t size, size_t align) {
|
||||
void *mempool_generic_acquire(MemPool *pool, MemArena *arena, size_t size, size_t align) {
|
||||
MemPoolObjectHeader *obj = pool->free_objects.as_generic;
|
||||
|
||||
if(obj) {
|
||||
|
@ -25,7 +16,7 @@ void *mempool_generic_acquire(MemPool *pool, size_t size, size_t align) {
|
|||
pool->free_objects.as_generic = obj->next;
|
||||
} else {
|
||||
assert(pool->num_used == pool->num_allocated);
|
||||
obj = marena_alloc_aligned(pool->arena, size, align);
|
||||
obj = marena_alloc_aligned(arena, size, align);
|
||||
++pool->num_allocated;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,6 @@ typedef struct MemPoolObjectHeader {
|
|||
} MemPoolObjectHeader;
|
||||
|
||||
#define MEMPOOL_BASE(elem_type) struct { \
|
||||
MemArena *arena; \
|
||||
union { \
|
||||
MemPoolObjectHeader *as_generic; \
|
||||
elem_type *as_specific; \
|
||||
|
@ -49,22 +48,17 @@ typedef MEMPOOL_BASE(max_align_t) MemPool;
|
|||
#define MEMPOOL_OBJTYPE(mpool) \
|
||||
typeof(*(mpool)->free_objects.as_specific)
|
||||
|
||||
void mempool_generic_init(MemPool *pool, MemArena *arena)
|
||||
attr_nonnull(1, 2);
|
||||
|
||||
void *mempool_generic_acquire(MemPool *pool, size_t size, size_t align)
|
||||
attr_returns_allocated attr_hot attr_nonnull(1);
|
||||
void *mempool_generic_acquire(MemPool *pool, MemArena *arena, size_t size, size_t align)
|
||||
attr_returns_allocated attr_hot attr_nonnull(1, 2);
|
||||
|
||||
void mempool_generic_release(MemPool *pool, void *object)
|
||||
attr_hot attr_nonnull(1, 2);
|
||||
|
||||
#define mempool_init(mpool, arena) \
|
||||
mempool_generic_init(MEMPOOL_CAST_TO_BASE(mpool), (arena));
|
||||
|
||||
#define mempool_acquire(mpool) ({ \
|
||||
#define mempool_acquire(mpool, arena) ({ \
|
||||
auto _mpool = mpool; \
|
||||
MEMPOOL_OBJTYPE(_mpool) *_obj = mempool_generic_acquire(\
|
||||
MEMPOOL_CAST_TO_BASE(_mpool), \
|
||||
(arena), \
|
||||
sizeof(MEMPOOL_OBJTYPE(_mpool)), \
|
||||
alignof(MEMPOOL_OBJTYPE(_mpool))); \
|
||||
*_obj = (typeof(*_obj)) {} ; \
|
||||
|
|
|
@ -19,11 +19,7 @@ void stage_objpools_init(void) {
|
|||
marena_reset(&stage_objects.arena);
|
||||
}
|
||||
|
||||
#define INIT_POOL(type, field) \
|
||||
mempool_init(&stage_objects.pools.field, &stage_objects.arena);
|
||||
|
||||
OBJECT_POOLS(INIT_POOL)
|
||||
#undef INIT_POOL
|
||||
stage_objects.pools = (typeof(stage_objects.pools)) {};
|
||||
}
|
||||
|
||||
void stage_objpools_shutdown(void) {
|
||||
|
|
|
@ -66,7 +66,7 @@ void stage_objpools_init(void);
|
|||
void stage_objpools_shutdown(void);
|
||||
|
||||
#define STAGE_ACQUIRE_OBJ(_type) \
|
||||
mempool_acquire(STAGE_OBJPOOL_BY_TYPE(_type))
|
||||
mempool_acquire(STAGE_OBJPOOL_BY_TYPE(_type), &stage_objects.arena)
|
||||
|
||||
#define STAGE_RELEASE_OBJ(_p_obj) \
|
||||
mempool_release(STAGE_OBJPOOL_BY_VARTYPE(_p_obj), (_p_obj))
|
||||
|
|
Loading…
Reference in a new issue