Skip to content

Commit 29be755

Browse files
committed
Merge branch 'en/mem-pool'
This merges the `mem_pool` changes (backported on top of v2.28.0) into Git for Windows' `main` branch early, i.e. before v2.29.0-rc0 is available, to be able to adjust our `fscache.c` early (which would otherwise conflict). Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 6d05883 + 30373b6 commit 29be755

File tree

5 files changed

+71
-52
lines changed

5 files changed

+71
-52
lines changed

fast-import.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -526,14 +526,6 @@ static unsigned int hc_str(const char *s, size_t len)
526526
return r;
527527
}
528528

529-
static char *pool_strdup(const char *s)
530-
{
531-
size_t len = strlen(s) + 1;
532-
char *r = mem_pool_alloc(&fi_mem_pool, len);
533-
memcpy(r, s, len);
534-
return r;
535-
}
536-
537529
static void insert_mark(struct mark_set *s, uintmax_t idnum, struct object_entry *oe)
538530
{
539531
while ((idnum >> s->shift) >= 1024) {
@@ -615,7 +607,7 @@ static struct branch *new_branch(const char *name)
615607
die("Branch name doesn't conform to GIT standards: %s", name);
616608

617609
b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch));
618-
b->name = pool_strdup(name);
610+
b->name = mem_pool_strdup(&fi_mem_pool, name);
619611
b->table_next_branch = branch_table[hc];
620612
b->branch_tree.versions[0].mode = S_IFDIR;
621613
b->branch_tree.versions[1].mode = S_IFDIR;
@@ -2806,7 +2798,7 @@ static void parse_new_tag(const char *arg)
28062798

28072799
t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
28082800
memset(t, 0, sizeof(struct tag));
2809-
t->name = pool_strdup(arg);
2801+
t->name = mem_pool_strdup(&fi_mem_pool, arg);
28102802
if (last_tag)
28112803
last_tag->next_tag = t;
28122804
else

mem-pool.c

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ static struct trace_key trace_mem_pool = TRACE_KEY_INIT(MEMPOOL);
1313
* `insert_after`. If `insert_after` is NULL, then insert block at the
1414
* head of the linked list.
1515
*/
16-
static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t block_alloc, struct mp_block *insert_after)
16+
static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool,
17+
size_t block_alloc,
18+
struct mp_block *insert_after)
1719
{
1820
struct mp_block *p;
1921

20-
mem_pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
22+
pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
2123
p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
2224

2325
p->next_free = (char *)p->space;
@@ -27,39 +29,32 @@ static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t b
2729
p->next_block = insert_after->next_block;
2830
insert_after->next_block = p;
2931
} else {
30-
p->next_block = mem_pool->mp_block;
31-
mem_pool->mp_block = p;
32+
p->next_block = pool->mp_block;
33+
pool->mp_block = p;
3234
}
3335

3436
return p;
3537
}
3638

37-
void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size)
39+
void mem_pool_init(struct mem_pool *pool, size_t initial_size)
3840
{
39-
struct mem_pool *pool;
40-
41-
if (*mem_pool)
42-
return;
43-
44-
pool = xcalloc(1, sizeof(*pool));
45-
41+
memset(pool, 0, sizeof(*pool));
4642
pool->block_alloc = BLOCK_GROWTH_SIZE;
4743

4844
if (initial_size > 0)
4945
mem_pool_alloc_block(pool, initial_size, NULL);
5046

51-
*mem_pool = pool;
5247
trace_printf_key(&trace_mem_pool, "mem_pool (%p): init (%"PRIuMAX") initial size\n",
5348
pool, (uintmax_t)initial_size);
5449
}
5550

56-
void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory)
51+
void mem_pool_discard(struct mem_pool *pool, int invalidate_memory)
5752
{
5853
struct mp_block *block, *block_to_free;
5954

6055
trace_printf_key(&trace_mem_pool, "mem_pool (%p): discard (%"PRIuMAX") unused\n",
61-
mem_pool, (uintmax_t)(mem_pool->mp_block->end - mem_pool->mp_block->next_free));
62-
block = mem_pool->mp_block;
56+
pool, (uintmax_t)(pool->mp_block->end - pool->mp_block->next_free));
57+
block = pool->mp_block;
6358
while (block)
6459
{
6560
block_to_free = block;
@@ -71,10 +66,11 @@ void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory)
7166
free(block_to_free);
7267
}
7368

74-
free(mem_pool);
69+
pool->mp_block = NULL;
70+
pool->pool_alloc = 0;
7571
}
7672

77-
void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
73+
void *mem_pool_alloc(struct mem_pool *pool, size_t len)
7874
{
7975
struct mp_block *p = NULL;
8076
void *r;
@@ -83,36 +79,54 @@ void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
8379
if (len & (sizeof(uintmax_t) - 1))
8480
len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
8581

86-
if (mem_pool->mp_block &&
87-
mem_pool->mp_block->end - mem_pool->mp_block->next_free >= len)
88-
p = mem_pool->mp_block;
82+
if (pool->mp_block &&
83+
pool->mp_block->end - pool->mp_block->next_free >= len)
84+
p = pool->mp_block;
8985

9086
if (!p) {
91-
if (len >= (mem_pool->block_alloc / 2))
92-
return mem_pool_alloc_block(mem_pool, len, mem_pool->mp_block);
87+
if (len >= (pool->block_alloc / 2))
88+
return mem_pool_alloc_block(pool, len, pool->mp_block);
9389

94-
p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc, NULL);
90+
p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
9591
}
9692

9793
r = p->next_free;
9894
p->next_free += len;
9995
return r;
10096
}
10197

