]> mj.ucw.cz Git - libucw.git/commitdiff
Resources: Added mempool class
authorMartin Mares <mj@ucw.cz>
Tue, 19 Apr 2011 13:39:13 +0000 (15:39 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 19 Apr 2011 13:39:13 +0000 (15:39 +0200)
ucw/Makefile
ucw/res-mempool.c [new file with mode: 0644]
ucw/respool.h

index fab1a16ec48eae3e6db74fe09762f7d7e4e4de1d..ff7707e55ec38072ae39b6c79ca78e56ef39702f 100644 (file)
@@ -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 (file)
index 0000000..51a85c5
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ *     The UCW Library -- Resources for Memory Pools
+ *
+ *     (c) 2011 Martin Mares <mj@ucw.cz>
+ *
+ *     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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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
index d05e9d0796adf33095efdc6be11fc9941f74e51c..22d40a9af83e3a2493d414e418ee87d041d5d7ef 100644 (file)
@@ -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