[PATCH 4/7] Add a memory pool library

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a memory pool library implemented using cpp macros. The macro can
be used to create a type-specific memory pool API. The memory nodes
themselves are stored in a treap, using macros in trp.h. Taken
directly from David Michael Barr's svn-dump-fast-export repository.

Signed-off-by: Ramkumar Ramachandra <artagnon@xxxxxxxxx>
---
 vcs-svn/obj_pool.h |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 61 insertions(+), 0 deletions(-)
 create mode 100644 vcs-svn/obj_pool.h

diff --git a/vcs-svn/obj_pool.h b/vcs-svn/obj_pool.h
new file mode 100644
index 0000000..d8b0842
--- /dev/null
+++ b/vcs-svn/obj_pool.h
@@ -0,0 +1,61 @@
+#ifndef OBJ_POOL_H_
+#define OBJ_POOL_H_
+
+#include <stdint.h>
+#include <stdlib.h>
+
+/*
+ * The obj_pool_gen() macro generates a type-specific memory pool
+ * implementation.
+ *
+ * Arguments:
+ *
+ *   pre              : Prefix for generated functions (ex: string_).
+ *   obj_t            : Type for treap data structure (ex: char).
+ *   intial_capacity  : The initial size of the memory pool (ex: 4096).
+ *
+ */
+#define obj_pool_gen(pre, obj_t, initial_capacity)                         \
+static struct {                                                            \
+    uint32_t size;                                                         \
+    uint32_t capacity;                                                     \
+    obj_t *base;                                                           \
+} pre##_pool = { 0, 0, NULL};                                              \
+static uint32_t pre##_alloc(uint32_t count)                                \
+{                                                                          \
+    uint32_t offset;                                                       \
+    while (pre##_pool.size + count > pre##_pool.capacity) {                \
+        if (pre##_pool.capacity) {                                         \
+            pre##_pool.capacity *= 2;                                      \
+        } else {                                                           \
+            pre##_pool.capacity = initial_capacity;                        \
+        }                                                                  \
+        pre##_pool.base =                                                  \
+            realloc(pre##_pool.base, pre##_pool.capacity * sizeof(obj_t)); \
+    }                                                                      \
+    offset = pre##_pool.size;                                              \
+    pre##_pool.size += count;                                              \
+    return offset;                                                         \
+}                                                                          \
+static void pre##_free(uint32_t count)                                     \
+{                                                                          \
+    pre##_pool.size -= count;                                              \
+}                                                                          \
+static uint32_t pre##_offset(obj_t *obj)                                   \
+{                                                                          \
+    return obj == NULL ? ~0 : obj - pre##_pool.base;                       \
+}                                                                          \
+static obj_t *pre##_pointer(uint32_t offset)                               \
+{                                                                          \
+    return offset >= pre##_pool.size ? NULL : &pre##_pool.base[offset];    \
+}                                                                          \
+static void pre##_reset(void)                                              \
+{                                                                          \
+    if (pre##_pool.base)                                                   \
+        free(pre##_pool.base);                                             \
+    pre##_pool.base = NULL;                                                \
+    pre##_pool.size = 0;                                                   \
+    pre##_pool.capacity = 0;                                               \
+}                                                                          \
+
+#endif
-- 
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]