Skip to content

Commit

Permalink
feat(core/objpool): add objpool_alloc_with_id and objpool_get_by_id
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
joaopeixoto13 committed Nov 5, 2024
1 parent cd6f46b commit f9d8d0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/core/inc/objpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
2 changes: 2 additions & 0 deletions src/core/inc/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion src/core/objpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit f9d8d0c

Please sign in to comment.