git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 02/11] reftable: pass opts as constant pointer

From
Patrick Steinhardt <ps@pks.im>
Date
May 13, 2024, 08:17 UTC
Message-ID
<f1c9914a77ab9bfe27a62e324cc3eb388f4118d9.1715587849.git.ps@pks.im>
In-Reply-To
<cover.1715587849.git.ps@pks.im>

We sometimes pass the refatble write options as value and sometimes as a pointer. This is quite confusing and makes the reader wonder whether the options get modified sometimes.

In fact, `reftable_new_writer()` does cause the caller-provided options to get updated when some values aren't set up. This is quite unexpected, but didn't cause any harm until now.

Adapt the code so that we do not modify the caller-provided values anymore. While at it, refactor the code to code to consistently pass the options as a constant pointer to clarify that the caller-provided opts will not ever get modified.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 refs/reftable-backend.c    |  6 ++---
 reftable/dump.c            |  2 +-
 reftable/reftable-stack.h  |  2 +-
 reftable/reftable-writer.h |  2 +-
 reftable/stack.c           |  7 ++++--
 reftable/stack_test.c      | 48 +++++++++++++++++++-------------------
 reftable/writer.c          | 17 +++++++++-----
 7 files changed, 46 insertions(+), 38 deletions(-)
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index 010ef811b6..f8f930380d 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -129,7 +129,7 @@ static struct reftable_stack *stack_for(struct reftable_ref_store *store,
 				    store->base.repo->commondir, wtname_buf.buf);
 
 			store->err = reftable_new_stack(&stack, wt_dir.buf,
-							store->write_options);
+							&store->write_options);
 			assert(store->err != REFTABLE_API_ERROR);
 			strmap_put(&store->worktree_stacks, wtname_buf.buf, stack);
 		}
@@ -263,7 +263,7 @@ static struct ref_store *reftable_be_init(struct repository *repo,
 	}
 	strbuf_addstr(&path, "/reftable");
 	refs->err = reftable_new_stack(&refs->main_stack, path.buf,
-				       refs->write_options);
+				       &refs->write_options);
 	if (refs->err)
 		goto done;
 
@@ -280,7 +280,7 @@ static struct ref_store *reftable_be_init(struct repository *repo,
 		strbuf_addf(&path, "%s/reftable", gitdir);
 
 		refs->err = reftable_new_stack(&refs->worktree_stack, path.buf,
-					       refs->write_options);
+					       &refs->write_options);
 		if (refs->err)
 			goto done;
 	}
diff --git a/reftable/dump.c b/reftable/dump.c
index 9c770a10cc..586f3eb288 100644
--- a/reftable/dump.c
+++ b/reftable/dump.c
@@ -29,7 +29,7 @@ static int compact_stack(const char *stackdir)
 	struct reftable_stack *stack = NULL;
 	struct reftable_write_options opts = { 0 };
 
-	int err = reftable_new_stack(&stack, stackdir, opts);
+	int err = reftable_new_stack(&stack, stackdir, &opts);
 	if (err < 0)
 		goto done;
 
diff --git a/reftable/reftable-stack.h b/reftable/reftable-stack.h
index 9c8e4eef49..c15632c401 100644
--- a/reftable/reftable-stack.h
+++ b/reftable/reftable-stack.h
@@ -29,7 +29,7 @@ struct reftable_stack;
  *  stored in 'dir'. Typically, this should be .git/reftables.
  */
 int reftable_new_stack(struct reftable_stack **dest, const char *dir,
-		       struct reftable_write_options opts);
+		       const struct reftable_write_options *opts);
 
 /* returns the update_index at which a next table should be written. */
 uint64_t reftable_stack_next_update_index(struct reftable_stack *st);
