| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_CACHE_INTERNAL_INL_H__ | 5 #ifndef NET_DISK_CACHE_CACHE_INTERNAL_INL_H__ |
| 6 #define NET_DISK_CACHE_CACHE_INTERNAL_INL_H__ | 6 #define NET_DISK_CACHE_CACHE_INTERNAL_INL_H__ |
| 7 | 7 |
| 8 #include "net/disk_cache/storage_block.h" | 8 #include "net/disk_cache/storage_block.h" |
| 9 | 9 |
| 10 #include "net/disk_cache/trace.h" | 10 #include "net/disk_cache/trace.h" |
| 11 | 11 |
| 12 namespace disk_cache { | 12 namespace disk_cache { |
| 13 | 13 |
| 14 template<typename T> StorageBlock<T>::StorageBlock(MappedFile* file, | 14 template<typename T> StorageBlock<T>::StorageBlock(MappedFile* file, |
| 15 Addr address) | 15 Addr address) |
| 16 : data_(NULL), file_(file), address_(address), modified_(false), | 16 : data_(NULL), file_(file), address_(address), modified_(false), |
| 17 own_data_(false), extended_(false) { | 17 own_data_(false), extended_(false) { |
| 18 if (address.num_blocks() > 1) | 18 if (address.num_blocks() > 1) |
| 19 extended_ = true; | 19 extended_ = true; |
| 20 DCHECK(!address.is_initialized() || sizeof(*data_) == address.BlockSize()); | 20 DCHECK(!address.is_initialized() || sizeof(*data_) == address.BlockSize()); |
| 21 } | 21 } |
| 22 | 22 |
| 23 template<typename T> StorageBlock<T>::~StorageBlock() { | 23 template<typename T> StorageBlock<T>::~StorageBlock() { |
| 24 if (modified_) | 24 if (modified_) |
| 25 Store(); | 25 Store(); |
| 26 if (own_data_) | 26 DeleteData(); |
| 27 delete data_; | |
| 28 } | 27 } |
| 29 | 28 |
| 30 template<typename T> void* StorageBlock<T>::buffer() const { | 29 template<typename T> void* StorageBlock<T>::buffer() const { |
| 31 return data_; | 30 return data_; |
| 32 } | 31 } |
| 33 | 32 |
| 34 template<typename T> size_t StorageBlock<T>::size() const { | 33 template<typename T> size_t StorageBlock<T>::size() const { |
| 35 if (!extended_) | 34 if (!extended_) |
| 36 return sizeof(*data_); | 35 return sizeof(*data_); |
| 37 return address_.num_blocks() * sizeof(*data_); | 36 return address_.num_blocks() * sizeof(*data_); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 51 address_.set_value(address.value()); | 50 address_.set_value(address.value()); |
| 52 if (address.num_blocks() > 1) | 51 if (address.num_blocks() > 1) |
| 53 extended_ = true; | 52 extended_ = true; |
| 54 | 53 |
| 55 DCHECK(sizeof(*data_) == address.BlockSize()); | 54 DCHECK(sizeof(*data_) == address.BlockSize()); |
| 56 return true; | 55 return true; |
| 57 } | 56 } |
| 58 | 57 |
| 59 template<typename T> void StorageBlock<T>::SetData(T* other) { | 58 template<typename T> void StorageBlock<T>::SetData(T* other) { |
| 60 DCHECK(!modified_); | 59 DCHECK(!modified_); |
| 61 if (own_data_) { | 60 DeleteData(); |
| 62 delete data_; | |
| 63 own_data_ = false; | |
| 64 } | |
| 65 data_ = other; | 61 data_ = other; |
| 66 } | 62 } |
| 67 | 63 |
| 68 template<typename T> void StorageBlock<T>::set_modified() { | 64 template<typename T> void StorageBlock<T>::set_modified() { |
| 69 DCHECK(data_); | 65 DCHECK(data_); |
| 70 modified_ = true; | 66 modified_ = true; |
| 71 } | 67 } |
| 72 | 68 |
| 73 template<typename T> T* StorageBlock<T>::Data() { | 69 template<typename T> T* StorageBlock<T>::Data() { |
| 74 if (!data_) | 70 if (!data_) |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 DCHECK(!data_); | 111 DCHECK(!data_); |
| 116 if (!extended_) { | 112 if (!extended_) { |
| 117 data_ = new T; | 113 data_ = new T; |
| 118 } else { | 114 } else { |
| 119 void* buffer = new char[address_.num_blocks() * sizeof(*data_)]; | 115 void* buffer = new char[address_.num_blocks() * sizeof(*data_)]; |
| 120 data_ = new(buffer) T; | 116 data_ = new(buffer) T; |
| 121 } | 117 } |
| 122 own_data_ = true; | 118 own_data_ = true; |
| 123 } | 119 } |
| 124 | 120 |
| 121 template<typename T> void StorageBlock<T>::DeleteData() { |
| 122 if (own_data_) { |
| 123 if (!extended_) { |
| 124 delete data_; |
| 125 } else { |
| 126 data_->~T(); |
| 127 delete[] reinterpret_cast<char*>(data_); |
| 128 } |
| 129 own_data_ = false; |
| 130 } |
| 131 } |
| 132 |
| 125 } // namespace disk_cache | 133 } // namespace disk_cache |
| 126 | 134 |
| 127 #endif // NET_DISK_CACHE_CACHE_INTERNAL_INL_H__ | 135 #endif // NET_DISK_CACHE_CACHE_INTERNAL_INL_H__ |
| 128 | 136 |
| OLD | NEW |