xref: /freebsd/sys/dev/vmware/vmci/vmci_utils.h (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
1 /*-
2  * Copyright (c) 2018 VMware, Inc.
3  *
4  * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
5  */
6 
7 /* Some common utilities used by the VMCI kernel module. */
8 
9 #ifndef _VMCI_UTILS_H_
10 #define _VMCI_UTILS_H_
11 
12 /*
13  *------------------------------------------------------------------------------
14  *
15  * vmci_hash_id --
16  *
17  *     Hash function used by the Simple Datagram API. Hashes only a VMCI ID (not
18  *     the full VMCI handle). Based on the djb2 hash function by Dan Bernstein.
19  *
20  * Result:
21  *     Returns guest call size.
22  *
23  * Side effects:
24  *     None.
25  *
26  *------------------------------------------------------------------------------
27  */
28 
29 static inline int
30 vmci_hash_id(vmci_id id, unsigned size)
31 {
32 	unsigned i;
33 	int hash = 5381;
34 
35 	for (i = 0; i < sizeof(id); i++)
36 		hash = ((hash << 5) + hash) + (uint8_t)(id >> (i * 8));
37 
38 	return (hash & (size - 1));
39 }
40 
41 #endif /* !_VMCI_UTILS_H_ */
42