move from printf to logX() - unfinished.

This commit is contained in:
2021-11-03 12:39:23 +01:00
parent 45c01f59f7
commit 0e117bc2f9
9 changed files with 178 additions and 93 deletions

View File

@@ -22,16 +22,16 @@
#include <stdlib.h>
#include "list.h"
#include "pool.h"
#include "debug.h"
void pool_stats(pool_t *pool)
{
if (pool) {
printf("[%s] pool [%p]: ", pool->name, (void *)pool);
printf("avail:%u ", pool->available);
printf("alloc:%u ", pool->allocated);
printf("grow:%u ", pool->growsize);
printf("eltsize:%lu ", pool->eltsize);
printf("\n");
# ifdef DEBUG_POOL
log_f(1, "[%s] pool [%p]: avail:%u alloc:%u grow:%u eltsize:%lu\n",
pool->name, (void *)pool, pool->available, pool->allocated,
pool->growsize, pool->eltsize);
# endif
}
}
@@ -39,8 +39,10 @@ pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize)
{
pool_t *pool;
printf("%s: name=[%s] growsize=%u eltsize=%lu\n",
__func__, name, growsize, eltsize);
# ifdef DEBUG_POOL
log_f(1, "name=[%s] growsize=%u eltsize=%lu\n",
name, growsize, eltsize);
# endif
/* we need at least this space in struct */
if (eltsize < sizeof (struct list_head))
return NULL;
@@ -57,15 +59,14 @@ pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize)
static uint32_t _pool_add(pool_t *pool, struct list_head *elt)
{
/*
printf("%s: pool=%p &head=%p elt=%p off1=%lu off2=%lu\n",
__func__,
# ifdef DEBUG_POOL
log_f(10, "pool=%p &head=%p elt=%p off1=%lu off2=%lu\n",
(void *)pool,
(void *)&pool->head,
(void *)elt,
(void *)&pool->head-(void *)pool,
offsetof(pool_t, head));
*/
# endif
list_add(elt, &pool->head);
return ++pool->available;
@@ -81,7 +82,6 @@ static struct list_head *_pool_get(pool_t *pool)
struct list_head *res = pool->head.next;
pool->available--;
list_del(res);
// printf("%s: res=%p\n", __func__, (void *)res);
return res;
}
@@ -93,28 +93,34 @@ void *pool_get(pool_t *pool)
void *alloc = malloc(pool->eltsize * pool->growsize);
void *cur;
uint32_t i;
printf("+++ %s [%s]: growing pool from %u to %u elements.\n", __func__,
# ifdef DEBUG_POOL
log_f(2, "[%s]: growing pool from %u to %u elements.\n",
pool->name,
pool->allocated,
pool->allocated + pool->growsize);
# endif
if (!alloc)
return NULL;
//printf(" (old=%u)\n", pool->allocated);
# ifdef DEBUG_POOL
log_f(5, " (old=%u)\n", pool->allocated);
# endif
pool->allocated += pool->growsize;
//printf(" (new=%u)\n", pool->allocated);
# ifdef DEBUG_POOL
log_f(5, " (new=%u)\n", pool->allocated);
# endif
for (i = 0; i < pool->growsize; ++i) {
cur = alloc + i * pool->eltsize;
//printf("%s: alloc=%p cur=%p\n", __func__, alloc, cur);
# ifdef DEBUG_POOL
log_f(5, "alloc=%p cur=%p\n", alloc, cur);
# endif
_pool_add(pool, (struct list_head *)cur);
}
pool_stats(pool);
}
//printf("%s: returning %p pointer\n", __func__, res);
return _pool_get(pool);
}
#ifdef POOLBIN
#ifdef BIN_pool
struct d {
uint16_t data1;
char c;
@@ -132,7 +138,8 @@ int main(int ac, char**av)
char ccur='z';
struct d *elt;
printf("%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt),
debug_init(3);
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)))) {
@@ -140,7 +147,7 @@ int main(int ac, char**av)
for (int cur=1; cur<ac; ++cur) {
total = atoi(av[cur]);
if (action == 0) { /* add elt to list */
printf("adding %d elements\n", total);
log_f(2, "adding %d elements\n", total);
for (int i = 0; i < total; ++i) {
elt = pool_get(pool);
elt->data1 = icur++;
@@ -150,7 +157,7 @@ int main(int ac, char**av)
pool_stats(pool);
action = 1;
} else { /* remove one elt from list */
printf("deleting %d elements\n", total);
log_f(2, "deleting %d elements\n", total);
for (int i = 0; i < total; ++i) {
if (!list_empty(&head)) {
elt = list_last_entry(&head, struct d, list);