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 struct alloc_state name##_state; \
23 void *alloc_##name##_node(void) \
25 return alloc_node(&name##_state, sizeof(type)); \
37 int count; /* total number of nodes allocated */
38 int nr; /* number of nodes left in current allocation */
39 void *p; /* first free node in current allocation */
42 static inline void *alloc_node(struct alloc_state *s, size_t node_size)
48 s->p = xmalloc(BLOCKING * node_size);
53 s->p = (char *)s->p + node_size;
54 memset(ret, 0, node_size);
58 DEFINE_ALLOCATOR(blob, struct blob)
59 DEFINE_ALLOCATOR(tree, struct tree)
60 DEFINE_ALLOCATOR(tag, struct tag)
61 DEFINE_ALLOCATOR(object, union any_object)
63 static struct alloc_state commit_state;
65 void *alloc_commit_node(void)
67 static int commit_count;
68 struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
69 c->index = commit_count++;
73 static void report(const char *name, unsigned int count, size_t size)
75 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
76 name, count, (uintmax_t) size);
79 #define REPORT(name, type) \
80 report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
82 void alloc_report(void)
84 REPORT(blob, struct blob);
85 REPORT(tree, struct tree);
86 REPORT(commit, struct commit);
87 REPORT(tag, struct tag);
88 REPORT(object, union any_object);