1 /******************************************************************************
2  *
3  * Copyright (c) 2009 Citrix Systems, Inc. (Grzegorz Milos)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; If not, see <http://www.gnu.org/licenses/>.
17  */
18 #ifndef __BIDIR_HASH_H__
19 #define __BIDIR_HASH_H__
20 
21 #include <stdint.h>
22 #include <string.h>
23 #include "memshr-priv.h"
24 
25 typedef struct vbdblk {
26     uint64_t sec;
27     uint16_t disk_id;
28 } vbdblk_t;
29 
30 
31 #if defined FINGERPRINT_MAP || BLOCK_MAP
32 #define DEFINE_SINGLE_MAP
33 #endif
34 
35 /*******************************************************/
36 /* Fingerprint map                                     */
37 /*******************************************************/
38 #if defined FINGERPRINT_MAP || !defined DEFINE_SINGLE_MAP
39 
40 #undef BIDIR_NAME_PREFIX
41 #undef BIDIR_KEY
42 #undef BIDIR_VALUE
43 #undef BIDIR_KEY_T
44 #undef BIDIR_VALUE_T
fgprtshr_fgprt_hash(uint32_t h)45 static uint32_t fgprtshr_fgprt_hash(uint32_t h)
46 {
47     return h;
48 }
49 
fgprtshr_mfn_hash(uint64_t m)50 static uint32_t fgprtshr_mfn_hash(uint64_t m)
51 {
52     return (uint32_t)m;
53 }
54 
fgprtshr_fgprt_cmp(uint32_t h1,uint32_t h2)55 static int fgprtshr_fgprt_cmp(uint32_t h1, uint32_t h2)
56 {
57     return (h1 == h2);
58 }
59 
fgprtshr_mfn_cmp(uint32_t m1,uint32_t m2)60 static int fgprtshr_mfn_cmp(uint32_t m1, uint32_t m2)
61 {
62     return (m1 == m2);
63 }
64 #define BIDIR_NAME_PREFIX       fgprtshr
65 #define BIDIR_KEY               fgprt
66 #define BIDIR_VALUE             mfn
67 #define BIDIR_KEY_T             uint32_t
68 #define BIDIR_VALUE_T           xen_mfn_t
69 #include "bidir-namedefs.h"
70 
71 #endif /* FINGERPRINT_MAP */
72 
73 
74 /*******************************************************/
75 /* Block<->Memory sharing handles                      */
76 /*******************************************************/
77 #if defined BLOCK_MAP || !defined DEFINE_SINGLE_MAP
78 
79 #undef BIDIR_NAME_PREFIX
80 #undef BIDIR_KEY
81 #undef BIDIR_VALUE
82 #undef BIDIR_KEY_T
83 #undef BIDIR_VALUE_T
84 
85 /* TODO better hashes! */
blockshr_block_hash(vbdblk_t block)86 static inline uint32_t blockshr_block_hash(vbdblk_t block)
87 {
88     return (uint32_t)(block.sec) ^ (uint32_t)(block.disk_id);
89 }
90 
blockshr_shrhnd_hash(share_tuple_t shrhnd)91 static inline uint32_t blockshr_shrhnd_hash(share_tuple_t shrhnd)
92 {
93     return ((uint32_t) shrhnd.handle);
94 }
95 
blockshr_block_cmp(vbdblk_t b1,vbdblk_t b2)96 static inline int blockshr_block_cmp(vbdblk_t b1, vbdblk_t b2)
97 {
98     return (b1.sec == b2.sec) && (b1.disk_id == b2.disk_id);
99 }
100 
blockshr_shrhnd_cmp(share_tuple_t h1,share_tuple_t h2)101 static inline int blockshr_shrhnd_cmp(share_tuple_t h1, share_tuple_t h2)
102 {
103     return ( !memcmp(&h1, &h2, sizeof(share_tuple_t)) );
104 }
105 #define BIDIR_NAME_PREFIX       blockshr
106 #define BIDIR_KEY               block
107 #define BIDIR_VALUE             shrhnd
108 #define BIDIR_KEY_T             vbdblk_t
109 #define BIDIR_VALUE_T           share_tuple_t
110 #include "bidir-namedefs.h"
111 
112 #endif /* BLOCK_MAP */
113 
114 #endif /* __BIDIR_HASH_H__ */
115