102-
void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
98+
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
10399
{
104100
size_t len = st_mult(count, size);
105-
void *r = mem_pool_alloc(mem_pool, len);
101+
void *r = mem_pool_alloc(pool, len);
106102
memset(r, 0, len);
107103
return r;
108104
}
109105

110-
int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
106+
char *mem_pool_strdup(struct mem_pool *pool, const char *str)
107+
{
108+
size_t len = strlen(str) + 1;
109+
char *ret = mem_pool_alloc(pool, len);
110+
111+
return memcpy(ret, str, len);
112+
}
113+
114+
char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
115+
{
116+
char *p = memchr(str, '\0', len);
117+
size_t actual_len = (p ? p - str : len);
118+
char *ret = mem_pool_alloc(pool, actual_len+1);
119+
120+
ret[actual_len] = '\0';
121+
return memcpy(ret, str, actual_len);
122+
}
123+
124+
int mem_pool_contains(struct mem_pool *pool, void *mem)
111125
{
112126
struct mp_block *p;
113127

114128
/* Check if memory is allocated in a block */
115-
for (p = mem_pool->mp_block; p; p = p->next_block)
129+
for (p = pool->mp_block; p; p = p->next_block)
116130
if ((mem >= ((void *)p->space)) &&
117131
(mem < ((void *)p->end)))
118132
return 1;

mem-pool.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ struct mem_pool {
2424
/*
2525
* Initialize mem_pool with specified initial size.
2626
*/
27-
void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size);
27+
void mem_pool_init(struct mem_pool *pool, size_t initial_size);
2828

2929
/*
30-
* Discard a memory pool and free all the memory it is responsible for.
30+
* Discard all the memory the memory pool is responsible for.
3131
*/
32-
void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory);
32+
void mem_pool_discard(struct mem_pool *pool, int invalidate_memory);
3333

3434
/*
3535
* Alloc memory from the mem_pool.
@@ -41,6 +41,12 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len);
4141
*/
4242
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
4343

44+
/*
45+
* Allocate memory from the memory pool and copy str into it.
46+
*/
47+
char *mem_pool_strdup(struct mem_pool *pool, const char *str);
48+
char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len);
49+
4450
/*
4551
* Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
4652
* 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);
5258
* Check if a memory pointed at by 'mem' is part of the range of
5359
* memory managed by the specified mem_pool.
5460
*/
55-
int mem_pool_contains(struct mem_pool *mem_pool, void *mem);
61+
int mem_pool_contains(struct mem_pool *pool, void *mem);
5662

5763
#endif

read-cache.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ static struct mem_pool *find_mem_pool(struct index_state *istate)
8989
else
9090
pool_ptr = &istate->ce_mem_pool;
9191

92-
if (!*pool_ptr)
93-
mem_pool_init(pool_ptr, 0);
92+
if (!*pool_ptr) {
93+
*pool_ptr = xmalloc(sizeof(**pool_ptr));
94+
mem_pool_init(*pool_ptr, 0);
95+
}
9496

9597
return *pool_ptr;
9698
}
@@ -2033,11 +2035,12 @@ static unsigned long load_all_cache_entries(struct index_state *istate,
20332035
{
20342036
unsigned long consumed;
20352037

2038+
istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
20362039
if (istate->version == 4) {
2037-
mem_pool_init(&istate->ce_mem_pool,
2040+
mem_pool_init(istate->ce_mem_pool,
20382041
estimate_cache_size_from_compressed(istate->cache_nr));
20392042
} else {
2040-
mem_pool_init(&istate->ce_mem_pool,
2043+
mem_pool_init(istate->ce_mem_pool,
20412044
estimate_cache_size(mmap_size, istate->cache_nr));
20422045
}
20432046

@@ -2097,7 +2100,8 @@ static unsigned long load_cache_entries_threaded(struct index_state *istate, con
20972100
if (istate->name_hash_initialized)
20982101
BUG("the name hash isn't thread safe");
20992102

2100-
mem_pool_init(&istate->ce_mem_pool, 0);
2103+
istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
2104+
mem_pool_init(istate->ce_mem_pool, 0);
21012105

21022106
/* ensure we have no more threads than we have blocks to process */
21032107
if (nr_threads > ieot->nr)
@@ -2124,11 +2128,12 @@ static unsigned long load_cache_entries_threaded(struct index_state *istate, con
21242128
nr = 0;
21252129
for (j = p->ieot_start; j < p->ieot_start + p->ieot_blocks; j++)
21262130
nr += p->ieot->entries[j].nr;
2131+
istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
21272132
if (istate->version == 4) {
2128-
mem_pool_init(&p->ce_mem_pool,
2133+
mem_pool_init(p->ce_mem_pool,
21292134
estimate_cache_size_from_compressed(nr));
21302135
} else {
2131-
mem_pool_init(&p->ce_mem_pool,
2136+
mem_pool_init(p->ce_mem_pool,
21322137
estimate_cache_size(mmap_size, nr));
21332138
}
21342139

@@ -2385,7 +2390,7 @@ int discard_index(struct index_state *istate)
23852390

23862391
if (istate->ce_mem_pool) {
23872392
mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries());
2388-
istate->ce_mem_pool = NULL;
2393+
FREE_AND_NULL(istate->ce_mem_pool);
23892394
}
23902395

23912396
return 0;

split-index.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ void move_cache_to_base_index(struct index_state *istate)
7979
if (si->base &&
8080
si->base->ce_mem_pool) {
8181

82-
if (!istate->ce_mem_pool)
83-
mem_pool_init(&istate->ce_mem_pool, 0);
82+
if (!istate->ce_mem_pool) {
83+
istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool));
84+
mem_pool_init(istate->ce_mem_pool, 0);
85+
}
8486

8587
mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
8688
}

0 commit comments

Comments
 (0)