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