Skip to content

Commit 5216728

Browse files
committed
feat(core/objpool): add objpool_alloc_with_id and objpool_get_by_id
This commit introduces two new methods to the objpool API. The first method, objpool_alloc_with_id, enables the allocation of an object within a pool and returns the index where the object was allocated. The second method, objpool_get_by_id, allows retrieving a previously allocated object using the index of the pool where it was initially allocated. Signed-off-by: João Peixoto <[email protected]>
1 parent 5b02a70 commit 5216728

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/core/inc/objpool.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ struct objpool {
3232

3333
void objpool_init(struct objpool* objpool);
3434
void* objpool_alloc(struct objpool* objpool);
35+
void* objpool_alloc_with_id(struct objpool* objpool, objpool_id_t* id);
3536
void objpool_free(struct objpool* objpool, void* obj);
3637

38+
inline void* objpool_get_by_id(struct objpool* objpool, objpool_id_t id)
39+
{
40+
if (id < objpool->num) {
41+
return (void*)((uintptr_t)objpool->pool + (objpool->objsize * id));
42+
}
43+
return NULL;
44+
}
45+
3746
#endif /* OBJPOOL_H */

src/core/inc/types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ typedef unsigned irqid_t;
4848

4949
typedef unsigned deviceid_t;
5050

51+
typedef size_t objpool_id_t;
52+
5153
typedef enum AS_SEC {
5254
/*--- HYP AS SECTIONS -----*/
5355
SEC_HYP_GLOBAL = 0,

src/core/objpool.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void objpool_init(struct objpool* objpool)
1212
memset(objpool->bitmap, 0, BITMAP_SIZE(objpool->num));
1313
}
1414

15-
void* objpool_alloc(struct objpool* objpool)
15+
void* objpool_alloc_with_id(struct objpool* objpool, objpool_id_t* id)
1616
{
1717
void* obj = NULL;
1818
spin_lock(&objpool->lock);
@@ -21,10 +21,18 @@ void* objpool_alloc(struct objpool* objpool)
2121
bitmap_set(objpool->bitmap, (size_t)n);
2222
obj = (void*)((uintptr_t)objpool->pool + (objpool->objsize * (size_t)n));
2323
}
24+
if (id != NULL) {
25+
*id = (objpool_id_t)n;
26+
}
2427
spin_unlock(&objpool->lock);
2528
return obj;
2629
}
2730

31+
void* objpool_alloc(struct objpool* objpool)
32+
{
33+
return objpool_alloc_with_id(objpool, NULL);
34+
}
35+
2836
void objpool_free(struct objpool* objpool, void* obj)
2937
{
3038
vaddr_t obj_addr = (vaddr_t)obj;

0 commit comments

Comments
 (0)