add object_as_type helper for casting objects
[git/git.git] / alloc.c
CommitLineData
855419f7
LT
1/*
2 * alloc.c - specialized allocator for internal objects
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 *
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.
11 */
12#include "cache.h"
13#include "object.h"
14#include "blob.h"
15#include "tree.h"
16#include "commit.h"
17#include "tag.h"
18
19#define BLOCKING 1024
20
2c1cbec1
LT
21union any_object {
22 struct object object;
23 struct blob blob;
24 struct tree tree;
25 struct commit commit;
26 struct tag tag;
27};
28
225ea220
RJ
29struct 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 */
33};
34
35static inline void *alloc_node(struct alloc_state *s, size_t node_size)
36{
37 void *ret;
38
39 if (!s->nr) {
40 s->nr = BLOCKING;
41 s->p = xmalloc(BLOCKING * node_size);
42 }
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;
49}
50
600e2a69
JK
51static struct alloc_state blob_state;
52void *alloc_blob_node(void)
53{
54 struct blob *b = alloc_node(&blob_state, sizeof(struct blob));
d36f51c1 55 b->object.type = OBJ_BLOB;
600e2a69
JK
56 return b;
57}
58
59static struct alloc_state tree_state;
60void *alloc_tree_node(void)
61{
62 struct tree *t = alloc_node(&tree_state, sizeof(struct tree));
d36f51c1 63 t->object.type = OBJ_TREE;
600e2a69
JK
64 return t;
65}
66
67static struct alloc_state tag_state;
68void *alloc_tag_node(void)
69{
70 struct tag *t = alloc_node(&tag_state, sizeof(struct tag));
d36f51c1 71 t->object.type = OBJ_TAG;
600e2a69
JK
72 return t;
73}
74
75static struct alloc_state object_state;
76void *alloc_object_node(void)
77{
78 struct object *obj = alloc_node(&object_state, sizeof(union any_object));
d36f51c1 79 obj->type = OBJ_NONE;
600e2a69
JK
80 return obj;
81}
855419f7 82
225ea220
RJ
83static struct alloc_state commit_state;
84
969eba63
JK
85void *alloc_commit_node(void)
86{
87 static int commit_count;
225ea220 88 struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
d36f51c1 89 c->object.type = OBJ_COMMIT;
969eba63
JK
90 c->index = commit_count++;
91 return c;
92}
93
4b25d091 94static void report(const char *name, unsigned int count, size_t size)
579d1fbf 95{
28bd70d8
JN
96 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
97 name, count, (uintmax_t) size);
579d1fbf
RJ
98}
99
c335d74d 100#define REPORT(name, type) \
225ea220 101 report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
855419f7
LT
102
103void alloc_report(void)
104{
c335d74d
JK
105 REPORT(blob, struct blob);
106 REPORT(tree, struct tree);
225ea220 107 REPORT(commit, struct commit);
c335d74d
JK
108 REPORT(tag, struct tag);
109 REPORT(object, union any_object);
855419f7 110}