]> git.ipfire.org Git - thirdparty/ccache.git/blame - src/InodeCache.hpp
C++-ify is_precompiled_header and move to Util
[thirdparty/ccache.git] / src / InodeCache.hpp
CommitLineData
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
JR
25#include <stdint.h>
26#include <string>
213d9883
OL
27
28class Config;
29class Context;
0a189c2c 30class Digest;
213d9883
OL
31
32class InodeCache
33{
34public:
35 // Specifies in which role a file was hashed, since the hash result does not
36 // only depend on the actual content but also what we used it for. Source code
37 // files are scanned for macros while binary files are not as one example.
38 enum class ContentType {
39 binary = 0,
40 code = 1,
41 code_with_sloppy_time_macros = 2,
42 precompiled_header = 3,
43 };
44
45 InodeCache(const Config& config);
46 ~InodeCache();
47
48 // Get saved hash digest and return value from a previous call to
49 // hash_source_code_file().
50 //
51 // Returns true if saved values could be retrieved from the cache, false
52 // otherwise.
53 bool get(const char* path,
54 ContentType type,
0a189c2c 55 Digest& file_digest,
213d9883
OL
56 int* return_value = nullptr);
57
58 // Put hash digest and return value from a successful call to
59 // hash_source_code_file().
60 //
61 // Returns true if values could be stored in the cache, false otherwise.
62 bool put(const char* path,
63 ContentType type,
0a189c2c 64 const Digest& file_digest,
213d9883
OL
65 int return_value = 0);
66
67 // Unmaps the current cache and removes the mapped file from disk.
68 //
69 // Returns true on success, false otherwise.
70 bool drop();
71
72 // Returns name of the persistent file.
73 std::string get_file();
74
75 // Returns total number of cache hits.
76 //
77 // Counters are incremented in debug mode only.
78 int64_t get_hits();
79
80 // Returns total number of cache misses.
81 //
82 // Counters are incremented in debug mode only.
83 int64_t get_misses();
84
85 // Returns total number of errors.
86 //
87 // Currently only lock errors will be counted, since the counter is not
88 // accessible before the file has been successfully mapped into memory.
89 //
90 // Counters are incremented in debug mode only.
91 int64_t get_errors();
92
93private:
94 struct Bucket;
95 struct Entry;
96 struct Key;
97 struct SharedRegion;
98
99 bool mmap_file(const std::string& inode_cache_file);
17f00366 100 static bool hash_inode(const char* 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};