From 46a64e45c1d3fa42376cffcf90cfa0b66d781772 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 19 Apr 2011 15:39:13 +0200 Subject: [PATCH] Resources: Added mempool class --- ucw/Makefile | 2 +- ucw/res-mempool.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++ ucw/respool.h | 3 +++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 ucw/res-mempool.c diff --git a/ucw/Makefile b/ucw/Makefile index fab1a16e..ff7707e5 100644 --- a/ucw/Makefile +++ b/ucw/Makefile @@ -34,7 +34,7 @@ LIBUCW_MODS= \ bbuf gary \ getopt \ strtonum \ - respool trans res-fd res-mem res-subpool + respool trans res-fd res-mem res-subpool res-mempool LIBUCW_MAIN_INCLUDES= \ lib.h log.h threads.h \ diff --git a/ucw/res-mempool.c b/ucw/res-mempool.c new file mode 100644 index 00000000..51a85c53 --- /dev/null +++ b/ucw/res-mempool.c @@ -0,0 +1,54 @@ +/* + * The UCW Library -- Resources for Memory Pools + * + * (c) 2011 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "ucw/lib.h" +#include "ucw/respool.h" +#include "ucw/mempool.h" + +#include +#include +#include + +static void +mp_res_free(struct resource *r) +{ + mp_delete(r->priv); +} + +static void +mp_res_dump(struct resource *r, uns indent UNUSED) +{ + printf(" pool=%p\n", r->priv); +} + +static const struct res_class mp_res_class = { + .name = "mempool", + .dump = mp_res_dump, + .free = mp_res_free, +}; + +struct resource * +res_mempool(struct mempool *mp) +{ + return res_new(&mp_res_class, mp); +} + +#ifdef TEST + +int main(void) +{ + struct respool *rp = rp_new("test", NULL); + rp_switch(rp); + res_mempool(mp_new(4096)); + rp_dump(rp, 0); + rp_delete(rp); + return 0; +} + +#endif diff --git a/ucw/respool.h b/ucw/respool.h index d05e9d07..22d40a9a 100644 --- a/ucw/respool.h +++ b/ucw/respool.h @@ -166,4 +166,7 @@ void *res_realloc(struct resource *res, size_t size); /** Re-allocates memory **/ struct resource *res_subpool(struct respool *rp); +struct mempool; +struct resource *res_mempool(struct mempool *mp); /** Creates a resource for the specified memory pool. **/ + #endif -- 2.39.2