]> git.ipfire.org Git - thirdparty/git.git/blame - alloc.c
completion(switch/checkout): treat --track and -t the same
[thirdparty/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 29struct alloc_state {
225ea220
RJ
30 int nr; /* number of nodes left in current allocation */
31 void *p; /* first free node in current allocation */
14ba97f8
SB
32
33 /* bookkeeping of allocations */
34 void **slabs;
35 int slab_nr, slab_alloc;
225ea220
RJ
36};
37
17313107 38struct alloc_state *allocate_alloc_state(void)
14ba97f8
SB
39{
40 return xcalloc(1, sizeof(struct alloc_state));
41}
42
43void clear_alloc_state(struct alloc_state *s)
44{
45 while (s->slab_nr > 0) {
46 s->slab_nr--;
47 free(s->slabs[s->slab_nr]);
48 }
49
50 FREE_AND_NULL(s->slabs);
51}
52
225ea220
RJ
53static inline void *alloc_node(struct alloc_state *s, size_t node_size)
54{
55 void *ret;
56
57 if (!s->nr) {
58 s->nr = BLOCKING;
59 s->p = xmalloc(BLOCKING * node_size);
14ba97f8
SB
60
61 ALLOC_GROW(s->slabs, s->slab_nr + 1, s->slab_alloc);
62 s->slabs[s->slab_nr++] = s->p;
225ea220
RJ
63 }
64 s->nr--;
225ea220
RJ
65 ret = s->p;
66 s->p = (char *)s->p + node_size;
67 memset(ret, 0, node_size);
14ba97f8 68
225ea220
RJ
69 return ret;
70}
71
14ba97f8 72void *alloc_blob_node(struct repository *r)
600e2a69 73{
14ba97f8 74 struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob));
d36f51c1 75 b->object.type = OBJ_BLOB;
600e2a69
JK
76 return b;
77}
78
14ba97f8 79void *alloc_tree_node(struct repository *r)
600e2a69 80{
14ba97f8 81 struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree));
d36f51c1 82 t->object.type = OBJ_TREE;
600e2a69
JK
83 return t;
84}
85
14ba97f8 86void *alloc_tag_node(struct repository *r)
600e2a69 87{
14ba97f8 88 struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag));
d36f51c1 89 t->object.type = OBJ_TAG;
600e2a69
JK
90 return t;
91}
92
14ba97f8 93void *alloc_object_node(struct repository *r)
600e2a69 94{
14ba97f8 95 struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object));
d36f51c1 96 obj->type = OBJ_NONE;
600e2a69
JK
97 return obj;
98}
855419f7 99
6da43d93
AK
100/*
101 * The returned count is to be used as an index into commit slabs,
102 * that are *NOT* maintained per repository, and that is why a single
103 * global counter is used.
104 */
105static unsigned int alloc_commit_index(void)
94d5a22c 106{
6da43d93
AK
107 static unsigned int parsed_commits_count;
108 return parsed_commits_count++;
94d5a22c
JK
109}
110
6da43d93 111void init_commit_node(struct commit *c)
969eba63 112{
d36f51c1 113 c->object.type = OBJ_COMMIT;
6da43d93 114 c->index = alloc_commit_index();
4468d443
SG
115}
116
117void *alloc_commit_node(struct repository *r)
118{
119 struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
6da43d93 120 init_commit_node(c);
969eba63
JK
121 return c;
122}