xref: /aosp_15_r20/external/libnl/include/netlink/hash.h (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /*
2  * This file was taken from http://ccodearchive.net/info/hash.html
3  * Changes to the original file include cleanups and removal of unwanted code
4  * and also code that depended on build_asert
5  */
6 #ifndef CCAN_HASH_H
7 #define CCAN_HASH_H
8 
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <endian.h>
12 
13 /* Stolen mostly from: lookup3.c, by Bob Jenkins, May 2006, Public Domain.
14  *
15  * http://burtleburtle.net/bob/c/lookup3.c
16  */
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #ifdef __LITTLE_ENDIAN
23 #   define HAVE_LITTLE_ENDIAN 1
24 #elif __BIG_ENDIAN
25 #   define HAVE_BIG_ENDIAN 1
26 #else
27 #error Unknown endianness.  Failure in endian.h
28 #endif
29 
30 /**
31  * hash - fast hash of an array for internal use
32  * @p: the array or pointer to first element
33  * @num: the number of elements to hash
34  * @base: the base number to roll into the hash (usually 0)
35  *
36  * The memory region pointed to by p is combined with the base to form
37  * a 32-bit hash.
38  *
39  * This hash will have different results on different machines, so is
40  * only useful for internal hashes (ie. not hashes sent across the
41  * network or saved to disk).
42  *
43  * It may also change with future versions: it could even detect at runtime
44  * what the fastest hash to use is.
45  *
46  * See also: hash64, hash_stable.
47  *
48  * Example:
49  *	#include <ccan/hash/hash.h>
50  *	#include <err.h>
51  *	#include <stdio.h>
52  *	#include <string.h>
53  *
54  *	// Simple demonstration: idential strings will have the same hash, but
55  *	// two different strings will probably not.
56  *	int main(int argc, char *argv[])
57  *	{
58  *		uint32_t hash1, hash2;
59  *
60  *		if (argc != 3)
61  *			err(1, "Usage: %s <string1> <string2>", argv[0]);
62  *
63  *		hash1 = __nl_hash(argv[1], strlen(argv[1]), 0);
64  *		hash2 = __nl_hash(argv[2], strlen(argv[2]), 0);
65  *		printf("Hash is %s\n", hash1 == hash2 ? "same" : "different");
66  *		return 0;
67  *	}
68  */
69 #define __nl_hash(p, num, base) nl_hash_any((p), (num)*sizeof(*(p)), (base))
70 
71 /* Our underlying operations. */
72 uint32_t nl_hash_any(const void *key, size_t length, uint32_t base);
73 
74 #ifdef __cplusplus
75 }
76 #endif
77 
78 #endif /* HASH_H */
79