1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IORAP_SRC_INODE2FILENAME_INODE_H_
16 #define IORAP_SRC_INODE2FILENAME_INODE_H_
17 
18 #include <functional>
19 #include <ostream>
20 #include <string>
21 
22 #include <stddef.h>
23 
24 namespace iorap::inode2filename {
25 
26 struct Inode {
27   size_t device_major;  // dev_t = makedev(major, minor)
28   size_t device_minor;
29   size_t inode;         // ino_t = inode
30 
31   static bool Parse(const std::string& str, /*out*/Inode* out, /*out*/std::string* error_msg);
32 
33   bool operator==(const Inode& rhs) const {
34     return device_major == rhs.device_major &&
35         device_minor == rhs.device_minor &&
36         inode == rhs.inode;
37   }
38 
39   bool operator!=(const Inode& rhs) const {
40     return !(*this == rhs);
41   }
42 };
43 
44 inline std::ostream& operator<<(std::ostream& os, const Inode& inode) {
45   os << inode.device_major << ":" << inode.device_minor << ":" << inode.inode;
46   return os;
47 }
48 
49 }  // namespace iorap::inode2filename
50 
51 namespace std {
52   template <>
53   struct hash<iorap::inode2filename::Inode> {
54       using argument_type = iorap::inode2filename::Inode;
55       using result_type = size_t;
56       result_type operator()(argument_type const& s) const noexcept {
57         // Hash the inode by using only the inode#. Ignore devices, we are extremely unlikely
58         // to ever collide on the devices.
59         result_type const h1 = std::hash<size_t>{}(s.inode);
60         return h1;
61       }
62   };
63 }  // namespace std
64 
65 #endif  // IORAP_SRC_INODE2FILENAME_INODE_H_
66