refactor pool_init -> pool_create

This commit is contained in:
2021-12-20 14:44:15 +01:00
parent 93ef9438b8
commit 118d7f8452
9 changed files with 49 additions and 18 deletions

View File

@@ -46,7 +46,7 @@ int ex2()
struct ranges *input; struct ranges *input;
struct ranges *list_cur; struct ranges *list_cur;
if (!(pool = pool_init("pool", 10, sizeof (struct ranges)))) if (!(pool = pool_create("pool", 10, sizeof (struct ranges))))
return -1; return -1;
while (scanf("%d", &val) != EOF) { while (scanf("%d", &val) != EOF) {

View File

@@ -84,7 +84,7 @@ static struct list_head *read_boards()
struct board *cur_board = &dummy; struct board *cur_board = &dummy;
int row = 0; int row = 0;
if (!(pool = pool_init("boards", 128, sizeof (struct board)))) if (!(pool = pool_create("boards", 128, sizeof (struct board))))
return NULL; return NULL;
while ((len = getline(&buf, &alloc, stdin)) >= 0) { while ((len = getline(&buf, &alloc, stdin)) >= 0) {

View File

@@ -96,7 +96,7 @@ static struct list_head *read_moves()
static pool_t *pool; static pool_t *pool;
move_t *move; move_t *move;
if (!(pool = pool_init("moves", 1024, sizeof (struct move)))) if (!(pool = pool_create("moves", 1024, sizeof (struct move))))
return NULL; return NULL;
while ((len = getline(&buf, &alloc, stdin)) >= 0) { while ((len = getline(&buf, &alloc, stdin)) >= 0) {

View File

@@ -174,7 +174,7 @@ static u64 part2()
data = read_file(); data = read_file();
if (!(pool = pool_init("stack", 128, sizeof (struct stack)))) if (!(pool = pool_create("stack", 128, sizeof (struct stack))))
return -1; return -1;
for (l = 0; l < data->nlines; ++l) { for (l = 0; l < data->nlines; ++l) {
for (c = 0; c < data->length; ++c) { for (c = 0; c < data->length; ++c) {

View File

@@ -202,7 +202,7 @@ int main(int ac, char **av)
if (optind < ac) if (optind < ac)
return usage(*av); return usage(*av);
if (!(links_pool = pool_init("links", 128, sizeof (struct link)))) if (!(links_pool = pool_create("links", 128, sizeof (struct link))))
return -1; return -1;
printf("%s : res=%d\n", *av, doit(part)); printf("%s : res=%d\n", *av, doit(part));

View File

@@ -194,7 +194,7 @@ int main(int ac, char **av)
if (optind < ac) if (optind < ac)
return usage(*av); return usage(*av);
if (!(pool = pool_init("stack", 1024, sizeof (pqueue_t)))) if (!(pool = pool_create("stack", 1024, sizeof (pqueue_t))))
return -1; return -1;
read_input(); read_input();

View File

@@ -20,25 +20,56 @@
#include "bits.h" #include "bits.h"
typedef struct { typedef struct {
struct list_head list_blocks; struct list_head list_blocks; /* list of allocated blocks in pool */
char data[]; char data[]; /* objects block */
} block_t; } block_t;
typedef struct { typedef struct {
char *name; char *name; /* pool name */
u32 available; u32 available; /* current available elements */
u32 allocated; u32 allocated; /* total objects allocated */
u32 growsize; u32 growsize; /* number of objects per block allocated */
size_t eltsize; size_t eltsize; /* object size */
u32 nblocks; u32 nblocks; /* number of blocks allocated */
struct list_head list_available; /* available nodes */ struct list_head list_available; /* available nodes */
struct list_head list_blocks; /* allocated blocks */ struct list_head list_blocks; /* allocated blocks */
} pool_t; } pool_t;
/**
* pool_stats - display some pool statistics
* @pool: the pool address.
*/
void pool_stats(pool_t *pool); void pool_stats(pool_t *pool);
pool_t *pool_init(const char *name, u32 grow, size_t size);
/**
* pool_create - create a new memory pool
* @name: the name to give to the pool.
* @grow: the number of elements to add when no more available.
* @size: the size of an element in pool.
*/
pool_t *pool_create(const char *name, u32 grow, size_t size);
/**
* pool_get - get an element from a pool
* @pool: the pool address.
*/
void *pool_get(pool_t *pool); void *pool_get(pool_t *pool);
/**
* pool_add - add (release) an element to a pool
* @pool: the pool address.
* @elt: the address of the object to add to the pool.
*/
u32 pool_add(pool_t *pool, void *elt); u32 pool_add(pool_t *pool, void *elt);
/**
* pool_destroy - destroy a pool.
* @pool: the pool address.
*
* Attention: All memory is freed, but no check is done whether all pool
* elements has been released. Referencing any pool object after this call
* is strongly discouraged.
*/
void pool_destroy(pool_t *pool); void pool_destroy(pool_t *pool);
#endif #endif

View File

@@ -38,7 +38,7 @@ void pool_stats(pool_t *pool)
} }
} }
pool_t *pool_init(const char *name, u32 growsize, size_t eltsize) pool_t *pool_create(const char *name, u32 growsize, size_t eltsize)
{ {
pool_t *pool; pool_t *pool;
@@ -182,7 +182,7 @@ int main(int ac, char**av)
log_f(1, "%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt), log_f(1, "%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt),
sizeof(*elt), offsetof(struct d, list)); sizeof(*elt), offsetof(struct d, list));
if ((pool = pool_init("dummy", 3, sizeof(*elt)))) { if ((pool = pool_create("dummy", 3, sizeof(*elt)))) {
pool_stats(pool); pool_stats(pool);
for (int cur=1; cur<ac; ++cur) { for (int cur=1; cur<ac; ++cur) {
total = atoi(av[cur]); total = atoi(av[cur]);

View File

@@ -46,7 +46,7 @@ int ex2()
struct ranges *input; struct ranges *input;
struct ranges *list_cur; struct ranges *list_cur;
if (!(pool = pool_init("pool", 10, sizeof (struct ranges)))) if (!(pool = pool_create("pool", 10, sizeof (struct ranges))))
return -1; return -1;
while (scanf("%d", &val) != EOF) { while (scanf("%d", &val) != EOF) {