]> git.ipfire.org Git - thirdparty/git.git/blob - alloc.c
reftable: read reftable files
[thirdparty/git.git] / alloc.c
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, but even more because it ends
8 * up with maximal alignment because it doesn't know what the object alignment
9 * for the new allocation is.
10 */
11 #include "cache.h"
12 #include "object.h"
13 #include "blob.h"
14 #include "tree.h"
15 #include "commit.h"
16 #include "tag.h"
17 #include "alloc.h"
18
19 #define BLOCKING 1024
20
21 union any_object {
22 struct object object;
23 struct blob blob;
24 struct tree tree;
25 struct commit commit;
26 struct tag tag;
27 };
28
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 */
33
34 /* bookkeeping of allocations */
35 void **slabs;
36 int slab_nr, slab_alloc;
37 };
38
39 struct alloc_state *allocate_alloc_state(void)
40 {
41 return xcalloc(1, sizeof(struct alloc_state));
42 }
43
44 void clear_alloc_state(struct alloc_state *s)
45 {
46 while (s->slab_nr > 0) {
47 s->slab_nr--;
48 free(s->slabs[s->slab_nr]);
49 }
50
51 FREE_AND_NULL(s->slabs);
52 }
53
54 static inline void *alloc_node(struct alloc_state *s, size_t node_size)
55 {
56 void *ret;
57
58 if (!s->nr) {
59 s->nr = BLOCKING;
60 s->p = xmalloc(BLOCKING * node_size);
61
62 ALLOC_GROW(s->slabs, s->slab_nr + 1, s->slab_alloc);
63 s->slabs[s->slab_nr++] = s->p;
64 }
65 s->nr--;
66 s->count++;
67 ret = s->p;
68 s->p = (char *)s->p + node_size;
69 memset(ret, 0, node_size);
70
71 return ret;
72 }
73
74 void *alloc_blob_node(struct repository *r)
75 {
76 struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob));
77 b->object.type = OBJ_BLOB;
78 return b;
79 }
80
81 void *alloc_tree_node(struct repository *r)
82 {
83 struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree));
84 t->object.type = OBJ_TREE;
85 return t;
86 }
87
88 void *alloc_tag_node(struct repository *r)
89 {
90 struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag));
91 t->object.type = OBJ_TAG;
92 return t;
93 }
94
95 void *alloc_object_node(struct repository *r)
96 {
97 struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object));
98 obj->type = OBJ_NONE;
99 return obj;
100 }
101
102 /*
103 * The returned count is to be used as an index into commit slabs,
104 * that are *NOT* maintained per repository, and that is why a single
105 * global counter is used.
106 */
107 static unsigned int alloc_commit_index(void)
108 {
109 static unsigned int parsed_commits_count;
110 return parsed_commits_count++;
111 }
112
113 void init_commit_node(struct commit *c)
114 {
115 c->object.type = OBJ_COMMIT;
116 c->index = alloc_commit_index();
117 }
118
119 void *alloc_commit_node(struct repository *r)
120 {
121 struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
122 init_commit_node(c);
123 return c;
124 }
125
126 static void report(const char *name, unsigned int count, size_t size)
127 {
128 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
129 name, count, (uintmax_t) size);
130 }
131
132 #define REPORT(name, type) \
133 report(#name, r->parsed_objects->name##_state->count, \
134 r->parsed_objects->name##_state->count * sizeof(type) >> 10)
135
136 void alloc_report(struct repository *r)
137 {
138 REPORT(blob, struct blob);
139 REPORT(tree, struct tree);
140 REPORT(commit, struct commit);
141 REPORT(tag, struct tag);
142 REPORT(object, union any_object);
143 }