2 * alloc.c - specialized allocator for internal objects
4 * Copyright (C) 2006 Linus Torvalds
6 * The standard malloc/free wastes too much space for objects, partly because
7 * it maintains all the allocation infrastructure (which isn't needed, since
8 * we never free an object descriptor anyway), but even more because it ends
9 * up with maximal alignment because it doesn't know what the object alignment
10 * for the new allocation is.
21 #define DEFINE_ALLOCATOR(name, type) \
22 static unsigned int name##_allocs; \
23 void *alloc_##name##_node(void) \
31 block = xmalloc(BLOCKING * sizeof(type)); \
36 memset(ret, 0, sizeof(type)); \
48 DEFINE_ALLOCATOR(blob
, struct blob
)
49 DEFINE_ALLOCATOR(tree
, struct tree
)
50 DEFINE_ALLOCATOR(raw_commit
, struct commit
)
51 DEFINE_ALLOCATOR(tag
, struct tag
)
52 DEFINE_ALLOCATOR(object
, union any_object
)
54 void *alloc_commit_node(void)
56 static int commit_count
;
57 struct commit
*c
= alloc_raw_commit_node();
58 c
->index
= commit_count
++;
62 static void report(const char *name
, unsigned int count
, size_t size
)
64 fprintf(stderr
, "%10s: %8u (%"PRIuMAX
" kB)\n",
65 name
, count
, (uintmax_t) size
);
68 #define REPORT(name, type) \
69 report(#name, name##_allocs, name##_allocs * sizeof(type) >> 10)
71 void alloc_report(void)
73 REPORT(blob
, struct blob
);
74 REPORT(tree
, struct tree
);
75 REPORT(raw_commit
, struct commit
);
76 REPORT(tag
, struct tag
);
77 REPORT(object
, union any_object
);