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
3 // found in the LICENSE file.
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.
8 #ifndef NET_DISK_CACHE_ADDR_H_
9 #define NET_DISK_CACHE_ADDR_H_
11 #include "net/base/net_export.h"
12 #include "net/disk_cache/disk_format_base.h"
14 namespace disk_cache
{
27 const int kMaxBlockSize
= 4096 * 4;
28 const int kMaxBlockFile
= 255;
29 const int kMaxNumBlocks
= 4;
30 const int kFirstAdditionalBlockFile
= 4;
31 const int kFirstAdditionalBlockFileV3
= 7;
33 // Defines a storage address for a cache record
36 // 1000 0000 0000 0000 0000 0000 0000 0000 : initialized bit
37 // 0111 0000 0000 0000 0000 0000 0000 0000 : file type
40 // 0 = separate file on disk
41 // 1 = rankings block file
42 // 2 = 256 byte block file
43 // 3 = 1k 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
50 // 0000 1111 1111 1111 1111 1111 1111 1111 : file# 0 - 268,435,456 (2^28)
53 // 0000 1100 0000 0000 0000 0000 0000 0000 : reserved bits
54 // 0000 0011 0000 0000 0000 0000 0000 0000 : number of contiguous blocks 1-4
55 // 0000 0000 1111 1111 0000 0000 0000 0000 : file selector 0 - 255
56 // 0000 0000 0000 0000 1111 1111 1111 1111 : block# 0 - 65,535 (2^16)
57 class NET_EXPORT_PRIVATE Addr
{
60 explicit Addr(CacheAddr address
) : value_(address
) {}
61 Addr(FileType file_type
, int max_blocks
, int block_file
, int index
) {
62 value_
= ((file_type
<< kFileTypeOffset
) & kFileTypeMask
) |
63 (((max_blocks
- 1) << kNumBlocksOffset
) & kNumBlocksMask
) |
64 ((block_file
<< kFileSelectorOffset
) & kFileSelectorMask
) |
65 (index
& kStartBlockMask
) | kInitializedMask
;
68 CacheAddr
value() const { return value_
; }
69 void set_value(CacheAddr address
) {
73 bool is_initialized() const {
74 return (value_
& kInitializedMask
) != 0;
77 bool is_separate_file() const {
78 return (value_
& kFileTypeMask
) == 0;
81 bool is_block_file() const {
82 return !is_separate_file();
85 FileType
file_type() const {
86 return static_cast<FileType
>((value_
& kFileTypeMask
) >> kFileTypeOffset
);
89 int FileNumber() const {
90 if (is_separate_file())
91 return value_
& kFileNameMask
;
93 return ((value_
& kFileSelectorMask
) >> kFileSelectorOffset
);
96 int start_block() const;
97 int num_blocks() const;
98 bool SetFileNumber(int file_number
);
99 int BlockSize() const {
100 return BlockSizeForFileType(file_type());
103 bool operator==(Addr other
) const {
104 return value_
== other
.value_
;
107 bool operator!=(Addr other
) const {
108 return value_
!= other
.value_
;
111 static Addr
FromEntryAddress(uint32 value
) {
112 return Addr(kInitializedMask
+ (BLOCK_ENTRIES
<< kFileTypeOffset
) + value
);
115 static Addr
FromEvictedAddress(uint32 value
) {
116 return Addr(kInitializedMask
+ (BLOCK_EVICTED
<< kFileTypeOffset
) + value
);
119 static int BlockSizeForFileType(FileType file_type
) {
140 static FileType
RequiredFileType(int size
) {
143 else if (size
< 4096)
145 else if (size
<= 4096 * 4)
151 static int RequiredBlocks(int size
, FileType file_type
) {
152 int block_size
= BlockSizeForFileType(file_type
);
153 return (size
+ block_size
- 1) / block_size
;
156 // Returns true if this address looks like a valid one.
157 bool SanityCheckV2() const;
158 bool SanityCheckV3() const;
159 bool SanityCheckForEntryV2() const;
160 bool SanityCheckForEntryV3() const;
161 bool SanityCheckForRankings() const;
164 uint32
reserved_bits() const {
165 return value_
& kReservedBitsMask
;
168 static const uint32 kInitializedMask
= 0x80000000;
169 static const uint32 kFileTypeMask
= 0x70000000;
170 static const uint32 kFileTypeOffset
= 28;
171 static const uint32 kReservedBitsMask
= 0x0c000000;
172 static const uint32 kNumBlocksMask
= 0x03000000;
173 static const uint32 kNumBlocksOffset
= 24;
174 static const uint32 kFileSelectorMask
= 0x00ff0000;
175 static const uint32 kFileSelectorOffset
= 16;
176 static const uint32 kStartBlockMask
= 0x0000FFFF;
177 static const uint32 kFileNameMask
= 0x0FFFFFFF;
182 } // namespace disk_cache
184 #endif // NET_DISK_CACHE_ADDR_H_