]>
Commit | Line | Data |
---|---|---|
213d9883 OL |
1 | // Copyright (C) 2020 Joel Rosdahl and other contributors |
2 | // | |
3 | // See doc/AUTHORS.adoc for a complete list of contributors. | |
4 | // | |
5 | // This program is free software; you can redistribute it and/or modify it | |
6 | // under the terms of the GNU General Public License as published by the Free | |
7 | // Software Foundation; either version 3 of the License, or (at your option) | |
8 | // any later version. | |
9 | // | |
10 | // This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | // more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License along with | |
16 | // this program; if not, write to the Free Software Foundation, Inc., 51 | |
17 | // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | ||
19 | #pragma once | |
20 | ||
21 | #include "system.hpp" | |
22 | ||
23 | #include "config.h" | |
24 | ||
2354adc2 | 25 | #include <string> |
213d9883 OL |
26 | |
27 | class Config; | |
28 | class Context; | |
0a189c2c | 29 | class Digest; |
213d9883 OL |
30 | |
31 | class InodeCache | |
32 | { | |
33 | public: | |
34 | // Specifies in which role a file was hashed, since the hash result does not | |
35 | // only depend on the actual content but also what we used it for. Source code | |
36 | // files are scanned for macros while binary files are not as one example. | |
37 | enum class ContentType { | |
38 | binary = 0, | |
39 | code = 1, | |
40 | code_with_sloppy_time_macros = 2, | |
41 | precompiled_header = 3, | |
42 | }; | |
43 | ||
44 | InodeCache(const Config& config); | |
45 | ~InodeCache(); | |
46 | ||
47 | // Get saved hash digest and return value from a previous call to | |
48 | // hash_source_code_file(). | |
49 | // | |
50 | // Returns true if saved values could be retrieved from the cache, false | |
51 | // otherwise. | |
2292fef1 | 52 | bool get(const std::string& path, |
213d9883 | 53 | ContentType type, |
0a189c2c | 54 | Digest& file_digest, |
213d9883 OL |
55 | int* return_value = nullptr); |
56 | ||
57 | // Put hash digest and return value from a successful call to | |
58 | // hash_source_code_file(). | |
59 | // | |
60 | // Returns true if values could be stored in the cache, false otherwise. | |
2292fef1 | 61 | bool put(const std::string& path, |
213d9883 | 62 | ContentType type, |
0a189c2c | 63 | const Digest& file_digest, |
213d9883 OL |
64 | int return_value = 0); |
65 | ||
66 | // Unmaps the current cache and removes the mapped file from disk. | |
67 | // | |
68 | // Returns true on success, false otherwise. | |
69 | bool drop(); | |
70 | ||
71 | // Returns name of the persistent file. | |
72 | std::string get_file(); | |
73 | ||
74 | // Returns total number of cache hits. | |
75 | // | |
76 | // Counters are incremented in debug mode only. | |
77 | int64_t get_hits(); | |
78 | ||
79 | // Returns total number of cache misses. | |
80 | // | |
81 | // Counters are incremented in debug mode only. | |
82 | int64_t get_misses(); | |
83 | ||
84 | // Returns total number of errors. | |
85 | // | |
86 | // Currently only lock errors will be counted, since the counter is not | |
87 | // accessible before the file has been successfully mapped into memory. | |
88 | // | |
89 | // Counters are incremented in debug mode only. | |
90 | int64_t get_errors(); | |
91 | ||
92 | private: | |
93 | struct Bucket; | |
94 | struct Entry; | |
95 | struct Key; | |
96 | struct SharedRegion; | |
97 | ||
98 | bool mmap_file(const std::string& inode_cache_file); | |
2292fef1 JR |
99 | static bool |
100 | hash_inode(const std::string& path, ContentType type, Digest& digest); | |
213d9883 | 101 | Bucket* acquire_bucket(uint32_t index); |
0a189c2c | 102 | Bucket* acquire_bucket(const Digest& key_digest); |
17f00366 JR |
103 | static void release_bucket(Bucket* bucket); |
104 | static bool create_new_file(const std::string& filename); | |
213d9883 OL |
105 | bool initialize(); |
106 | ||
107 | const Config& m_config; | |
77bf7bc3 JR |
108 | struct SharedRegion* m_sr = nullptr; |
109 | bool m_failed = false; | |
213d9883 | 110 | }; |