summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2014-07-10 12:21:28 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2014-07-10 12:21:28 +0200
commited4e975631a87780918cb39505c969048cd7d7ff (patch)
tree422ff66949f567d7268679b0a1ef69380ed4c45a
parent18eb6ec8233cb3cb607abdf289da6a57cc252bb2 (diff)
downloadlibgit2-ed4e975631a87780918cb39505c969048cd7d7ff.tar.gz
tree-cache: extract the allocation
-rw-r--r--src/tree-cache.c34
-rw-r--r--src/tree-cache.h1
2 files changed, 23 insertions, 12 deletions
diff --git a/src/tree-cache.c b/src/tree-cache.c
index 49afd6e49..aec94938d 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -74,7 +74,6 @@ static int read_tree_internal(git_tree_cache **out,
git_tree_cache *tree = NULL;
const char *name_start, *buffer;
int count;
- size_t name_len;
buffer = name_start = *buffer_in;
@@ -84,17 +83,8 @@ static int read_tree_internal(git_tree_cache **out,
if (++buffer >= buffer_end)
goto corrupted;
- name_len = strlen(name_start);
- tree = git__malloc(sizeof(git_tree_cache) + name_len + 1);
- GITERR_CHECK_ALLOC(tree);
-
- memset(tree, 0x0, sizeof(git_tree_cache));
- tree->parent = parent;
-
- /* NUL-terminated tree name */
- tree->namelen = name_len;
- memcpy(tree->name, name_start, name_len);
- tree->name[name_len] = '\0';
+ if (git_tree_cache_new(&tree, name_start, parent) < 0)
+ return -1;
/* Blank-terminated ASCII decimal number of entries in this tree */
if (git__strtol32(&count, buffer, &buffer, 10) < 0)
@@ -164,6 +154,26 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer
return 0;
}
+int git_tree_cache_new(git_tree_cache **out, const char *name, git_tree_cache *parent)
+{
+ size_t name_len;
+ git_tree_cache *tree;
+
+ name_len = strlen(name);
+ tree = git__malloc(sizeof(git_tree_cache) + name_len + 1);
+ GITERR_CHECK_ALLOC(tree);
+
+ memset(tree, 0x0, sizeof(git_tree_cache));
+ tree->parent = parent;
+ /* NUL-terminated tree name */
+ tree->namelen = name_len;
+ memcpy(tree->name, name, name_len);
+ tree->name[name_len] = '\0';
+
+ *out = tree;
+ return 0;
+}
+
void git_tree_cache_free(git_tree_cache *tree)
{
unsigned int i;
diff --git a/src/tree-cache.h b/src/tree-cache.h
index 90c82dbbf..64da256e3 100644
--- a/src/tree-cache.h
+++ b/src/tree-cache.h
@@ -27,6 +27,7 @@ typedef struct git_tree_cache git_tree_cache;
int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size);
void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path);
const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path);
+int git_tree_cache_new(git_tree_cache **out, const char *name, git_tree_cache *parent);
void git_tree_cache_free(git_tree_cache *tree);
#endif