clang  9.0.0svn
MemoryBufferCache.h
Go to the documentation of this file.
1 //===- MemoryBufferCache.h - Cache for loaded memory buffers ----*- C++ -*-===//
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 
9 #ifndef LLVM_CLANG_BASIC_MEMORYBUFFERCACHE_H
10 #define LLVM_CLANG_BASIC_MEMORYBUFFERCACHE_H
11 
12 #include "llvm/ADT/IntrusiveRefCntPtr.h"
13 #include "llvm/ADT/StringMap.h"
14 #include <memory>
15 
16 namespace llvm {
17 class MemoryBuffer;
18 } // end namespace llvm
19 
20 namespace clang {
21 
22 /// Manage memory buffers across multiple users.
23 ///
24 /// Ensures that multiple users have a consistent view of each buffer. This is
25 /// used by \a CompilerInstance when building PCMs to ensure that each \a
26 /// ModuleManager sees the same files.
27 ///
28 /// \a finalizeCurrentBuffers() should be called before creating a new user.
29 /// This locks in the current buffers, ensuring that no buffer that has already
30 /// been accessed can be purged, preventing use-after-frees.
31 class MemoryBufferCache : public llvm::RefCountedBase<MemoryBufferCache> {
32  struct BufferEntry {
33  std::unique_ptr<llvm::MemoryBuffer> Buffer;
34 
35  /// Track the timeline of when this was added to the cache.
36  unsigned Index;
37  };
38 
39  /// Cache of buffers.
40  llvm::StringMap<BufferEntry> Buffers;
41 
42  /// Monotonically increasing index.
43  unsigned NextIndex = 0;
44 
45  /// Bumped to prevent "older" buffers from being removed.
46  unsigned FirstRemovableIndex = 0;
47 
48 public:
49  /// Store the Buffer under the Filename.
50  ///
51  /// \pre There is not already buffer is not already in the cache.
52  /// \return a reference to the buffer as a convenience.
53  llvm::MemoryBuffer &addBuffer(llvm::StringRef Filename,
54  std::unique_ptr<llvm::MemoryBuffer> Buffer);
55 
56  /// Try to remove a buffer from the cache.
57  ///
58  /// \return false on success, iff \c !isBufferFinal().
59  bool tryToRemoveBuffer(llvm::StringRef Filename);
60 
61  /// Get a pointer to the buffer if it exists; else nullptr.
62  llvm::MemoryBuffer *lookupBuffer(llvm::StringRef Filename);
63 
64  /// Check whether the buffer is final.
65  ///
66  /// \return true iff \a finalizeCurrentBuffers() has been called since the
67  /// buffer was added. This prevents buffers from being removed.
68  bool isBufferFinal(llvm::StringRef Filename);
69 
70  /// Finalize the current buffers in the cache.
71  ///
72  /// Should be called when creating a new user to ensure previous uses aren't
73  /// invalidated.
74  void finalizeCurrentBuffers();
75 };
76 
77 } // end namespace clang
78 
79 #endif // LLVM_CLANG_BASIC_MEMORYBUFFERCACHE_H
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:29
Manage memory buffers across multiple users.
StringRef Filename
Definition: Format.cpp:1623
Dataflow Directional Tag Classes.