X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=alloc.c;h=216c23a6f854c614d38c743cd7687a37f304161b;hb=ed82edc402c271a707da632083f1f4c19155d573;hp=e3b22f43228cae90290150ad2a5c81ed97f337de;hpb=811476d22480dd444b8eca9834829efce9648637;p=git.git diff --git a/alloc.c b/alloc.c index e3b22f432..216c23a6f 100644 --- a/alloc.c +++ b/alloc.c @@ -18,29 +18,54 @@ #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) {