Skip to content

Commit b9a7ac2

Browse files
newrengitster
authored andcommitted
hash-ll, hashmap: move oidhash() to hash-ll
oidhash() was used by both hashmap and khash, which makes sense. However, the location of this function in hashmap.[ch] meant that khash.h had to depend upon hashmap.h, making people unfamiliar with khash think that it was built upon hashmap. (Or at least, I personally was confused for a while about this in the past.) Move this function to hash-ll, so that khash.h can stop depending upon hashmap.h. This has another benefit as well: it allows us to remove hashmap.h's dependency on hash-ll.h. While some callers of hashmap.h were making use of oidhash, most were not, so this change provides another way to reduce the number of includes. Diff best viewed with `--color-moved`. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a034e91 commit b9a7ac2

File tree

7 files changed

+22
-23
lines changed

7 files changed

+22
-23
lines changed

decorate.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* data.
44
*/
55
#include "git-compat-util.h"
6-
#include "hashmap.h"
76
#include "object.h"
87
#include "decorate.h"
98

dir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef DIR_H
22
#define DIR_H
33

4+
#include "hash-ll.h"
45
#include "hashmap.h"
56
#include "pathspec.h"
67
#include "statinfo.h"

hash-ll.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,25 @@ static inline void oid_set_algo(struct object_id *oid, const struct git_hash_alg
270270
oid->algo = hash_algo_by_ptr(algop);
271271
}
272272

273+
/*
274+
* Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
275+
* for use in hash tables. Cryptographic hashes are supposed to have
276+
* uniform distribution, so in contrast to `memhash()`, this just copies
277+
* the first `sizeof(int)` bytes without shuffling any bits. Note that
278+
* the results will be different on big-endian and little-endian
279+
* platforms, so they should not be stored or transferred over the net.
280+
*/
281+
static inline unsigned int oidhash(const struct object_id *oid)
282+
{
283+
/*
284+
* Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
285+
* platforms that don't support unaligned reads.
286+
*/
287+
unsigned int hash;
288+
memcpy(&hash, oid->hash, sizeof(hash));
289+
return hash;
290+
}
291+
273292
const char *empty_tree_oid_hex(void);
274293
const char *empty_blob_oid_hex(void);
275294

hashmap.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef HASHMAP_H
22
#define HASHMAP_H
33

4-
#include "hash-ll.h"
5-
64
/*
75
* Generic implementation of hash-based key-value mappings.
86
*
@@ -120,25 +118,6 @@ unsigned int memhash(const void *buf, size_t len);
120118
unsigned int memihash(const void *buf, size_t len);
121119
unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len);
122120

123-
/*
124-
* Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
125-
* for use in hash tables. Cryptographic hashes are supposed to have
126-
* uniform distribution, so in contrast to `memhash()`, this just copies
127-
* the first `sizeof(int)` bytes without shuffling any bits. Note that
128-
* the results will be different on big-endian and little-endian
129-
* platforms, so they should not be stored or transferred over the net.
130-
*/
131-
static inline unsigned int oidhash(const struct object_id *oid)
132-
{
133-
/*
134-
* Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
135-
* platforms that don't support unaligned reads.
136-
*/
137-
unsigned int hash;
138-
memcpy(&hash, oid->hash, sizeof(hash));
139-
return hash;
140-
}
141-
142121
/*
143122
* struct hashmap_entry is an opaque structure representing an entry in the
144123
* hash table.

khash.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#ifndef __AC_KHASH_H
2727
#define __AC_KHASH_H
2828

29-
#include "hashmap.h"
3029
#include "hash.h"
3130

3231
#define AC_VERSION_KHASH_H "0.2.8"

remote.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef REMOTE_H
22
#define REMOTE_H
33

4+
#include "hash-ll.h"
45
#include "hashmap.h"
56
#include "refspec.h"
67

serve.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "git-compat-util.h"
22
#include "repository.h"
33
#include "config.h"
4+
#include "hash-ll.h"
45
#include "pkt-line.h"
56
#include "version.h"
67
#include "ls-refs.h"

0 commit comments

Comments
 (0)