mirror of
https://git.proxmox.com/git/libgit2
synced 2025-07-09 14:36:01 +00:00
pool: Dot not assume mallocs are zeroed out
This commit is contained in:
parent
66eb7660a8
commit
d3416dfe29
@ -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)
|
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)
|
static int commit_error(git_commit_list_node *commit, const char *msg)
|
||||||
|
@ -626,7 +626,7 @@ static int merge_conflict_resolve_one_renamed(
|
|||||||
git_oid__cmp(&conflict->our_entry.id, &conflict->their_entry.id) != 0)
|
git_oid__cmp(&conflict->our_entry.id, &conflict->their_entry.id) != 0)
|
||||||
return 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;
|
return -1;
|
||||||
|
|
||||||
if (ours_changed)
|
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)
|
(error = git_odb_write(&automerge_oid, odb, result.ptr, result.len, GIT_OBJ_BLOB)) < 0)
|
||||||
goto done;
|
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);
|
GITERR_CHECK_ALLOC(index_entry);
|
||||||
|
|
||||||
index_entry->path = git_pool_strdup(&diff_list->pool, result.path);
|
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_merge_diff *conflict;
|
||||||
git_pool *pool = &diff_list->pool;
|
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;
|
return NULL;
|
||||||
|
|
||||||
if (index_entry_dup_pool(&conflict->ancestor_entry, pool, entries[TREE_IDX_ANCESTOR]) < 0 ||
|
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;
|
int error = 0;
|
||||||
git_index_entry *entry;
|
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);
|
GITERR_CHECK_ALLOC(entry);
|
||||||
|
|
||||||
if ((error = index_entry_dup_pool(entry, &diff_list->pool, tree_items[0])) >= 0)
|
if ((error = index_entry_dup_pool(entry, &diff_list->pool, tree_items[0])) >= 0)
|
||||||
|
24
src/pool.c
24
src/pool.c
@ -51,7 +51,6 @@ void git_pool_clear(git_pool *pool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
pool->pages = NULL;
|
pool->pages = NULL;
|
||||||
pool->items = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void git_pool_swap(git_pool *a, git_pool *b)
|
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;
|
size_t alloc_size;
|
||||||
|
|
||||||
if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) ||
|
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;
|
return NULL;
|
||||||
|
|
||||||
page->size = new_page_size;
|
page->size = new_page_size;
|
||||||
@ -81,15 +80,12 @@ static void *pool_alloc_page(git_pool *pool, uint32_t size)
|
|||||||
page->next = pool->pages;
|
page->next = pool->pages;
|
||||||
|
|
||||||
pool->pages = page;
|
pool->pages = page;
|
||||||
pool->items++;
|
|
||||||
|
|
||||||
return page->data;
|
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;
|
git_pool_page *page = pool->pages;
|
||||||
void *ptr = NULL;
|
void *ptr = NULL;
|
||||||
|
|
||||||
@ -98,11 +94,25 @@ void *git_pool_malloc(git_pool *pool, uint32_t items)
|
|||||||
|
|
||||||
ptr = &page->data[page->size - page->avail];
|
ptr = &page->data[page->size - page->avail];
|
||||||
page->avail -= size;
|
page->avail -= size;
|
||||||
pool->items++;
|
|
||||||
|
|
||||||
return ptr;
|
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 *git_pool_strndup(git_pool *pool, const char *str, size_t n)
|
||||||
{
|
{
|
||||||
char *ptr = NULL;
|
char *ptr = NULL;
|
||||||
|
13
src/pool.h
13
src/pool.h
@ -31,7 +31,6 @@ typedef struct {
|
|||||||
git_pool_page *pages; /* pages with space left */
|
git_pool_page *pages; /* pages with space left */
|
||||||
uint32_t item_size; /* size of single alloc unit in bytes */
|
uint32_t item_size; /* size of single alloc unit in bytes */
|
||||||
uint32_t page_size; /* size of page in bytes */
|
uint32_t page_size; /* size of page in bytes */
|
||||||
uint32_t items;
|
|
||||||
} git_pool;
|
} 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.
|
* Allocate space for one or more items from a pool.
|
||||||
*/
|
*/
|
||||||
extern void *git_pool_malloc(git_pool *pool, uint32_t items);
|
extern void *git_pool_malloc(git_pool *pool, uint32_t items);
|
||||||
|
extern void *git_pool_mallocz(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allocate space and duplicate string data into it.
|
* Allocate space and duplicate string data into it.
|
||||||
|
Loading…
Reference in New Issue
Block a user