1 //===- MemoryBufferCache.h - Cache for loaded memory buffers ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_BASIC_MEMORYBUFFERCACHE_H
11 #define LLVM_CLANG_BASIC_MEMORYBUFFERCACHE_H
13 #include "llvm/ADT/IntrusiveRefCntPtr.h"
14 #include "llvm/ADT/StringMap.h"
19 } // end namespace llvm
23 /// Manage memory buffers across multiple users.
25 /// Ensures that multiple users have a consistent view of each buffer. This is
26 /// used by \a CompilerInstance when building PCMs to ensure that each \a
27 /// ModuleManager sees the same files.
29 /// \a finalizeCurrentBuffers() should be called before creating a new user.
30 /// This locks in the current buffers, ensuring that no buffer that has already
31 /// been accessed can be purged, preventing use-after-frees.
32 class MemoryBufferCache : public llvm::RefCountedBase<MemoryBufferCache> {
34 std::unique_ptr<llvm::MemoryBuffer> Buffer;
36 /// Track the timeline of when this was added to the cache.
41 llvm::StringMap<BufferEntry> Buffers;
43 /// Monotonically increasing index.
44 unsigned NextIndex = 0;
46 /// Bumped to prevent "older" buffers from being removed.
47 unsigned FirstRemovableIndex = 0;
50 /// Store the Buffer under the Filename.
52 /// \pre There is not already buffer is not already in the cache.
53 /// \return a reference to the buffer as a convenience.
54 llvm::MemoryBuffer &addBuffer(llvm::StringRef Filename,
55 std::unique_ptr<llvm::MemoryBuffer> Buffer);
57 /// Try to remove a buffer from the cache.
59 /// \return false on success, iff \c !isBufferFinal().
60 bool tryToRemoveBuffer(llvm::StringRef Filename);
62 /// Get a pointer to the buffer if it exists; else nullptr.
63 llvm::MemoryBuffer *lookupBuffer(llvm::StringRef Filename);
65 /// Check whether the buffer is final.
67 /// \return true iff \a finalizeCurrentBuffers() has been called since the
68 /// buffer was added. This prevents buffers from being removed.
69 bool isBufferFinal(llvm::StringRef Filename);
71 /// Finalize the current buffers in the cache.
73 /// Should be called when creating a new user to ensure previous uses aren't
75 void finalizeCurrentBuffers();
78 } // end namespace clang
80 #endif // LLVM_CLANG_BASIC_MEMORYBUFFERCACHE_H