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; | |
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]) | |
gavinp
2013/06/27 07:06:18
Nit: I'd find this more readable with () around bl
| |
204 have_space = true; | |
205 } | |
206 | |
207 return have_space; | |
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 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
686 } | 698 } |
687 | 699 |
688 base::FilePath BlockFiles::Name(int index) { | 700 base::FilePath BlockFiles::Name(int index) { |
689 // The file format allows for 256 files. | 701 // The file format allows for 256 files. |
690 DCHECK(index < 256 || index >= 0); | 702 DCHECK(index < 256 || index >= 0); |
691 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); | 703 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); |
692 return path_.AppendASCII(tmp); | 704 return path_.AppendASCII(tmp); |
693 } | 705 } |
694 | 706 |
695 } // namespace disk_cache | 707 } // namespace disk_cache |
OLD | NEW |