|
|
|||
File indexing completed on 2026-05-10 08:44:28
0001 //===- Caching.h - LLVM Local File Cache ------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This file defines the CachedFileStream and the localCache function, which 0010 // simplifies caching files on the local filesystem in a directory whose 0011 // contents are managed by a CachePruningPolicy. 0012 // 0013 //===----------------------------------------------------------------------===// 0014 0015 #ifndef LLVM_SUPPORT_CACHING_H 0016 #define LLVM_SUPPORT_CACHING_H 0017 0018 #include "llvm/Support/Error.h" 0019 0020 namespace llvm { 0021 0022 class MemoryBuffer; 0023 0024 /// This class wraps an output stream for a file. Most clients should just be 0025 /// able to return an instance of this base class from the stream callback, but 0026 /// if a client needs to perform some action after the stream is written to, 0027 /// that can be done by deriving from this class and overriding the destructor. 0028 class CachedFileStream { 0029 public: 0030 CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS, 0031 std::string OSPath = "") 0032 : OS(std::move(OS)), ObjectPathName(OSPath) {} 0033 std::unique_ptr<raw_pwrite_stream> OS; 0034 std::string ObjectPathName; 0035 virtual ~CachedFileStream() = default; 0036 }; 0037 0038 /// This type defines the callback to add a file that is generated on the fly. 0039 /// 0040 /// Stream callbacks must be thread safe. 0041 using AddStreamFn = std::function<Expected<std::unique_ptr<CachedFileStream>>( 0042 unsigned Task, const Twine &ModuleName)>; 0043 0044 /// This is a callable that manages file caching operations. It accepts a task 0045 /// ID \p Task, a unique key \p Key, and a module name \p ModuleName, and 0046 /// returns AddStreamFn(). This function determines whether a cache hit or miss 0047 /// occurs and handles the appropriate actions. 0048 using FileCacheFunction = std::function<Expected<AddStreamFn>( 0049 unsigned Task, StringRef Key, const Twine &ModuleName)>; 0050 0051 /// This type represents a file cache system that manages caching of files. 0052 /// It encapsulates a caching function and the directory path where the cache is 0053 /// stored. To request an item from the cache, pass a unique string as the Key. 0054 /// For hits, the cached file will be added to the link and this function will 0055 /// return AddStreamFn(). For misses, the cache will return a stream callback 0056 /// which must be called at most once to produce content for the stream. The 0057 /// file stream produced by the stream callback will add the file to the link 0058 /// after the stream is written to. ModuleName is the unique module identifier 0059 /// for the bitcode module the cache is being checked for. 0060 /// 0061 /// Clients generally look like this: 0062 /// 0063 /// if (AddStreamFn AddStream = Cache(Task, Key, ModuleName)) 0064 /// ProduceContent(AddStream); 0065 /// 0066 /// CacheDirectoryPath stores the directory path where cached files are kept. 0067 struct FileCache { 0068 FileCache(FileCacheFunction CacheFn, const std::string &DirectoryPath) 0069 : CacheFunction(std::move(CacheFn)), CacheDirectoryPath(DirectoryPath) {} 0070 FileCache() = default; 0071 0072 Expected<AddStreamFn> operator()(unsigned Task, StringRef Key, 0073 const Twine &ModuleName) { 0074 assert(isValid() && "Invalid cache function"); 0075 return CacheFunction(Task, Key, ModuleName); 0076 } 0077 const std::string &getCacheDirectoryPath() const { 0078 return CacheDirectoryPath; 0079 } 0080 bool isValid() const { return static_cast<bool>(CacheFunction); } 0081 0082 private: 0083 FileCacheFunction CacheFunction = nullptr; 0084 std::string CacheDirectoryPath; 0085 }; 0086 0087 /// This type defines the callback to add a pre-existing file (e.g. in a cache). 0088 /// 0089 /// Buffer callbacks must be thread safe. 0090 using AddBufferFn = std::function<void(unsigned Task, const Twine &ModuleName, 0091 std::unique_ptr<MemoryBuffer> MB)>; 0092 0093 /// Create a local file system cache which uses the given cache name, temporary 0094 /// file prefix, cache directory and file callback. This function does not 0095 /// immediately create the cache directory if it does not yet exist; this is 0096 /// done lazily the first time a file is added. The cache name appears in error 0097 /// messages for errors during caching. The temporary file prefix is used in the 0098 /// temporary file naming scheme used when writing files atomically. 0099 Expected<FileCache> localCache( 0100 const Twine &CacheNameRef, const Twine &TempFilePrefixRef, 0101 const Twine &CacheDirectoryPathRef, 0102 AddBufferFn AddBuffer = [](size_t Task, const Twine &ModuleName, 0103 std::unique_ptr<MemoryBuffer> MB) {}); 0104 } // namespace llvm 0105 0106 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|