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 #include "net/disk_cache/addr.h" | 5 #include "net/disk_cache/addr.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace { | |
| 10 | |
| 11 bool AreReservedBitsValid(uint32 value) { | |
|
gavinp
2013/06/16 23:44:15
Make this an inline getter.
| |
| 12 // These two bits of a block address should not be set. | |
| 13 const uint32 kReservedBitsMask = 0x0c000000; | |
|
gavinp
2013/06/16 23:44:15
Move this into the header.
| |
| 14 return !(value & kReservedBitsMask); | |
| 15 } | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 9 namespace disk_cache { | 19 namespace disk_cache { |
| 10 | 20 |
| 11 int Addr::start_block() const { | 21 int Addr::start_block() const { |
| 12 DCHECK(is_block_file()); | 22 DCHECK(is_block_file()); |
| 13 return value_ & kStartBlockMask; | 23 return value_ & kStartBlockMask; |
| 14 } | 24 } |
| 15 | 25 |
| 16 int Addr::num_blocks() const { | 26 int Addr::num_blocks() const { |
| 17 DCHECK(is_block_file() || !value_); | 27 DCHECK(is_block_file() || !value_); |
| 18 return ((value_ & kNumBlocksMask) >> kNumBlocksOffset) + 1; | 28 return ((value_ & kNumBlocksMask) >> kNumBlocksOffset) + 1; |
| 19 } | 29 } |
| 20 | 30 |
| 21 bool Addr::SetFileNumber(int file_number) { | 31 bool Addr::SetFileNumber(int file_number) { |
| 22 DCHECK(is_separate_file()); | 32 DCHECK(is_separate_file()); |
| 23 if (file_number & ~kFileNameMask) | 33 if (file_number & ~kFileNameMask) |
| 24 return false; | 34 return false; |
| 25 value_ = kInitializedMask | file_number; | 35 value_ = kInitializedMask | file_number; |
| 26 return true; | 36 return true; |
| 27 } | 37 } |
| 28 | 38 |
| 29 bool Addr::SanityCheck() const { | 39 bool Addr::SanityCheckV2() const { |
| 30 if (!is_initialized()) | 40 if (!is_initialized()) |
| 31 return !value_; | 41 return !value_; |
| 32 | 42 |
| 33 if (((value_ & kFileTypeMask) >> kFileTypeOffset) > 4) | 43 if (file_type() > BLOCK_4K) |
| 34 return false; | 44 return false; |
| 35 | 45 |
| 36 if (is_separate_file()) | 46 if (is_separate_file()) |
| 37 return true; | 47 return true; |
| 38 | 48 |
| 39 const uint32 kReservedBitsMask = 0x0c000000; | 49 return AreReservedBitsValid(value_); |
| 40 return !(value_ & kReservedBitsMask); | 50 } |
| 51 | |
| 52 bool Addr::SanityCheckV3() const { | |
| 53 if (!is_initialized()) | |
| 54 return !value_; | |
| 55 | |
| 56 // For actual entries, SanityCheckForEntryV3 should be used. | |
| 57 if (file_type() > BLOCK_FILES) | |
| 58 return false; | |
| 59 | |
| 60 if (is_separate_file()) | |
| 61 return true; | |
| 62 | |
| 63 return AreReservedBitsValid(value_); | |
| 41 } | 64 } |
| 42 | 65 |
| 43 bool Addr::SanityCheckForEntryV2() const { | 66 bool Addr::SanityCheckForEntryV2() const { |
| 44 if (!SanityCheck() || !is_initialized()) | 67 if (!SanityCheckV2() || !is_initialized()) |
| 45 return false; | 68 return false; |
| 46 | 69 |
| 47 if (is_separate_file() || file_type() != BLOCK_256) | 70 if (is_separate_file() || file_type() != BLOCK_256) |
| 48 return false; | 71 return false; |
| 49 | 72 |
| 50 return true; | 73 return true; |
| 51 } | 74 } |
| 52 | 75 |
| 76 bool Addr::SanityCheckForEntryV3() const { | |
| 77 if (!is_initialized()) | |
| 78 return false; | |
| 79 | |
| 80 if (!AreReservedBitsValid(value_)) | |
| 81 return false; | |
| 82 | |
| 83 if (file_type() != BLOCK_ENTRIES && file_type() != BLOCK_EVICTED) | |
| 84 return false; | |
| 85 | |
| 86 if (num_blocks() != 1) | |
| 87 return false; | |
| 88 | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 53 bool Addr::SanityCheckForRankings() const { | 92 bool Addr::SanityCheckForRankings() const { |
| 54 if (!SanityCheck() || !is_initialized()) | 93 if (!SanityCheckV2() || !is_initialized()) |
| 55 return false; | 94 return false; |
| 56 | 95 |
| 57 if (is_separate_file() || file_type() != RANKINGS || num_blocks() != 1) | 96 if (is_separate_file() || file_type() != RANKINGS || num_blocks() != 1) |
| 58 return false; | 97 return false; |
| 59 | 98 |
| 60 return true; | 99 return true; |
| 61 } | 100 } |
| 62 | 101 |
| 63 } // namespace disk_cache | 102 } // namespace disk_cache |
| OLD | NEW |