Skip to content

Commit e52dc5c

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 53edde6 + 2435d26 commit e52dc5c

File tree

5 files changed

+70
-52
lines changed

5 files changed

+70
-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: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
* `insert_after`. If `insert_after` is NULL, then insert block at the
1313
* head of the linked list.
1414
*/
15-
static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t block_alloc, struct mp_block *insert_after)
15+
static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool,
16+
size_t block_alloc,
17+
struct mp_block *insert_after)
1618
{
1719
struct mp_block *p;
1820

19-
mem_pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
21+
pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
2022
p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
2123

2224
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
2628
p->next_block = insert_after->next_block;
2729
insert_after->next_block = p;
2830
} else {
29-
p->next_block = mem_pool->mp_block;
30-
mem_pool->mp_block = p;
31+
p->next_block = pool->mp_block;
32+
pool->mp_block = p;
3133
}
3234

3335
return p;
3436
}
3537

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

4743
if (initial_size > 0)
4844
mem_pool_alloc_block(pool, initial_size, NULL);
49-
50-
*mem_pool = pool;
5145
}
5246

53-
void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory)
47+
void mem_pool_discard(struct mem_pool *pool, int invalidate_memory)
5448
{
5549
struct mp_block *block, *block_to_free;
5650

57-
block = mem_pool->mp_block;
51+
block = pool->mp_block;
5852
while (block)
5953
{
6054
block_to_free = block;
@@ -66,10 +60,11 @@ void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory)
6660
free(block_to_free);
6761
}
6862

69-
free(mem_pool);
63+
pool->mp_block = NULL;
64+
pool->pool_alloc = 0;
7065
}
7166

72-
void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
67+
void *mem_pool_alloc(struct mem_pool *pool, size_t len)
7368
{
7469
struct mp_block *p = NULL;
7570
void *r;
@@ -78,36 +73,54 @@ void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
7873
if (len & (sizeof(uintmax_t) - 1))
7974
len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
8075

81-
if (mem_pool->mp_block &&
82-
mem_pool->mp_block->end - mem_pool->mp_block->next_free >= len)
83-
p = mem_pool->mp_block;
76+
if (pool->mp_block &&
77+
pool->mp_block->end - pool->mp_block->next_free >= len)
78+
p = pool->mp_block;
8479

8580
if (!p) {
86-
if (len >= (mem_pool->block_alloc / 2))
87-
return mem_pool_alloc_block(mem_pool, len, mem_pool->mp_block);
81+
if (len >= (pool->block_alloc / 2))
82+
return mem_pool_alloc_block(pool, len, pool->mp_block);
8883

89-
p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc, NULL);
84+
p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
9085
}
9186

9287
r = p->next_free;
9388
p->next_free += len;
9489
return r;
9590
}
9691

97-
void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
92+
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
9893
{
9994
size_t len = st_mult(count, size);
100-
void *r = mem_pool_alloc(mem_pool, len);
95+
void *r = mem_pool_alloc(pool, len);
10196
memset(r, 0, len);
10297
return r;
10398
}
10499

105-
int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
100+
char *mem_pool_strdup(struct mem_pool *pool, const char *str)
101+
{
102+
size_t len = strlen(str) + 1;
103+
char *ret = mem_pool_alloc(pool, len);
104+
105+
return memcpy(ret, str, len);
106+
}
107+
108+
char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
109+
{
110+
char *p = memchr(str, '\0', len);
111+
size_t actual_len = (p ? p - str : len);
112+
char *ret = mem_pool_alloc(pool, actual_len+1);
113+
114+
ret[actual_len] = '\0';
115+
return memcpy(ret, str, actual_len);
116+
}
117+
118+
int mem_pool_contains(struct mem_pool *pool, void *mem)
106119
{
107120
struct mp_block *p;
108121

109122
/* Check if memory is allocated in a block */
110-
for (p = mem_pool->mp_block; p; p = p->next_block)
123+
for (p = pool->mp_block; p; p = p->next_block)
111124
if ((mem >= ((void *)p->space)) &&
112125
(mem < ((void *)p->end)))
113126
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
}
@@ -2020,11 +2022,12 @@ static unsigned long load_all_cache_entries(struct index_state *istate,
20202022
{
20212023
unsigned long consumed;
20222024

2025+
istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
20232026
if (istate->version == 4) {
2024-
mem_pool_init(&istate->ce_mem_pool,
2027+
mem_pool_init(istate->ce_mem_pool,
20252028
estimate_cache_size_from_compressed(istate->cache_nr));
20262029
} else {
2027-
mem_pool_init(&istate->ce_mem_pool,
2030+
mem_pool_init(istate->ce_mem_pool,
20282031
estimate_cache_size(mmap_size, istate->cache_nr));
20292032
}
20302033

@@ -2084,7 +2087,8 @@ static unsigned long load_cache_entries_threaded(struct index_state *istate, con
20842087
if (istate->name_hash_initialized)
20852088
BUG("the name hash isn't thread safe");
20862089

2087-
mem_pool_init(&istate->ce_mem_pool, 0);
2090+
istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
2091+
mem_pool_init(istate->ce_mem_pool, 0);
20882092

20892093
/* ensure we have no more threads than we have blocks to process */
20902094
if (nr_threads > ieot->nr)
@@ -2111,11 +2115,12 @@ static unsigned long load_cache_entries_threaded(struct index_state *istate, con
21112115
nr = 0;
21122116
for (j = p->ieot_start; j < p->ieot_start + p->ieot_blocks; j++)
21132117
nr += p->ieot->entries[j].nr;
2118+
istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
21142119
if (istate->version == 4) {
2115-
mem_pool_init(&p->ce_mem_pool,
2120+
mem_pool_init(p->ce_mem_pool,
21162121
estimate_cache_size_from_compressed(nr));
21172122
} else {
2118-
mem_pool_init(&p->ce_mem_pool,
2123+
mem_pool_init(p->ce_mem_pool,
21192124
estimate_cache_size(mmap_size, nr));
21202125
}
21212126

@@ -2372,7 +2377,7 @@ int discard_index(struct index_state *istate)
23722377

23732378
if (istate->ce_mem_pool) {
23742379
mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries());
2375-
istate->ce_mem_pool = NULL;
2380+
FREE_AND_NULL(istate->ce_mem_pool);
23762381
}
23772382

23782383
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)