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 "base/logging.h" | 5 #include "base/logging.h" |
6 #include "net/disk_cache/flash/format.h" | 6 #include "net/disk_cache/flash/format.h" |
7 #include "net/disk_cache/flash/segment.h" | 7 #include "net/disk_cache/flash/segment.h" |
8 #include "net/disk_cache/flash/storage.h" | 8 #include "net/disk_cache/flash/storage.h" |
9 | 9 |
10 namespace disk_cache { | 10 namespace disk_cache { |
11 | 11 |
12 Segment::Segment(int32 index, bool read_only, Storage* storage) | 12 Segment::Segment(int32 index, bool read_only, Storage* storage) |
13 : read_only_(read_only), | 13 : read_only_(read_only), |
14 init_(false), | 14 init_(false), |
15 storage_(storage), | 15 storage_(storage), |
16 offset_(index * kFlashSegmentSize), | 16 offset_(index * kFlashSegmentSize), |
17 summary_offset_(offset_ + kFlashSegmentSize - kFlashSummarySize), | 17 summary_offset_(offset_ + kFlashSegmentSize - kFlashSummarySize), |
18 write_offset_(offset_) { | 18 write_offset_(offset_) { |
19 DCHECK(storage); | 19 DCHECK(storage); |
20 DCHECK(storage->size() % kFlashSegmentSize == 0); | 20 DCHECK(storage->size() % kFlashSegmentSize == 0); |
21 DCHECK(offset_ >= 0 && offset_ + kFlashSegmentSize <= storage->size()); | |
22 } | 21 } |
23 | 22 |
24 Segment::~Segment() { | 23 Segment::~Segment() { |
25 DCHECK(read_only_); | 24 DCHECK(!init_ || read_only_); |
26 } | 25 } |
27 | 26 |
28 bool Segment::Init() { | 27 bool Segment::Init() { |
29 if (init_) | 28 if (init_) |
30 return false; | 29 return false; |
31 | 30 |
| 31 if (offset_ < 0 || offset_ + kFlashSegmentSize > storage_->size()) |
| 32 return false; |
| 33 |
32 if (!read_only_) { | 34 if (!read_only_) { |
33 init_ = true; | 35 init_ = true; |
34 return true; | 36 return true; |
35 } | 37 } |
36 | 38 |
37 int32 summary[kFlashMaxEntryCount + 1]; | 39 int32 summary[kFlashMaxEntryCount + 1]; |
38 if (!storage_->Read(summary, kFlashSummarySize, summary_offset_)) | 40 if (!storage_->Read(summary, kFlashSummarySize, summary_offset_)) |
39 return false; | 41 return false; |
40 | 42 |
41 size_t entry_count = summary[0]; | 43 size_t entry_count = summary[0]; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 } | 77 } |
76 | 78 |
77 bool Segment::Close() { | 79 bool Segment::Close() { |
78 DCHECK(init_); | 80 DCHECK(init_); |
79 if (read_only_) | 81 if (read_only_) |
80 return true; | 82 return true; |
81 | 83 |
82 DCHECK(header_offsets_.size() <= kFlashMaxEntryCount); | 84 DCHECK(header_offsets_.size() <= kFlashMaxEntryCount); |
83 | 85 |
84 int32 summary[kFlashMaxEntryCount + 1]; | 86 int32 summary[kFlashMaxEntryCount + 1]; |
| 87 memset(summary, 0, kFlashSummarySize); |
85 summary[0] = header_offsets_.size(); | 88 summary[0] = header_offsets_.size(); |
86 std::copy(header_offsets_.begin(), header_offsets_.end(), summary + 1); | 89 std::copy(header_offsets_.begin(), header_offsets_.end(), summary + 1); |
87 if (!storage_->Write(summary, kFlashSummarySize, summary_offset_)) | 90 if (!storage_->Write(summary, kFlashSummarySize, summary_offset_)) |
88 return false; | 91 return false; |
89 | 92 |
90 read_only_ = true; | 93 read_only_ = true; |
91 return true; | 94 return true; |
92 } | 95 } |
93 | 96 |
94 bool Segment::CanHold(int32 size) const { | 97 bool Segment::CanHold(int32 size) const { |
95 return header_offsets_.size() < kFlashMaxEntryCount && | 98 return header_offsets_.size() < kFlashMaxEntryCount && |
96 write_offset_ + size <= summary_offset_; | 99 write_offset_ + size <= summary_offset_; |
97 } | 100 } |
98 | 101 |
99 } // namespace disk_cache | 102 } // namespace disk_cache |
OLD | NEW |