commit: push commit_index update into alloc_commit_node
authorJeff King <peff@peff.net>
Tue, 10 Jun 2014 21:39:04 +0000 (17:39 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 12 Jun 2014 17:29:42 +0000 (10:29 -0700)
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>
alloc.c
commit.c
diff --git a/alloc.c b/alloc.c
index 38ff7e7e8ee9240c8a0fc3bf665388c0ea315ced..eb22a45c9d0acf08a6024094bd0a2090a7307945 100644 (file)
--- a/alloc.c
+++ b/alloc.c
@@ -47,10 +47,18 @@ union any_object {
 
 DEFINE_ALLOCATOR(blob, struct blob)
 DEFINE_ALLOCATOR(tree, struct tree)
-DEFINE_ALLOCATOR(commit, struct commit)
+DEFINE_ALLOCATOR(raw_commit, struct commit)
 DEFINE_ALLOCATOR(tag, struct tag)
 DEFINE_ALLOCATOR(object, union any_object)
 
+void *alloc_commit_node(void)
+{
+       static int commit_count;
+       struct commit *c = alloc_raw_commit_node();
+       c->index = commit_count++;
+       return c;
+}
+
 static void report(const char *name, unsigned int count, size_t size)
 {
        fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
@@ -64,7 +72,7 @@ void alloc_report(void)
 {
        REPORT(blob, struct blob);
        REPORT(tree, struct tree);
-       REPORT(commit, struct commit);
+       REPORT(raw_commit, struct commit);
        REPORT(tag, struct tag);
        REPORT(object, union any_object);
 }
index bd3d5afcd691dec674f6bc53b9c216fa75ee00d9..fbdc480ccf17b4d241e0a950e8dd44dfe9d684ce 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -17,7 +17,6 @@ static struct commit_extra_header *read_commit_extra_header_lines(const char *bu
 int save_commit_buffer = 1;
 
 const char *commit_type = "commit";
-static int commit_count;
 
 static struct commit *check_commit(struct object *obj,
                                   const unsigned char *sha1,
@@ -64,7 +63,6 @@ struct commit *lookup_commit(const unsigned char *sha1)
        struct object *obj = lookup_object(sha1);
        if (!obj) {
                struct commit *c = alloc_commit_node();
-               c->index = commit_count++;
                return create_object(sha1, OBJ_COMMIT, c);
        }
        if (!obj->type)