1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // See net/disk_cache/disk_cache.h for the public interface. 6 7 #ifndef NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_H_ 8 #define NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_H_ 9 10 #include <stddef.h> 11 #include <stdint.h> 12 13 #include "base/memory/raw_ptr.h" 14 #include "net/disk_cache/blockfile/addr.h" 15 #include "net/disk_cache/blockfile/mapped_file.h" 16 17 namespace disk_cache { 18 19 // This class encapsulates common behavior of a single "block" of data that is 20 // stored on a block-file. It implements the FileBlock interface, so it can be 21 // serialized directly to the backing file. 22 // This object provides a memory buffer for the related data, and it can be used 23 // to actually share that memory with another instance of the class. 24 // 25 // The following example shows how to share storage with another object: 26 // StorageBlock<TypeA> a(file, address); 27 // StorageBlock<TypeB> b(file, address); 28 // a.Load(); 29 // DoSomething(a.Data()); 30 // b.SetData(a.Data()); 31 // ModifySomething(b.Data()); 32 // // Data modified on the previous call will be saved by b's destructor. 33 // b.set_modified(); 34 template<typename T> 35 class StorageBlock : public FileBlock { 36 public: 37 StorageBlock(MappedFile* file, Addr address); 38 39 StorageBlock(const StorageBlock&) = delete; 40 StorageBlock& operator=(const StorageBlock&) = delete; 41 42 ~StorageBlock() override; 43 44 // Deeps copies from another block. Neither this nor |other| should be 45 // |modified|. 46 void CopyFrom(StorageBlock<T>* other); 47 48 // FileBlock interface. 49 void* buffer() const override; 50 size_t size() const override; 51 int offset() const override; 52 53 // Allows the overide of dummy values passed on the constructor. 54 bool LazyInit(MappedFile* file, Addr address); 55 56 // Sets the internal storage to share the memory provided by other instance. 57 void SetData(T* other); 58 59 // Deletes the data, even if it was modified and not saved. This object must 60 // own the memory buffer (it cannot be shared). 61 void Discard(); 62 63 // Stops sharing the data with another object. 64 void StopSharingData(); 65 66 // Sets the object to lazily save the in-memory data on destruction. 67 void set_modified(); 68 69 // Forgets that the data was modified, so it's not lazily saved. 70 void clear_modified(); 71 72 // Gets a pointer to the internal storage (allocates storage if needed). 73 T* Data(); 74 75 // Returns true if there is data associated with this object. 76 bool HasData() const; 77 78 // Returns true if the internal hash is correct. 79 bool VerifyHash() const; 80 81 // Returns true if this object owns the data buffer, false if it is shared. 82 bool own_data() const; 83 84 const Addr address() const; 85 86 // Loads and store the data. 87 bool Load(); 88 bool Store(); 89 bool Load(FileIOCallback* callback, bool* completed); 90 bool Store(FileIOCallback* callback, bool* completed); 91 92 private: 93 void AllocateData(); 94 void DeleteData(); 95 uint32_t CalculateHash() const; 96 97 raw_ptr<T> data_ = nullptr; 98 // DanglingUntriaged is largely needed for when this class is owned by an 99 // EntryImpl that is deleted after the Backend. 100 raw_ptr<MappedFile, DanglingUntriaged> file_; 101 Addr address_; 102 bool modified_ = false; 103 // Is data_ owned by this object or shared with someone else. 104 bool own_data_ = false; 105 bool extended_ = false; // Used to store an entry of more than one block. 106 }; 107 108 } // namespace disk_cache 109 110 #endif // NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_H_ 111