Skip to content

Commit 8c3f3f2

Browse files
Ramsay Jonesgitster
authored andcommitted
alloc.c: remove the alloc_raw_commit_node() function
In order to encapsulate the setting of the unique commit index, commit 969eba6 ("commit: push commit_index update into alloc_commit_node", 10-06-2014) introduced a (logically private) intermediary allocator function. However, this function (alloc_raw_commit_node()) was declared as a public function, which undermines its entire purpose. Introduce an inline function, alloc_node(), which implements the main logic of the allocator used by DEFINE_ALLOCATOR, and redefine the macro in terms of the new function. In addition, use the new function in the implementation of the alloc_commit_node() allocator, rather than the intermediary allocator, which can now be removed. Noticed by sparse ("symbol 'alloc_raw_commit_node' was not declared. Should it be static?"). Signed-off-by: Ramsay Jones <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 740c281 commit 8c3f3f2

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

alloc.c

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,10 @@
1919
#define BLOCKING 1024
2020

2121
#define DEFINE_ALLOCATOR(name, type) \
22-
static unsigned int name##_allocs; \
22+
static struct alloc_state name##_state; \
2323
void *alloc_##name##_node(void) \
2424
{ \
25-
static int nr; \
26-
static type *block; \
27-
void *ret; \
28-
\
29-
if (!nr) { \
30-
nr = BLOCKING; \
31-
block = xmalloc(BLOCKING * sizeof(type)); \
32-
} \
33-
nr--; \
34-
name##_allocs++; \
35-
ret = block++; \
36-
memset(ret, 0, sizeof(type)); \
37-
return ret; \
25+
return alloc_node(&name##_state, sizeof(type)); \
3826
}
3927

4028
union any_object {
@@ -45,16 +33,39 @@ union any_object {
4533
struct tag tag;
4634
};
4735

36+
struct alloc_state {
37+
int count; /* total number of nodes allocated */
38+
int nr; /* number of nodes left in current allocation */
39+
void *p; /* first free node in current allocation */
40+
};
41+
42+
static inline void *alloc_node(struct alloc_state *s, size_t node_size)
43+
{
44+
void *ret;
45+
46+
if (!s->nr) {
47+
s->nr = BLOCKING;
48+
s->p = xmalloc(BLOCKING * node_size);
49+
}
50+
s->nr--;
51+
s->count++;
52+
ret = s->p;
53+
s->p = (char *)s->p + node_size;
54+
memset(ret, 0, node_size);
55+
return ret;
56+
}
57+
4858
DEFINE_ALLOCATOR(blob, struct blob)
4959
DEFINE_ALLOCATOR(tree, struct tree)
50-
DEFINE_ALLOCATOR(raw_commit, struct commit)
5160
DEFINE_ALLOCATOR(tag, struct tag)
5261
DEFINE_ALLOCATOR(object, union any_object)
5362

63+
static struct alloc_state commit_state;
64+
5465
void *alloc_commit_node(void)
5566
{
5667
static int commit_count;
57-
struct commit *c = alloc_raw_commit_node();
68+
struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
5869
c->index = commit_count++;
5970
return c;
6071
}
@@ -66,13 +77,13 @@ static void report(const char *name, unsigned int count, size_t size)
6677
}
6778

6879
#define REPORT(name, type) \
69-
report(#name, name##_allocs, name##_allocs * sizeof(type) >> 10)
80+
report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
7081

7182
void alloc_report(void)
7283
{
7384
REPORT(blob, struct blob);
7485
REPORT(tree, struct tree);
75-
REPORT(raw_commit, struct commit);
86+
REPORT(commit, struct commit);
7687
REPORT(tag, struct tag);
7788
REPORT(object, union any_object);
7889
}

0 commit comments

Comments
 (0)