Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This is an internal class that handles the address of a cache record. | 5 // This is an internal class that handles the address of a cache record. |
| 6 // See net/disk_cache/disk_cache.h for the public interface of the cache. | 6 // See net/disk_cache/disk_cache.h for the public interface of the cache. |
| 7 | 7 |
| 8 #ifndef NET_DISK_CACHE_ADDR_H_ | 8 #ifndef NET_DISK_CACHE_ADDR_H_ |
| 9 #define NET_DISK_CACHE_ADDR_H_ | 9 #define NET_DISK_CACHE_ADDR_H_ |
| 10 | 10 |
| 11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
| 12 #include "net/disk_cache/disk_format_base.h" | 12 #include "net/disk_cache/disk_format_base.h" |
| 13 | 13 |
| 14 namespace disk_cache { | 14 namespace disk_cache { |
| 15 | 15 |
| 16 enum FileType { | 16 enum FileType { |
| 17 EXTERNAL = 0, | 17 EXTERNAL = 0, |
| 18 RANKINGS = 1, | 18 RANKINGS = 1, |
| 19 BLOCK_256, | 19 BLOCK_256 = 2, |
| 20 BLOCK_1K, | 20 BLOCK_1K = 3, |
| 21 BLOCK_4K, | 21 BLOCK_4K = 4, |
| 22 BLOCK_FILES = 5, | |
| 23 BLOCK_ENTRIES = 6, | |
| 24 BLOCK_EVICTED = 7 | |
| 22 }; | 25 }; |
| 23 | 26 |
| 24 const int kMaxBlockSize = 4096 * 4; | 27 const int kMaxBlockSize = 4096 * 4; |
| 25 const int kMaxBlockFile = 255; | 28 const int kMaxBlockFile = 255; |
| 26 const int kMaxNumBlocks = 4; | 29 const int kMaxNumBlocks = 4; |
| 27 const int kFirstAdditionalBlockFile = 4; | 30 const int kFirstAdditionalBlockFile = 4; |
| 31 const int kFirstAdditionalBlockFileV3 = 7; | |
| 28 | 32 |
| 29 // Defines a storage address for a cache record | 33 // Defines a storage address for a cache record |
| 30 // | 34 // |
| 31 // Header: | 35 // Header: |
| 32 // 1000 0000 0000 0000 0000 0000 0000 0000 : initialized bit | 36 // 1000 0000 0000 0000 0000 0000 0000 0000 : initialized bit |
| 33 // 0111 0000 0000 0000 0000 0000 0000 0000 : file type | 37 // 0111 0000 0000 0000 0000 0000 0000 0000 : file type |
| 34 // | 38 // |
| 35 // File type values: | 39 // File type values: |
| 36 // 0 = separate file on disk | 40 // 0 = separate file on disk |
| 37 // 1 = rankings block file | 41 // 1 = rankings block file |
| 38 // 2 = 256 byte block file | 42 // 2 = 256 byte block file |
| 39 // 3 = 1k byte block file | 43 // 3 = 1k byte block file |
| 40 // 4 = 4k byte block file | 44 // 4 = 4k byte block file |
| 45 // 5 = external files block file | |
| 46 // 6 = active entries block file | |
| 47 // 7 = evicted entries block file | |
| 41 // | 48 // |
| 42 // If separate file: | 49 // If separate file: |
| 43 // 0000 1111 1111 1111 1111 1111 1111 1111 : file# 0 - 268,435,456 (2^28) | 50 // 0000 1111 1111 1111 1111 1111 1111 1111 : file# 0 - 268,435,456 (2^28) |
| 44 // | 51 // |
| 45 // If block file: | 52 // If block file: |
| 46 // 0000 1100 0000 0000 0000 0000 0000 0000 : reserved bits | 53 // 0000 1100 0000 0000 0000 0000 0000 0000 : reserved bits |
| 47 // 0000 0011 0000 0000 0000 0000 0000 0000 : number of contiguous blocks 1-4 | 54 // 0000 0011 0000 0000 0000 0000 0000 0000 : number of contiguous blocks 1-4 |
| 48 // 0000 0000 1111 1111 0000 0000 0000 0000 : file selector 0 - 255 | 55 // 0000 0000 1111 1111 0000 0000 0000 0000 : file selector 0 - 255 |
| 49 // 0000 0000 0000 0000 1111 1111 1111 1111 : block# 0 - 65,535 (2^16) | 56 // 0000 0000 0000 0000 1111 1111 1111 1111 : block# 0 - 65,535 (2^16) |
| 50 class NET_EXPORT_PRIVATE Addr { | 57 class NET_EXPORT_PRIVATE Addr { |
| 51 public: | 58 public: |
| 52 Addr() : value_(0) {} | 59 Addr() : value_(0) {} |
| 53 explicit Addr(CacheAddr address) : value_(address) {} | 60 explicit Addr(CacheAddr address) : value_(address) {} |
| 54 Addr(FileType file_type, int max_blocks, int block_file, int index) { | 61 Addr(FileType file_type, int max_blocks, int block_file, int index) { |
| 55 value_ = ((file_type << kFileTypeOffset) & kFileTypeMask) | | 62 value_ = ((file_type << kFileTypeOffset) & kFileTypeMask) | |
|
gavinp
2013/06/12 21:06:50
While we're here, what do you think of this idea:
rvargas (doing something else)
2013/06/12 21:25:27
I hope you don't mind, but I don't like the union
| |
| 56 (((max_blocks - 1) << kNumBlocksOffset) & kNumBlocksMask) | | 63 (((max_blocks - 1) << kNumBlocksOffset) & kNumBlocksMask) | |
| 57 ((block_file << kFileSelectorOffset) & kFileSelectorMask) | | 64 ((block_file << kFileSelectorOffset) & kFileSelectorMask) | |
| 58 (index & kStartBlockMask) | kInitializedMask; | 65 (index & kStartBlockMask) | kInitializedMask; |
| 59 } | 66 } |
| 60 | 67 |
| 61 CacheAddr value() const { return value_; } | 68 CacheAddr value() const { return value_; } |
| 62 void set_value(CacheAddr address) { | 69 void set_value(CacheAddr address) { |
| 63 value_ = address; | 70 value_ = address; |
| 64 } | 71 } |
| 65 | 72 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 94 } | 101 } |
| 95 | 102 |
| 96 bool operator==(Addr other) const { | 103 bool operator==(Addr other) const { |
| 97 return value_ == other.value_; | 104 return value_ == other.value_; |
| 98 } | 105 } |
| 99 | 106 |
| 100 bool operator!=(Addr other) const { | 107 bool operator!=(Addr other) const { |
| 101 return value_ != other.value_; | 108 return value_ != other.value_; |
| 102 } | 109 } |
| 103 | 110 |
| 111 // Conversions between the address of an external file and the address of the | |
| 112 // control block of that external file. | |
| 113 Addr AsExternal() const; | |
| 114 Addr AsBlockFile() const; | |
| 115 | |
| 116 static Addr FromEntryAddress(uint32 value) { | |
| 117 return Addr(((8 + BLOCK_ENTRIES) << kFileTypeOffset) + value); | |
|
gavinp
2013/06/13 13:10:58
8?
rvargas (doing something else)
2013/06/13 19:07:25
Rewrote it on a more explicit way.
| |
| 118 } | |
| 119 | |
| 120 static Addr FromEvictedAddress(uint32 value) { | |
| 121 return Addr(((8 + BLOCK_EVICTED) << kFileTypeOffset) + value); | |
| 122 } | |
| 123 | |
| 104 static int BlockSizeForFileType(FileType file_type) { | 124 static int BlockSizeForFileType(FileType file_type) { |
| 105 switch (file_type) { | 125 switch (file_type) { |
| 106 case RANKINGS: | 126 case RANKINGS: |
| 107 return 36; | 127 return 36; |
| 108 case BLOCK_256: | 128 case BLOCK_256: |
| 109 return 256; | 129 return 256; |
| 110 case BLOCK_1K: | 130 case BLOCK_1K: |
| 111 return 1024; | 131 return 1024; |
| 112 case BLOCK_4K: | 132 case BLOCK_4K: |
| 113 return 4096; | 133 return 4096; |
| 134 case BLOCK_FILES: | |
| 135 return 8; | |
| 136 case BLOCK_ENTRIES: | |
| 137 return 104; | |
|
gavinp
2013/06/13 13:10:58
These magic numbers are killing me. Why 104?
rvargas (doing something else)
2013/06/13 19:07:25
Because there is a COMPILE_ASSERT(sizeof(EntryReco
| |
| 138 case BLOCK_EVICTED: | |
| 139 return 48; | |
| 114 default: | 140 default: |
| 115 return 0; | 141 return 0; |
| 116 } | 142 } |
| 117 } | 143 } |
| 118 | 144 |
| 119 static FileType RequiredFileType(int size) { | 145 static FileType RequiredFileType(int size) { |
| 120 if (size < 1024) | 146 if (size < 1024) |
| 121 return BLOCK_256; | 147 return BLOCK_256; |
| 122 else if (size < 4096) | 148 else if (size < 4096) |
| 123 return BLOCK_1K; | 149 return BLOCK_1K; |
| 124 else if (size <= 4096 * 4) | 150 else if (size <= 4096 * 4) |
| 125 return BLOCK_4K; | 151 return BLOCK_4K; |
| 126 else | 152 else |
| 127 return EXTERNAL; | 153 return EXTERNAL; |
| 128 } | 154 } |
| 129 | 155 |
| 130 static int RequiredBlocks(int size, FileType file_type) { | 156 static int RequiredBlocks(int size, FileType file_type) { |
| 131 int block_size = BlockSizeForFileType(file_type); | 157 int block_size = BlockSizeForFileType(file_type); |
| 132 return (size + block_size - 1) / block_size; | 158 return (size + block_size - 1) / block_size; |
| 133 } | 159 } |
| 134 | 160 |
| 135 // Returns true if this address looks like a valid one. | 161 // Returns true if this address looks like a valid one. |
| 136 bool SanityCheck() const; | 162 bool SanityCheckV2() const; |
| 163 bool SanityCheckV3() const; | |
| 137 bool SanityCheckForEntryV2() const; | 164 bool SanityCheckForEntryV2() const; |
| 165 bool SanityCheckForEntryV3() const; | |
| 138 bool SanityCheckForRankings() const; | 166 bool SanityCheckForRankings() const; |
| 139 | 167 |
| 140 private: | 168 private: |
| 141 static const uint32 kInitializedMask = 0x80000000; | 169 static const uint32 kInitializedMask = 0x80000000; |
| 142 static const uint32 kFileTypeMask = 0x70000000; | 170 static const uint32 kFileTypeMask = 0x70000000; |
| 143 static const uint32 kFileTypeOffset = 28; | 171 static const uint32 kFileTypeOffset = 28; |
| 144 static const uint32 kNumBlocksMask = 0x03000000; | 172 static const uint32 kNumBlocksMask = 0x03000000; |
| 145 static const uint32 kNumBlocksOffset = 24; | 173 static const uint32 kNumBlocksOffset = 24; |
| 146 static const uint32 kFileSelectorMask = 0x00ff0000; | 174 static const uint32 kFileSelectorMask = 0x00ff0000; |
| 147 static const uint32 kFileSelectorOffset = 16; | 175 static const uint32 kFileSelectorOffset = 16; |
| 148 static const uint32 kStartBlockMask = 0x0000FFFF; | 176 static const uint32 kStartBlockMask = 0x0000FFFF; |
| 149 static const uint32 kFileNameMask = 0x0FFFFFFF; | 177 static const uint32 kFileNameMask = 0x0FFFFFFF; |
| 150 | 178 |
| 151 CacheAddr value_; | 179 CacheAddr value_; |
| 152 }; | 180 }; |
| 153 | 181 |
| 154 } // namespace disk_cache | 182 } // namespace disk_cache |
| 155 | 183 |
| 156 #endif // NET_DISK_CACHE_ADDR_H_ | 184 #endif // NET_DISK_CACHE_ADDR_H_ |
| OLD | NEW |