]> git.ipfire.org Git - thirdparty/git.git/commitdiff
cache API: add a "INDEX_STATE_INIT" macro/function, add release_index()
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Thu, 12 Jan 2023 12:55:27 +0000 (13:55 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 16 Jan 2023 18:46:58 +0000 (10:46 -0800)
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>
12 files changed:
apply.c
builtin/difftool.c
builtin/sparse-checkout.c
builtin/stash.c
builtin/worktree.c
cache.h
merge-recursive.c
read-cache.c
repository.c
revision.c
split-index.c
unpack-trees.c

diff --git a/apply.c b/apply.c
index 85822280476dc9b729943119802c60d76fd74134..821fe411ec8e4c2ae924dc38e5732cce7e4e603a 100644 (file)
--- 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 1f9d4324df529c56efbe0ffc9dce6b854f27d737..758e7bd3b6ad743e30b30ec4e222f0742dd07f09 100644 (file)
@@ -361,7 +361,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
        struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
        struct hashmap_iter iter;
        struct pair_entry *entry;
-       struct index_state wtindex = { 0 };
+       struct index_state wtindex = INDEX_STATE_INIT;
        struct checkout lstate, rstate;
        int err = 0;
        struct child_process cmd = CHILD_PROCESS_INIT;
index 58a22503f049baef60fafc363e0f5d82d1ddf368..dc332c6d05fa7022eed51a7c66e721a21dac30d5 100644 (file)
@@ -217,6 +217,7 @@ static int update_working_directory(struct pattern_list *pl)
        o.head_idx = -1;
        o.src_index = r->index;
        o.dst_index = r->index;
+       index_state_init(&o.result);
        o.skip_sparse_checkout = 0;
        o.pl = pl;
 
index bb0fd861434aae2e9dcdd7b83ec9dfdada38a457..a5f13fb1e9f240c01d40604bf9f42a69505ad135 100644 (file)
@@ -1137,7 +1137,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
        int ret = 0;
        struct strbuf untracked_msg = STRBUF_INIT;
        struct child_process cp_upd_index = CHILD_PROCESS_INIT;
-       struct index_state istate = { NULL };
+       struct index_state istate = INDEX_STATE_INIT;
 
        cp_upd_index.git_cmd = 1;
        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,
        }
 
 done:
-       discard_index(&istate);
+       release_index(&istate);
        strbuf_release(&untracked_msg);
        remove_path(stash_index_path.buf);
        return ret;
@@ -1176,7 +1176,7 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
 {
        int ret = 0;
        struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
-       struct index_state istate = { NULL };
+       struct index_state istate = INDEX_STATE_INIT;
 
        if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,
                                0, NULL)) {
@@ -1199,7 +1199,7 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
        }
 
 done:
-       discard_index(&istate);
+       release_index(&istate);
        return ret;
 }
 
@@ -1209,7 +1209,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
        int ret = 0;
        struct child_process cp_read_tree = CHILD_PROCESS_INIT;
        struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
-       struct index_state istate = { NULL };
+       struct index_state istate = INDEX_STATE_INIT;
        char *old_index_env = NULL, *old_repo_index_file;
 
        remove_path(stash_index_path.buf);
@@ -1260,7 +1260,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
        }
 
 done:
-       discard_index(&istate);
+       release_index(&istate);
        remove_path(stash_index_path.buf);
        return ret;
 }
@@ -1271,7 +1271,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
        struct rev_info rev;
        struct child_process cp_upd_index = CHILD_PROCESS_INIT;
        struct strbuf diff_output = STRBUF_INIT;
-       struct index_state istate = { NULL };
+       struct index_state istate = INDEX_STATE_INIT;
 
        init_revisions(&rev, NULL);
        copy_pathspec(&rev.prune_data, ps);
@@ -1319,7 +1319,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
        }
 
 done:
-       discard_index(&istate);
+       release_index(&istate);
        release_revisions(&rev);
        strbuf_release(&diff_output);
        remove_path(stash_index_path.buf);
index 591d659faea1684b19a5371b773c6d8493f8d796..311d6e90750d18243b446916c3813549d075ba2b 100644 (file)
@@ -923,7 +923,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
 
 static void validate_no_submodules(const struct worktree *wt)
 {
-       struct index_state istate = { NULL };
+       struct index_state istate = INDEX_STATE_INIT;
        struct strbuf path = STRBUF_INIT;
        int i, found_submodules = 0;
 
diff --git a/cache.h b/cache.h
index 21ed9633b2a78193c02fda5cd91960a38fcbdf2e..be28fce12ac6ff9f61183596f778fbb5b0a84908 100644 (file)
--- 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);
index 2fd0aa968751f7defa06a51148f75bb5838b44d4..0948b3ea9612331189b7a1773f0a0aaceb45ab16 100644 (file)
@@ -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)
index 1782e940353c3366e621dc94bdacabe3b0d91bb8..a91ec90f591079f1fb3c3ccfb449dded7e1ff720 100644 (file)
@@ -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
index 3427085fd6d2e44fa32c3e0ebc4ffba7a4c8286f..a5805fa33b1cb1f2cf7d1a1bd4a5ad00aa96ddc7 100644 (file)
@@ -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)
index 100e5ad5110b1f851801c11639434da7191a229e..fb090886f5ba0367f853a52b5584261b595d05f4 100644 (file)
@@ -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 */
index 9d0ccc30d00e35965f47d55253166f3d8ce142e2..a5b56c0c37fc83e8e44efb86b930a33c182ea0ce 100644 (file)
@@ -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;
index 2381cd7cac4b0f9b3595ab03bc2bdef55d01126b..d1d1b0f319e7cd4103c527f57156359b9f3dbb45 100644 (file)
@@ -1905,7 +1905,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
                populate_from_existing_patterns(o, &pl);
        }
 
-       memset(&o->result, 0, sizeof(o->result));
+       index_state_init(&o->result);
        o->result.initialized = 1;
        o->result.timestamp.sec = o->src_index->timestamp.sec;
        o->result.timestamp.nsec = o->src_index->timestamp.nsec;