-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from openbfdev/refactor-mempool
Refactor mempool
- Loading branch information
Showing
3 changed files
with
147 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,114 @@ | ||
#include "bfdev/list.h" | ||
#include <bfdev/allocator.h> | ||
#include <xdbd_pool.h> | ||
/* SPDX-License-Identifier: GPL-2.0-or-later */ | ||
/* | ||
* Copyright(c) 2024 John Sanpe <[email protected]> | ||
*/ | ||
|
||
#include <xdbd.h> | ||
#include <bfdev/align.h> | ||
#include <bfdev/bug.h> | ||
|
||
xdbd_pool_t *xdbd_create_pool() { | ||
xdbd_pool_t *pool = NULL; | ||
static inline void * | ||
cache_alloc(xdbd_pool_t *pool, size_t size) | ||
{ | ||
xdbd_cache_t *cache; | ||
void *retval; | ||
|
||
pool = bfdev_malloc(NULL, sizeof(xdbd_pool_t)); | ||
if (pool == NULL) { | ||
return NULL; | ||
bfdev_align_high_adj(size, XDBD_ALIGN_SIZE); | ||
|
||
cache = bfdev_slist_first_entry(&pool->cache, xdbd_cache_t, list); | ||
if (bfdev_likely(cache)) { | ||
retval = bfdev_allocpool_alloc(&cache->pool, size, 0); | ||
if (bfdev_likely(cache)) | ||
return retval; | ||
} | ||
|
||
bfdev_list_head_init(&pool->head); | ||
return pool; | ||
cache = bfdev_malloc(NULL, sizeof(*cache)); | ||
if (bfdev_unlikely(!cache)) | ||
return NULL; | ||
|
||
retval = bfdev_malloc(NULL, XDBD_PAGE_SIZE); | ||
if (bfdev_unlikely(!retval)) | ||
return NULL; | ||
|
||
cache->data = retval; | ||
bfdev_allocpool_init(&cache->pool, retval, XDBD_PAGE_SIZE); | ||
bfdev_slist_add(&pool->cache, &cache->list); | ||
|
||
retval = bfdev_allocpool_alloc(&cache->pool, size, 0); | ||
BFDEV_BUG_ON(!retval); | ||
|
||
return retval; | ||
} | ||
|
||
void *xdbd_palloc(xdbd_pool_t *pool, unsigned size) { | ||
xdbd_pool_node_t *node = NULL; | ||
node = bfdev_malloc(NULL, sizeof(xdbd_pool_node_t)); | ||
if (node == NULL) { | ||
static inline void * | ||
block_alloc(xdbd_pool_t *pool, size_t size) | ||
{ | ||
xdbd_block_t *block; | ||
void *retval; | ||
|
||
bfdev_align_high_adj(size, XDBD_PAGE_SIZE); | ||
|
||
block = bfdev_malloc(NULL, sizeof(*block)); | ||
if (bfdev_unlikely(!block)) | ||
return NULL; | ||
} | ||
|
||
node->buf = bfdev_zalloc(NULL, size); | ||
if (node->buf == NULL) { | ||
bfdev_free(NULL, node); | ||
retval = bfdev_malloc(NULL, size); | ||
if (bfdev_unlikely(!retval)) | ||
return NULL; | ||
} | ||
|
||
bfdev_list_add(&pool->head, &node->list); | ||
return node->buf; | ||
block->data = retval; | ||
bfdev_slist_add(&pool->block, &block->list); | ||
|
||
return retval; | ||
} | ||
|
||
void xdbd_destroy_pool(xdbd_pool_t *pool) { | ||
void * | ||
xdbd_palloc(xdbd_pool_t *pool, size_t size) | ||
{ | ||
if (size < XDBD_PAGE_SIZE) | ||
return cache_alloc(pool, size); | ||
|
||
return block_alloc(pool, size); | ||
} | ||
|
||
xdbd_pool_t * | ||
xdbd_create_pool() | ||
{ | ||
xdbd_pool_t *pool; | ||
|
||
pool = bfdev_malloc(NULL, sizeof(*pool)); | ||
if (bfdev_unlikely(!pool)) | ||
return NULL; | ||
|
||
bfdev_slist_head_init(&pool->cache); | ||
bfdev_slist_head_init(&pool->block); | ||
|
||
return pool; | ||
} | ||
|
||
void | ||
xdbd_release_pool(xdbd_pool_t *pool) | ||
{ | ||
xdbd_cache_t *cache, *tcache; | ||
xdbd_block_t *block, *tblock; | ||
|
||
bfdev_slist_for_each_entry_safe(cache, tcache, &pool->cache, list) { | ||
bfdev_free(NULL, cache->data); | ||
bfdev_free(NULL, cache); | ||
} | ||
|
||
bfdev_slist_for_each_entry_safe(block, tblock, &pool->block, list) { | ||
bfdev_free(NULL, block->data); | ||
bfdev_free(NULL, block); | ||
} | ||
|
||
bfdev_slist_head_init(&pool->cache); | ||
bfdev_slist_head_init(&pool->block); | ||
} | ||
|
||
//FIXME: | ||
return; | ||
void | ||
xdbd_destroy_pool(xdbd_pool_t *pool) | ||
{ | ||
xdbd_release_pool(pool); | ||
bfdev_free(NULL, pool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,55 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-or-later */ | ||
/* | ||
* Copyright(c) 2024 John Sanpe <[email protected]> | ||
*/ | ||
|
||
#ifndef __XDBD_POOL__H__ | ||
#define __XDBD_POOL__H__ | ||
|
||
#include <bfdev/list.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <bfdev/allocator.h> | ||
#include <bfdev/allocpool.h> | ||
#include <bfdev/slist.h> | ||
|
||
#ifndef XDBD_PAGE_SIZE | ||
# define XDBD_PAGE_SIZE 4096 | ||
#endif | ||
|
||
#ifndef XDBD_ALIGN_SIZE | ||
# define XDBD_ALIGN_SIZE BFDEV_BYTES_PER_LONG | ||
#endif | ||
|
||
typedef struct xdbd_pool xdbd_pool_t; | ||
typedef struct xdbd_cache xdbd_cache_t; | ||
typedef struct xdbd_block xdbd_block_t; | ||
|
||
struct xdbd_pool { | ||
bfdev_slist_head_t cache; | ||
bfdev_slist_head_t block; | ||
}; | ||
|
||
struct xdbd_cache { | ||
bfdev_slist_head_t list; | ||
bfdev_allocpool_t pool; | ||
void *data; | ||
}; | ||
|
||
struct xdbd_block { | ||
bfdev_slist_head_t list; | ||
void *data; | ||
}; | ||
|
||
extern __bfdev_malloc void * | ||
xdbd_palloc(xdbd_pool_t *pool, size_t size); | ||
|
||
typedef struct { | ||
void *buf; | ||
bfdev_list_head_t list; | ||
} xdbd_pool_node_t; | ||
extern xdbd_pool_t * | ||
xdbd_create_pool(); | ||
|
||
typedef struct { | ||
bfdev_list_head_t head; | ||
} xdbd_pool_t; | ||
extern void | ||
xdbd_release_pool(xdbd_pool_t *pool); | ||
|
||
xdbd_pool_t *xdbd_create_pool(); | ||
void *xdbd_palloc(xdbd_pool_t *pool, unsigned size); | ||
void xdbd_destroy_pool(xdbd_pool_t *pool); | ||
extern void | ||
xdbd_destroy_pool(xdbd_pool_t *pool); | ||
|
||
#endif /*__XDBD_POOL__H__*/ |