diff --git a/reftable/reftable-writer.h b/reftable/reftable-writer.h
index b601a69a40..03df3a4963 100644
--- a/reftable/reftable-writer.h
+++ b/reftable/reftable-writer.h
@@ -88,7 +88,7 @@ struct reftable_stats {
 struct reftable_writer *
 reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
 		    int (*flush_func)(void *),
-		    void *writer_arg, struct reftable_write_options *opts);
+		    void *writer_arg, const struct reftable_write_options *opts);
 
 /* Set the range of update indices for the records we will add. When writing a
    table into a stack, the min should be at least
diff --git a/reftable/stack.c b/reftable/stack.c
index 54e7473f3a..d2e68be7e8 100644
--- a/reftable/stack.c
+++ b/reftable/stack.c
@@ -54,12 +54,15 @@ static int reftable_fd_flush(void *arg)
 }
 
 int reftable_new_stack(struct reftable_stack **dest, const char *dir,
-		       struct reftable_write_options opts)
+		       const struct reftable_write_options *_opts)
 {
 	struct reftable_stack *p = reftable_calloc(1, sizeof(*p));
 	struct strbuf list_file_name = STRBUF_INIT;
+	struct reftable_write_options opts = {0};
 	int err = 0;
 
+	if (_opts)
+		opts = *_opts;
 	if (opts.hash_id == 0)
 		opts.hash_id = GIT_SHA1_FORMAT_ID;
 
@@ -1438,7 +1441,7 @@ int reftable_stack_print_directory(const char *stackdir, uint32_t hash_id)
 	struct reftable_merged_table *merged = NULL;
 	struct reftable_table table = { NULL };
 
-	int err = reftable_new_stack(&stack, stackdir, opts);
+	int err = reftable_new_stack(&stack, stackdir, &opts);
 	if (err < 0)
 		goto done;
 
diff --git a/reftable/stack_test.c b/reftable/stack_test.c
index e17ad4dc62..d15f11d712 100644
--- a/reftable/stack_test.c
+++ b/reftable/stack_test.c
@@ -163,7 +163,7 @@ static void test_reftable_stack_add_one(void)
 	};
 	struct reftable_ref_record dest = { NULL };
 	struct stat stat_result = { 0 };
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_add(st, &write_test_ref, &ref);
@@ -232,10 +232,10 @@ static void test_reftable_stack_uptodate(void)
 	/* simulate multi-process access to the same stack
 	   by creating two stacks for the same directory.
 	 */
-	err = reftable_new_stack(&st1, dir, opts);
+	err = reftable_new_stack(&st1, dir, &opts);
 	EXPECT_ERR(err);
 
