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/block_files.h" | 5 #include "net/disk_cache/block_files.h" |
6 | 6 |
7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 uint32 map_block = header_->allocation_map[i]; | 170 uint32 map_block = header_->allocation_map[i]; |
171 | 171 |
172 for (int j = 0; j < 8; j++, map_block >>= 4) { | 172 for (int j = 0; j < 8; j++, map_block >>= 4) { |
173 int type = GetMapBlockType(map_block); | 173 int type = GetMapBlockType(map_block); |
174 if (type) | 174 if (type) |
175 header_->empty[type -1]++; | 175 header_->empty[type -1]++; |
176 } | 176 } |
177 } | 177 } |
178 } | 178 } |
179 | 179 |
180 bool BlockHeader::NeedToGrowBlockFile(int block_count) { | 180 bool BlockHeader::NeedToGrowBlockFile(int block_count) const { |
181 bool have_space = false; | 181 bool have_space = false; |
182 int empty_blocks = 0; | 182 int empty_blocks = 0; |
183 for (int i = 0; i < kMaxNumBlocks; i++) { | 183 for (int i = 0; i < kMaxNumBlocks; i++) { |
184 empty_blocks += header_->empty[i] * (i + 1); | 184 empty_blocks += header_->empty[i] * (i + 1); |
185 if (i >= block_count - 1 && header_->empty[i]) | 185 if (i >= block_count - 1 && header_->empty[i]) |
186 have_space = true; | 186 have_space = true; |
187 } | 187 } |
188 | 188 |
189 if (header_->next_file && (empty_blocks < kMaxBlocks / 10)) { | 189 if (header_->next_file && (empty_blocks < kMaxBlocks / 10)) { |
190 // This file is almost full but we already created another one, don't use | 190 // This file is almost full but we already created another one, don't use |
191 // this file yet so that it is easier to find empty blocks when we start | 191 // this file yet so that it is easier to find empty blocks when we start |
192 // using this file again. | 192 // using this file again. |
193 return true; | 193 return true; |
194 } | 194 } |
195 return !have_space; | 195 return !have_space; |
196 } | 196 } |
197 | 197 |
198 bool BlockHeader::CanAllocate(int block_count) const { | |
199 bool have_space = false; | |
200 int empty_blocks = 0; | |
gavinp
2013/06/28 21:47:45
What's empty_blocks for?
rvargas (doing something else)
2013/07/11 19:54:55
left overs... sorry about that.
| |
201 for (int i = 0; i < kMaxNumBlocks; i++) { | |
202 empty_blocks += header_->empty[i] * (i + 1); | |
203 if (i >= (block_count - 1) && header_->empty[i]) | |
204 have_space = true; | |
gavinp
2013/06/28 21:47:45
Can we just return true here?
| |
205 } | |
206 | |
207 return have_space; | |
gavinp
2013/06/28 21:47:45
And return false here?
| |
208 } | |
209 | |
198 int BlockHeader::EmptyBlocks() const { | 210 int BlockHeader::EmptyBlocks() const { |
199 int empty_blocks = 0; | 211 int empty_blocks = 0; |
200 for (int i = 0; i < disk_cache::kMaxNumBlocks; i++) { | 212 for (int i = 0; i < disk_cache::kMaxNumBlocks; i++) { |
201 empty_blocks += header_->empty[i] * (i + 1); | 213 empty_blocks += header_->empty[i] * (i + 1); |
202 if (header_->empty[i] < 0) | 214 if (header_->empty[i] < 0) |
203 return 0; | 215 return 0; |
204 } | 216 } |
205 return empty_blocks; | 217 return empty_blocks; |
206 } | 218 } |
207 | 219 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 if (!RemoveEmptyFile(static_cast<FileType>(i + 1))) | 265 if (!RemoveEmptyFile(static_cast<FileType>(i + 1))) |
254 return false; | 266 return false; |
255 } | 267 } |
256 | 268 |
257 init_ = true; | 269 init_ = true; |
258 return true; | 270 return true; |
259 } | 271 } |
260 | 272 |
261 MappedFile* BlockFiles::GetFile(Addr address) { | 273 MappedFile* BlockFiles::GetFile(Addr address) { |
262 DCHECK(thread_checker_->CalledOnValidThread()); | 274 DCHECK(thread_checker_->CalledOnValidThread()); |
263 DCHECK(block_files_.size() >= 4); | 275 DCHECK_GE(block_files_.size(), |
276 static_cast<size_t>(kFirstAdditionalBlockFile)); | |
264 DCHECK(address.is_block_file() || !address.is_initialized()); | 277 DCHECK(address.is_block_file() || !address.is_initialized()); |
265 if (!address.is_initialized()) | 278 if (!address.is_initialized()) |
266 return NULL; | 279 return NULL; |
267 | 280 |
268 int file_index = address.FileNumber(); | 281 int file_index = address.FileNumber(); |
269 if (static_cast<unsigned int>(file_index) >= block_files_.size() || | 282 if (static_cast<unsigned int>(file_index) >= block_files_.size() || |
270 !block_files_[file_index]) { | 283 !block_files_[file_index]) { |
271 // We need to open the file | 284 // We need to open the file |
272 if (!OpenBlockFile(file_index)) | 285 if (!OpenBlockFile(file_index)) |
273 return NULL; | 286 return NULL; |
274 } | 287 } |
275 DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index)); | 288 DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index)); |
276 return block_files_[file_index]; | 289 return block_files_[file_index]; |
277 } | 290 } |
278 | 291 |
279 bool BlockFiles::CreateBlock(FileType block_type, int block_count, | 292 bool BlockFiles::CreateBlock(FileType block_type, int block_count, |
gavinp
2013/06/28 21:47:45
When is CreateBlock called for EXTERNAL, FILES, EN
rvargas (doing something else)
2013/07/11 19:54:55
Done.
| |
280 Addr* block_address) { | 293 Addr* block_address) { |
281 DCHECK(thread_checker_->CalledOnValidThread()); | 294 DCHECK(thread_checker_->CalledOnValidThread()); |
282 if (block_type < RANKINGS || block_type > BLOCK_4K || | 295 if (block_type < RANKINGS || block_type > BLOCK_4K || |
gavinp
2013/06/28 21:47:45
These inequalities are hard for me to read, the or
rvargas (doing something else)
2013/07/11 19:54:55
Done.
| |
283 block_count < 1 || block_count > 4) | 296 block_count < 1 || block_count > kMaxNumBlocks) { |
284 return false; | 297 return false; |
298 } | |
285 if (!init_) | 299 if (!init_) |
gavinp
2013/06/28 21:47:45
Nit: could just add this to the earlier test, and
rvargas (doing something else)
2013/07/11 19:54:55
In my mind the two checks do two different things:
| |
286 return false; | 300 return false; |
287 | 301 |
288 MappedFile* file = FileForNewBlock(block_type, block_count); | 302 MappedFile* file = FileForNewBlock(block_type, block_count); |
289 if (!file) | 303 if (!file) |
290 return false; | 304 return false; |
291 | 305 |
292 ScopedFlush flush(file); | 306 ScopedFlush flush(file); |
293 BlockHeader header(file); | 307 BlockHeader header(file); |
294 | 308 |
295 int target_size = 0; | 309 int target_size = 0; |
296 for (int i = block_count; i <= 4; i++) { | 310 for (int i = block_count; i <= kMaxNumBlocks; i++) { |
297 if (header->empty[i - 1]) { | 311 if (header->empty[i - 1]) { |
298 target_size = i; | 312 target_size = i; |
299 break; | 313 break; |
300 } | 314 } |
301 } | 315 } |
302 | 316 |
303 DCHECK(target_size); | 317 DCHECK(target_size); |
304 int index; | 318 int index; |
305 if (!header.CreateMapBlock(target_size, block_count, &index)) | 319 if (!header.CreateMapBlock(target_size, block_count, &index)) |
306 return false; | 320 return false; |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
664 *load = 0; | 678 *load = 0; |
665 for (;;) { | 679 for (;;) { |
666 if (!block_files_[index] && !OpenBlockFile(index)) | 680 if (!block_files_[index] && !OpenBlockFile(index)) |
667 return; | 681 return; |
668 | 682 |
669 BlockFileHeader* header = | 683 BlockFileHeader* header = |
670 reinterpret_cast<BlockFileHeader*>(block_files_[index]->buffer()); | 684 reinterpret_cast<BlockFileHeader*>(block_files_[index]->buffer()); |
671 | 685 |
672 max_blocks += header->max_entries; | 686 max_blocks += header->max_entries; |
673 int used = header->max_entries; | 687 int used = header->max_entries; |
674 for (int i = 0; i < 4; i++) { | 688 for (int i = 0; i < kMaxNumBlocks; i++) { |
675 used -= header->empty[i] * (i + 1); | 689 used -= header->empty[i] * (i + 1); |
676 DCHECK_GE(used, 0); | 690 DCHECK_GE(used, 0); |
677 } | 691 } |
678 *used_count += used; | 692 *used_count += used; |
679 | 693 |
680 if (!header->next_file) | 694 if (!header->next_file) |
681 break; | 695 break; |
682 index = header->next_file; | 696 index = header->next_file; |
683 } | 697 } |
684 if (max_blocks) | 698 if (max_blocks) |
685 *load = *used_count * 100 / max_blocks; | 699 *load = *used_count * 100 / max_blocks; |
686 } | 700 } |
687 | 701 |
688 base::FilePath BlockFiles::Name(int index) { | 702 base::FilePath BlockFiles::Name(int index) { |
689 // The file format allows for 256 files. | 703 // The file format allows for 256 files. |
690 DCHECK(index < 256 || index >= 0); | 704 DCHECK(index < 256 || index >= 0); |
691 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); | 705 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); |
692 return path_.AppendASCII(tmp); | 706 return path_.AppendASCII(tmp); |
693 } | 707 } |
694 | 708 |
695 } // namespace disk_cache | 709 } // namespace disk_cache |
OLD | NEW |