make_transient_cache_entry(): optionally alloc from mem_pool

Allow make_transient_cache_entry() to optionally receive a mem_pool struct in which it should allocate the entry. This will be used in the following patch, to store some transient entries which should persist until parallel checkout finishes. Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matheus Tavares committed May 4, 2021 at 13:27 UTC 96168827802b08c4adf2a036594ab235b2c5630f
6 files changed +23 -13
builtin/checkout--worker.c
+1 -1
@@ -39,7 +39,7 @@ static void packet_to_pc_item(const char *buffer, int len,
39 }
40
41 memset(pc_item, 0, sizeof(*pc_item));
42 - pc_item->ce = make_empty_transient_cache_entry(fixed_portion->name_len);
42 + pc_item->ce = make_empty_transient_cache_entry(fixed_portion->name_len, NULL);
43 pc_item->ce->ce_namelen = fixed_portion->name_len;
44 pc_item->ce->ce_mode = fixed_portion->ce_mode;
45 memcpy(pc_item->ce->name, variant, pc_item->ce->ce_namelen);
builtin/checkout.c
+1 -1
@@ -291,7 +291,7 @@ static int checkout_merged(int pos, const struct checkout *state, int *nr_checko
291 if (write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid))
292 die(_("Unable to add merge result for '%s'"), path);
293 free(result_buf.ptr);
294 - ce = make_transient_cache_entry(mode, &oid, path, 2);
294 + ce = make_transient_cache_entry(mode, &oid, path, 2, NULL);
295 if (!ce)
296 die(_("make_cache_entry failed for path '%s'"), path);
297 status = checkout_entry(ce, state, NULL, nr_checkouts);
builtin/difftool.c
+1 -1
@@ -323,7 +323,7 @@ static int checkout_path(unsigned mode, struct object_id *oid,
323 struct cache_entry *ce;
324 int ret;
325
326 - ce = make_transient_cache_entry(mode, oid, path, 0);
326 + ce = make_transient_cache_entry(mode, oid, path, 0, NULL);
327 ret = checkout_entry(ce, state, NULL, NULL);
328
329 discard_cache_entry(ce);
cache.h
+9 -5
@@ -356,16 +356,20 @@ struct cache_entry *make_empty_cache_entry(struct index_state *istate,
356 size_t name_len);
357
358 /*
359 - * Create a cache_entry that is not intended to be added to an index.
360 - * Caller is responsible for discarding the cache_entry
361 - * with `discard_cache_entry`.
359 + * Create a cache_entry that is not intended to be added to an index. If
360 + * `ce_mem_pool` is not NULL, the entry is allocated within the given memory
361 + * pool. Caller is responsible for discarding "loose" entries with
362 + * `discard_cache_entry()` and the memory pool with
363 + * `mem_pool_discard(ce_mem_pool, should_validate_cache_entries())`.
364 */
365 struct cache_entry *make_transient_cache_entry(unsigned int mode,
366 const struct object_id *oid,
367 const char *path,
366 - int stage);
368 + int stage,
369 + struct mem_pool *ce_mem_pool);
370
368 -struct cache_entry *make_empty_transient_cache_entry(size_t name_len);
371 +struct cache_entry *make_empty_transient_cache_entry(size_t len,
372 + struct mem_pool *ce_mem_pool);
373
374 /*
375 * Discard cache entry.
read-cache.c
+10 -4
@@ -813,8 +813,11 @@ struct cache_entry *make_empty_cache_entry(struct index_state *istate, size_t le
813 return mem_pool__ce_calloc(find_mem_pool(istate), len);
814 }
815
816 -struct cache_entry *make_empty_transient_cache_entry(size_t len)
816 +struct cache_entry *make_empty_transient_cache_entry(size_t len,
817 + struct mem_pool *ce_mem_pool)
818 {
819 + if (ce_mem_pool)
820 + return mem_pool__ce_calloc(ce_mem_pool, len);
821 return xcalloc(1, cache_entry_size(len));
822 }
823
@@ -848,8 +851,11 @@ struct cache_entry *make_cache_entry(struct index_state *istate,
851 return ret;
852 }
853
851 -struct cache_entry *make_transient_cache_entry(unsigned int mode, const struct object_id *oid,
852 - const char *path, int stage)
854 +struct cache_entry *make_transient_cache_entry(unsigned int mode,
855 + const struct object_id *oid,
856 + const char *path,
857 + int stage,
858 + struct mem_pool *ce_mem_pool)
859 {
860 struct cache_entry *ce;
861 int len;
@@ -860,7 +866,7 @@ struct cache_entry *make_transient_cache_entry(unsigned int mode, const struct o
866 }
867
868 len = strlen(path);
863 - ce = make_empty_transient_cache_entry(len);
869 + ce = make_empty_transient_cache_entry(len, ce_mem_pool);
870
871 oidcpy(&ce->oid, oid);
872 memcpy(ce->name, path, len);
unpack-trees.c
+1 -1
@@ -1034,7 +1034,7 @@ static struct cache_entry *create_ce_entry(const struct traverse_info *info,
1034 size_t len = traverse_path_len(info, tree_entry_len(n));
1035 struct cache_entry *ce =
1036 is_transient ?
1037 - make_empty_transient_cache_entry(len) :
1037 + make_empty_transient_cache_entry(len, NULL) :
1038 make_empty_cache_entry(istate, len);
1039
1040 ce->ce_mode = create_ce_mode(n->mode);