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 <avarab@gmail.com> Acked-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jan 12, 2023 at 13:55 UTC 2f6b1eb794ee1f152c1a4b519e3b6dcecd0b487f
12 files changed +52 -30
apply.c
+1 -1
@@ -4105,7 +4105,7 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
4105 static int build_fake_ancestor(struct apply_state *state, struct patch *list)
4106 {
4107 struct patch *patch;
4108 - struct index_state result = { NULL };
4108 + struct index_state result = INDEX_STATE_INIT;
4109 struct lock_file lock = LOCK_INIT;
4110 int res;
4111
builtin/difftool.c
+1 -1
@@ -361,7 +361,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
361 struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
362 struct hashmap_iter iter;
363 struct pair_entry *entry;
364 - struct index_state wtindex = { 0 };
364 + struct index_state wtindex = INDEX_STATE_INIT;
365 struct checkout lstate, rstate;
366 int err = 0;
367 struct child_process cmd = CHILD_PROCESS_INIT;
builtin/sparse-checkout.c
+1
@@ -217,6 +217,7 @@ static int update_working_directory(struct pattern_list *pl)
217 o.head_idx = -1;
218 o.src_index = r->index;
219 o.dst_index = r->index;
220 + index_state_init(&o.result);
221 o.skip_sparse_checkout = 0;
222 o.pl = pl;
223
builtin/stash.c
+8 -8
@@ -1137,7 +1137,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1137 int ret = 0;
1138 struct strbuf untracked_msg = STRBUF_INIT;
1139 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1140 - struct index_state istate = { NULL };
1140 + struct index_state istate = INDEX_STATE_INIT;
1141
1142 cp_upd_index.git_cmd = 1;
1143 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,
1165 }
1166
1167 done:
1168 - discard_index(&istate);
1168 + release_index(&istate);
1169 strbuf_release(&untracked_msg);
1170 remove_path(stash_index_path.buf);
1171 return ret;
@@ -1176,7 +1176,7 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
1176 {
1177 int ret = 0;
1178 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1179 - struct index_state istate = { NULL };
1179 + struct index_state istate = INDEX_STATE_INIT;
1180
1181 if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,
1182 0, NULL)) {
@@ -1199,7 +1199,7 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
1199 }
1200
1201 done:
1202 - discard_index(&istate);
1202 + release_index(&istate);
1203 return ret;
1204 }
1205
@@ -1209,7 +1209,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
1209 int ret = 0;
1210 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1211 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1212 - struct index_state istate = { NULL };
1212 + struct index_state istate = INDEX_STATE_INIT;
1213 char *old_index_env = NULL, *old_repo_index_file;
1214
1215 remove_path(stash_index_path.buf);
@@ -1260,7 +1260,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
1260 }
1261
1262 done:
1263 - discard_index(&istate);
1263 + release_index(&istate);
1264 remove_path(stash_index_path.buf);
1265 return ret;
1266 }
@@ -1271,7 +1271,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
1271 struct rev_info rev;
1272 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1273 struct strbuf diff_output = STRBUF_INIT;
1274 - struct index_state istate = { NULL };
1274 + struct index_state istate = INDEX_STATE_INIT;
1275
1276 init_revisions(&rev, NULL);
1277 copy_pathspec(&rev.prune_data, ps);
@@ -1319,7 +1319,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
1319 }
1320
1321 done:
1322 - discard_index(&istate);
1322 + release_index(&istate);
1323 release_revisions(&rev);
1324 strbuf_release(&diff_output);
1325 remove_path(stash_index_path.buf);
builtin/worktree.c
+1 -1
@@ -923,7 +923,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
923
924 static void validate_no_submodules(const struct worktree *wt)
925 {
926 - struct index_state istate = { NULL };
926 + struct index_state istate = INDEX_STATE_INIT;
927 struct strbuf path = STRBUF_INIT;
928 int i, found_submodules = 0;
929
cache.h
+13
@@ -360,6 +360,19 @@ struct index_state {
360 struct pattern_list *sparse_checkout_patterns;
361 };
362
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 +
376 /* Name hashing */
377 int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
378 void add_name_hash(struct index_state *istate, struct cache_entry *ce);
merge-recursive.c
+1 -1
@@ -412,7 +412,7 @@ static int unpack_trees_start(struct merge_options *opt,
412 {
413 int rc;
414 struct tree_desc t[3];
415 - struct index_state tmp_index = { NULL };
415 + struct index_state tmp_index = INDEX_STATE_INIT;
416
417 memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
418 if (opt->priv->call_depth)
read-cache.c
+18 -13
@@ -2490,9 +2490,10 @@ int read_index_from(struct index_state *istate, const char *path,
2490
2491 trace_performance_enter();
2492 if (split_index->base)
2493 - discard_index(split_index->base);
2493 + release_index(split_index->base);
2494 else
2495 - CALLOC_ARRAY(split_index->base, 1);
2495 + ALLOC_ARRAY(split_index->base, 1);
2496 + index_state_init(split_index->base);
2497
2498 base_oid_hex = oid_to_hex(&split_index->base_oid);
2499 base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
@@ -2531,7 +2532,13 @@ int is_index_unborn(struct index_state *istate)
2532 return (!istate->cache_nr && !istate->timestamp.sec);
2533 }
2534
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)
2542 {
2543 /*
2544 * Cache entries in istate->cache[] should have been allocated
@@ -2543,20 +2550,12 @@ void discard_index(struct index_state *istate)
2550 validate_cache_entries(istate);
2551
2552 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;
2553 free_name_hash(istate);
2554 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);
2557 discard_split_index(istate);
2558 free_untracked_cache(istate->untracked);
2559 - istate->untracked = NULL;
2559
2560 if (istate->sparse_checkout_patterns) {
2561 clear_pattern_list(istate->sparse_checkout_patterns);
@@ -2569,6 +2568,12 @@ void discard_index(struct index_state *istate)
2568 }
2569 }
2570
2571 +void discard_index(struct index_state *istate)
2572 +{
2573 + release_index(istate);
2574 + index_state_init(istate);
2575 +}
2576 +
2577 /*
2578 * Validate the cache entries of this index.
2579 * All cache entries associated with this index
repository.c
+4 -2
@@ -302,8 +302,10 @@ int repo_read_index(struct repository *repo)
302 {
303 int res;
304
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 + }
309
310 /* Complete the double-reference */
311 if (!repo->index->repo)
revision.c
+1 -1
@@ -1813,7 +1813,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1813 worktrees = get_worktrees();
1814 for (p = worktrees; *p; p++) {
1815 struct worktree *wt = *p;
1816 - struct index_state istate = { NULL };
1816 + struct index_state istate = INDEX_STATE_INIT;
1817
1818 if (wt->is_current)
1819 continue; /* current index already taken care of */
split-index.c
+2 -1
@@ -90,7 +90,8 @@ void move_cache_to_base_index(struct index_state *istate)
90 mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
91 }
92
93 - CALLOC_ARRAY(si->base, 1);
93 + ALLOC_ARRAY(si->base, 1);
94 + index_state_init(si->base);
95 si->base->version = istate->version;
96 /* zero timestamp disables racy test in ce_write_index() */
97 si->base->timestamp = istate->timestamp;
unpack-trees.c
+1 -1
@@ -1905,7 +1905,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1905 populate_from_existing_patterns(o, &pl);
1906 }
1907
1908 - memset(&o->result, 0, sizeof(o->result));
1908 + index_state_init(&o->result);
1909 o->result.initialized = 1;
1910 o->result.timestamp.sec = o->src_index->timestamp.sec;
1911 o->result.timestamp.nsec = o->src_index->timestamp.nsec;