Commit 9abc78ae6157167682f061b4f73eea51ab7d7342

Russell Belfer 2013-07-07T21:56:11

Convert commit->parent_ids to git_array_t This converts the array of parent SHAs from a git_vector where each SHA has to be separately allocated to a git_array_t where all the SHAs can be kept in one block. Since the two collections have almost identical APIs, there isn't much involved in making the change. I did add an API to git_array_t so that it could be allocated at a precise initial size.

diff --git a/src/array.h b/src/array.h
index 2d77c71..7075706 100644
--- a/src/array.h
+++ b/src/array.h
@@ -30,6 +30,9 @@
 #define git_array_init(a) \
 	do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0)
 
+#define git_array_init_to_size(a, desired) \
+	do { (a).size = 0; (a).asize = desired; (a).ptr = git__calloc(desired, sizeof(*(a).ptr)); } while (0)
+
 #define git_array_clear(a) \
 	do { git__free((a).ptr); git_array_init(a); } while (0)
 
diff --git a/src/commit.c b/src/commit.c
index cf50c2d..cc912a7 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -19,24 +19,11 @@
 
 #include <stdarg.h>
 
-static void clear_parents(git_commit *commit)
-{
-	size_t i;
-
-	for (i = 0; i < commit->parent_ids.length; ++i) {
-		git_oid *parent = git_vector_get(&commit->parent_ids, i);
-		git__free(parent);
-	}
-
-	git_vector_clear(&commit->parent_ids);
-}
-
 void git_commit__free(void *_commit)
 {
 	git_commit *commit = _commit;
 
-	clear_parents(commit);
-	git_vector_free(&commit->parent_ids);
+	git_array_clear(commit->parent_ids);
 
 	git_signature_free(commit->author);
 	git_signature_free(commit->committer);
@@ -44,6 +31,7 @@ void git_commit__free(void *_commit)
 	git__free(commit->raw_header);
 	git__free(commit->message);
 	git__free(commit->message_encoding);
+
 	git__free(commit);
 }
 
@@ -198,8 +186,8 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
 	if (parent_count < 1)
 		parent_count = 1;
 
-	if (git_vector_init(&commit->parent_ids, parent_count, NULL) < 0)
-		return -1;
+	git_array_init_to_size(commit->parent_ids, parent_count);
+	GITERR_CHECK_ARRAY(commit->parent_ids);
 
 	if (git_oid__parse(&commit->tree_id, &buffer, buffer_end, "tree ") < 0)
 		goto bad_buffer;
@@ -209,13 +197,10 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
 	 */
 
 	while (git_oid__parse(&parent_id, &buffer, buffer_end, "parent ") == 0) {
-		git_oid *new_id = git__malloc(sizeof(git_oid));
+		git_oid *new_id = git_array_alloc(commit->parent_ids);
 		GITERR_CHECK_ALLOC(new_id);
 
 		git_oid_cpy(new_id, &parent_id);
-
-		if (git_vector_insert(&commit->parent_ids, new_id) < 0)
-			return -1;
 	}
 
 	commit->author = git__malloc(sizeof(git_signature));
@@ -284,7 +269,7 @@ GIT_COMMIT_GETTER(const char *, message_encoding, commit->message_encoding)
 GIT_COMMIT_GETTER(const char *, raw_header, commit->raw_header)
 GIT_COMMIT_GETTER(git_time_t, time, commit->committer->when.time)
 GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset)
-GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)commit->parent_ids.length)
+GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)git_array_size(commit->parent_ids))
 GIT_COMMIT_GETTER(const git_oid *, tree_id, &commit->tree_id);
 
 int git_commit_tree(git_tree **tree_out, const git_commit *commit)
@@ -298,7 +283,7 @@ const git_oid *git_commit_parent_id(
 {
 	assert(commit);
 
-	return git_vector_get(&commit->parent_ids, n);
+	return git_array_get(commit->parent_ids, n);
 }
 
 int git_commit_parent(
diff --git a/src/commit.h b/src/commit.h
index 70d8fc6..22fc898 100644
--- a/src/commit.h
+++ b/src/commit.h
@@ -10,14 +10,14 @@
 #include "git2/commit.h"
 #include "tree.h"
 #include "repository.h"
-#include "vector.h"
+#include "array.h"
 
 #include <time.h>
 
 struct git_commit {
 	git_object object;
 
-	git_vector parent_ids;
+	git_array_t(git_oid) parent_ids;
 	git_oid tree_id;
 
 	git_signature *author;