Index: net/disk_cache/memory/mem_entry_impl.h |
diff --git a/net/disk_cache/memory/mem_entry_impl.h b/net/disk_cache/memory/mem_entry_impl.h |
index 52a1696f63c457be200f402ef5c1ed2c336ab6e3..11634e6ab9a883a64856f31d5e459b65707eb36f 100644 |
--- a/net/disk_cache/memory/mem_entry_impl.h |
+++ b/net/disk_cache/memory/mem_entry_impl.h |
@@ -7,9 +7,15 @@ |
#include <stdint.h> |
+#include <string> |
+#include <vector> |
+ |
#include "base/containers/hash_tables.h" |
+#include "base/containers/linked_list.h" |
+#include "base/gtest_prod_util.h" |
#include "base/macros.h" |
#include "base/memory/scoped_ptr.h" |
+#include "base/time/time.h" |
#include "net/disk_cache/disk_cache.h" |
#include "net/log/net_log.h" |
@@ -18,8 +24,8 @@ namespace disk_cache { |
class MemBackendImpl; |
// This class implements the Entry interface for the memory-only cache. An |
-// object of this class represents a single entry on the cache. We use two |
-// types of entries, parent and child to support sparse caching. |
+// object of this class represents a single entry on the cache. We use two types |
+// of entries, parent and child to support sparse caching. |
// |
// A parent entry is non-sparse until a sparse method is invoked (i.e. |
// ReadSparseData, WriteSparseData, GetAvailableRange) when sparse information |
@@ -31,64 +37,62 @@ class MemBackendImpl; |
// ReadData and WriteData cannot be applied to them. The lifetime of a child |
// entry is managed by the parent entry that created it except that the entry |
// can be evicted independently. A child entry does not have a key and it is not |
-// registered in the backend's entry map. It is registered in the backend's |
-// ranking list to enable eviction of a partial content. |
+// registered in the backend's entry map. |
// |
-// A sparse entry has a fixed maximum size and can be partially filled. There |
-// can only be one continous filled region in a sparse entry, as illustrated by |
-// the following example: |
+// A sparse child entry has a fixed maximum size and can be partially |
+// filled. There can only be one continous filled region in a sparse entry, as |
+// illustrated by the following example: |
// | xxx ooooo | |
// x = unfilled region |
// o = filled region |
-// It is guranteed that there is at most one unfilled region and one filled |
+// It is guaranteed that there is at most one unfilled region and one filled |
// region, and the unfilled region (if there is one) is always before the filled |
// region. The book keeping for filled region in a sparse entry is done by using |
-// the variable |child_first_pos_| (inclusive). |
+// the variable |child_first_pos_|. |
-class MemEntryImpl : public Entry { |
+class NET_EXPORT_PRIVATE MemEntryImpl final |
+ : public Entry, |
+ public base::LinkNode<MemEntryImpl> { |
public: |
enum EntryType { |
- kParentEntry, |
- kChildEntry, |
+ PARENT_ENTRY, |
+ CHILD_ENTRY, |
}; |
- explicit MemEntryImpl(MemBackendImpl* backend); |
+ // Provided to better document calls to |UpdateStateOnUse()|. |
+ enum EntryModified { |
+ ENTRY_WAS_NOT_MODIFIED, |
+ ENTRY_WAS_MODIFIED, |
+ }; |
- // Performs the initialization of a EntryImpl that will be added to the |
- // cache. |
- bool CreateEntry(const std::string& key, net::NetLog* net_log); |
+ // Constructor for parent entries. |
+ MemEntryImpl(MemBackendImpl* backend, |
+ const std::string& key, |
+ net::NetLog* net_log); |
- // Permanently destroys this entry. |
- void InternalDoom(); |
+ // Constructor for child entries. |
+ MemEntryImpl(MemBackendImpl* backend, |
+ int child_id, |
+ MemEntryImpl* parent, |
+ net::NetLog* net_log); |
void Open(); |
- bool InUse(); |
- |
- MemEntryImpl* next() const { |
- return next_; |
- } |
+ bool InUse() const; |
- MemEntryImpl* prev() const { |
- return prev_; |
- } |
+ EntryType type() const { return parent_ ? CHILD_ENTRY : PARENT_ENTRY; } |
+ const std::string& key() const { return key_; } |
+ const MemEntryImpl* parent() const { return parent_; } |
+ int child_id() const { return child_id_; } |
+ base::Time last_used() const { return last_used_; } |
- void set_next(MemEntryImpl* next) { |
- next_ = next; |
- } |
+ // The in-memory size of this entry to use for the purposes of eviction. |
+ int GetStorageSize() const; |
- void set_prev(MemEntryImpl* prev) { |
- prev_ = prev; |
- } |
+ // Update an entry's position in the backend LRU list and set |last_used_|. If |
+ // the entry was modified, also update |last_modified_|. |
+ void UpdateStateOnUse(EntryModified modified_enum); |
- EntryType type() const { |
- return parent_ ? kChildEntry : kParentEntry; |
- } |
- |
- const net::BoundNetLog& net_log() { |
- return net_log_; |
- } |
- |
- // Entry interface. |
+ // From disk_cache::Entry: |
void Doom() override; |
void Close() override; |
std::string GetKey() const override; |
@@ -123,11 +127,15 @@ class MemEntryImpl : public Entry { |
int ReadyForSparseIO(const CompletionCallback& callback) override; |
private: |
+ MemEntryImpl(MemBackendImpl* backend, |
+ const std::string& key, |
+ int child_id, |
+ MemEntryImpl* parent, |
+ net::NetLog* net_log); |
+ |
typedef base::hash_map<int, MemEntryImpl*> EntryMap; |
- enum { |
- NUM_STREAMS = 3 |
- }; |
+ static const int kNumStreams = 3; |
~MemEntryImpl() override; |
@@ -138,53 +146,35 @@ class MemEntryImpl : public Entry { |
bool truncate); |
int InternalReadSparseData(int64_t offset, IOBuffer* buf, int buf_len); |
int InternalWriteSparseData(int64_t offset, IOBuffer* buf, int buf_len); |
- |
- // Old Entry interface. |
- int GetAvailableRange(int64_t offset, int len, int64_t* start); |
- |
- // Grows and cleans up the data buffer. |
- void PrepareTarget(int index, int offset, int buf_len); |
- |
- // Updates ranking information. |
- void UpdateRank(bool modified); |
+ int InternalGetAvailableRange(int64_t offset, int len, int64_t* start); |
// Initializes the children map and sparse info. This method is only called |
// on a parent entry. |
bool InitSparseInfo(); |
- // Performs the initialization of a MemEntryImpl as a child entry. |
- // |parent| is the pointer to the parent entry. |child_id| is the ID of |
- // the new child. |
- bool InitChildEntry(MemEntryImpl* parent, int child_id, net::NetLog* net_log); |
- |
// Returns an entry responsible for |offset|. The returned entry can be a |
// child entry or this entry itself if |offset| points to the first range. |
// If such entry does not exist and |create| is true, a new child entry is |
// created. |
- MemEntryImpl* OpenChild(int64_t offset, bool create); |
+ MemEntryImpl* GetChild(int64_t offset, bool create); |
// Finds the first child located within the range [|offset|, |offset + len|). |
// Returns the number of bytes ahead of |offset| to reach the first available |
// bytes in the entry. The first child found is output to |child|. |
int FindNextChild(int64_t offset, int len, MemEntryImpl** child); |
- // Removes child indexed by |child_id| from the children map. |
- void DetachChild(int child_id); |
- |
std::string key_; |
- std::vector<char> data_[NUM_STREAMS]; // User data. |
- int32_t data_size_[NUM_STREAMS]; |
+ std::vector<char> data_[kNumStreams]; // User data. |
int ref_count_; |
int child_id_; // The ID of a child entry. |
int child_first_pos_; // The position of the first byte in a child |
// entry. |
- MemEntryImpl* next_; // Pointers for the LRU list. |
- MemEntryImpl* prev_; |
- MemEntryImpl* parent_; // Pointer to the parent entry. |
+ // Pointer to the parent entry, or nullptr if this entry is a parent entry. |
+ MemEntryImpl* parent_; |
scoped_ptr<EntryMap> children_; |
- base::Time last_modified_; // LRU information. |
+ base::Time last_modified_; |
base::Time last_used_; |
MemBackendImpl* backend_; // Back pointer to the cache. |
bool doomed_; // True if this entry was removed from the cache. |