commit: push commit_index update into alloc_commit_node

Whenever we create a commit object via lookup_commit, we give it a unique index to be used with the commit-slab API. The theory is that any "struct commit" we create would follow this code path, so any such struct would get an index. However, callers could use alloc_commit_node() directly (and get multiple commits with index 0). Let's push the indexing into alloc_commit_node so that it's hard for callers to get it wrong. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 10, 2014 at 17:39 UTC 969eba6341a5af8ac52c67e26462548ed05e23e3
2 files changed +10 -4
alloc.c
+10 -2
@@ -47,10 +47,18 @@ union any_object {
47
48 DEFINE_ALLOCATOR(blob, struct blob)
49 DEFINE_ALLOCATOR(tree, struct tree)
50 -DEFINE_ALLOCATOR(commit, struct commit)
50 +DEFINE_ALLOCATOR(raw_commit, struct commit)
51 DEFINE_ALLOCATOR(tag, struct tag)
52 DEFINE_ALLOCATOR(object, union any_object)
53
54 +void *alloc_commit_node(void)
55 +{
56 + static int commit_count;
57 + struct commit *c = alloc_raw_commit_node();
58 + c->index = commit_count++;
59 + return c;
60 +}
61 +
62 static void report(const char *name, unsigned int count, size_t size)
63 {
64 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
@@ -64,7 +72,7 @@ void alloc_report(void)
72 {
73 REPORT(blob, struct blob);
74 REPORT(tree, struct tree);
67 - REPORT(commit, struct commit);
75 + REPORT(raw_commit, struct commit);
76 REPORT(tag, struct tag);
77 REPORT(object, union any_object);
78 }
commit.c
-2
@@ -17,7 +17,6 @@ static struct commit_extra_header *read_commit_extra_header_lines(const char *bu
17 int save_commit_buffer = 1;
18
19 const char *commit_type = "commit";
20 -static int commit_count;
20
21 static struct commit *check_commit(struct object *obj,
22 const unsigned char *sha1,
@@ -64,7 +63,6 @@ struct commit *lookup_commit(const unsigned char *sha1)
63 struct object *obj = lookup_object(sha1);
64 if (!obj) {
65 struct commit *c = alloc_commit_node();
67 - c->index = commit_count++;
66 return create_object(sha1, OBJ_COMMIT, c);
67 }
68 if (!obj->type)