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

[PATCH 6/6] oid_pos(): access table through const pointers

From
Jeff King <peff@peff.net>
Date
Jan 28, 2021, 06:20 UTC
Message-ID
<YBJXp9Tr9kHrgYsp@coredump.intra.peff.net>
In-Reply-To
<YBJVuckmbGriVa//@coredump.intra.peff.net>

When we are looking up an oid in an array, we obviously don't need to write to the array. Let's mark it as const in the function interfaces, as well as in the local variables we use to derference the void pointer (note a few cases use pointers-to-pointers, so we mark everything const).

Signed-off-by: Jeff King <peff@peff.net>
---
This is just a hygiene thing. I think it's pretty unlikely for one of
these 2-line access functions to somehow forget that the table is
supposed to be const.
 builtin/name-rev.c  | 4 ++--
 commit-graph.c      | 4 ++--
 commit.c            | 4 ++--
 hash-lookup.c       | 2 +-
 hash-lookup.h       | 4 ++--
 oid-array.c         | 4 ++--
 pack-bitmap-write.c | 4 ++--
 7 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index 27138fdce4..b221d30014 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -390,9 +390,9 @@ static void name_tips(void)
 	}
 }
 
-static const struct object_id *nth_tip_table_ent(size_t ix, void *table_)
+static const struct object_id *nth_tip_table_ent(size_t ix, const void *table_)
 {
-	struct tip_table_entry *table = table_;
+	const struct tip_table_entry *table = table_;
 	return &table[ix].oid;
 }
 
diff --git a/commit-graph.c b/commit-graph.c
index 248f1efb73..b09fce5f57 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1012,9 +1012,9 @@ static int write_graph_chunk_oids(struct hashfile *f,
 	return 0;
 }
 
-static const struct object_id *commit_to_oid(size_t index, void *table)
+static const struct object_id *commit_to_oid(size_t index, const void *table)
 {
-	struct commit **commits = table;
+	const struct commit * const *commits = table;
 	return &commits[index]->object.oid;
 }
 
diff --git a/commit.c b/commit.c
index 39eab5b36b..fd2831dad3 100644
--- a/commit.c
+++ b/commit.c
@@ -105,9 +105,9 @@ static timestamp_t parse_commit_date(const char *buf, const char *tail)
 	return parse_timestamp(dateptr, NULL, 10);
 }
 
-static const struct object_id *commit_graft_oid_access(size_t index, void *table)
+static const struct object_id *commit_graft_oid_access(size_t index, const void *table)
 {
-	struct commit_graft **commit_graft_table = table;
+	const struct commit_graft * const *commit_graft_table = table;
 	return &commit_graft_table[index]->oid;
 }
 
diff --git a/hash-lookup.c b/hash-lookup.c
index d15bb34574..b98ed5e11e 100644
--- a/hash-lookup.c
+++ b/hash-lookup.c
@@ -50,7 +50,7 @@ static uint32_t take2(const struct object_id *oid, size_t ofs)
  * The oid of element i (between 0 and nr - 1) should be returned
  * by "fn(i, table)".
  */
-int oid_pos(const struct object_id *oid, void *table, size_t nr,
+int oid_pos(const struct object_id *oid, const void *table, size_t nr,
 	    oid_access_fn fn)
 {
 	size_t hi = nr;
diff --git a/hash-lookup.h b/hash-lookup.h
index 7b3ecad1f0..dbd71ebaf7 100644
--- a/hash-lookup.h
+++ b/hash-lookup.h
@@ -1,10 +1,10 @@
 #ifndef HASH_LOOKUP_H
 #define HASH_LOOKUP_H
 
-typedef const struct object_id *oid_access_fn(size_t index, void *table);
+typedef const struct object_id *oid_access_fn(size_t index, const void *table);
 
 int oid_pos(const struct object_id *oid,
-	    void *table,
+	    const void *table,
 	    size_t nr,
 	    oid_access_fn fn);
 
diff --git a/oid-array.c b/oid-array.c
index a19235afbf..73ba76e9e9 100644
--- a/oid-array.c
+++ b/oid-array.c
@@ -22,9 +22,9 @@ void oid_array_sort(struct oid_array *array)
 	array->sorted = 1;
 }
 
-static const struct object_id *oid_access(size_t index, void *table)
+static const struct object_id *oid_access(size_t index, const void *table)
 {
-	struct object_id *array = table;
+	const struct object_id *array = table;
 	return &array[index];
 }
 
diff --git a/pack-bitmap-write.c b/pack-bitmap-write.c
index f21259dfc8..88d9e696a5 100644
--- a/pack-bitmap-write.c
+++ b/pack-bitmap-write.c
@@ -610,9 +610,9 @@ static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
 		die("Failed to write bitmap index");
 }
 
-static const struct object_id *oid_access(size_t pos, void *table)
+static const struct object_id *oid_access(size_t pos, const void *table)
 {
-	struct pack_idx_entry **index = table;
+	const struct pack_idx_entry * const *index = table;
 	return &index[pos]->oid;
 }
 
-- 
2.30.0.758.gfe500d6872
Previous: Jeff King
Message 15 of 15 in “convert hash_pos() to oid_pos()”
  1. 0/6 convert hash_pos() to oid_pos()Jeff King, Jan 28, 2021
  2. 1/6 commit_graft_pos(): take an oid instead of a bare hashJeff King, Jan 28, 2021
  3. Derrick StoleeJan 28, 2021
  4. 2/6 rerere: check dirname format while iterating rr_cache directoryJeff King, Jan 28, 2021
  5. 3/6 rerere: tighten rr-cache dirname checkJeff King, Jan 28, 2021
  6. Taylor BlauJan 28, 2021
  7. Jeff KingJan 28, 2021
  8. Jeff KingJan 28, 2021
  9. Junio C HamanoJan 28, 2021
  10. Junio C HamanoJan 28, 2021
  11. Jeff KingJan 28, 2021
  12. 5/6 hash_pos(): convert to oid_pos()Jeff King, Jan 28, 2021
  13. Taylor BlauJan 28, 2021
  14. Jeff KingJan 29, 2021
  15. 6/6 oid_pos(): access table through const pointersJeff King, Jan 28, 2021

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.