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