clang 23.0.0git
InMemoryModuleCache.cpp
Go to the documentation of this file.
1//===- InMemoryModuleCache.cpp - Cache for loaded memory buffers ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/Support/MemoryBuffer.h"
11
12using namespace clang;
13
15InMemoryModuleCache::getPCMState(llvm::StringRef Filename) const {
16 auto I = PCMs.find(Filename);
17 if (I == PCMs.end())
18 return Unknown;
19 if (I->second.IsFinal)
20 return Final;
21 return I->second.Buffer ? Tentative : ToBuild;
22}
23
24llvm::MemoryBuffer &
25InMemoryModuleCache::addPCM(llvm::StringRef Filename,
26 std::unique_ptr<llvm::MemoryBuffer> Buffer,
27 off_t Size, time_t ModTime) {
28 auto Insertion = PCMs.insert(
29 std::make_pair(Filename, PCM(std::move(Buffer), Size, ModTime)));
30 assert(Insertion.second && "Already has a PCM");
31 return *Insertion.first->second.Buffer;
32}
33
34llvm::MemoryBuffer &
35InMemoryModuleCache::addBuiltPCM(llvm::StringRef Filename,
36 std::unique_ptr<llvm::MemoryBuffer> Buffer,
37 off_t Size, time_t ModTime) {
38 auto &PCM = PCMs[Filename];
39 assert(!PCM.IsFinal && "Trying to override finalized PCM?");
40 assert(!PCM.Buffer && "Trying to override tentative PCM?");
41 PCM.Buffer = std::move(Buffer);
42 PCM.Size = Size;
43 PCM.ModTime = ModTime;
44 PCM.IsFinal = true;
45 return *PCM.Buffer;
46}
47
48llvm::MemoryBuffer *InMemoryModuleCache::lookupPCM(llvm::StringRef Filename,
49 off_t &Size,
50 time_t &ModTime) const {
51 auto I = PCMs.find(Filename);
52 if (I == PCMs.end())
53 return nullptr;
54 Size = I->second.Size;
55 ModTime = I->second.ModTime;
56 return I->second.Buffer.get();
57}
58
59bool InMemoryModuleCache::isPCMFinal(llvm::StringRef Filename) const {
60 return getPCMState(Filename) == Final;
61}
62
63bool InMemoryModuleCache::shouldBuildPCM(llvm::StringRef Filename) const {
64 return getPCMState(Filename) == ToBuild;
65}
66
67bool InMemoryModuleCache::tryToDropPCM(llvm::StringRef Filename) {
68 auto I = PCMs.find(Filename);
69 assert(I != PCMs.end() && "PCM to remove is unknown...");
70
71 auto &PCM = I->second;
72 assert(PCM.Buffer && "PCM to remove is scheduled to be built...");
73
74 if (PCM.IsFinal)
75 return true;
76
77 PCM.Buffer.reset();
78 return false;
79}
80
81void InMemoryModuleCache::finalizePCM(llvm::StringRef Filename) {
82 auto I = PCMs.find(Filename);
83 assert(I != PCMs.end() && "PCM to finalize is unknown...");
84
85 auto &PCM = I->second;
86 assert(PCM.Buffer && "Trying to finalize a dropped PCM...");
87 PCM.IsFinal = true;
88}
llvm::MemoryBuffer * lookupPCM(llvm::StringRef Filename, off_t &Size, time_t &ModTime) const
Get a pointer to the PCM if it exists and set Size and ModTime to its on-disk size and modification t...
llvm::MemoryBuffer & addPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer, off_t Size, time_t ModTime)
Store the PCM under the Filename.
State getPCMState(llvm::StringRef Filename) const
Get the state of the PCM.
State
There are four states for a PCM.
bool isPCMFinal(llvm::StringRef Filename) const
Check whether the PCM is final and has been shown to work.
void finalizePCM(llvm::StringRef Filename)
Mark a PCM as final.
bool tryToDropPCM(llvm::StringRef Filename)
Try to remove a buffer from the cache.
bool shouldBuildPCM(llvm::StringRef Filename) const
Check whether the PCM is waiting to be built.
llvm::MemoryBuffer & addBuiltPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer, off_t Size, time_t ModTime)
Store a just-built PCM under the Filename.
The JSON file list parser is used to communicate input to InstallAPI.