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 committedJan 12, 2023 at 13:55 UTC2f6b1eb794ee1f152c1a4b519e3b6dcecd0b487f
12 files changed+52-30
apply.c
+1-1
index 8582228047..821fe411ec 100644--- a/apply.c+++ b/apply.c@@ -4105,7 +4105,7 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid) static int build_fake_ancestor(struct apply_state *state, struct patch *list) { struct patch *patch;- struct index_state result = { NULL };+ struct index_state result = INDEX_STATE_INIT; struct lock_file lock = LOCK_INIT; int res;
index 21ed9633b2..be28fce12a 100644--- a/cache.h+++ b/cache.h@@ -360,6 +360,19 @@ struct index_state { struct pattern_list *sparse_checkout_patterns; };+/**+ * A "struct index_state istate" must be initialized with+ * INDEX_STATE_INIT or the corresponding index_state_init().+ *+ * If the variable won't be used again, use release_index() to free()+ * its resources. If it needs to be used again use discard_index(),+ * which does the same thing, but will use use index_state_init() at+ * the end.+ */+#define INDEX_STATE_INIT { 0 }+void index_state_init(struct index_state *istate);+void release_index(struct index_state *istate);+ /* Name hashing */ int test_lazy_init_name_hash(struct index_state *istate, int try_threaded); void add_name_hash(struct index_state *istate, struct cache_entry *ce);
merge-recursive.c
+1-1
index 2fd0aa9687..0948b3ea96 100644--- a/merge-recursive.c+++ b/merge-recursive.c@@ -412,7 +412,7 @@ static int unpack_trees_start(struct merge_options *opt, { int rc; struct tree_desc t[3];- struct index_state tmp_index = { NULL };+ struct index_state tmp_index = INDEX_STATE_INIT; memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts)); if (opt->priv->call_depth)
read-cache.c
+18-13
index 1782e94035..a91ec90f59 100644--- a/read-cache.c+++ b/read-cache.c@@ -2490,9 +2490,10 @@ int read_index_from(struct index_state *istate, const char *path, trace_performance_enter(); if (split_index->base)- discard_index(split_index->base);+ release_index(split_index->base); else- CALLOC_ARRAY(split_index->base, 1);+ ALLOC_ARRAY(split_index->base, 1);+ index_state_init(split_index->base); base_oid_hex = oid_to_hex(&split_index->base_oid); base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);@@ -2531,7 +2532,13 @@ int is_index_unborn(struct index_state *istate) return (!istate->cache_nr && !istate->timestamp.sec); }-void discard_index(struct index_state *istate)+void index_state_init(struct index_state *istate)+{+ struct index_state blank = INDEX_STATE_INIT;+ memcpy(istate, &blank, sizeof(*istate));+}++void release_index(struct index_state *istate) { /* * Cache entries in istate->cache[] should have been allocated@@ -2543,20 +2550,12 @@ void discard_index(struct index_state *istate) validate_cache_entries(istate); resolve_undo_clear_index(istate);- istate->cache_nr = 0;- istate->cache_changed = 0;- istate->timestamp.sec = 0;- istate->timestamp.nsec = 0; free_name_hash(istate); cache_tree_free(&(istate->cache_tree));- istate->initialized = 0;- istate->fsmonitor_has_run_once = 0;- FREE_AND_NULL(istate->fsmonitor_last_update);- FREE_AND_NULL(istate->cache);- istate->cache_alloc = 0;+ free(istate->fsmonitor_last_update);+ free(istate->cache); discard_split_index(istate); free_untracked_cache(istate->untracked);- istate->untracked = NULL; if (istate->sparse_checkout_patterns) { clear_pattern_list(istate->sparse_checkout_patterns);@@ -2569,6 +2568,12 @@ void discard_index(struct index_state *istate) } }+void discard_index(struct index_state *istate)+{+ release_index(istate);+ index_state_init(istate);+}+ /* * Validate the cache entries of this index. * All cache entries associated with this index
repository.c
+4-2
index 3427085fd6..a5805fa33b 100644--- a/repository.c+++ b/repository.c@@ -302,8 +302,10 @@ int repo_read_index(struct repository *repo) { int res;- if (!repo->index)- CALLOC_ARRAY(repo->index, 1);+ if (!repo->index) {+ ALLOC_ARRAY(repo->index, 1);+ index_state_init(repo->index);+ } /* Complete the double-reference */ if (!repo->index->repo)
revision.c
+1-1
index 100e5ad511..fb090886f5 100644--- a/revision.c+++ b/revision.c@@ -1813,7 +1813,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags) worktrees = get_worktrees(); for (p = worktrees; *p; p++) { struct worktree *wt = *p;- struct index_state istate = { NULL };+ struct index_state istate = INDEX_STATE_INIT; if (wt->is_current) continue; /* current index already taken care of */
split-index.c
+2-1
index 9d0ccc30d0..a5b56c0c37 100644--- a/split-index.c+++ b/split-index.c@@ -90,7 +90,8 @@ void move_cache_to_base_index(struct index_state *istate) mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool); }- CALLOC_ARRAY(si->base, 1);+ ALLOC_ARRAY(si->base, 1);+ index_state_init(si->base); si->base->version = istate->version; /* zero timestamp disables racy test in ce_write_index() */ si->base->timestamp = istate->timestamp;