Skip to content

Commit 2f6b1eb

Browse files
avargitster
authored andcommitted
cache API: add a "INDEX_STATE_INIT" macro/function, add release_index()
Hopefully in some not so distant future, we'll get advantages from always initializing the "repo" member of the "struct index_state". To make that easier let's introduce an initialization macro & function. The various ad-hoc initialization of the structure can then be changed over to it, and we can remove the various "0" assignments in discard_index() in favor of calling index_state_init() at the end. While not strictly necessary, let's also change the CALLOC_ARRAY() of various "struct index_state *" to use an ALLOC_ARRAY() followed by index_state_init() instead. We're then adding the release_index() function and converting some callers (including some of these allocations) over to it if they either won't need to use their "struct index_state" again, or are just about to call index_state_init(). Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Acked-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5bdf6d4 commit 2f6b1eb

File tree

12 files changed

+52
-30
lines changed

12 files changed

+52
-30
lines changed

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4105,7 +4105,7 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
41054105
static int build_fake_ancestor(struct apply_state *state, struct patch *list)
41064106
{
41074107
struct patch *patch;
4108-
struct index_state result = { NULL };
4108+
struct index_state result = INDEX_STATE_INIT;
41094109
struct lock_file lock = LOCK_INIT;
41104110
int res;
41114111

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
361361
struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
362362
struct hashmap_iter iter;
363363
struct pair_entry *entry;
364-
struct index_state wtindex = { 0 };
364+
struct index_state wtindex = INDEX_STATE_INIT;
365365
struct checkout lstate, rstate;
366366
int err = 0;
367367
struct child_process cmd = CHILD_PROCESS_INIT;

builtin/sparse-checkout.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ static int update_working_directory(struct pattern_list *pl)
217217
o.head_idx = -1;
218218
o.src_index = r->index;
219219
o.dst_index = r->index;
220+
index_state_init(&o.result);
220221
o.skip_sparse_checkout = 0;
221222
o.pl = pl;
222223

builtin/stash.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
11371137
int ret = 0;
11381138
struct strbuf untracked_msg = STRBUF_INIT;
11391139
struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1140-
struct index_state istate = { NULL };
1140+
struct index_state istate = INDEX_STATE_INIT;
11411141

11421142
cp_upd_index.git_cmd = 1;
11431143
strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
@@ -1165,7 +1165,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
11651165
}
11661166

11671167
done:
1168-
discard_index(&istate);
1168+
release_index(&istate);
11691169
strbuf_release(&untracked_msg);
11701170
remove_path(stash_index_path.buf);
11711171
return ret;
@@ -1176,7 +1176,7 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
11761176
{
11771177
int ret = 0;
11781178
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1179-
struct index_state istate = { NULL };
1179+
struct index_state istate = INDEX_STATE_INIT;
11801180

11811181
if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,
11821182
0, NULL)) {
@@ -1199,7 +1199,7 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
11991199
}
12001200

12011201
done:
1202-
discard_index(&istate);
1202+
release_index(&istate);
12031203
return ret;
12041204
}
12051205

@@ -1209,7 +1209,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
12091209
int ret = 0;
12101210
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
12111211
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1212-
struct index_state istate = { NULL };
1212+
struct index_state istate = INDEX_STATE_INIT;
12131213
char *old_index_env = NULL, *old_repo_index_file;
12141214

12151215
remove_path(stash_index_path.buf);
@@ -1260,7 +1260,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
12601260
}
12611261

12621262
done:
1263-
discard_index(&istate);
1263+
release_index(&istate);
12641264
remove_path(stash_index_path.buf);
12651265
return ret;
12661266
}
@@ -1271,7 +1271,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
12711271
struct rev_info rev;
12721272
struct child_process cp_upd_index = CHILD_PROCESS_INIT;
12731273
struct strbuf diff_output = STRBUF_INIT;
1274-
struct index_state istate = { NULL };
1274+
struct index_state istate = INDEX_STATE_INIT;
12751275

12761276
init_revisions(&rev, NULL);
12771277
copy_pathspec(&rev.prune_data, ps);
@@ -1319,7 +1319,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
13191319
}
13201320

13211321
done:
1322-
discard_index(&istate);
1322+
release_index(&istate);
13231323
release_revisions(&rev);
13241324
strbuf_release(&diff_output);
13251325
remove_path(stash_index_path.buf);

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
923923

