// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_ #define NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_ #include "net/disk_cache/blockfile/storage_block.h" #include #include #include "base/hash.h" #include "base/logging.h" #include "net/disk_cache/blockfile/trace.h" namespace disk_cache { template StorageBlock::StorageBlock(MappedFile* file, Addr address) : data_(NULL), file_(file), address_(address), modified_(false), own_data_(false), extended_(false) { if (address.num_blocks() > 1) extended_ = true; DCHECK(!address.is_initialized() || sizeof(*data_) == address.BlockSize()); } template StorageBlock::~StorageBlock() { if (modified_) Store(); DeleteData(); } template void* StorageBlock::buffer() const { return data_; } template size_t StorageBlock::size() const { if (!extended_) return sizeof(*data_); return address_.num_blocks() * sizeof(*data_); } template int StorageBlock::offset() const { return address_.start_block() * address_.BlockSize(); } template bool StorageBlock::LazyInit(MappedFile* file, Addr address) { if (file_ || address_.is_initialized()) { NOTREACHED(); return false; } file_ = file; address_.set_value(address.value()); if (address.num_blocks() > 1) extended_ = true; DCHECK(sizeof(*data_) == address.BlockSize()); return true; } template void StorageBlock::SetData(T* other) { DCHECK(!modified_); DeleteData(); data_ = other; } template void StorageBlock::Discard() { if (!data_) return; if (!own_data_) { NOTREACHED(); return; } DeleteData(); data_ = NULL; modified_ = false; extended_ = false; } template void StorageBlock::StopSharingData() { if (!data_ || own_data_) return; DCHECK(!modified_); data_ = NULL; } template void StorageBlock::set_modified() { DCHECK(data_); modified_ = true; } template void StorageBlock::clear_modified() { modified_ = false; } template T* StorageBlock::Data() { if (!data_) AllocateData(); return data_; } template bool StorageBlock::HasData() const { return (NULL != data_); } template bool StorageBlock::VerifyHash() const { uint32_t hash = CalculateHash(); return (!data_->self_hash || data_->self_hash == hash); } template bool StorageBlock::own_data() const { return own_data_; } template const Addr StorageBlock::address() const { return address_; } template bool StorageBlock::Load() { if (file_) { if (!data_) AllocateData(); if (file_->Load(this)) { modified_ = false; return true; } } LOG(WARNING) << "Failed data load."; Trace("Failed data load."); return false; } template bool StorageBlock::Store() { if (file_ && data_) { data_->self_hash = CalculateHash(); if (file_->Store(this)) { modified_ = false; return true; } } LOG(ERROR) << "Failed data store."; Trace("Failed data store."); return false; } template bool StorageBlock::Load(FileIOCallback* callback, bool* completed) { if (file_) { if (!data_) AllocateData(); if (file_->Load(this, callback, completed)) { modified_ = false; return true; } } LOG(WARNING) << "Failed data load."; Trace("Failed data load."); return false; } template bool StorageBlock::Store(FileIOCallback* callback, bool* completed) { if (file_ && data_) { data_->self_hash = CalculateHash(); if (file_->Store(this, callback, completed)) { modified_ = false; return true; } } LOG(ERROR) << "Failed data store."; Trace("Failed data store."); return false; } template void StorageBlock::AllocateData() { DCHECK(!data_); if (!extended_) { data_ = new T; } else { void* buffer = new char[address_.num_blocks() * sizeof(*data_)]; data_ = new(buffer) T; } own_data_ = true; } template void StorageBlock::DeleteData() { if (own_data_) { if (!extended_) { delete data_; } else { data_->~T(); delete[] reinterpret_cast(data_); } own_data_ = false; } } template uint32_t StorageBlock::CalculateHash() const { return base::Hash(data_, offsetof(T, self_hash)); } } // namespace disk_cache #endif // NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_INL_H_