1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // See net/disk_cache/disk_cache.h for the public interface.
7 #ifndef NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_H_
8 #define NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_H_
10 #include "net/disk_cache/blockfile/addr.h"
11 #include "net/disk_cache/blockfile/mapped_file.h"
13 namespace disk_cache
{
15 // This class encapsulates common behavior of a single "block" of data that is
16 // stored on a block-file. It implements the FileBlock interface, so it can be
17 // serialized directly to the backing file.
18 // This object provides a memory buffer for the related data, and it can be used
19 // to actually share that memory with another instance of the class.
21 // The following example shows how to share storage with another object:
22 // StorageBlock<TypeA> a(file, address);
23 // StorageBlock<TypeB> b(file, address);
25 // DoSomething(a.Data());
26 // b.SetData(a.Data());
27 // ModifySomething(b.Data());
28 // // Data modified on the previous call will be saved by b's destructor.
31 class StorageBlock
: public FileBlock
{
33 StorageBlock(MappedFile
* file
, Addr address
);
34 virtual ~StorageBlock();
36 // FileBlock interface.
37 void* buffer() const override
;
38 size_t size() const override
;
39 int offset() const override
;
41 // Allows the overide of dummy values passed on the constructor.
42 bool LazyInit(MappedFile
* file
, Addr address
);
44 // Sets the internal storage to share the memory provided by other instance.
45 void SetData(T
* other
);
47 // Deletes the data, even if it was modified and not saved. This object must
48 // own the memory buffer (it cannot be shared).
51 // Stops sharing the data with another object.
52 void StopSharingData();
54 // Sets the object to lazily save the in-memory data on destruction.
57 // Forgets that the data was modified, so it's not lazily saved.
58 void clear_modified();
60 // Gets a pointer to the internal storage (allocates storage if needed).
63 // Returns true if there is data associated with this object.
66 // Returns true if the internal hash is correct.
67 bool VerifyHash() const;
69 // Returns true if this object owns the data buffer, false if it is shared.
70 bool own_data() const;
72 const Addr
address() const;
74 // Loads and store the data.
77 bool Load(FileIOCallback
* callback
, bool* completed
);
78 bool Store(FileIOCallback
* callback
, bool* completed
);
83 uint32
CalculateHash() const;
89 bool own_data_
; // Is data_ owned by this object or shared with someone else.
90 bool extended_
; // Used to store an entry of more than one block.
92 DISALLOW_COPY_AND_ASSIGN(StorageBlock
);
95 } // namespace disk_cache
97 #endif // NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_H_