From 039dc71a7cb824300e242f8abc0fcb19dac93641 Mon Sep 17 00:00:00 2001 From: Karsten Blees Date: Thu, 3 Jul 2014 00:20:20 +0200 Subject: hashmap: factor out getting a hash code from a SHA1 Copying the first bytes of a SHA1 is duplicated in six places, however, the implications (the actual value would depend on the endianness of the platform) is documented only once. Add a properly documented API for this. Signed-off-by: Karsten Blees Signed-off-by: Junio C Hamano --- hashmap.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'hashmap.h') diff --git a/hashmap.h b/hashmap.h index a816ad47b..c51fd0abf 100644 --- a/hashmap.h +++ b/hashmap.h @@ -13,6 +13,17 @@ extern unsigned int strihash(const char *buf); extern unsigned int memhash(const void *buf, size_t len); extern unsigned int memihash(const void *buf, size_t len); +static inline unsigned int sha1hash(const unsigned char *sha1) +{ + /* + * Equivalent to 'return *(unsigned int *)sha1;', but safe on + * platforms that don't support unaligned reads. + */ + unsigned int hash; + memcpy(&hash, sha1, sizeof(hash)); + return hash; +} + /* data structures */ struct hashmap_entry { -- cgit v1.2.1