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