15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See net/disk_cache/disk_cache.h for the public interface.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_H_
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_H_
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/disk_cache/blockfile/addr.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/disk_cache/blockfile/mapped_file.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace disk_cache {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class encapsulates common behavior of a single "block" of data that is
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// stored on a block-file. It implements the FileBlock interface, so it can be
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// serialized directly to the backing file.
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This object provides a memory buffer for the related data, and it can be used
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to actually share that memory with another instance of the class.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The following example shows how to share storage with another object:
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    StorageBlock<TypeA> a(file, address);
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    StorageBlock<TypeB> b(file, address);
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    a.Load();
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    DoSomething(a.Data());
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    b.SetData(a.Data());
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    ModifySomething(b.Data());
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    // Data modified on the previous call will be saved by b's destructor.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    b.set_modified();
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<typename T>
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StorageBlock : public FileBlock {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StorageBlock(MappedFile* file, Addr address);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~StorageBlock();
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // FileBlock interface.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void* buffer() const;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual size_t size() const;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int offset() const;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allows the overide of dummy values passed on the constructor.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool LazyInit(MappedFile* file, Addr address);
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the internal storage to share the memory provided by other instance.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetData(T* other);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the data, even if it was modified and not saved. This object must
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // own the memory buffer (it cannot be shared).
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Discard();
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stops sharing the data with another object.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StopSharingData();
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the object to lazily save the in-memory data on destruction.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_modified();
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Forgets that the data was modified, so it's not lazily saved.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void clear_modified();
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets a pointer to the internal storage (allocates storage if needed).
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  T* Data();
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if there is data associated with this object.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasData() const;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the internal hash is correct.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool VerifyHash() const;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this object owns the data buffer, false if it is shared.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool own_data() const;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Addr address() const;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Loads and store the data.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Load();
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Store();
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool Load(FileIOCallback* callback, bool* completed);
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool Store(FileIOCallback* callback, bool* completed);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AllocateData();
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DeleteData();
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 CalculateHash() const;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  T* data_;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MappedFile* file_;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Addr address_;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool modified_;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool own_data_;  // Is data_ owned by this object or shared with someone else.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool extended_;  // Used to store an entry of more than one block.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(StorageBlock);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace disk_cache
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // NET_DISK_CACHE_BLOCKFILE_STORAGE_BLOCK_H_
98