| 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 16 matching lines...) Expand all Loading... |
| 136 if (file_->Store(this)) { | 160 if (file_->Store(this)) { |
| 137 modified_ = false; | 161 modified_ = false; |
| 138 return true; | 162 return true; |
| 139 } | 163 } |
| 140 } | 164 } |
| 141 LOG(ERROR) << "Failed data store."; | 165 LOG(ERROR) << "Failed data store."; |
| 142 Trace("Failed data store."); | 166 Trace("Failed data store."); |
| 143 return false; | 167 return false; |
| 144 } | 168 } |
| 145 | 169 |
| 170 template<typename T> bool StorageBlock<T>::Load(FileIOCallback* callback, |
| 171 bool* completed) { |
| 172 if (file_) { |
| 173 if (!data_) |
| 174 AllocateData(); |
| 175 |
| 176 if (file_->Load(this, callback, completed)) { |
| 177 modified_ = false; |
| 178 return true; |
| 179 } |
| 180 } |
| 181 LOG(WARNING) << "Failed data load."; |
| 182 Trace("Failed data load."); |
| 183 return false; |
| 184 } |
| 185 |
| 186 template<typename T> bool StorageBlock<T>::Store(FileIOCallback* callback, |
| 187 bool* completed) { |
| 188 if (file_ && data_) { |
| 189 data_->self_hash = CalculateHash(); |
| 190 if (file_->Store(this, callback, completed)) { |
| 191 modified_ = false; |
| 192 return true; |
| 193 } |
| 194 } |
| 195 LOG(ERROR) << "Failed data store."; |
| 196 Trace("Failed data store."); |
| 197 return false; |
| 198 } |
| 199 |
| 146 template<typename T> void StorageBlock<T>::AllocateData() { | 200 template<typename T> void StorageBlock<T>::AllocateData() { |
| 147 DCHECK(!data_); | 201 DCHECK(!data_); |
| 148 if (!extended_) { | 202 if (!extended_) { |
| 149 data_ = new T; | 203 data_ = new T; |
| 150 } else { | 204 } else { |
| 151 void* buffer = new char[address_.num_blocks() * sizeof(*data_)]; | 205 void* buffer = new char[address_.num_blocks() * sizeof(*data_)]; |
| 152 data_ = new(buffer) T; | 206 data_ = new(buffer) T; |
| 153 } | 207 } |
| 154 own_data_ = true; | 208 own_data_ = true; |
| 155 } | 209 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 166 } | 220 } |
| 167 } | 221 } |
| 168 | 222 |
| 169 template<typename T> uint32 StorageBlock<T>::CalculateHash() const { | 223 template<typename T> uint32 StorageBlock<T>::CalculateHash() const { |
| 170 return base::Hash(reinterpret_cast<char*>(data_), offsetof(T, self_hash)); | 224 return base::Hash(reinterpret_cast<char*>(data_), offsetof(T, self_hash)); |
| 171 } | 225 } |
| 172 | 226 |
| 173 } // namespace disk_cache | 227 } // namespace disk_cache |
| 174 | 228 |
| 175 #endif // NET_DISK_CACHE_STORAGE_BLOCK_INL_H_ | 229 #endif // NET_DISK_CACHE_STORAGE_BLOCK_INL_H_ |
| OLD | NEW |