From: "Shawn O. Pearce" <spearce@spearce.org>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH 02/11] Reorganize the object memory pools so we can release them
Date: Fri, 9 Nov 2007 06:06:13 -0500 [thread overview]
Message-ID: <20071109110613.GB19368@spearce.org> (raw)
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
reply other threads:[~2007-11-09 11:06 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071109110613.GB19368@spearce.org \
--to=spearce@spearce.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox