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_BLOCKFILE_ADDR_H_
9 #define NET_DISK_CACHE_BLOCKFILE_ADDR_H_
11 #include "net/base/net_export.h"
12 #include "net/disk_cache/blockfile/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)
58 // Note that an Addr can be used to "point" to a variety of different objects,
59 // from a given type of entry to random blobs of data. Conceptually, an Addr is
60 // just a number that someone can inspect to find out how to locate the desired
61 // record. Most users will not care about the specific bits inside Addr, for
62 // example, what parts of it point to a file number; only the code that has to
63 // select a specific file would care about those specific bits.
65 // From a general point of view, an Addr has a total capacity of 2^24 entities,
66 // in that it has 24 bits that can identify individual records. Note that the
67 // address space is bigger for independent files (2^28), but that would not be
69 class NET_EXPORT_PRIVATE Addr
{
72 explicit Addr(CacheAddr address
) : value_(address
) {}
73 Addr(FileType file_type
, int max_blocks
, int block_file
, int index
) {
74 value_
= ((file_type
<< kFileTypeOffset
) & kFileTypeMask
) |
75 (((max_blocks
- 1) << kNumBlocksOffset
) & kNumBlocksMask
) |
76 ((block_file
<< kFileSelectorOffset
) & kFileSelectorMask
) |
77 (index
& kStartBlockMask
) | kInitializedMask
;
80 CacheAddr
value() const { return value_
; }
81 void set_value(CacheAddr address
) {
85 bool is_initialized() const {
86 return (value_
& kInitializedMask
) != 0;
89 bool is_separate_file() const {
90 return (value_
& kFileTypeMask
) == 0;
93 bool is_block_file() const {
94 return !is_separate_file();
97 FileType
file_type() const {
98 return static_cast<FileType
>((value_
& kFileTypeMask
) >> kFileTypeOffset
);
101 int FileNumber() const {
102 if (is_separate_file())
103 return value_
& kFileNameMask
;
105 return ((value_
& kFileSelectorMask
) >> kFileSelectorOffset
);
108 int start_block() const;
109 int num_blocks() const;
110 bool SetFileNumber(int file_number
);
111 int BlockSize() const {
112 return BlockSizeForFileType(file_type());
115 bool operator==(Addr other
) const {
116 return value_
== other
.value_
;
119 bool operator!=(Addr other
) const {
120 return value_
!= other
.value_
;
123 static int BlockSizeForFileType(FileType file_type
) {
144 static FileType
RequiredFileType(int size
) {
147 else if (size
< 4096)
149 else if (size
<= 4096 * 4)
155 static int RequiredBlocks(int size
, FileType file_type
) {
156 int block_size
= BlockSizeForFileType(file_type
);
157 return (size
+ block_size
- 1) / block_size
;
160 // Returns true if this address looks like a valid one.
161 bool SanityCheckV2() const;
162 bool SanityCheckV3() const;
163 bool SanityCheckForEntryV2() const;
164 bool SanityCheckForEntryV3() const;
165 bool SanityCheckForRankings() const;
168 uint32
reserved_bits() const {
169 return value_
& kReservedBitsMask
;
172 static const uint32 kInitializedMask
= 0x80000000;
173 static const uint32 kFileTypeMask
= 0x70000000;
174 static const uint32 kFileTypeOffset
= 28;
175 static const uint32 kReservedBitsMask
= 0x0c000000;
176 static const uint32 kNumBlocksMask
= 0x03000000;
177 static const uint32 kNumBlocksOffset
= 24;
178 static const uint32 kFileSelectorMask
= 0x00ff0000;
179 static const uint32 kFileSelectorOffset
= 16;
180 static const uint32 kStartBlockMask
= 0x0000FFFF;
181 static const uint32 kFileNameMask
= 0x0FFFFFFF;
186 } // namespace disk_cache
188 #endif // NET_DISK_CACHE_BLOCKFILE_ADDR_H_