From 98c431b6f9c767657e1c8cb57370fd1db82b341e Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 28 Jan 2021 01:12:35 -0500 Subject: commit_graft_pos(): take an oid instead of a bare hash All of our callers have an object_id, and are just dereferencing the hash field to pass to us. Let's take the actual object_id instead. We still access the hash to pass to hash_pos, but it's a step in the right direction. This makes the callers slightly simpler, but also gets rid of the untyped pointer, as well as the now-inaccurate name "sha1". Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- commit.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'commit.c') diff --git a/commit.c b/commit.c index bab8d5ab07..fa26729ba5 100644 --- a/commit.c +++ b/commit.c @@ -111,9 +111,9 @@ static const unsigned char *commit_graft_sha1_access(size_t index, void *table) return commit_graft_table[index]->oid.hash; } -int commit_graft_pos(struct repository *r, const unsigned char *sha1) +int commit_graft_pos(struct repository *r, const struct object_id *oid) { - return hash_pos(sha1, r->parsed_objects->grafts, + return hash_pos(oid->hash, r->parsed_objects->grafts, r->parsed_objects->grafts_nr, commit_graft_sha1_access); } @@ -121,7 +121,7 @@ int commit_graft_pos(struct repository *r, const unsigned char *sha1) int register_commit_graft(struct repository *r, struct commit_graft *graft, int ignore_dups) { - int pos = commit_graft_pos(r, graft->oid.hash); + int pos = commit_graft_pos(r, &graft->oid); if (0 <= pos) { if (ignore_dups) @@ -232,7 +232,7 @@ struct commit_graft *lookup_commit_graft(struct repository *r, const struct obje { int pos; prepare_commit_graft(r); - pos = commit_graft_pos(r, oid->hash); + pos = commit_graft_pos(r, oid); if (pos < 0) return NULL; return r->parsed_objects->grafts[pos]; -- cgit v1.2.3 From 45ee13b942b26925b33d827bda2856e1ed0af0b7 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 28 Jan 2021 01:19:42 -0500 Subject: hash_pos(): convert to oid_pos() All of our callers are actually looking up an object_id, not a bare hash. Likewise, the arrays they are looking in are actual arrays of object_id (not just raw bytes of hashes, as we might find in a pack .idx; those are handled by bsearch_hash()). Using an object_id gives us more type safety, and makes the callers slightly shorter. It also gets rid of the word "sha1" from several access functions, though we could obviously also rename those with s/sha1/hash/. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/name-rev.c | 8 ++++---- commit-graph.c | 28 ++++++++++++++-------------- commit.c | 10 +++++----- hash-lookup.c | 18 +++++++++--------- hash-lookup.h | 10 +++++----- oid-array.c | 6 +++--- pack-bitmap-write.c | 6 +++--- 7 files changed, 43 insertions(+), 43 deletions(-) (limited to 'commit.c') diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 3fe71a8c01..27138fdce4 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -390,10 +390,10 @@ static void name_tips(void) } } -static const unsigned char *nth_tip_table_ent(size_t ix, void *table_) +static const struct object_id *nth_tip_table_ent(size_t ix, void *table_) { struct tip_table_entry *table = table_; - return table[ix].oid.hash; + return &table[ix].oid; } static const char *get_exact_ref_match(const struct object *o) @@ -408,8 +408,8 @@ static const char *get_exact_ref_match(const struct object *o) tip_table.sorted = 1; } - found = hash_pos(o->oid.hash, tip_table.table, tip_table.nr, - nth_tip_table_ent); + found = oid_pos(&o->oid, tip_table.table, tip_table.nr, + nth_tip_table_ent); if (0 <= found) return tip_table.table[found].refname; return NULL; diff --git a/commit-graph.c b/commit-graph.c index f3486ec18f..248f1efb73 100644 --- a/commit-graph.c +++ b/commit-graph.c @@ -1012,10 +1012,10 @@ static int write_graph_chunk_oids(struct hashfile *f, return 0; } -static const unsigned char *commit_to_sha1(size_t index, void *table) +static const struct object_id *commit_to_oid(size_t index, void *table) { struct commit **commits = table; - return commits[index]->object.oid.hash; + return &commits[index]->object.oid; } static int write_graph_chunk_data(struct hashfile *f, @@ -1043,10 +1043,10 @@ static int write_graph_chunk_data(struct hashfile *f, if (!parent) edge_value = GRAPH_PARENT_NONE; else { - edge_value = hash_pos(parent->item->object.oid.hash, - ctx->commits.list, - ctx->commits.nr, - commit_to_sha1); + edge_value = oid_pos(&parent->item->object.oid, + ctx->commits.list, + ctx->commits.nr, + commit_to_oid); if (edge_value >= 0) edge_value += ctx->new_num_commits_in_base; @@ -1074,10 +1074,10 @@ static int write_graph_chunk_data(struct hashfile *f, else if (parent->next) edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges; else { - edge_value = hash_pos(parent->item->object.oid.hash, - ctx->commits.list, - ctx->commits.nr, - commit_to_sha1); + edge_value = oid_pos(&parent->item->object.oid, + ctx->commits.list, + ctx->commits.nr, + commit_to_oid); if (edge_value >= 0) edge_value += ctx->new_num_commits_in_base; @@ -1143,10 +1143,10 @@ static int write_graph_chunk_extra_edges(struct hashfile *f, /* Since num_parents > 2, this initializer is safe. */ for (parent = (*list)->parents->next; parent; parent = parent->next) { - int edge_value = hash_pos(parent->item->object.oid.hash, - ctx->commits.list, - ctx->commits.nr, - commit_to_sha1); + int edge_value = oid_pos(&parent->item->object.oid, + ctx->commits.list, + ctx->commits.nr, + commit_to_oid); if (edge_value >= 0) edge_value += ctx->new_num_commits_in_base; diff --git a/commit.c b/commit.c index fa26729ba5..39eab5b36b 100644 --- a/commit.c +++ b/commit.c @@ -105,17 +105,17 @@ static timestamp_t parse_commit_date(const char *buf, const char *tail) return parse_timestamp(dateptr, NULL, 10); } -static const unsigned char *commit_graft_sha1_access(size_t index, void *table) +static const struct object_id *commit_graft_oid_access(size_t index, void *table) { struct commit_graft **commit_graft_table = table; - return commit_graft_table[index]->oid.hash; + return &commit_graft_table[index]->oid; } int commit_graft_pos(struct repository *r, const struct object_id *oid) { - return hash_pos(oid->hash, r->parsed_objects->grafts, - r->parsed_objects->grafts_nr, - commit_graft_sha1_access); + return oid_pos(oid, r->parsed_objects->grafts, + r->parsed_objects->grafts_nr, + commit_graft_oid_access); } int register_commit_graft(struct repository *r, struct commit_graft *graft, diff --git a/hash-lookup.c b/hash-lookup.c index 1191856a32..d15bb34574 100644 --- a/hash-lookup.c +++ b/hash-lookup.c @@ -1,9 +1,9 @@ #include "cache.h" #include "hash-lookup.h" -static uint32_t take2(const unsigned char *hash) +static uint32_t take2(const struct object_id *oid, size_t ofs) { - return ((hash[0] << 8) | hash[1]); + return ((oid->hash[ofs] << 8) | oid->hash[ofs + 1]); } /* @@ -47,11 +47,11 @@ static uint32_t take2(const unsigned char *hash) */ /* * The table should contain "nr" elements. - * The hash of element i (between 0 and nr - 1) should be returned + * The oid of element i (between 0 and nr - 1) should be returned * by "fn(i, table)". */ -int hash_pos(const unsigned char *hash, void *table, size_t nr, - hash_access_fn fn) +int oid_pos(const struct object_id *oid, void *table, size_t nr, + oid_access_fn fn) { size_t hi = nr; size_t lo = 0; @@ -64,9 +64,9 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr, size_t lov, hiv, miv, ofs; for (ofs = 0; ofs < the_hash_algo->rawsz - 2; ofs += 2) { - lov = take2(fn(0, table) + ofs); - hiv = take2(fn(nr - 1, table) + ofs); - miv = take2(hash + ofs); + lov = take2(fn(0, table), ofs); + hiv = take2(fn(nr - 1, table), ofs); + miv = take2(oid, ofs); if (miv < lov) return -1; if (hiv < miv) @@ -88,7 +88,7 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr, do { int cmp; - cmp = hashcmp(fn(mi, table), hash); + cmp = oidcmp(fn(mi, table), oid); if (!cmp) return mi; if (cmp > 0) diff --git a/hash-lookup.h b/hash-lookup.h index 5d476dec72..7b3ecad1f0 100644 --- a/hash-lookup.h +++ b/hash-lookup.h @@ -1,12 +1,12 @@ #ifndef HASH_LOOKUP_H #define HASH_LOOKUP_H -typedef const unsigned char *hash_access_fn(size_t index, void *table); +typedef const struct object_id *oid_access_fn(size_t index, void *table); -int hash_pos(const unsigned char *hash, - void *table, - size_t nr, - hash_access_fn fn); +int oid_pos(const struct object_id *oid, + void *table, + size_t nr, + oid_access_fn fn); /* * Searches for hash in table, using the given fanout table to determine the diff --git a/oid-array.c b/oid-array.c index 889b311f22..a19235afbf 100644 --- a/oid-array.c +++ b/oid-array.c @@ -22,16 +22,16 @@ void oid_array_sort(struct oid_array *array) array->sorted = 1; } -static const unsigned char *sha1_access(size_t index, void *table) +static const struct object_id *oid_access(size_t index, void *table) { struct object_id *array = table; - return array[index].hash; + return &array[index]; } int oid_array_lookup(struct oid_array *array, const struct object_id *oid) { oid_array_sort(array); - return hash_pos(oid->hash, array->oid, array->nr, sha1_access); + return oid_pos(oid, array->oid, array->nr, oid_access); } void oid_array_clear(struct oid_array *array) diff --git a/pack-bitmap-write.c b/pack-bitmap-write.c index 92460a6126..f21259dfc8 100644 --- a/pack-bitmap-write.c +++ b/pack-bitmap-write.c @@ -610,10 +610,10 @@ static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap) die("Failed to write bitmap index"); } -static const unsigned char *sha1_access(size_t pos, void *table) +static const struct object_id *oid_access(size_t pos, void *table) { struct pack_idx_entry **index = table; - return index[pos]->oid.hash; + return &index[pos]->oid; } static void write_selected_commits_v1(struct hashfile *f, @@ -626,7 +626,7 @@ static void write_selected_commits_v1(struct hashfile *f, struct bitmapped_commit *stored = &writer.selected[i]; int commit_pos = - hash_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access); + oid_pos(&stored->commit->object.oid, index, index_nr, oid_access); if (commit_pos < 0) BUG("trying to write commit not in index"); -- cgit v1.2.3 From 8380dcd700e80cd22745bcffb3625f90f943950c Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 28 Jan 2021 01:20:23 -0500 Subject: oid_pos(): access table through const pointers 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 Signed-off-by: Junio C Hamano --- 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(-) (limited to 'commit.c') 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; } -- cgit v1.2.3