]> mj.ucw.cz Git - libucw.git/commitdiff
I always wanted to rename the rather inconsistent memory pool modules.
authorMartin Mares <mj@ucw.cz>
Sat, 10 Jul 2004 21:04:23 +0000 (21:04 +0000)
committerMartin Mares <mj@ucw.cz>
Sat, 10 Jul 2004 21:04:23 +0000 (21:04 +0000)
After the big library earthquake I caused with my unicode.h changes,
this is the small fish, so let's go.

lib/Makefile
lib/mempool-str.c [new file with mode: 0644]
lib/mempool.c [new file with mode: 0644]
lib/mempool.h [new file with mode: 0644]
lib/pool-str.c [deleted file]
lib/pool.c [deleted file]
lib/pools.h [deleted file]

index 57ff42a8676f32dff7ec110bd8b8ce5a471efd20..381190a07097d1fda79f4b45f00903215cd7fb70 100644 (file)
@@ -5,7 +5,7 @@ PROGS+=obj/lib/db-tool obj/lib/buckettool
 TESTS+=obj/lib/regex-ut
 
 LIBSH_MODS= \
-       alloc alloc_str realloc pool pool-str \
+       alloc alloc_str realloc mempool mempool-str \
        mmap pagecache partmap hashfunc \
        lists sorter bitsig \
        log log-file proctitle \
