Skip to content

Commit

Permalink
feat pool: added cleanup function
Browse files Browse the repository at this point in the history
Signed-off-by: John Sanpe <[email protected]>
  • Loading branch information
sanpeqf committed Apr 14, 2024
1 parent e6e7d25 commit 89ae5b9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/core/xdbd_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ static inline void *block_alloc(xdbd_pool_t *pool, size_t size) {
return retval;
}

static void
resource_cleanup(bfdev_respool_node_t *node, void *unused)
{
xdbd_pool_cleanup_t *cleanup;
cleanup = bfdev_container_of(node, xdbd_pool_cleanup_t, node);
cleanup->func(cleanup->pdata);
}

void *xdbd_palloc(xdbd_pool_t *pool, size_t size) {
if (bfdev_unlikely(!size)) {
bfdev_log_alert("malloc zero !!!\n");
Expand All @@ -86,6 +94,24 @@ void *xdbd_pcalloc(xdbd_pool_t *pool, size_t size) {
return p;
}

int xdbd_pool_cleanup_add(xdbd_pool_t *pool, xdbd_pool_cleanup_pt func,
void *pdata)
{
xdbd_pool_cleanup_t *cleanup;

cleanup = xdbd_pcalloc(pool, sizeof(xdbd_pool_cleanup_t));
if (bfdev_unlikely(!cleanup))
return XDBD_ENOMEM;

cleanup->node.release = resource_cleanup;
cleanup->func = func;
cleanup->pdata = pdata;

bfdev_respool_insert(&pool->respool, &cleanup->node);

return XDBD_OK;
}

xdbd_pool_t *xdbd_create_pool() {
xdbd_pool_t *pool;

Expand All @@ -95,6 +121,7 @@ xdbd_pool_t *xdbd_create_pool() {

bfdev_slist_head_init(&pool->cache);
bfdev_slist_head_init(&pool->block);
bfdev_respool_init(&pool->respool);

return pool;
}
Expand All @@ -103,6 +130,7 @@ void xdbd_release_pool(xdbd_pool_t *pool) {
xdbd_cache_t *cache, *tcache;
xdbd_block_t *block, *tblock;

bfdev_respool_release_all(&pool->respool, NULL);
bfdev_slist_for_each_entry_safe(cache, tcache, &pool->cache, list) {
bfdev_free(NULL, cache->data);
bfdev_free(NULL, cache);
Expand Down
16 changes: 13 additions & 3 deletions src/core/xdbd_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
#define __XDBD_POOL__H__

#include <stddef.h>
#include <bfdev/allocator.h>
#include <bfdev/allocpool.h>
#include <bfdev/slist.h>
#include <bfdev.h>

#ifndef XDBD_PAGE_SIZE
# define XDBD_PAGE_SIZE 4096
Expand All @@ -22,12 +20,23 @@
typedef struct xdbd_pool xdbd_pool_t;
typedef struct xdbd_cache xdbd_cache_t;
typedef struct xdbd_block xdbd_block_t;
typedef struct xdbd_pool_cleanup xdbd_pool_cleanup_t;

typedef void
(*xdbd_pool_cleanup_pt)(void *pdata);

struct xdbd_pool {
bfdev_respool_t respool;
bfdev_slist_head_t cache;
bfdev_slist_head_t block;
};

struct xdbd_pool_cleanup {
bfdev_respool_node_t node;
xdbd_pool_cleanup_pt func;
void *pdata;
};

struct xdbd_cache {
bfdev_slist_head_t list;
bfdev_allocpool_t pool;
Expand All @@ -41,6 +50,7 @@ struct xdbd_block {

__bfdev_malloc void *xdbd_palloc(xdbd_pool_t *pool, size_t size);
void *xdbd_pcalloc(xdbd_pool_t *pool, size_t size);
int xdbd_pool_cleanup_add(xdbd_pool_t *pool, xdbd_pool_cleanup_pt func, void *pdata);
xdbd_pool_t *xdbd_create_pool();
void xdbd_release_pool(xdbd_pool_t *pool);
void xdbd_destroy_pool(xdbd_pool_t *pool);
Expand Down

0 comments on commit 89ae5b9

Please sign in to comment.