tree-cache: extract the allocation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
diff --git a/src/tree-cache.c b/src/tree-cache.c
index 49afd6e..aec9493 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 7801712..592a352 100644
--- a/src/tree-cache.h
+++ b/src/tree-cache.h
@@ -25,6 +25,7 @@ typedef struct {
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