Home | History | Annotate | Download | only in disk_cache
      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
      3 // found in the LICENSE file.
      4 
      5 // See net/disk_cache/disk_cache.h for the public interface.
      6 
      7 #ifndef NET_DISK_CACHE_STORAGE_BLOCK_H__
      8 #define NET_DISK_CACHE_STORAGE_BLOCK_H__
      9 
     10 #include "net/disk_cache/addr.h"
     11 #include "net/disk_cache/mapped_file.h"
     12 
     13 namespace disk_cache {
     14 
     15 class EntryImpl;
     16 
     17 // This class encapsulates common behavior of a single "block" of data that is
     18 // stored on a block-file. It implements the FileBlock interface, so it can be
     19 // serialized directly to the backing file.
     20 // This object provides a memory buffer for the related data, and it can be used
     21 // to actually share that memory with another instance of the class.
     22 //
     23 // The following example shows how to share storage with another object:
     24 //    StorageBlock<TypeA> a(file, address);
     25 //    StorageBlock<TypeB> b(file, address);
     26 //    a.Load();
     27 //    DoSomething(a.Data());
     28 //    b.SetData(a.Data());
     29 //    ModifySomething(b.Data());
     30 //    // Data modified on the previous call will be saved by b's destructor.
     31 //    b.set_modified();
     32 template<typename T>
     33 class StorageBlock : public FileBlock {
     34  public:
     35   StorageBlock(MappedFile* file, Addr address);
     36   virtual ~StorageBlock();
     37 
     38   // FileBlock interface.
     39   virtual void* buffer() const;
     40   virtual size_t size() const;
     41   virtual int offset() const;
     42 
     43   // Allows the overide of dummy values passed on the constructor.
     44   bool LazyInit(MappedFile* file, Addr address);
     45 
     46   // Sets the internal storage to share the memory provided by other instance.
     47   void SetData(T* other);
     48 
     49   // Deletes the data, even if it was modified and not saved. This object must
     50   // own the memory buffer (it cannot be shared).
     51   void Discard();
     52 
     53   // Stops sharing the data with another object.
     54   void StopSharingData();
     55 
     56   // Sets the object to lazily save the in-memory data on destruction.
     57   void set_modified();
     58 
     59   // Gets a pointer to the internal storage (allocates storage if needed).
     60   T* Data();
     61 
     62   // Returns true if there is data associated with this object.
     63   bool HasData() const;
     64 
     65   // Returns true if this object owns the data buffer, false if it is shared.
     66   bool own_data() const;
     67 
     68   const Addr address() const;
     69 
     70   // Loads and store the data.
     71   bool Load();
     72   bool Store();
     73 
     74  private:
     75   void AllocateData();
     76   void DeleteData();
     77 
     78   T* data_;
     79   MappedFile* file_;
     80   Addr address_;
     81   bool modified_;
     82   bool own_data_;  // Is data_ owned by this object or shared with someone else.
     83   bool extended_;  // Used to store an entry of more than one block.
     84 
     85   DISALLOW_EVIL_CONSTRUCTORS(StorageBlock);
     86 };
     87 
     88 typedef StorageBlock<EntryStore> CacheEntryBlock;
     89 typedef StorageBlock<RankingsNode> CacheRankingsBlock;
     90 
     91 }  // namespace disk_cache
     92 
     93 #endif  // NET_DISK_CACHE_STORAGE_BLOCK_H__
     94