diff --git a/lib/mempool-str.c b/lib/mempool-str.c
new file mode 100644 (file)
index 0000000..065597d
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ *     Sherlock Library -- Memory Pools (String Operations)
+ *
+ *     (c) 2004 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 "lib/lib.h"
+#include "lib/mempool.h"
+
+#include <alloca.h>
+#include <string.h>
+
+char *
+mp_strdup(struct mempool *p, char *s)
+{
+  uns l = strlen(s) + 1;
+  char *t = mp_alloc_fast_noalign(p, l);
+  memcpy(t, s, l);
+  return t;
+}
+
+char *
+mp_multicat(struct mempool *p, ...)
+{
+  va_list args, a;
+  va_start(args, p);
+  char *x, *y;
+  uns cnt = 0;
+  a = args;
+  while (x = va_arg(a, char *))
+    cnt++;
+  uns *sizes = alloca(cnt * sizeof(uns));
+  uns len = 1;
+  cnt = 0;
+  a = args;
+  while (x = va_arg(a, char *))
+    len += sizes[cnt++] = strlen(x);
+  char *buf = mp_alloc_fast_noalign(p, len);
+  y = buf;
+  a = args;
+  cnt = 0;
+  while (x = va_arg(a, char *))
+    {
+      memcpy(y, x, sizes[cnt]);
+      y += sizes[cnt++];
+    }
+  *y = 0;
+  return buf;
+}
diff --git a/lib/mempool.c b/lib/mempool.c
new file mode 100644 (file)
index 0000000..e28512f
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ *     Sherlock Library -- Memory Pools (One-Time Allocation)
+ *
+ *     (c) 1997--2001 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 "lib/lib.h"
+#include "lib/mempool.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+struct memchunk {
+  struct memchunk *next;
+  byte data[0];
+};
+
+struct mempool *
+mp_new(uns size)
+{
+  struct mempool *p = xmalloc(sizeof(struct mempool));
+
+  size -= sizeof(struct memchunk);
+  p->free = p->last = NULL;
+  p->first = p->current = p->first_large = NULL;
+  p->plast = &p->first;
+  p->chunk_size = size;
+  p->threshold = size / 3;
+  return p;
+}
+
+void
+mp_delete(struct mempool *p)
+{
+  struct memchunk *c, *d;
+
+  for(d=p->first; d; d = c)
+    {
+      c = d->next;
+      xfree(d);
+    }
+  for(d=p->first_large; d; d = c)
+    {
+      c = d->next;
+      xfree(d);
+    }
+  xfree(p);
+}
+
+void
+mp_flush(struct mempool *p)
+{
+  struct memchunk *c;
+
+  p->free = p->last = NULL;
+  p->current = p->first;
+  while (c = p->first_large)
+    {
+      p->first_large = c->next;
+      xfree(c);
+    }
+}
+
+void *
+mp_alloc(struct mempool *p, uns s)
+{
+  if (s <= p->threshold)
+    {
+      byte *x = (byte *)(((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
+      if (x + s > p->last)
+       {
+         struct memchunk *c;
+
+         if (p->current)
+           {
+             /* Still have free chunks from previous incarnation */
+             c = p->current;
+             p->current = c->next;
+           }
+         else
+           {
+             c = xmalloc(sizeof(struct memchunk) + p->chunk_size);
+             *p->plast = c;
+             p->plast = &c->next;
+             c->next = NULL;
+           }
+         x = c->data;
+         p->last = x + p->chunk_size;
+       }
+      p->free = x + s;
+      return x;
+    }
+  else
+    {
+      struct memchunk *c = xmalloc(sizeof(struct memchunk) + s);
+      c->next = p->first_large;
+      p->first_large = c;
+      return c->data;
+    }
+}
+
+void *
+mp_alloc_zero(struct mempool *p, uns s)
+{
+  void *x = mp_alloc(p, s);
+  bzero(x, s);
+  return x;
+}
diff --git a/lib/mempool.h b/lib/mempool.h
new file mode 100644 (file)
index 0000000..6058c5e
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ *     Sherlock Library -- Memory Pools
+ *
+ *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#ifndef _SHERLOCK_POOLS_H
+#define _SHERLOCK_POOLS_H
+
+#ifndef POOL_ALIGN
+#define POOL_ALIGN CPU_STRUCT_ALIGN
+#endif
+
+struct mempool {
+  byte *free, *last;
+  struct memchunk *first, *current, **plast;
+  struct memchunk *first_large;
+  uns chunk_size, threshold;
+};
+
+struct mempool *mp_new(uns);
+void mp_delete(struct mempool *);
+void mp_flush(struct mempool *);
+void *mp_alloc(struct mempool *, uns);
+void *mp_alloc_zero(struct mempool *, uns);
+
+static inline void *mp_alloc_fast(struct mempool *p, uns l)
+{
+  byte *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
+  byte *ee = f + l;
+  if (ee > p->last)
+    return mp_alloc(p, l);
+  p->free = ee;
+  return f;
+}
+
+static inline void *mp_alloc_fast_noalign(struct mempool *p, uns l)
+{
+  byte *f = p->free;
+  byte *ee = f + l;
+  if (ee > p->last)
+    return mp_alloc(p, l);
+  p->free = ee;
+  return f;
+}
+
+static inline void *
+mp_start_string(struct mempool *p, uns l)
+{
+  ASSERT(l <= p->chunk_size);
+  return mp_alloc(p, l);
+}
+
+static inline void
+mp_end_string(struct mempool *p, void *stop)
+{
+  p->free = stop;
+}
+
+/* pool-str.c */
+
+char *mp_strdup(struct mempool *, char *);
+char *mp_multicat(struct mempool *, ...);
+static inline char *
+mp_strcat(struct mempool *mp, char *x, char *y)
+{
+  return mp_multicat(mp, x, y, NULL);
+}
+
+#endif
diff --git a/lib/pool-str.c b/lib/pool-str.c
deleted file mode 100644 (file)
index 8dc6d23..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- *     Sherlock Library -- Memory Pools (String Operations)
- *
- *     (c) 2004 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 "lib/lib.h"
-#include "lib/pools.h"
-
-#include <alloca.h>
-#include <string.h>
-
-char *
-mp_strdup(struct mempool *p, char *s)
-{
-  uns l = strlen(s) + 1;
-  char *t = mp_alloc_fast_noalign(p, l);
-  memcpy(t, s, l);
-  return t;
-}
-
-char *
-mp_multicat(struct mempool *p, ...)
-{
-  va_list args, a;
-  va_start(args, p);
-  char *x, *y;
-  uns cnt = 0;
-  a = args;
-  while (x = va_arg(a, char *))
-    cnt++;
-  uns *sizes = alloca(cnt * sizeof(uns));
-  uns len = 1;
-  cnt = 0;
-  a = args;
-  while (x = va_arg(a, char *))
-    len += sizes[cnt++] = strlen(x);
-  char *buf = mp_alloc_fast_noalign(p, len);
-  y = buf;
-  a = args;
-  cnt = 0;
-  while (x = va_arg(a, char *))
-    {
-      memcpy(y, x, sizes[cnt]);
-      y += sizes[cnt++];
-    }
-  *y = 0;
-  return buf;
-}
diff --git a/lib/pool.c b/lib/pool.c
deleted file mode 100644 (file)
index 8b7c372..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- *     Sherlock Library -- Memory Pools (One-Time Allocation)
- *
- *     (c) 1997--2001 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 "lib/lib.h"
-#include "lib/pools.h"
-
-#include <stdlib.h>
-#include <string.h>
-
-struct memchunk {
-  struct memchunk *next;
-  byte data[0];
-};
-
-struct mempool *
-mp_new(uns size)
-{
-  struct mempool *p = xmalloc(sizeof(struct mempool));
-
-  size -= sizeof(struct memchunk);
-  p->free = p->last = NULL;
-  p->first = p->current = p->first_large = NULL;
-  p->plast = &p->first;
-  p->chunk_size = size;
-  p->threshold = size / 3;
-  return p;
-}
-
-void
-mp_delete(struct mempool *p)
-{
-  struct memchunk *c, *d;
-
-  for(d=p->first; d; d = c)
-    {
-      c = d->next;
-      xfree(d);
-    }
-  for(d=p->first_large; d; d = c)
-    {
-      c = d->next;
-      xfree(d);
-    }
-  xfree(p);
-}
-
-void
-mp_flush(struct mempool *p)
-{
-  struct memchunk *c;
-
-  p->free = p->last = NULL;
-  p->current = p->first;
-  while (c = p->first_large)
-    {
-      p->first_large = c->next;
-      xfree(c);
-    }
-}
-
-void *
-mp_alloc(struct mempool *p, uns s)
-{
-  if (s <= p->threshold)
-    {
-      byte *x = (byte *)(((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
-      if (x + s > p->last)
-       {
-         struct memchunk *c;
-
-         if (p->current)
-           {
-             /* Still have free chunks from previous incarnation */
-             c = p->current;
-             p->current = c->next;
-           }
-         else
-           {
-             c = xmalloc(sizeof(struct memchunk) + p->chunk_size);
-             *p->plast = c;
-             p->plast = &c->next;
-             c->next = NULL;
-           }
-         x = c->data;
-         p->last = x + p->chunk_size;
-       }
-      p->free = x + s;
-      return x;
-    }
-  else
-    {
-      struct memchunk *c = xmalloc(sizeof(struct memchunk) + s);
-      c->next = p->first_large;
-      p->first_large = c;
-      return c->data;
-    }
-}
-
-void *
-mp_alloc_zero(struct mempool *p, uns s)
-{
-  void *x = mp_alloc(p, s);
-  bzero(x, s);
-  return x;
-}
diff --git a/lib/pools.h b/lib/pools.h
deleted file mode 100644 (file)
index 6058c5e..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- *     Sherlock Library -- Memory Pools
- *
- *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
- *
- *     This software may be freely distributed and used according to the terms
- *     of the GNU Lesser General Public License.
- */
-
-#ifndef _SHERLOCK_POOLS_H
-#define _SHERLOCK_POOLS_H
-
-#ifndef POOL_ALIGN
-#define POOL_ALIGN CPU_STRUCT_ALIGN
-#endif
-
-struct mempool {
-  byte *free, *last;
-  struct memchunk *first, *current, **plast;
-  struct memchunk *first_large;
-  uns chunk_size, threshold;
-};
-
-struct mempool *mp_new(uns);
-void mp_delete(struct mempool *);
-void mp_flush(struct mempool *);
-void *mp_alloc(struct mempool *, uns);
-void *mp_alloc_zero(struct mempool *, uns);
-
-static inline void *mp_alloc_fast(struct mempool *p, uns l)
-{
-  byte *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
-  byte *ee = f + l;
-  if (ee > p->last)
-    return mp_alloc(p, l);
-  p->free = ee;
-  return f;
-}
-
-static inline void *mp_alloc_fast_noalign(struct mempool *p, uns l)
-{
-  byte *f = p->free;
-  byte *ee = f + l;
-  if (ee > p->last)
-    return mp_alloc(p, l);
-  p->free = ee;
-  return f;
-}
-
-static inline void *
-mp_start_string(struct mempool *p, uns l)
-{
-  ASSERT(l <= p->chunk_size);
-  return mp_alloc(p, l);
-}
-
-static inline void
-mp_end_string(struct mempool *p, void *stop)
-{
-  p->free = stop;
-}
-
-/* pool-str.c */
-
-char *mp_strdup(struct mempool *, char *);
-char *mp_multicat(struct mempool *, ...);
-static inline char *
-mp_strcat(struct mempool *mp, char *x, char *y)
-{
-  return mp_multicat(mp, x, y, NULL);
-}
-
-#endif