-	err = reftable_new_stack(&st2, dir, opts);
+	err = reftable_new_stack(&st2, dir, &opts);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_add(st1, &write_test_ref, &ref1);
@@ -270,7 +270,7 @@ static void test_reftable_stack_transaction_api(void)
 	};
 	struct reftable_ref_record dest = { NULL };
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	reftable_addition_destroy(add);
@@ -304,7 +304,7 @@ static void test_reftable_stack_transaction_api_performs_auto_compaction(void)
 	struct reftable_stack *st = NULL;
 	int i, n = 20, err;
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	for (i = 0; i <= n; i++) {
@@ -365,7 +365,7 @@ static void test_reftable_stack_auto_compaction_fails_gracefully(void)
 	char *dir = get_tmp_dir(__LINE__);
 	int err;
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_add(st, write_test_ref, &ref);
@@ -418,7 +418,7 @@ static void test_reftable_stack_update_index_check(void)
 		.value.symref = "master",
 	};
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_add(st, &write_test_ref, &ref1);
@@ -437,7 +437,7 @@ static void test_reftable_stack_lock_failure(void)
 	struct reftable_stack *st = NULL;
 	int err, i;
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 	for (i = -1; i != REFTABLE_EMPTY_TABLE_ERROR; i--) {
 		err = reftable_stack_add(st, &write_error, &i);
@@ -465,7 +465,7 @@ static void test_reftable_stack_add(void)
 	struct stat stat_result;
 	int N = ARRAY_SIZE(refs);
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	for (i = 0; i < N; i++) {
@@ -575,7 +575,7 @@ static void test_reftable_stack_log_normalize(void)
 		.update_index = 1,
 	};
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	input.value.update.message = "one\ntwo";
@@ -617,7 +617,7 @@ static void test_reftable_stack_tombstone(void)
 	struct reftable_ref_record dest = { NULL };
 	struct reftable_log_record log_dest = { NULL };
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	/* even entries add the refs, odd entries delete them. */
@@ -701,18 +701,18 @@ static void test_reftable_stack_hash_id(void)
 	struct reftable_stack *st_default = NULL;
 	struct reftable_ref_record dest = { NULL };
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_add(st, &write_test_ref, &ref);
 	EXPECT_ERR(err);
 
 	/* can't read it with the wrong hash ID. */
-	err = reftable_new_stack(&st32, dir, opts32);
+	err = reftable_new_stack(&st32, dir, &opts32);
 	EXPECT(err == REFTABLE_FORMAT_ERROR);
 
 	/* check that we can read it back with default opts too. */
-	err = reftable_new_stack(&st_default, dir, opts_default);
+	err = reftable_new_stack(&st_default, dir, &opts_default);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_read_ref(st_default, "master", &dest);
@@ -756,7 +756,7 @@ static void test_reflog_expire(void)
 	};
 	struct reftable_log_record log = { NULL };
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	for (i = 1; i <= N; i++) {
@@ -825,13 +825,13 @@ static void test_empty_add(void)
 	char *dir = get_tmp_dir(__LINE__);
 	struct reftable_stack *st2 = NULL;
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_add(st, &write_nothing, NULL);
 	EXPECT_ERR(err);
 
-	err = reftable_new_stack(&st2, dir, opts);
+	err = reftable_new_stack(&st2, dir, &opts);
 	EXPECT_ERR(err);
 	clear_dir(dir);
 	reftable_stack_destroy(st);
@@ -858,7 +858,7 @@ static void test_reftable_stack_auto_compaction(void)
 	int err, i;
 	int N = 100;
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	for (i = 0; i < N; i++) {
@@ -894,7 +894,7 @@ static void test_reftable_stack_add_performs_auto_compaction(void)
 	char *dir = get_tmp_dir(__LINE__);
 	int err, i, n = 20;
 
-	err = reftable_new_stack(&st, dir, opts);
+	err = reftable_new_stack(&st, dir, &opts);
 	EXPECT_ERR(err);
 
 	for (i = 0; i <= n; i++) {
@@ -942,7 +942,7 @@ static void test_reftable_stack_compaction_concurrent(void)
 	int err, i;
 	int N = 3;
 
-	err = reftable_new_stack(&st1, dir, opts);
+	err = reftable_new_stack(&st1, dir, &opts);
 	EXPECT_ERR(err);
 
 	for (i = 0; i < N; i++) {
@@ -959,7 +959,7 @@ static void test_reftable_stack_compaction_concurrent(void)
 		EXPECT_ERR(err);
 	}
 
-	err = reftable_new_stack(&st2, dir, opts);
+	err = reftable_new_stack(&st2, dir, &opts);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_compact_all(st1, NULL);
@@ -991,7 +991,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
 	int err, i;
 	int N = 3;
 
-	err = reftable_new_stack(&st1, dir, opts);
+	err = reftable_new_stack(&st1, dir, &opts);
 	EXPECT_ERR(err);
 
 	for (i = 0; i < N; i++) {
@@ -1008,7 +1008,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
 		EXPECT_ERR(err);
 	}
 
-	err = reftable_new_stack(&st2, dir, opts);
+	err = reftable_new_stack(&st2, dir, &opts);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_compact_all(st1, NULL);
@@ -1017,7 +1017,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
 	unclean_stack_close(st1);
 	unclean_stack_close(st2);
 
-	err = reftable_new_stack(&st3, dir, opts);
+	err = reftable_new_stack(&st3, dir, &opts);
 	EXPECT_ERR(err);
 
 	err = reftable_stack_clean(st3);
diff --git a/reftable/writer.c b/reftable/writer.c
index 10eccaaa07..4cc6e2ebd8 100644
--- a/reftable/writer.c
+++ b/reftable/writer.c
@@ -122,20 +122,25 @@ static struct strbuf reftable_empty_strbuf = STRBUF_INIT;
 struct reftable_writer *
 reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
 		    int (*flush_func)(void *),
-		    void *writer_arg, struct reftable_write_options *opts)
+		    void *writer_arg, const struct reftable_write_options *_opts)
 {
 	struct reftable_writer *wp = reftable_calloc(1, sizeof(*wp));
-	strbuf_init(&wp->block_writer_data.last_key, 0);
-	options_set_defaults(opts);
-	if (opts->block_size >= (1 << 24)) {
+	struct reftable_write_options opts = {0};
+
+	if (_opts)
+		opts = *_opts;
+	options_set_defaults(&opts);
+	if (opts.block_size >= (1 << 24)) {
 		/* TODO - error return? */
 		abort();
 	}
+
+	strbuf_init(&wp->block_writer_data.last_key, 0);
 	wp->last_key = reftable_empty_strbuf;
-	REFTABLE_CALLOC_ARRAY(wp->block, opts->block_size);
+	REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size);
 	wp->write = writer_func;
 	wp->write_arg = writer_arg;
-	wp->opts = *opts;
+	wp->opts = opts;
 	wp->flush = flush_func;
 	writer_reinit_block_writer(wp, BLOCK_TYPE_REF);
 
-- 
2.45.GIT
Previous: Patrick SteinhardtNext: Karthik Nayak
Message 57 of 78 in “reftable: expose write options as config”
  1. 00/11 reftable: expose write options as configPatrick Steinhardt, May 2, 2024
  2. 01/11 reftable: consistently refer to `reftable_write_options` as `opts`Patrick Steinhardt, May 2, 2024
  3. Karthik NayakMay 10, 2024
  4. Patrick SteinhardtMay 10, 2024
  5. 02/11 reftable: consistently pass write opts as valuePatrick Steinhardt, May 2, 2024
  6. 03/11 reftable/writer: drop static variable used to initialize strbufPatrick Steinhardt, May 2, 2024
  7. 04/11 reftable/writer: improve error when passed an invalid block sizePatrick Steinhardt, May 2, 2024
  8. 05/11 reftable/dump: support dumping a table's block structurePatrick Steinhardt, May 2, 2024
  9. 06/11 refs/reftable: allow configuring block sizePatrick Steinhardt, May 2, 2024
  10. Karthik NayakMay 10, 2024
  11. Patrick SteinhardtMay 10, 2024
  12. 07/11 reftable: use `uint16_t` to track restart intervalPatrick Steinhardt, May 2, 2024
  13. 08/11 refs/reftable: allow configuring restart intervalPatrick Steinhardt, May 2, 2024
  14. 09/11 refs/reftable: allow disabling writing the object indexPatrick Steinhardt, May 2, 2024
  15. 10/11 reftable: make the compaction factor configurablePatrick Steinhardt, May 2, 2024
  16. Karthik NayakMay 10, 2024
  17. Patrick SteinhardtMay 10, 2024
  18. 11/11 refs/reftable: allow configuring geometric factorPatrick Steinhardt, May 2, 2024
  19. Karthik NayakMay 10, 2024
  20. Patrick SteinhardtMay 10, 2024
  21. Patrick SteinhardtMay 2, 2024
  22. Junio C HamanoMay 3, 2024
  23. Patrick SteinhardtMay 6, 2024
  24. Justin ToblerMay 6, 2024
  25. Karthik NayakMay 10, 2024
  26. Patrick SteinhardtMay 10, 2024
  27. 00/11 reftable: expose write options as configPatrick Steinhardt, May 10, 2024
  28. 01/11 reftable: consistently refer to `reftable_write_options` as `opts`Patrick Steinhardt, May 10, 2024
  29. Junio C HamanoMay 10, 2024
  30. 02/11 reftable: consistently pass write opts as valuePatrick Steinhardt, May 10, 2024
  31. Junio C HamanoMay 10, 2024
  32. Patrick SteinhardtMay 13, 2024
  33. 03/11 reftable/writer: drop static variable used to initialize strbufPatrick Steinhardt, May 10, 2024
  34. Junio C HamanoMay 10, 2024
  35. 04/11 reftable/writer: improve error when passed an invalid block sizePatrick Steinhardt, May 10, 2024
  36. Junio C HamanoMay 10, 2024
  37. Patrick SteinhardtMay 13, 2024
  38. 05/11 reftable/dump: support dumping a table's block structurePatrick Steinhardt, May 10, 2024
  39. Junio C HamanoMay 13, 2024
  40. 06/11 refs/reftable: allow configuring block sizePatrick Steinhardt, May 10, 2024
  41. 07/11 reftable: use `uint16_t` to track restart intervalPatrick Steinhardt, May 10, 2024
  42. Junio C HamanoMay 13, 2024
  43. Patrick SteinhardtMay 14, 2024
  44. 08/11 refs/reftable: allow configuring restart intervalPatrick Steinhardt, May 10, 2024
  45. Junio C HamanoMay 10, 2024
  46. Patrick SteinhardtMay 13, 2024
  47. 09/11 refs/reftable: allow disabling writing the object indexPatrick Steinhardt, May 10, 2024
  48. 10/11 reftable: make the compaction factor configurablePatrick Steinhardt, May 10, 2024
  49. Junio C HamanoMay 10, 2024
  50. Patrick SteinhardtMay 13, 2024
  51. Junio C HamanoMay 13, 2024
  52. Patrick SteinhardtMay 14, 2024
  53. 11/11 refs/reftable: allow configuring geometric factorPatrick Steinhardt, May 10, 2024
  54. Karthik NayakMay 10, 2024
  55. 00/11 reftable: expose write options as configPatrick Steinhardt, May 13, 2024
  56. 01/11 reftable: consistently refer to `reftable_write_options` as `opts`Patrick Steinhardt, May 13, 2024
  57. 02/11 reftable: pass opts as constant pointerPatrick Steinhardt, May 13, 2024
  58. Karthik NayakMay 17, 2024
  59. Justin ToblerMay 21, 2024
  60. Patrick SteinhardtMay 22, 2024
  61. 03/11 reftable/writer: drop static variable used to initialize strbufPatrick Steinhardt, May 13, 2024
  62. 04/11 reftable/writer: improve error when passed an invalid block sizePatrick Steinhardt, May 13, 2024
  63. 05/11 reftable/dump: support dumping a table's block structurePatrick Steinhardt, May 13, 2024
  64. Justin ToblerMay 21, 2024
  65. Patrick SteinhardtMay 22, 2024
  66. 06/11 refs/reftable: allow configuring block sizePatrick Steinhardt, May 13, 2024
  67. Karthik NayakMay 17, 2024
  68. 07/11 reftable: use `uint16_t` to track restart intervalPatrick Steinhardt, May 13, 2024
  69. 08/11 refs/reftable: allow configuring restart intervalPatrick Steinhardt, May 13, 2024
  70. Justin ToblerMay 21, 2024
  71. Patrick SteinhardtMay 22, 2024
  72. 09/11 refs/reftable: allow disabling writing the object indexPatrick Steinhardt, May 13, 2024
  73. 10/11 reftable: make the compaction factor configurablePatrick Steinhardt, May 13, 2024
  74. 11/11 refs/reftable: allow configuring geometric factorPatrick Steinhardt, May 13, 2024
  75. Karthik NayakMay 17, 2024
  76. Patrick SteinhardtMay 17, 2024
  77. Justin ToblerMay 21, 2024
  78. Patrick SteinhardtMay 22, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.