Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3416df

Browse files
committed
pool: Dot not assume mallocs are zeroed out
1 parent 66eb766 commit d3416df

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

src/commit_list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_
4747

4848
git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk)
4949
{
50-
return (git_commit_list_node *)git_pool_malloc(&walk->commit_pool, COMMIT_ALLOC);
50+
return (git_commit_list_node *)git_pool_mallocz(&walk->commit_pool, 1);
5151
}
5252

5353
static int commit_error(git_commit_list_node *commit, const char *msg)

src/merge.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ static int merge_conflict_resolve_one_renamed(
626626
git_oid__cmp(&conflict->our_entry.id, &conflict->their_entry.id) != 0)
627627
return 0;
628628

629-
if ((merged = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL)
629+
if ((merged = git_pool_mallocz(&diff_list->pool, sizeof(git_index_entry))) == NULL)
630630
return -1;
631631

632632
if (ours_changed)
@@ -711,7 +711,7 @@ static int merge_conflict_resolve_automerge(
711711
(error = git_odb_write(&automerge_oid, odb, result.ptr, result.len, GIT_OBJ_BLOB)) < 0)
712712
goto done;
713713

714-
if ((index_entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL)
714+
if ((index_entry = git_pool_mallocz(&diff_list->pool, sizeof(git_index_entry))) == NULL)
715715
GITERR_CHECK_ALLOC(index_entry);
716716

717717
index_entry->path = git_pool_strdup(&diff_list->pool, result.path);
@@ -1342,7 +1342,7 @@ static git_merge_diff *merge_diff_from_index_entries(
13421342
git_merge_diff *conflict;
13431343
git_pool *pool = &diff_list->pool;
13441344

1345-
if ((conflict = git_pool_malloc(pool, sizeof(git_merge_diff))) == NULL)
1345+
if ((conflict = git_pool_mallocz(pool, sizeof(git_merge_diff))) == NULL)
13461346
return NULL;
13471347

13481348
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(
13831383
int error = 0;
13841384
git_index_entry *entry;
13851385

1386-
entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry));
1386+
entry = git_pool_mallocz(&diff_list->pool, sizeof(git_index_entry));
13871387
GITERR_CHECK_ALLOC(entry);
13881388

13891389
if ((error = index_entry_dup_pool(entry, &diff_list->pool, tree_items[0])) >= 0)

src/pool.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ void git_pool_clear(git_pool *pool)
5151
}
5252

5353
pool->pages = NULL;
54-
pool->items = 0;
5554
}
5655

5756
void git_pool_swap(git_pool *a, git_pool *b)
@@ -73,23 +72,20 @@ static void *pool_alloc_page(git_pool *pool, uint32_t size)
7372
size_t alloc_size;
7473

7574
if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) ||
76-
!(page = git__calloc(1, alloc_size)))
75+
!(page = git__malloc(alloc_size)))
7776
return NULL;
7877

7978
page->size = new_page_size;
8079
page->avail = new_page_size - size;
8180
page->next = pool->pages;
8281

8382
pool->pages = page;
84-
pool->items++;
8583

8684
return page->data;
8785
}
8886

89-
void *git_pool_malloc(git_pool *pool, uint32_t items)
87+
static void *pool_alloc(git_pool *pool, uint32_t size)
9088
{
91-
const uint32_t size = items * pool->item_size;
92-
9389
git_pool_page *page = pool->pages;
9490
void *ptr = NULL;
9591

@@ -98,11 +94,25 @@ void *git_pool_malloc(git_pool *pool, uint32_t items)
9894

9995
ptr = &page->data[page->size - page->avail];
10096
page->avail -= size;
101-
pool->items++;
10297

10398
return ptr;
10499
}
105100

101+
void *git_pool_malloc(git_pool *pool, uint32_t items)
102+
{
103+
const uint32_t size = items * pool->item_size;
104+
return pool_alloc(pool, size);
105+
}
106+
107+
void *git_pool_mallocz(git_pool *pool, uint32_t items)
108+
{
109+
const uint32_t size = items * pool->item_size;
110+
void *ptr = pool_alloc(pool, size);
111+
if (ptr)
112+
memset(ptr, 0x0, size);
113+
return ptr;
114+
}
115+
106116
char *git_pool_strndup(git_pool *pool, const char *str, size_t n)
107117
{
108118
char *ptr = NULL;

src/pool.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ typedef struct {
3131
git_pool_page *pages; /* pages with space left */
3232
uint32_t item_size; /* size of single alloc unit in bytes */
3333
uint32_t page_size; /* size of page in bytes */
34-
uint32_t items;
3534
} git_pool;
3635

3736
/**
@@ -66,17 +65,7 @@ extern void git_pool_swap(git_pool *a, git_pool *b);
6665
* Allocate space for one or more items from a pool.
6766
*/
6867
extern void *git_pool_malloc(git_pool *pool, uint32_t items);
69-
70-
/**
71-
* Allocate space and zero it out.
72-
*/
73-
GIT_INLINE(void *) git_pool_mallocz(git_pool *pool, uint32_t items)
74-
{
75-
void *ptr = git_pool_malloc(pool, items);
76-
if (ptr)
77-
memset(ptr, 0, (size_t)items * (size_t)pool->item_size);
78-
return ptr;
79-
}
68+
extern void *git_pool_mallocz(git_pool *pool, uint32_t items);
8069

8170
/**
8271
* Allocate space and duplicate string data into it.

0 commit comments

Comments
 (0)