alloc: write out allocator definitions
[git.git] / alloc.c
blob03e458bad02329927fffa12180122253c4671fc8
1 /*
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.
12 #include "cache.h"
13 #include "object.h"
14 #include "blob.h"
15 #include "tree.h"
16 #include "commit.h"
17 #include "tag.h"
19 #define BLOCKING 1024
21 union any_object {
22 struct object object;
23 struct blob blob;
24 struct tree tree;
25 struct commit commit;
26 struct tag tag;
29 struct alloc_state {
30 int count; /* total number of nodes allocated */
31 int nr; /* number of nodes left in current allocation */
32 void *p; /* first free node in current allocation */
35 static inline void *alloc_node(struct alloc_state *s, size_t node_size)
37 void *ret;
39 if (!s->nr) {
40 s->nr = BLOCKING;
41 s->p = xmalloc(BLOCKING * node_size);
43 s->nr--;
44 s->count++;
45 ret = s->p;
46 s->p = (char *)s->p + node_size;
47 memset(ret, 0, node_size);
48 return ret;
51 static struct alloc_state blob_state;
52 void *alloc_blob_node(void)
54 struct blob *b = alloc_node(&blob_state, sizeof(struct blob));
55 return b;
58 static struct alloc_state tree_state;
59 void *alloc_tree_node(void)
61 struct tree *t = alloc_node(&tree_state, sizeof(struct tree));
62 return t;
65 static struct alloc_state tag_state;
66 void *alloc_tag_node(void)
68 struct tag *t = alloc_node(&tag_state, sizeof(struct tag));
69 return t;
72 static struct alloc_state object_state;
73 void *alloc_object_node(void)
75 struct object *obj = alloc_node(&object_state, sizeof(union any_object));
76 return obj;
79 static struct alloc_state commit_state;
81 void *alloc_commit_node(void)
83 static int commit_count;
84 struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
85 c->index = commit_count++;
86 return c;
89 static void report(const char *name, unsigned int count, size_t size)
91 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
92 name, count, (uintmax_t) size);
95 #define REPORT(name, type) \
96 report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
98 void alloc_report(void)
100 REPORT(blob, struct blob);
101 REPORT(tree, struct tree);
102 REPORT(commit, struct commit);
103 REPORT(tag, struct tag);
104 REPORT(object, union any_object);