Merge branch 'js/gitdir-at-unc-root'
[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
14ba97f8 7 * it maintains all the allocation infrastructure, but even more because it ends
855419f7
LT
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"
14ba97f8 17#include "alloc.h"
855419f7
LT
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 */
14ba97f8
SB
33
34 /* bookkeeping of allocations */
35 void **slabs;
36 int slab_nr, slab_alloc;
225ea220
RJ
37};
38
17313107 39struct alloc_state *allocate_alloc_state(void)
14ba97f8
SB
40{
41 return xcalloc(1, sizeof(struct alloc_state));
42}
43
44void 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
225ea220
RJ
54static 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);
14ba97f8
SB
61
62 ALLOC_GROW(s->slabs, s->slab_nr + 1, s->slab_alloc);
63 s->slabs[s->slab_nr++] = s->p;
225ea220
RJ
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);
14ba97f8 70
225ea220
RJ
71 return ret;
72}
73
14ba97f8 74void *alloc_blob_node(struct repository *r)
600e2a69 75{
14ba97f8 76 struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob));
d36f51c1 77 b->object.type = OBJ_BLOB;
600e2a69
JK
78 return b;
79}
80
14ba97f8 81void *alloc_tree_node(struct repository *r)
600e2a69 82{
14ba97f8 83 struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree));
d36f51c1 84 t->object.type = OBJ_TREE;
600e2a69
JK
85 return t;
86}
87
14ba97f8 88void *alloc_tag_node(struct repository *r)
600e2a69 89{
14ba97f8 90 struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag));
d36f51c1 91 t->object.type = OBJ_TAG;
600e2a69
JK
92 return t;
93}
94
14ba97f8 95void *alloc_object_node(struct repository *r)
600e2a69 96{
14ba97f8 97 struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object));
d36f51c1 98 obj->type = OBJ_NONE;
600e2a69
JK
99 return obj;
100}
855419f7 101
4468d443 102static unsigned int alloc_commit_index(struct repository *r)
94d5a22c 103{
14ba97f8 104 return r->parsed_objects->commit_count++;
94d5a22c
JK
105}
106
4468d443 107void init_commit_node(struct repository *r, struct commit *c)
969eba63 108{
d36f51c1 109 c->object.type = OBJ_COMMIT;
14ba97f8 110 c->index = alloc_commit_index(r);
177722b3 111 c->graph_pos = COMMIT_NOT_FROM_GRAPH;
83073cc9 112 c->generation = GENERATION_NUMBER_INFINITY;
4468d443
SG
113}
114
115void *alloc_commit_node(struct repository *r)
116{
117 struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
118 init_commit_node(r, c);
969eba63
JK
119 return c;
120}
121
4b25d091 122static void report(const char *name, unsigned int count, size_t size)
579d1fbf 123{
28bd70d8
JN
124 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
125 name, count, (uintmax_t) size);
579d1fbf
RJ
126}
127
c335d74d 128#define REPORT(name, type) \
14ba97f8
SB
129 report(#name, r->parsed_objects->name##_state->count, \
130 r->parsed_objects->name##_state->count * sizeof(type) >> 10)
855419f7 131
14ba97f8 132void alloc_report(struct repository *r)
855419f7 133{
c335d74d
JK
134 REPORT(blob, struct blob);
135 REPORT(tree, struct tree);
225ea220 136 REPORT(commit, struct commit);
c335d74d
JK
137 REPORT(tag, struct tag);
138 REPORT(object, union any_object);
855419f7 139}