pool.c: remove pool (list_head) offset, std: c99 -> gnu99
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@ core
|
|||||||
GPATH
|
GPATH
|
||||||
GRTAGS
|
GRTAGS
|
||||||
GTAGS
|
GTAGS
|
||||||
fen
|
fen
|
||||||
|
pool
|
13
Makefile
13
Makefile
@@ -5,14 +5,14 @@ SRCDIR=./src
|
|||||||
|
|
||||||
SRC=$(wildcard $(SRCDIR)/*.c)
|
SRC=$(wildcard $(SRCDIR)/*.c)
|
||||||
INC=$(wildcard $(SRCDIR)/*.h)
|
INC=$(wildcard $(SRCDIR)/*.h)
|
||||||
BIN=fen
|
BIN=fen pool
|
||||||
|
|
||||||
CFLAGS += -std=c99
|
CFLAGS += -std=gnu99
|
||||||
CFLAGS += -g
|
CFLAGS += -g
|
||||||
CFLAGS += -Wall
|
CFLAGS += -Wall
|
||||||
CFLAGS += -Wextra
|
CFLAGS += -Wextra
|
||||||
CFLAGS += -pedantic
|
#CFLAGS += -pedantic
|
||||||
CFLAGS += -Wno-pointer-arith
|
#CFLAGS += -Wno-pointer-arith
|
||||||
#CFLAGS += -Werror
|
#CFLAGS += -Werror
|
||||||
CFLAGS += -Wmissing-declarations
|
CFLAGS += -Wmissing-declarations
|
||||||
|
|
||||||
@@ -26,3 +26,8 @@ fen: CFLAGS+=-DFENBIN
|
|||||||
fen: $(SRC)
|
fen: $(SRC)
|
||||||
echo SRC=$(SRC)
|
echo SRC=$(SRC)
|
||||||
$(CC) $(CFLAGS) $? -o $@
|
$(CC) $(CFLAGS) $? -o $@
|
||||||
|
|
||||||
|
pool: CFLAGS+=-DPOOLBIN
|
||||||
|
pool: $(SRC)
|
||||||
|
echo SRC=$(SRC)
|
||||||
|
$(CC) $(CFLAGS) $? -o $@
|
||||||
|
32
src/pool.c
32
src/pool.c
@@ -18,11 +18,8 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
//extern char* strdup(const char*);
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
//#include <stdio.h>
|
|
||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "pool.h"
|
#include "pool.h"
|
||||||
|
|
||||||
@@ -34,23 +31,23 @@ void pool_stats(pool_t *pool)
|
|||||||
printf("alloc:%u ", pool->allocated);
|
printf("alloc:%u ", pool->allocated);
|
||||||
printf("grow:%u ", pool->growsize);
|
printf("grow:%u ", pool->growsize);
|
||||||
printf("eltsize:%lu ", pool->eltsize);
|
printf("eltsize:%lu ", pool->eltsize);
|
||||||
printf("offset:%lu ", pool->list_offset);
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize,
|
pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize)
|
||||||
ptrdiff_t list_offset)
|
|
||||||
{
|
{
|
||||||
pool_t *pool;
|
pool_t *pool;
|
||||||
|
|
||||||
|
/* we need at least this space in struct */
|
||||||
|
if (eltsize < sizeof (struct list_head))
|
||||||
|
return NULL;
|
||||||
if ((pool = malloc(sizeof (*pool)))) {
|
if ((pool = malloc(sizeof (*pool)))) {
|
||||||
pool->name = strdup(name);
|
pool->name = strdup(name);
|
||||||
pool->growsize = growsize;
|
pool->growsize = growsize;
|
||||||
pool->eltsize = eltsize;
|
pool->eltsize = eltsize;
|
||||||
pool->available = 0;
|
pool->available = 0;
|
||||||
pool->allocated = 0;
|
pool->allocated = 0;
|
||||||
pool->list_offset = list_offset;
|
|
||||||
INIT_LIST_HEAD(&pool->head);
|
INIT_LIST_HEAD(&pool->head);
|
||||||
}
|
}
|
||||||
return pool;
|
return pool;
|
||||||
@@ -74,8 +71,7 @@ static uint32_t _pool_add(pool_t *pool, struct list_head *elt)
|
|||||||
|
|
||||||
uint32_t pool_add(pool_t *pool, void *elt)
|
uint32_t pool_add(pool_t *pool, void *elt)
|
||||||
{
|
{
|
||||||
struct list_head *plist = elt + pool->list_offset;;
|
return _pool_add(pool, elt);
|
||||||
return _pool_add(pool, plist);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct list_head *_pool_get(pool_t *pool)
|
static struct list_head *_pool_get(pool_t *pool)
|
||||||
@@ -83,7 +79,7 @@ static struct list_head *_pool_get(pool_t *pool)
|
|||||||
struct list_head *res = pool->head.next;
|
struct list_head *res = pool->head.next;
|
||||||
pool->available--;
|
pool->available--;
|
||||||
list_del(res);
|
list_del(res);
|
||||||
//printf("%s: res=%p\n", __func__, (void *)res);
|
// printf("%s: res=%p\n", __func__, (void *)res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +92,6 @@ void *pool_get(pool_t *pool)
|
|||||||
pool->allocated);
|
pool->allocated);
|
||||||
void *alloc = malloc(pool->eltsize * pool->growsize);
|
void *alloc = malloc(pool->eltsize * pool->growsize);
|
||||||
void *cur;
|
void *cur;
|
||||||
struct list_head *plist;
|
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
|
||||||
if (!alloc)
|
if (!alloc)
|
||||||
@@ -105,16 +100,13 @@ void *pool_get(pool_t *pool)
|
|||||||
//printf(" (old=%u)\n", pool->allocated);
|
//printf(" (old=%u)\n", pool->allocated);
|
||||||
for (i = 0; i < pool->growsize; ++i) {
|
for (i = 0; i < pool->growsize; ++i) {
|
||||||
cur = alloc + i * pool->eltsize;
|
cur = alloc + i * pool->eltsize;
|
||||||
plist = cur + pool->list_offset;
|
printf("%s: alloc=%p cur=%p\n", __func__,
|
||||||
printf("%s: alloc=%p cur=%p plist=%p off=%lu\n", __func__,
|
alloc, cur);
|
||||||
alloc, cur, (void *)plist, (void *)plist-cur);
|
_pool_add(pool, (struct list_head *)cur);
|
||||||
_pool_add(pool, plist);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//list_del(pool->head.next);
|
//printf("%s: returning %p pointer\n", __func__, res);
|
||||||
void *res = _pool_get(pool);
|
return _pool_get(pool);
|
||||||
printf("%s: returning %p pointer\n", __func__, res);
|
|
||||||
return res - pool->list_offset;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef POOLBIN
|
#ifdef POOLBIN
|
||||||
@@ -138,7 +130,7 @@ int main(int ac, char**av)
|
|||||||
printf("%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt),
|
printf("%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), offsetof(struct d, list)))) {
|
if ((pool = pool_init("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]);
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
/* pool.h - A simple pool manager.
|
/* pool.h - A simple memory pool manager.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||||
* Licensed under the GNU General Public License v3.0 or later.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
@@ -24,13 +24,11 @@ typedef struct {
|
|||||||
uint32_t allocated;
|
uint32_t allocated;
|
||||||
uint32_t growsize;
|
uint32_t growsize;
|
||||||
size_t eltsize;
|
size_t eltsize;
|
||||||
ptrdiff_t list_offset;
|
|
||||||
struct list_head head;
|
struct list_head head;
|
||||||
} pool_t;
|
} pool_t;
|
||||||
|
|
||||||
void pool_stats(pool_t *pool);
|
void pool_stats(pool_t *pool);
|
||||||
pool_t *pool_init(const char *name, uint32_t grow, size_t size, ptrdiff_t offset);
|
pool_t *pool_init(const char *name, uint32_t grow, size_t size);
|
||||||
//pool_t *pool_init(const char *name, uint32_t grow, size_t size);
|
|
||||||
void *pool_get(pool_t *pool);
|
void *pool_get(pool_t *pool);
|
||||||
uint32_t pool_add(pool_t *pool, void *elt);
|
uint32_t pool_add(pool_t *pool, void *elt);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user