diff --git a/src/core/inc/objpool.h b/src/core/inc/objpool.h index 33957d76..5cceeb1f 100644 --- a/src/core/inc/objpool.h +++ b/src/core/inc/objpool.h @@ -32,6 +32,15 @@ struct objpool { void objpool_init(struct objpool* objpool); void* objpool_alloc(struct objpool* objpool); +void* objpool_alloc_with_id(struct objpool* objpool, objpool_id_t* id); void objpool_free(struct objpool* objpool, void* obj); +inline void* objpool_get_by_id(struct objpool* objpool, objpool_id_t id) +{ + if (id < objpool->num) { + return (void*)((uintptr_t)objpool->pool + (objpool->objsize * id)); + } + return NULL; +} + #endif /* OBJPOOL_H */ diff --git a/src/core/inc/types.h b/src/core/inc/types.h index e9cf250a..b6494c31 100644 --- a/src/core/inc/types.h +++ b/src/core/inc/types.h @@ -48,6 +48,8 @@ typedef unsigned irqid_t; typedef unsigned deviceid_t; +typedef size_t objpool_id_t; + typedef enum AS_SEC { /*--- HYP AS SECTIONS -----*/ SEC_HYP_GLOBAL = 0, diff --git a/src/core/objpool.c b/src/core/objpool.c index 11fa8234..9665dad6 100644 --- a/src/core/objpool.c +++ b/src/core/objpool.c @@ -12,7 +12,7 @@ void objpool_init(struct objpool* objpool) memset(objpool->bitmap, 0, BITMAP_SIZE(objpool->num)); } -void* objpool_alloc(struct objpool* objpool) +void* objpool_alloc_with_id(struct objpool* objpool, objpool_id_t* id) { void* obj = NULL; spin_lock(&objpool->lock); @@ -21,10 +21,18 @@ void* objpool_alloc(struct objpool* objpool) bitmap_set(objpool->bitmap, (size_t)n); obj = (void*)((uintptr_t)objpool->pool + (objpool->objsize * (size_t)n)); } + if (id != NULL) { + *id = (objpool_id_t)n; + } spin_unlock(&objpool->lock); return obj; } +void* objpool_alloc(struct objpool* objpool) +{ + return objpool_alloc_with_id(objpool, NULL); +} + void objpool_free(struct objpool* objpool, void* obj) { vaddr_t obj_addr = (vaddr_t)obj;