OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_DISK_CACHE_STORAGE_BLOCK_INL_H_ | 5 #ifndef NET_DISK_CACHE_STORAGE_BLOCK_INL_H_ |
6 #define NET_DISK_CACHE_STORAGE_BLOCK_INL_H_ | 6 #define NET_DISK_CACHE_STORAGE_BLOCK_INL_H_ |
7 | 7 |
8 #include "net/disk_cache/storage_block.h" | 8 #include "net/disk_cache/storage_block.h" |
9 | 9 |
10 #include "base/hash.h" | 10 #include "base/hash.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "net/disk_cache/trace.h" | 12 #include "net/disk_cache/trace.h" |
13 | 13 |
14 namespace disk_cache { | 14 namespace disk_cache { |
15 | 15 |
| 16 template<typename T> StorageBlock<T>::StorageBlock() |
| 17 : data_(NULL), |
| 18 file_(NULL), |
| 19 modified_(false), |
| 20 own_data_(false), |
| 21 extended_(false) { |
| 22 } |
| 23 |
16 template<typename T> StorageBlock<T>::StorageBlock(MappedFile* file, | 24 template<typename T> StorageBlock<T>::StorageBlock(MappedFile* file, |
17 Addr address) | 25 Addr address) |
18 : data_(NULL), file_(file), address_(address), modified_(false), | 26 : data_(NULL), file_(file), address_(address), modified_(false), |
19 own_data_(false), extended_(false) { | 27 own_data_(false), extended_(false) { |
20 if (address.num_blocks() > 1) | 28 if (address.num_blocks() > 1) |
21 extended_ = true; | 29 extended_ = true; |
22 DCHECK(!address.is_initialized() || sizeof(*data_) == address.BlockSize()); | 30 DCHECK(!address.is_initialized() || sizeof(*data_) == address.BlockSize()); |
23 } | 31 } |
24 | 32 |
25 template<typename T> StorageBlock<T>::~StorageBlock() { | 33 template<typename T> StorageBlock<T>::~StorageBlock() { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 template<typename T> void StorageBlock<T>::clear_modified() { | 99 template<typename T> void StorageBlock<T>::clear_modified() { |
92 modified_ = false; | 100 modified_ = false; |
93 } | 101 } |
94 | 102 |
95 template<typename T> T* StorageBlock<T>::Data() { | 103 template<typename T> T* StorageBlock<T>::Data() { |
96 if (!data_) | 104 if (!data_) |
97 AllocateData(); | 105 AllocateData(); |
98 return data_; | 106 return data_; |
99 } | 107 } |
100 | 108 |
| 109 template<typename T> T* StorageBlock<T>::ReleaseData() { |
| 110 if (!own_data_ || !data_) |
| 111 return NULL; |
| 112 |
| 113 data_->self_hash = CalculateHash(); |
| 114 T* result = data_; |
| 115 data_ = NULL; |
| 116 own_data_ = false; |
| 117 modified_ = false; |
| 118 return result; |
| 119 } |
| 120 |
101 template<typename T> bool StorageBlock<T>::HasData() const { | 121 template<typename T> bool StorageBlock<T>::HasData() const { |
102 return (NULL != data_); | 122 return (NULL != data_); |
103 } | 123 } |
104 | 124 |
105 template<typename T> bool StorageBlock<T>::VerifyHash() const { | 125 template<typename T> bool StorageBlock<T>::VerifyHash() const { |
106 uint32 hash = CalculateHash(); | 126 uint32 hash = CalculateHash(); |
107 return (!data_->self_hash || data_->self_hash == hash); | 127 return (!data_->self_hash || data_->self_hash == hash); |
108 } | 128 } |
109 | 129 |
| 130 template<typename T> void StorageBlock<T>::UpdateHash() { |
| 131 data_->self_hash = CalculateHash(); |
| 132 } |
| 133 |
110 template<typename T> bool StorageBlock<T>::own_data() const { | 134 template<typename T> bool StorageBlock<T>::own_data() const { |
111 return own_data_; | 135 return own_data_; |
112 } | 136 } |
113 | 137 |
114 template<typename T> const Addr StorageBlock<T>::address() const { | 138 template<typename T> const Addr StorageBlock<T>::address() const { |
115 return address_; | 139 return address_; |
116 } | 140 } |
117 | 141 |
118 template<typename T> bool StorageBlock<T>::Load() { | 142 template<typename T> bool StorageBlock<T>::Load() { |
119 if (file_) { | 143 if (file_) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 } | 220 } |
197 } | 221 } |
198 | 222 |
199 template<typename T> uint32 StorageBlock<T>::CalculateHash() const { | 223 template<typename T> uint32 StorageBlock<T>::CalculateHash() const { |
200 return base::Hash(reinterpret_cast<char*>(data_), offsetof(T, self_hash)); | 224 return base::Hash(reinterpret_cast<char*>(data_), offsetof(T, self_hash)); |
201 } | 225 } |
202 | 226 |
203 } // namespace disk_cache | 227 } // namespace disk_cache |
204 | 228 |
205 #endif // NET_DISK_CACHE_STORAGE_BLOCK_INL_H_ | 229 #endif // NET_DISK_CACHE_STORAGE_BLOCK_INL_H_ |
OLD | NEW |