add "test-absolute-path" to .gitignore
[gitweb.git] / alloc.c
diff --git a/alloc.c b/alloc.c
index e3b22f43228cae90290150ad2a5c81ed97f337de..216c23a6f854c614d38c743cd7687a37f304161b 100644 (file)
--- a/alloc.c
+++ b/alloc.c
 
 #define BLOCKING 1024
 
-#define DEFINE_ALLOCATOR(name)                                 \
+#define DEFINE_ALLOCATOR(name, type)                           \
 static unsigned int name##_allocs;                             \
-struct name *alloc_##name##_node(void)                         \
+void *alloc_##name##_node(void)                                        \
 {                                                              \
        static int nr;                                          \
-       static struct name *block;                              \
+       static type *block;                                     \
+       void *ret;                                              \
                                                                \
        if (!nr) {                                              \
                nr = BLOCKING;                                  \
-               block = xcalloc(BLOCKING, sizeof(struct name)); \
+               block = xmalloc(BLOCKING * sizeof(type));       \
        }                                                       \
        nr--;                                                   \
        name##_allocs++;                                        \
-       return block++;                                         \
+       ret = block++;                                          \
+       memset(ret, 0, sizeof(type));                           \
+       return ret;                                             \
 }
 
-DEFINE_ALLOCATOR(blob)
-DEFINE_ALLOCATOR(tree)
-DEFINE_ALLOCATOR(commit)
-DEFINE_ALLOCATOR(tag)
+union any_object {
+       struct object object;
+       struct blob blob;
+       struct tree tree;
+       struct commit commit;
+       struct tag tag;
+};
+
+DEFINE_ALLOCATOR(blob, struct blob)
+DEFINE_ALLOCATOR(tree, struct tree)
+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)   \
-       fprintf(stderr, "%10s: %8u (%zu kB)\n", #name, name##_allocs, name##_allocs*sizeof(struct name) >> 10)
+    report(#name, name##_allocs, name##_allocs*sizeof(struct name) >> 10)
 
 void alloc_report(void)
 {