Merge branch 'en/mem-pool'
API update. * en/mem-pool: mem-pool: use consistent pool variable name mem-pool: use more standard initialization and finalization mem-pool: add convenience functions for strdup and strndup
This commit is contained in:
commit
edab8a8d07
@ -526,14 +526,6 @@ static unsigned int hc_str(const char *s, size_t len)
|
||||
return r;
|
||||
}
|
||||
|
||||
static char *pool_strdup(const char *s)
|
||||
{
|
||||
size_t len = strlen(s) + 1;
|
||||
char *r = mem_pool_alloc(&fi_mem_pool, len);
|
||||
memcpy(r, s, len);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void insert_mark(struct mark_set *s, uintmax_t idnum, struct object_entry *oe)
|
||||
{
|
||||
while ((idnum >> s->shift) >= 1024) {
|
||||
@ -615,7 +607,7 @@ static struct branch *new_branch(const char *name)
|
||||
die("Branch name doesn't conform to GIT standards: %s", name);
|
||||
|
||||
b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch));
|
||||
b->name = pool_strdup(name);
|
||||
b->name = mem_pool_strdup(&fi_mem_pool, name);
|
||||
b->table_next_branch = branch_table[hc];
|
||||
b->branch_tree.versions[0].mode = S_IFDIR;
|
||||
b->branch_tree.versions[1].mode = S_IFDIR;
|
||||
@ -2806,7 +2798,7 @@ static void parse_new_tag(const char *arg)
|
||||
|
||||
t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
|
||||
memset(t, 0, sizeof(struct tag));
|
||||
t->name = pool_strdup(arg);
|
||||
t->name = mem_pool_strdup(&fi_mem_pool, arg);
|
||||
if (last_tag)
|
||||
last_tag->next_tag = t;
|
||||
else
|
||||
|
69
mem-pool.c
69
mem-pool.c
@ -12,11 +12,13 @@
|
||||
* `insert_after`. If `insert_after` is NULL, then insert block at the
|
||||
* head of the linked list.
|
||||
*/
|
||||
static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t block_alloc, struct mp_block *insert_after)
|
||||
static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool,
|
||||
size_t block_alloc,
|
||||
struct mp_block *insert_after)
|
||||
{
|
||||
struct mp_block *p;
|
||||
|
||||
mem_pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
|
||||
pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
|
||||
p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
|
||||
|
||||
p->next_free = (char *)p->space;
|
||||
@ -26,35 +28,27 @@ static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t b
|
||||
p->next_block = insert_after->next_block;
|
||||
insert_after->next_block = p;
|
||||
} else {
|
||||
p->next_block = mem_pool->mp_block;
|
||||
mem_pool->mp_block = p;
|
||||
p->next_block = pool->mp_block;
|
||||
pool->mp_block = p;
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size)
|
||||
void mem_pool_init(struct mem_pool *pool, size_t initial_size)
|
||||
{
|
||||
struct mem_pool *pool;
|
||||
|
||||
if (*mem_pool)
|
||||
return;
|
||||
|
||||
pool = xcalloc(1, sizeof(*pool));
|
||||
|
||||
memset(pool, 0, sizeof(*pool));
|
||||
pool->block_alloc = BLOCK_GROWTH_SIZE;
|
||||
|
||||
if (initial_size > 0)
|
||||
mem_pool_alloc_block(pool, initial_size, NULL);
|
||||
|
||||
*mem_pool = pool;
|
||||
}
|
||||
|
||||
void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory)
|
||||
void mem_pool_discard(struct mem_pool *pool, int invalidate_memory)
|
||||
{
|
||||
struct mp_block *block, *block_to_free;
|
||||
|
||||
block = mem_pool->mp_block;
|
||||
block = pool->mp_block;
|
||||
while (block)
|
||||
{
|
||||
block_to_free = block;
|
||||
@ -66,10 +60,11 @@ void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory)
|
||||
free(block_to_free);
|
||||
}
|
||||
|
||||
free(mem_pool);
|
||||
pool->mp_block = NULL;
|
||||
pool->pool_alloc = 0;
|
||||
}
|
||||
|
||||
void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
|
||||
void *mem_pool_alloc(struct mem_pool *pool, size_t len)
|
||||
{
|
||||
struct mp_block *p = NULL;
|
||||
void *r;
|
||||
@ -78,15 +73,15 @@ void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
|
||||
if (len & (sizeof(uintmax_t) - 1))
|
||||
len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
|
||||
|
||||
if (mem_pool->mp_block &&
|
||||
mem_pool->mp_block->end - mem_pool->mp_block->next_free >= len)
|
||||
p = mem_pool->mp_block;
|
||||
if (pool->mp_block &&
|
||||
pool->mp_block->end - pool->mp_block->next_free >= len)
|
||||
p = pool->mp_block;
|
||||
|
||||
if (!p) {
|
||||
if (len >= (mem_pool->block_alloc / 2))
|
||||
return mem_pool_alloc_block(mem_pool, len, mem_pool->mp_block);
|
||||
if (len >= (pool->block_alloc / 2))
|
||||
return mem_pool_alloc_block(pool, len, pool->mp_block);
|
||||
|
||||
p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc, NULL);
|
||||
p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
|
||||
}
|
||||
|
||||
r = p->next_free;
|
||||
@ -94,20 +89,38 @@ void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
|
||||
return r;
|
||||
}
|
||||
|
||||
void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
|
||||
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
|
||||
{
|
||||
size_t len = st_mult(count, size);
|
||||
void *r = mem_pool_alloc(mem_pool, len);
|
||||
void *r = mem_pool_alloc(pool, len);
|
||||
memset(r, 0, len);
|
||||
return r;
|
||||
}
|
||||
|
||||
int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
|
||||
char *mem_pool_strdup(struct mem_pool *pool, const char *str)
|
||||
{
|
||||
size_t len = strlen(str) + 1;
|
||||
char *ret = mem_pool_alloc(pool, len);
|
||||
|
||||
return memcpy(ret, str, len);
|
||||
}
|
||||
|
||||
char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
|
||||
{
|
||||
char *p = memchr(str, '\0', len);
|
||||
size_t actual_len = (p ? p - str : len);
|
||||
char *ret = mem_pool_alloc(pool, actual_len+1);
|
||||
|
||||
ret[actual_len] = '\0';
|
||||
return memcpy(ret, str, actual_len);
|
||||
}
|
||||
|
||||
int mem_pool_contains(struct mem_pool *pool, void *mem)
|
||||
{
|
||||
struct mp_block *p;
|
||||
|
||||
/* Check if memory is allocated in a block */
|
||||
for (p = mem_pool->mp_block; p; p = p->next_block)
|
||||
for (p = pool->mp_block; p; p = p->next_block)
|
||||
if ((mem >= ((void *)p->space)) &&
|
||||
(mem < ((void *)p->end)))
|
||||
return 1;
|
||||
|
14
mem-pool.h
14
mem-pool.h
@ -24,12 +24,12 @@ struct mem_pool {
|
||||
/*
|
||||
* Initialize mem_pool with specified initial size.
|
||||
*/
|
||||
void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size);
|
||||
void mem_pool_init(struct mem_pool *pool, size_t initial_size);
|
||||
|
||||
/*
|
||||
* Discard a memory pool and free all the memory it is responsible for.
|
||||
* Discard all the memory the memory pool is responsible for.
|
||||
*/
|
||||
void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory);
|
||||
void mem_pool_discard(struct mem_pool *pool, int invalidate_memory);
|
||||
|
||||
/*
|
||||
* Alloc memory from the mem_pool.
|
||||
@ -41,6 +41,12 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len);
|
||||
*/
|
||||
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
|
||||
|
||||
/*
|
||||
* Allocate memory from the memory pool and copy str into it.
|
||||
*/
|
||||
char *mem_pool_strdup(struct mem_pool *pool, const char *str);
|
||||
char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len);
|
||||
|
||||
/*
|
||||
* Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
|
||||
* pool will be empty and not contain any memory. It still needs to be free'd
|
||||
@ -52,6 +58,6 @@ void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src);
|
||||
* Check if a memory pointed at by 'mem' is part of the range of
|
||||
* memory managed by the specified mem_pool.
|
||||
*/
|
||||
int mem_pool_contains(struct mem_pool *mem_pool, void *mem);
|
||||
int mem_pool_contains(struct mem_pool *pool, void *mem);
|
||||
|
||||
#endif
|
||||
|
21
read-cache.c
21
read-cache.c
@ -89,8 +89,10 @@ static struct mem_pool *find_mem_pool(struct index_state *istate)
|
||||
else
|
||||
pool_ptr = &istate->ce_mem_pool;
|
||||
|
||||
if (!*pool_ptr)
|
||||
mem_pool_init(pool_ptr, 0);
|
||||
if (!*pool_ptr) {
|
||||
*pool_ptr = xmalloc(sizeof(**pool_ptr));
|
||||
mem_pool_init(*pool_ptr, 0);
|
||||
}
|
||||
|
||||
return *pool_ptr;
|
||||
}
|
||||
@ -2006,11 +2008,12 @@ static unsigned long load_all_cache_entries(struct index_state *istate,
|
||||
{
|
||||
unsigned long consumed;
|
||||
|
||||
istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
|
||||
if (istate->version == 4) {
|
||||
mem_pool_init(&istate->ce_mem_pool,
|
||||
mem_pool_init(istate->ce_mem_pool,
|
||||
estimate_cache_size_from_compressed(istate->cache_nr));
|
||||
} else {
|
||||
mem_pool_init(&istate->ce_mem_pool,
|
||||
mem_pool_init(istate->ce_mem_pool,
|
||||
estimate_cache_size(mmap_size, istate->cache_nr));
|
||||
}
|
||||
|
||||
@ -2070,7 +2073,8 @@ static unsigned long load_cache_entries_threaded(struct index_state *istate, con
|
||||
if (istate->name_hash_initialized)
|
||||
BUG("the name hash isn't thread safe");
|
||||
|
||||
mem_pool_init(&istate->ce_mem_pool, 0);
|
||||
istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
|
||||
mem_pool_init(istate->ce_mem_pool, 0);
|
||||
|
||||
/* ensure we have no more threads than we have blocks to process */
|
||||
if (nr_threads > ieot->nr)
|
||||
@ -2097,11 +2101,12 @@ static unsigned long load_cache_entries_threaded(struct index_state *istate, con
|
||||
nr = 0;
|
||||
for (j = p->ieot_start; j < p->ieot_start + p->ieot_blocks; j++)
|
||||
nr += p->ieot->entries[j].nr;
|
||||
istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
|
||||
if (istate->version == 4) {
|
||||
mem_pool_init(&p->ce_mem_pool,
|
||||
mem_pool_init(p->ce_mem_pool,
|
||||
estimate_cache_size_from_compressed(nr));
|
||||
} else {
|
||||
mem_pool_init(&p->ce_mem_pool,
|
||||
mem_pool_init(p->ce_mem_pool,
|
||||
estimate_cache_size(mmap_size, nr));
|
||||
}
|
||||
|
||||
@ -2358,7 +2363,7 @@ int discard_index(struct index_state *istate)
|
||||
|
||||
if (istate->ce_mem_pool) {
|
||||
mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries());
|
||||
istate->ce_mem_pool = NULL;
|
||||
FREE_AND_NULL(istate->ce_mem_pool);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -79,8 +79,10 @@ void move_cache_to_base_index(struct index_state *istate)
|
||||
if (si->base &&
|
||||
si->base->ce_mem_pool) {
|
||||
|
||||
if (!istate->ce_mem_pool)
|
||||
mem_pool_init(&istate->ce_mem_pool, 0);
|
||||
if (!istate->ce_mem_pool) {
|
||||
istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool));
|
||||
mem_pool_init(istate->ce_mem_pool, 0);
|
||||
}
|
||||
|
||||
mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user