diff --git a/2021/day01/aoc-c.c b/2021/day01/aoc-c.c index ccef676..3cb524c 100644 --- a/2021/day01/aoc-c.c +++ b/2021/day01/aoc-c.c @@ -46,7 +46,7 @@ int ex2() struct ranges *input; struct ranges *list_cur; - if (!(pool = pool_init("pool", 10, sizeof (struct ranges)))) + if (!(pool = pool_create("pool", 10, sizeof (struct ranges)))) return -1; while (scanf("%d", &val) != EOF) { diff --git a/2021/day04/aoc-c.c b/2021/day04/aoc-c.c index 3c49832..1d51b16 100644 --- a/2021/day04/aoc-c.c +++ b/2021/day04/aoc-c.c @@ -84,7 +84,7 @@ static struct list_head *read_boards() struct board *cur_board = &dummy; int row = 0; - if (!(pool = pool_init("boards", 128, sizeof (struct board)))) + if (!(pool = pool_create("boards", 128, sizeof (struct board)))) return NULL; while ((len = getline(&buf, &alloc, stdin)) >= 0) { diff --git a/2021/day05/aoc-c.c b/2021/day05/aoc-c.c index 6042851..56fc159 100644 --- a/2021/day05/aoc-c.c +++ b/2021/day05/aoc-c.c @@ -96,7 +96,7 @@ static struct list_head *read_moves() static pool_t *pool; move_t *move; - if (!(pool = pool_init("moves", 1024, sizeof (struct move)))) + if (!(pool = pool_create("moves", 1024, sizeof (struct move)))) return NULL; while ((len = getline(&buf, &alloc, stdin)) >= 0) { diff --git a/2021/day09/aoc-c.c b/2021/day09/aoc-c.c index fb238c9..f5d6dc9 100644 --- a/2021/day09/aoc-c.c +++ b/2021/day09/aoc-c.c @@ -174,7 +174,7 @@ static u64 part2() data = read_file(); - if (!(pool = pool_init("stack", 128, sizeof (struct stack)))) + if (!(pool = pool_create("stack", 128, sizeof (struct stack)))) return -1; for (l = 0; l < data->nlines; ++l) { for (c = 0; c < data->length; ++c) { diff --git a/2021/day12/aoc-c.c b/2021/day12/aoc-c.c index 45f6bc0..23bdb7f 100644 --- a/2021/day12/aoc-c.c +++ b/2021/day12/aoc-c.c @@ -202,7 +202,7 @@ int main(int ac, char **av) if (optind < ac) 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; printf("%s : res=%d\n", *av, doit(part)); diff --git a/2021/day15/aoc-c.c b/2021/day15/aoc-c.c index f8b912c..caf6bc2 100644 --- a/2021/day15/aoc-c.c +++ b/2021/day15/aoc-c.c @@ -194,7 +194,7 @@ int main(int ac, char **av) if (optind < ac) return usage(*av); - if (!(pool = pool_init("stack", 1024, sizeof (pqueue_t)))) + if (!(pool = pool_create("stack", 1024, sizeof (pqueue_t)))) return -1; read_input(); diff --git a/2021/include/pool.h b/2021/include/pool.h index 9810c7a..b2f7727 100644 --- a/2021/include/pool.h +++ b/2021/include/pool.h @@ -20,25 +20,56 @@ #include "bits.h" typedef struct { - struct list_head list_blocks; - char data[]; + struct list_head list_blocks; /* list of allocated blocks in pool */ + char data[]; /* objects block */ } block_t; typedef struct { - char *name; - u32 available; - u32 allocated; - u32 growsize; - size_t eltsize; - u32 nblocks; + char *name; /* pool name */ + u32 available; /* current available elements */ + u32 allocated; /* total objects allocated */ + u32 growsize; /* number of objects per block allocated */ + size_t eltsize; /* object size */ + u32 nblocks; /* number of blocks allocated */ struct list_head list_available; /* available nodes */ struct list_head list_blocks; /* allocated blocks */ } pool_t; +/** + * pool_stats - display some pool statistics + * @pool: the pool address. + */ 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); + +/** + * 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); + +/** + * 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); #endif diff --git a/2021/libsrc/pool.c b/2021/libsrc/pool.c index 34ad445..d1f2ddc 100644 --- a/2021/libsrc/pool.c +++ b/2021/libsrc/pool.c @@ -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; @@ -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), 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); for (int cur=1; cur