924924
static void validate_no_submodules(const struct worktree *wt)
925925
{
926-
struct index_state istate = { NULL };
926+
struct index_state istate = INDEX_STATE_INIT;
927927
struct strbuf path = STRBUF_INIT;
928928
int i, found_submodules = 0;
929929

cache.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,19 @@ struct index_state {
360360
struct pattern_list *sparse_checkout_patterns;
361361
};
362362

363+
/**
364+
* A "struct index_state istate" must be initialized with
365+
* INDEX_STATE_INIT or the corresponding index_state_init().
366+
*
367+
* If the variable won't be used again, use release_index() to free()
368+
* its resources. If it needs to be used again use discard_index(),
369+
* which does the same thing, but will use use index_state_init() at
370+
* the end.
371+
*/
372+
#define INDEX_STATE_INIT { 0 }
373+
void index_state_init(struct index_state *istate);
374+
void release_index(struct index_state *istate);
375+
363376
/* Name hashing */
364377
int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
365378
void add_name_hash(struct index_state *istate, struct cache_entry *ce);

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ static int unpack_trees_start(struct merge_options *opt,
412412
{
413413
int rc;
414414
struct tree_desc t[3];
415-
struct index_state tmp_index = { NULL };
415+
struct index_state tmp_index = INDEX_STATE_INIT;
416416

417417
memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
418418
if (opt->priv->call_depth)

read-cache.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,9 +2490,10 @@ int read_index_from(struct index_state *istate, const char *path,
24902490

24912491
trace_performance_enter();
24922492
if (split_index->base)
2493-
discard_index(split_index->base);
2493+
release_index(split_index->base);
24942494
else
2495-
CALLOC_ARRAY(split_index->base, 1);
2495+
ALLOC_ARRAY(split_index->base, 1);
2496+
index_state_init(split_index->base);
24962497

24972498
base_oid_hex = oid_to_hex(&split_index->base_oid);
24982499
base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
@@ -2531,7 +2532,13 @@ int is_index_unborn(struct index_state *istate)
25312532
return (!istate->cache_nr && !istate->timestamp.sec);
25322533
}
25332534

2534-
void discard_index(struct index_state *istate)
2535+
void index_state_init(struct index_state *istate)
2536+
{
2537+
struct index_state blank = INDEX_STATE_INIT;
2538+
memcpy(istate, &blank, sizeof(*istate));
2539+
}
2540+
2541+
void release_index(struct index_state *istate)
25352542
{
25362543
/*
25372544
* Cache entries in istate->cache[] should have been allocated
@@ -2543,20 +2550,12 @@ void discard_index(struct index_state *istate)
25432550
validate_cache_entries(istate);
25442551

25452552
resolve_undo_clear_index(istate);
2546-
istate->cache_nr = 0;
2547-
istate->cache_changed = 0;
2548-
istate->timestamp.sec = 0;
2549-
istate->timestamp.nsec = 0;
25502553
free_name_hash(istate);
25512554
cache_tree_free(&(istate->cache_tree));
2552-
istate->initialized = 0;
2553-
istate->fsmonitor_has_run_once = 0;
2554-
FREE_AND_NULL(istate->fsmonitor_last_update);
2555-
FREE_AND_NULL(istate->cache);
2556-
istate->cache_alloc = 0;
2555+
free(istate->fsmonitor_last_update);
2556+
free(istate->cache);
25572557
discard_split_index(istate);
25582558
free_untracked_cache(istate->untracked);
2559-
istate->untracked = NULL;
25602559

25612560
if (istate->sparse_checkout_patterns) {
25622561
clear_pattern_list(istate->sparse_checkout_patterns);
@@ -2569,6 +2568,12 @@ void discard_index(struct index_state *istate)
25692568
}
25702569
}
25712570

2571+
void discard_index(struct index_state *istate)
2572+
{
2573+
release_index(istate);
2574+
index_state_init(istate);
2575+
}
2576+
25722577
/*
25732578
* Validate the cache entries of this index.
25742579
* All cache entries associated with this index

repository.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,10 @@ int repo_read_index(struct repository *repo)
302302
{
303303
int res;
304304

305-
if (!repo->index)
306-
CALLOC_ARRAY(repo->index, 1);
305+
if (!repo->index) {
306+
ALLOC_ARRAY(repo->index, 1);
307+
index_state_init(repo->index);
308+
}
307309

308310
/* Complete the double-reference */
309311
if (!repo->index->repo)

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
18131813
worktrees = get_worktrees();
18141814
for (p = worktrees; *p; p++) {
18151815
struct worktree *wt = *p;
1816-
struct index_state istate = { NULL };
1816+
struct index_state istate = INDEX_STATE_INIT;
18171817

18181818
if (wt->is_current)
18191819
continue; /* current index already taken care of */

split-index.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ void move_cache_to_base_index(struct index_state *istate)
9090
mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
9191
}
9292

93-
CALLOC_ARRAY(si->base, 1);
93+
ALLOC_ARRAY(si->base, 1);
94+
index_state_init(si->base);
9495
si->base->version = istate->version;
9596
/* zero timestamp disables racy test in ce_write_index() */
9697
si->base->timestamp = istate->timestamp;

unpack-trees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
19051905
populate_from_existing_patterns(o, &pl);
19061906
}
19071907

1908-
memset(&o->result, 0, sizeof(o->result));
1908+
index_state_init(&o->result);
19091909
o->result.initialized = 1;
19101910
o->result.timestamp.sec = o->src_index->timestamp.sec;
19111911
o->result.timestamp.nsec = o->src_index->timestamp.nsec;

0 commit comments

Comments
 (0)