1 // Copyright 2015 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 // 17 // LRU cache decorator for binary_parse::PagedByteArray subclasses. 18 19 #ifndef PIEX_BINARY_PARSE_CACHED_PAGED_BYTE_ARRAY_H_ 20 #define PIEX_BINARY_PARSE_CACHED_PAGED_BYTE_ARRAY_H_ 21 22 #include <mutex> 23 #include <vector> 24 25 #if !defined(WIN32_LEAN_AND_MEAN) 26 #define WIN32_LEAN_AND_MEAN 27 #endif 28 #include "src/binary_parse/range_checked_byte_ptr.h" 29 30 namespace piex { 31 namespace binary_parse { 32 33 class CachedPagedByteArray : public PagedByteArray { 34 public: 35 // Decorates 'paged_byte_array' with a LRU cache layer of the size 36 // 'cache_size'. 37 explicit CachedPagedByteArray(const PagedByteArray* paged_byte_array, 38 size_t cache_size); 39 length()40 virtual size_t length() const { return paged_byte_array_->length(); } 41 pageSize()42 virtual size_t pageSize() const { return paged_byte_array_->pageSize(); } 43 44 virtual void getPage(size_t page_index, const unsigned char** begin, 45 const unsigned char** end, 46 PagedByteArray::PagePtr* page) const; 47 48 private: 49 struct CachedPage { 50 size_t index; 51 PagedByteArray::PagePtr page; 52 const unsigned char* begin; 53 const unsigned char* end; 54 }; 55 56 // Disallow copy construction and assignment. 57 CachedPagedByteArray(const CachedPagedByteArray&); 58 void operator=(const CachedPagedByteArray&); 59 60 // Gets the index of the page if it is in the cache and returns true, else 61 // returns false. 62 bool getFromCache(size_t page_index, size_t* cache_index) const; 63 64 mutable std::mutex mutex_; 65 const PagedByteArray* paged_byte_array_; 66 const size_t cache_size_; 67 mutable std::vector<CachedPage> cached_pages_; 68 }; 69 70 } // namespace binary_parse 71 } // namespace piex 72 73 #endif // PIEX_BINARY_PARSE_CACHED_PAGED_BYTE_ARRAY_H_ 74