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 #ifndef NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_
6 #define NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_
8 #include "net/disk_cache/blockfile/storage_block.h"
10 #include "base/hash.h"
11 #include "base/logging.h"
12 #include "net/disk_cache/blockfile/trace.h"
14 namespace disk_cache
{
16 template<typename T
> StorageBlock
<T
>::StorageBlock(MappedFile
* file
,
18 : data_(NULL
), file_(file
), address_(address
), modified_(false),
19 own_data_(false), extended_(false) {
20 if (address
.num_blocks() > 1)
22 DCHECK(!address
.is_initialized() || sizeof(*data_
) == address
.BlockSize());
25 template<typename T
> StorageBlock
<T
>::~StorageBlock() {
31 template<typename T
> void* StorageBlock
<T
>::buffer() const {
35 template<typename T
> size_t StorageBlock
<T
>::size() const {
37 return sizeof(*data_
);
38 return address_
.num_blocks() * sizeof(*data_
);
41 template<typename T
> int StorageBlock
<T
>::offset() const {
42 return address_
.start_block() * address_
.BlockSize();
45 template<typename T
> bool StorageBlock
<T
>::LazyInit(MappedFile
* file
,
47 if (file_
|| address_
.is_initialized()) {
52 address_
.set_value(address
.value());
53 if (address
.num_blocks() > 1)
56 DCHECK(sizeof(*data_
) == address
.BlockSize());
60 template<typename T
> void StorageBlock
<T
>::SetData(T
* other
) {
66 template<typename T
> void StorageBlock
<T
>::Discard() {
79 template<typename T
> void StorageBlock
<T
>::StopSharingData() {
80 if (!data_
|| own_data_
)
86 template<typename T
> void StorageBlock
<T
>::set_modified() {
91 template<typename T
> void StorageBlock
<T
>::clear_modified() {
95 template<typename T
> T
* StorageBlock
<T
>::Data() {
101 template<typename T
> bool StorageBlock
<T
>::HasData() const {
102 return (NULL
!= data_
);
105 template<typename T
> bool StorageBlock
<T
>::VerifyHash() const {
106 uint32 hash
= CalculateHash();
107 return (!data_
->self_hash
|| data_
->self_hash
== hash
);
110 template<typename T
> bool StorageBlock
<T
>::own_data() const {
114 template<typename T
> const Addr StorageBlock
<T
>::address() const {
118 template<typename T
> bool StorageBlock
<T
>::Load() {
123 if (file_
->Load(this)) {
128 LOG(WARNING
) << "Failed data load.";
129 Trace("Failed data load.");
133 template<typename T
> bool StorageBlock
<T
>::Store() {
134 if (file_
&& data_
) {
135 data_
->self_hash
= CalculateHash();
136 if (file_
->Store(this)) {
141 LOG(ERROR
) << "Failed data store.";
142 Trace("Failed data store.");
146 template<typename T
> bool StorageBlock
<T
>::Load(FileIOCallback
* callback
,
152 if (file_
->Load(this, callback
, completed
)) {
157 LOG(WARNING
) << "Failed data load.";
158 Trace("Failed data load.");
162 template<typename T
> bool StorageBlock
<T
>::Store(FileIOCallback
* callback
,
164 if (file_
&& data_
) {
165 data_
->self_hash
= CalculateHash();
166 if (file_
->Store(this, callback
, completed
)) {
171 LOG(ERROR
) << "Failed data store.";
172 Trace("Failed data store.");
176 template<typename T
> void StorageBlock
<T
>::AllocateData() {
181 void* buffer
= new char[address_
.num_blocks() * sizeof(*data_
)];
182 data_
= new(buffer
) T
;
187 template<typename T
> void StorageBlock
<T
>::DeleteData() {
193 delete[] reinterpret_cast<char*>(data_
);
199 template<typename T
> uint32 StorageBlock
<T
>::CalculateHash() const {
200 return base::Hash(reinterpret_cast<char*>(data_
), offsetof(T
, self_hash
));
203 } // namespace disk_cache
205 #endif // NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_