* [PATCH 02/11] Reorganize the object memory pools so we can release them
@ 2007-11-09 11:06 Shawn O. Pearce
0 siblings, 0 replies; only message in thread
From: Shawn O. Pearce @ 2007-11-09 11:06 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
This changes the object memory allocators in alloc.c so that we
are storing all information about each allocator within a single
struct, rather than hiding it inside of the allocation function.
By keeping this state information in a struct we are better able to
organize a function that would release the allocated memory blocks
back to the system allocator.
Each block allocated via xmalloc() is chained together into a linked
list so we can free them when if we later need to. A nice side
effect of this change is we can avoid the increment of the number
of allocations and instead compute this after the fact based upon
the number of blocks in the linked lists.
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
alloc.c | 86 ++++++++++++++++++++++++++++++++++++++++----------------------
1 files changed, 55 insertions(+), 31 deletions(-)
diff --git a/alloc.c b/alloc.c
index 216c23a..a61ae41 100644
--- a/alloc.c
+++ b/alloc.c
@@ -18,23 +18,62 @@
#define BLOCKING 1024
-#define DEFINE_ALLOCATOR(name, type) \
-static unsigned int name##_allocs; \
+struct node_block
+{
+ struct node_block *next;
+};
+
+struct node_pool
+{
+ struct node_block *block_list;
+ char *base;
+ size_t nr;
+};
+
+#ifdef NO_C99_FORMAT
+#define SZ_FMT "%5u"
+#else
+#define SZ_FMT "%5zu"
+#endif
+
+static void report(const char* name, struct node_pool *p, size_t sz)
+{
+ unsigned int count = 0;
+ struct node_block *b;
+
+ for (b = p->block_list; b; b = b->next)
+ count += BLOCKING;
+ count -= p->nr;
+
+ sz = (count * sz) >> 10;
+ fprintf(stderr, "%10s: %8u (" SZ_FMT " kB)\n", name, count, sz);
+}
+
+#undef SZ_FMT
+
+#define DEFINE_ALLOCATOR(name, t) \
+static struct node_pool name##_pool; \
void *alloc_##name##_node(void) \
{ \
- static int nr; \
- static type *block; \
void *ret; \
\
- if (!nr) { \
- nr = BLOCKING; \
- block = xmalloc(BLOCKING * sizeof(type)); \
+ if (!name##_pool.nr) { \
+ struct node_block *b; \
+ b = xmalloc(sizeof(*b) + BLOCKING * sizeof(t)); \
+ b->next = name##_pool.block_list; \
+ name##_pool.block_list = b; \
+ name##_pool.base = (char*)(b + 1); \
+ name##_pool.nr = BLOCKING; \
} \
- nr--; \
- name##_allocs++; \
- ret = block++; \
- memset(ret, 0, sizeof(type)); \
+ ret = name##_pool.base; \
+ name##_pool.nr--; \
+ name##_pool.base += sizeof(t); \
+ memset(ret, 0, sizeof(t)); \
return ret; \
+} \
+static void report_##name(void) \
+{ \
+ report(#name, &name##_pool, sizeof(t)); \
}
union any_object {
@@ -51,26 +90,11 @@ DEFINE_ALLOCATOR(commit, struct commit)
DEFINE_ALLOCATOR(tag, struct tag)
DEFINE_ALLOCATOR(object, union any_object)
-#ifdef NO_C99_FORMAT
-#define SZ_FMT "%u"
-#else
-#define SZ_FMT "%zu"
-#endif
-
-static void report(const char* name, unsigned int count, size_t size)
-{
- fprintf(stderr, "%10s: %8u (" SZ_FMT " kB)\n", name, count, size);
-}
-
-#undef SZ_FMT
-
-#define REPORT(name) \
- report(#name, name##_allocs, name##_allocs*sizeof(struct name) >> 10)
-
void alloc_report(void)
{
- REPORT(blob);
- REPORT(tree);
- REPORT(commit);
- REPORT(tag);
+ report_blob();
+ report_tree();
+ report_commit();
+ report_tag();
+ report_object();
}
--
1.5.3.5.1622.g41d10
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2007-11-09 11:06 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-09 11:06 [PATCH 02/11] Reorganize the object memory pools so we can release them Shawn O. Pearce
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox