git
/
alloc.c
123 строки · 2.7 Кб
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 "git-compat-util.h"
12#include "object.h"
13#include "blob.h"
14#include "tree.h"
15#include "commit.h"
16#include "repository.h"
17#include "tag.h"
18#include "alloc.h"
19
20#define BLOCKING 1024
21
22union any_object {
23struct object object;
24struct blob blob;
25struct tree tree;
26struct commit commit;
27struct tag tag;
28};
29
30struct alloc_state {
31int nr; /* number of nodes left in current allocation */
32void *p; /* first free node in current allocation */
33
34/* bookkeeping of allocations */
35void **slabs;
36int slab_nr, slab_alloc;
37};
38
39struct alloc_state *allocate_alloc_state(void)
40{
41return xcalloc(1, sizeof(struct alloc_state));
42}
43
44void clear_alloc_state(struct alloc_state *s)
45{
46while (s->slab_nr > 0) {
47s->slab_nr--;
48free(s->slabs[s->slab_nr]);
49}
50
51FREE_AND_NULL(s->slabs);
52}
53
54static inline void *alloc_node(struct alloc_state *s, size_t node_size)
55{
56void *ret;
57
58if (!s->nr) {
59s->nr = BLOCKING;
60s->p = xmalloc(BLOCKING * node_size);
61
62ALLOC_GROW(s->slabs, s->slab_nr + 1, s->slab_alloc);
63s->slabs[s->slab_nr++] = s->p;
64}
65s->nr--;
66ret = s->p;
67s->p = (char *)s->p + node_size;
68memset(ret, 0, node_size);
69
70return ret;
71}
72
73void *alloc_blob_node(struct repository *r)
74{
75struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob));
76b->object.type = OBJ_BLOB;
77return b;
78}
79
80void *alloc_tree_node(struct repository *r)
81{
82struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree));
83t->object.type = OBJ_TREE;
84return t;
85}
86
87void *alloc_tag_node(struct repository *r)
88{
89struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag));
90t->object.type = OBJ_TAG;
91return t;
92}
93
94void *alloc_object_node(struct repository *r)
95{
96struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object));
97obj->type = OBJ_NONE;
98return obj;
99}
100
101/*
102* The returned count is to be used as an index into commit slabs,
103* that are *NOT* maintained per repository, and that is why a single
104* global counter is used.
105*/
106static unsigned int alloc_commit_index(void)
107{
108static unsigned int parsed_commits_count;
109return parsed_commits_count++;
110}
111
112void init_commit_node(struct commit *c)
113{
114c->object.type = OBJ_COMMIT;
115c->index = alloc_commit_index();
116}
117
118void *alloc_commit_node(struct repository *r)
119{
120struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
121init_commit_node(c);
122return c;
123}
124