pool: Dot not assume mallocs are zeroed out
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
diff --git a/src/commit_list.c b/src/commit_list.c
index 53612d5..28948c8 100644
--- a/src/commit_list.c
+++ b/src/commit_list.c
@@ -47,7 +47,7 @@ git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_
git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk)
{
- return (git_commit_list_node *)git_pool_malloc(&walk->commit_pool, COMMIT_ALLOC);
+ return (git_commit_list_node *)git_pool_mallocz(&walk->commit_pool, 1);
}
static int commit_error(git_commit_list_node *commit, const char *msg)
diff --git a/src/merge.c b/src/merge.c
index e84b1d3..a124cbd 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -626,7 +626,7 @@ static int merge_conflict_resolve_one_renamed(
git_oid__cmp(&conflict->our_entry.id, &conflict->their_entry.id) != 0)
return 0;
- if ((merged = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL)
+ if ((merged = git_pool_mallocz(&diff_list->pool, sizeof(git_index_entry))) == NULL)
return -1;
if (ours_changed)
@@ -711,7 +711,7 @@ static int merge_conflict_resolve_automerge(
(error = git_odb_write(&automerge_oid, odb, result.ptr, result.len, GIT_OBJ_BLOB)) < 0)
goto done;
- if ((index_entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL)
+ if ((index_entry = git_pool_mallocz(&diff_list->pool, sizeof(git_index_entry))) == NULL)
GITERR_CHECK_ALLOC(index_entry);
index_entry->path = git_pool_strdup(&diff_list->pool, result.path);
@@ -1342,7 +1342,7 @@ static git_merge_diff *merge_diff_from_index_entries(
git_merge_diff *conflict;
git_pool *pool = &diff_list->pool;
- if ((conflict = git_pool_malloc(pool, sizeof(git_merge_diff))) == NULL)
+ if ((conflict = git_pool_mallocz(pool, sizeof(git_merge_diff))) == NULL)
return NULL;
if (index_entry_dup_pool(&conflict->ancestor_entry, pool, entries[TREE_IDX_ANCESTOR]) < 0 ||
@@ -1383,7 +1383,7 @@ static int merge_diff_list_insert_unmodified(
int error = 0;
git_index_entry *entry;
- entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry));
+ entry = git_pool_mallocz(&diff_list->pool, sizeof(git_index_entry));
GITERR_CHECK_ALLOC(entry);
if ((error = index_entry_dup_pool(entry, &diff_list->pool, tree_items[0])) >= 0)
diff --git a/src/pool.c b/src/pool.c
index fe6c429..aab63be 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -51,7 +51,6 @@ void git_pool_clear(git_pool *pool)
}
pool->pages = NULL;
- pool->items = 0;
}
void git_pool_swap(git_pool *a, git_pool *b)
@@ -73,7 +72,7 @@ static void *pool_alloc_page(git_pool *pool, uint32_t size)
size_t alloc_size;
if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) ||
- !(page = git__calloc(1, alloc_size)))
+ !(page = git__malloc(alloc_size)))
return NULL;
page->size = new_page_size;
@@ -81,15 +80,12 @@ static void *pool_alloc_page(git_pool *pool, uint32_t size)
page->next = pool->pages;
pool->pages = page;
- pool->items++;
return page->data;
}
-void *git_pool_malloc(git_pool *pool, uint32_t items)
+static void *pool_alloc(git_pool *pool, uint32_t size)
{
- const uint32_t size = items * pool->item_size;
-
git_pool_page *page = pool->pages;
void *ptr = NULL;
@@ -98,11 +94,25 @@ void *git_pool_malloc(git_pool *pool, uint32_t items)
ptr = &page->data[page->size - page->avail];
page->avail -= size;
- pool->items++;
return ptr;
}
+void *git_pool_malloc(git_pool *pool, uint32_t items)
+{
+ const uint32_t size = items * pool->item_size;
+ return pool_alloc(pool, size);
+}
+
+void *git_pool_mallocz(git_pool *pool, uint32_t items)
+{
+ const uint32_t size = items * pool->item_size;
+ void *ptr = pool_alloc(pool, size);
+ if (ptr)
+ memset(ptr, 0x0, size);
+ return ptr;
+}
+
char *git_pool_strndup(git_pool *pool, const char *str, size_t n)
{
char *ptr = NULL;
diff --git a/src/pool.h b/src/pool.h
index ef71d4e..f9ac867 100644
--- a/src/pool.h
+++ b/src/pool.h
@@ -31,7 +31,6 @@ typedef struct {
git_pool_page *pages; /* pages with space left */
uint32_t item_size; /* size of single alloc unit in bytes */
uint32_t page_size; /* size of page in bytes */
- uint32_t items;
} git_pool;
/**
@@ -66,17 +65,7 @@ extern void git_pool_swap(git_pool *a, git_pool *b);
* Allocate space for one or more items from a pool.
*/
extern void *git_pool_malloc(git_pool *pool, uint32_t items);
-
-/**
- * Allocate space and zero it out.
- */
-GIT_INLINE(void *) git_pool_mallocz(git_pool *pool, uint32_t items)
-{
- void *ptr = git_pool_malloc(pool, items);
- if (ptr)
- memset(ptr, 0, (size_t)items * (size_t)pool->item_size);
- return ptr;
-}
+extern void *git_pool_mallocz(git_pool *pool, uint32_t items);
/**
* Allocate space and duplicate string data into it.