OLD | NEW |
(Empty) | |
| 1 //==- ThreadedStreamingCache.h - Cache for StreamingMemoryObject -*- C++ -*-==// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 |
| 10 #ifndef THREADEDSTREAMINGCACHE_H |
| 11 #define THREADEDSTREAMINGCACHE_H |
| 12 |
| 13 #include "llvm/Support/ErrorHandling.h" |
| 14 #include "llvm/Support/Mutex.h" |
| 15 #include "llvm/Support/StreamingMemoryObject.h" |
| 16 |
| 17 namespace llvm { |
| 18 |
| 19 // An implementation of StreamingMemoryObject for use in multithreaded |
| 20 // translation. Each thread has one of these objects, each of which has a |
| 21 // pointer to a shared StreamingMemoryObject. This object is effectively |
| 22 // a thread-local cache for the bitcode streamer to avoid contention, since |
| 23 // bits are only read from the bitcode stream one word at a time. |
| 24 |
| 25 class ThreadedStreamingCache : public llvm::StreamingMemoryObject { |
| 26 public: |
| 27 explicit ThreadedStreamingCache(llvm::StreamingMemoryObject *S); |
| 28 uint64_t getExtent() const override; |
| 29 uint64_t readBytes(uint8_t *Buf, uint64_t Size, |
| 30 uint64_t Address) const override; |
| 31 const uint8_t *getPointer(uint64_t Address, |
| 32 uint64_t Size) const override { |
| 33 // This could be fixed by ensuring the bytes are fetched and making a copy, |
| 34 // requiring that the bitcode size be known, or otherwise ensuring that |
| 35 // the memory doesn't go away/get reallocated, but it's |
| 36 // not currently necessary. Users that need the pointer don't stream. |
| 37 llvm_unreachable("getPointer in streaming memory objects not allowed"); |
| 38 return NULL; |
| 39 } |
| 40 bool isValidAddress(uint64_t Address) const override; |
| 41 |
| 42 /// Drop s bytes from the front of the stream, pushing the positions of the |
| 43 /// remaining bytes down by s. This is used to skip past the bitcode header, |
| 44 /// since we don't know a priori if it's present, and we can't put bytes |
| 45 /// back into the stream once we've read them. |
| 46 bool dropLeadingBytes(size_t S) override; |
| 47 |
| 48 /// If the data object size is known in advance, many of the operations can |
| 49 /// be made more efficient, so this method should be called before reading |
| 50 /// starts (although it can be called anytime). |
| 51 void setKnownObjectSize(size_t Size) override; |
| 52 private: |
| 53 const static uint64_t kCacheSize = 4 * 4096; |
| 54 const static uint64_t kCacheSizeMask = ~(kCacheSize - 1); |
| 55 static llvm::sys::SmartMutex<false> StreamerLock; |
| 56 |
| 57 // Fetch up to kCacheSize worth of data starting from Address, into the |
| 58 // CacheBase, and set MinObjectSize to the new known edge. |
| 59 // If at EOF, MinObjectSize reflects the final size. |
| 60 void fetchCacheLine(uint64_t Address) const; |
| 61 |
| 62 llvm::StreamingMemoryObject *Streamer; |
| 63 // Cached data for addresses [CacheBase, CacheBase + kCacheSize) |
| 64 mutable std::vector<unsigned char> Cache; |
| 65 // The MemoryObject is at least this size. Used as a cache for isValidAddress. |
| 66 mutable uint64_t MinObjectSize; |
| 67 // Current base address for the cache. |
| 68 mutable uint64_t CacheBase; |
| 69 |
| 70 ThreadedStreamingCache( |
| 71 const ThreadedStreamingCache&) LLVM_DELETED_FUNCTION; |
| 72 void operator=(const ThreadedStreamingCache&) LLVM_DELETED_FUNCTION; |
| 73 }; |
| 74 |
| 75 } // namespace llvm |
| 76 |
| 77 #endif // THREADEDSTREAMINGCACHE_H |
OLD | NEW |