| 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_BLOCKFILE_STORAGE_BLOCK_INL_H_ | 5 #ifndef NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_ |
| 6 #define NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_ | 6 #define NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_ |
| 7 | 7 |
| 8 #include "net/disk_cache/blockfile/storage_block.h" | 8 #include "net/disk_cache/blockfile/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/blockfile/trace.h" | 12 #include "net/disk_cache/blockfile/trace.h" |
| 13 | 13 |
| 14 namespace disk_cache { | 14 namespace disk_cache { |
| 15 | 15 |
| 16 template<typename T> StorageBlock<T>::StorageBlock(MappedFile* file, | 16 template <typename T> |
| 17 Addr address) | 17 StorageBlock<T>::StorageBlock(MappedFile* file, Addr address) |
| 18 : data_(NULL), file_(file), address_(address), modified_(false), | 18 : data_(NULL), |
| 19 own_data_(false), extended_(false) { | 19 file_(file), |
| 20 address_(address), |
| 21 modified_(false), |
| 22 own_data_(false), |
| 23 extended_(false) { |
| 20 if (address.num_blocks() > 1) | 24 if (address.num_blocks() > 1) |
| 21 extended_ = true; | 25 extended_ = true; |
| 22 DCHECK(!address.is_initialized() || sizeof(*data_) == address.BlockSize()); | 26 DCHECK(!address.is_initialized() || sizeof(*data_) == address.BlockSize()); |
| 23 } | 27 } |
| 24 | 28 |
| 25 template<typename T> StorageBlock<T>::~StorageBlock() { | 29 template <typename T> |
| 30 StorageBlock<T>::~StorageBlock() { |
| 26 if (modified_) | 31 if (modified_) |
| 27 Store(); | 32 Store(); |
| 28 DeleteData(); | 33 DeleteData(); |
| 29 } | 34 } |
| 30 | 35 |
| 31 template<typename T> void* StorageBlock<T>::buffer() const { | 36 template <typename T> |
| 37 void* StorageBlock<T>::buffer() const { |
| 32 return data_; | 38 return data_; |
| 33 } | 39 } |
| 34 | 40 |
| 35 template<typename T> size_t StorageBlock<T>::size() const { | 41 template <typename T> |
| 42 size_t StorageBlock<T>::size() const { |
| 36 if (!extended_) | 43 if (!extended_) |
| 37 return sizeof(*data_); | 44 return sizeof(*data_); |
| 38 return address_.num_blocks() * sizeof(*data_); | 45 return address_.num_blocks() * sizeof(*data_); |
| 39 } | 46 } |
| 40 | 47 |
| 41 template<typename T> int StorageBlock<T>::offset() const { | 48 template <typename T> |
| 49 int StorageBlock<T>::offset() const { |
| 42 return address_.start_block() * address_.BlockSize(); | 50 return address_.start_block() * address_.BlockSize(); |
| 43 } | 51 } |
| 44 | 52 |
| 45 template<typename T> bool StorageBlock<T>::LazyInit(MappedFile* file, | 53 template <typename T> |
| 46 Addr address) { | 54 bool StorageBlock<T>::LazyInit(MappedFile* file, Addr address) { |
| 47 if (file_ || address_.is_initialized()) { | 55 if (file_ || address_.is_initialized()) { |
| 48 NOTREACHED(); | 56 NOTREACHED(); |
| 49 return false; | 57 return false; |
| 50 } | 58 } |
| 51 file_ = file; | 59 file_ = file; |
| 52 address_.set_value(address.value()); | 60 address_.set_value(address.value()); |
| 53 if (address.num_blocks() > 1) | 61 if (address.num_blocks() > 1) |
| 54 extended_ = true; | 62 extended_ = true; |
| 55 | 63 |
| 56 DCHECK(sizeof(*data_) == address.BlockSize()); | 64 DCHECK(sizeof(*data_) == address.BlockSize()); |
| 57 return true; | 65 return true; |
| 58 } | 66 } |
| 59 | 67 |
| 60 template<typename T> void StorageBlock<T>::SetData(T* other) { | 68 template <typename T> |
| 69 void StorageBlock<T>::SetData(T* other) { |
| 61 DCHECK(!modified_); | 70 DCHECK(!modified_); |
| 62 DeleteData(); | 71 DeleteData(); |
| 63 data_ = other; | 72 data_ = other; |
| 64 } | 73 } |
| 65 | 74 |
| 66 template<typename T> void StorageBlock<T>::Discard() { | 75 template <typename T> |
| 76 void StorageBlock<T>::Discard() { |
| 67 if (!data_) | 77 if (!data_) |
| 68 return; | 78 return; |
| 69 if (!own_data_) { | 79 if (!own_data_) { |
| 70 NOTREACHED(); | 80 NOTREACHED(); |
| 71 return; | 81 return; |
| 72 } | 82 } |
| 73 DeleteData(); | 83 DeleteData(); |
| 74 data_ = NULL; | 84 data_ = NULL; |
| 75 modified_ = false; | 85 modified_ = false; |
| 76 extended_ = false; | 86 extended_ = false; |
| 77 } | 87 } |
| 78 | 88 |
| 79 template<typename T> void StorageBlock<T>::StopSharingData() { | 89 template <typename T> |
| 90 void StorageBlock<T>::StopSharingData() { |
| 80 if (!data_ || own_data_) | 91 if (!data_ || own_data_) |
| 81 return; | 92 return; |
| 82 DCHECK(!modified_); | 93 DCHECK(!modified_); |
| 83 data_ = NULL; | 94 data_ = NULL; |
| 84 } | 95 } |
| 85 | 96 |
| 86 template<typename T> void StorageBlock<T>::set_modified() { | 97 template <typename T> |
| 98 void StorageBlock<T>::set_modified() { |
| 87 DCHECK(data_); | 99 DCHECK(data_); |
| 88 modified_ = true; | 100 modified_ = true; |
| 89 } | 101 } |
| 90 | 102 |
| 91 template<typename T> void StorageBlock<T>::clear_modified() { | 103 template <typename T> |
| 104 void StorageBlock<T>::clear_modified() { |
| 92 modified_ = false; | 105 modified_ = false; |
| 93 } | 106 } |
| 94 | 107 |
| 95 template<typename T> T* StorageBlock<T>::Data() { | 108 template <typename T> |
| 109 T* StorageBlock<T>::Data() { |
| 96 if (!data_) | 110 if (!data_) |
| 97 AllocateData(); | 111 AllocateData(); |
| 98 return data_; | 112 return data_; |
| 99 } | 113 } |
| 100 | 114 |
| 101 template<typename T> bool StorageBlock<T>::HasData() const { | 115 template <typename T> |
| 116 bool StorageBlock<T>::HasData() const { |
| 102 return (NULL != data_); | 117 return (NULL != data_); |
| 103 } | 118 } |
| 104 | 119 |
| 105 template<typename T> bool StorageBlock<T>::VerifyHash() const { | 120 template <typename T> |
| 121 bool StorageBlock<T>::VerifyHash() const { |
| 106 uint32 hash = CalculateHash(); | 122 uint32 hash = CalculateHash(); |
| 107 return (!data_->self_hash || data_->self_hash == hash); | 123 return (!data_->self_hash || data_->self_hash == hash); |
| 108 } | 124 } |
| 109 | 125 |
| 110 template<typename T> bool StorageBlock<T>::own_data() const { | 126 template <typename T> |
| 127 bool StorageBlock<T>::own_data() const { |
| 111 return own_data_; | 128 return own_data_; |
| 112 } | 129 } |
| 113 | 130 |
| 114 template<typename T> const Addr StorageBlock<T>::address() const { | 131 template <typename T> |
| 132 const Addr StorageBlock<T>::address() const { |
| 115 return address_; | 133 return address_; |
| 116 } | 134 } |
| 117 | 135 |
| 118 template<typename T> bool StorageBlock<T>::Load() { | 136 template <typename T> |
| 137 bool StorageBlock<T>::Load() { |
| 119 if (file_) { | 138 if (file_) { |
| 120 if (!data_) | 139 if (!data_) |
| 121 AllocateData(); | 140 AllocateData(); |
| 122 | 141 |
| 123 if (file_->Load(this)) { | 142 if (file_->Load(this)) { |
| 124 modified_ = false; | 143 modified_ = false; |
| 125 return true; | 144 return true; |
| 126 } | 145 } |
| 127 } | 146 } |
| 128 LOG(WARNING) << "Failed data load."; | 147 LOG(WARNING) << "Failed data load."; |
| 129 Trace("Failed data load."); | 148 Trace("Failed data load."); |
| 130 return false; | 149 return false; |
| 131 } | 150 } |
| 132 | 151 |
| 133 template<typename T> bool StorageBlock<T>::Store() { | 152 template <typename T> |
| 153 bool StorageBlock<T>::Store() { |
| 134 if (file_ && data_) { | 154 if (file_ && data_) { |
| 135 data_->self_hash = CalculateHash(); | 155 data_->self_hash = CalculateHash(); |
| 136 if (file_->Store(this)) { | 156 if (file_->Store(this)) { |
| 137 modified_ = false; | 157 modified_ = false; |
| 138 return true; | 158 return true; |
| 139 } | 159 } |
| 140 } | 160 } |
| 141 LOG(ERROR) << "Failed data store."; | 161 LOG(ERROR) << "Failed data store."; |
| 142 Trace("Failed data store."); | 162 Trace("Failed data store."); |
| 143 return false; | 163 return false; |
| 144 } | 164 } |
| 145 | 165 |
| 146 template<typename T> bool StorageBlock<T>::Load(FileIOCallback* callback, | 166 template <typename T> |
| 147 bool* completed) { | 167 bool StorageBlock<T>::Load(FileIOCallback* callback, bool* completed) { |
| 148 if (file_) { | 168 if (file_) { |
| 149 if (!data_) | 169 if (!data_) |
| 150 AllocateData(); | 170 AllocateData(); |
| 151 | 171 |
| 152 if (file_->Load(this, callback, completed)) { | 172 if (file_->Load(this, callback, completed)) { |
| 153 modified_ = false; | 173 modified_ = false; |
| 154 return true; | 174 return true; |
| 155 } | 175 } |
| 156 } | 176 } |
| 157 LOG(WARNING) << "Failed data load."; | 177 LOG(WARNING) << "Failed data load."; |
| 158 Trace("Failed data load."); | 178 Trace("Failed data load."); |
| 159 return false; | 179 return false; |
| 160 } | 180 } |
| 161 | 181 |
| 162 template<typename T> bool StorageBlock<T>::Store(FileIOCallback* callback, | 182 template <typename T> |
| 163 bool* completed) { | 183 bool StorageBlock<T>::Store(FileIOCallback* callback, bool* completed) { |
| 164 if (file_ && data_) { | 184 if (file_ && data_) { |
| 165 data_->self_hash = CalculateHash(); | 185 data_->self_hash = CalculateHash(); |
| 166 if (file_->Store(this, callback, completed)) { | 186 if (file_->Store(this, callback, completed)) { |
| 167 modified_ = false; | 187 modified_ = false; |
| 168 return true; | 188 return true; |
| 169 } | 189 } |
| 170 } | 190 } |
| 171 LOG(ERROR) << "Failed data store."; | 191 LOG(ERROR) << "Failed data store."; |
| 172 Trace("Failed data store."); | 192 Trace("Failed data store."); |
| 173 return false; | 193 return false; |
| 174 } | 194 } |
| 175 | 195 |
| 176 template<typename T> void StorageBlock<T>::AllocateData() { | 196 template <typename T> |
| 197 void StorageBlock<T>::AllocateData() { |
| 177 DCHECK(!data_); | 198 DCHECK(!data_); |
| 178 if (!extended_) { | 199 if (!extended_) { |
| 179 data_ = new T; | 200 data_ = new T; |
| 180 } else { | 201 } else { |
| 181 void* buffer = new char[address_.num_blocks() * sizeof(*data_)]; | 202 void* buffer = new char[address_.num_blocks() * sizeof(*data_)]; |
| 182 data_ = new(buffer) T; | 203 data_ = new (buffer) T; |
| 183 } | 204 } |
| 184 own_data_ = true; | 205 own_data_ = true; |
| 185 } | 206 } |
| 186 | 207 |
| 187 template<typename T> void StorageBlock<T>::DeleteData() { | 208 template <typename T> |
| 209 void StorageBlock<T>::DeleteData() { |
| 188 if (own_data_) { | 210 if (own_data_) { |
| 189 if (!extended_) { | 211 if (!extended_) { |
| 190 delete data_; | 212 delete data_; |
| 191 } else { | 213 } else { |
| 192 data_->~T(); | 214 data_->~T(); |
| 193 delete[] reinterpret_cast<char*>(data_); | 215 delete[] reinterpret_cast<char*>(data_); |
| 194 } | 216 } |
| 195 own_data_ = false; | 217 own_data_ = false; |
| 196 } | 218 } |
| 197 } | 219 } |
| 198 | 220 |
| 199 template<typename T> uint32 StorageBlock<T>::CalculateHash() const { | 221 template <typename T> |
| 222 uint32 StorageBlock<T>::CalculateHash() const { |
| 200 return base::Hash(reinterpret_cast<char*>(data_), offsetof(T, self_hash)); | 223 return base::Hash(reinterpret_cast<char*>(data_), offsetof(T, self_hash)); |
| 201 } | 224 } |
| 202 | 225 |
| 203 } // namespace disk_cache | 226 } // namespace disk_cache |
| 204 | 227 |
| 205 #endif // NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_ | 228 #endif // NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_ |
| OLD | NEW |