Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/mapped_file.h" | 5 #include "net/disk_cache/mapped_file.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | |
| 9 #include "net/disk_cache/disk_cache.h" | 10 #include "net/disk_cache/disk_cache.h" |
| 10 | 11 |
| 11 namespace disk_cache { | 12 namespace disk_cache { |
| 12 | 13 |
| 13 void* MappedFile::Init(const FilePath& name, size_t size) { | 14 void* MappedFile::Init(const FilePath& name, size_t size) { |
| 14 DCHECK(!init_); | 15 DCHECK(!init_); |
| 15 if (init_ || !File::Init(name)) | 16 if (init_ || !File::Init(name)) |
| 16 return NULL; | 17 return NULL; |
| 17 | 18 |
| 18 buffer_ = NULL; | 19 buffer_ = NULL; |
| 19 init_ = true; | 20 init_ = true; |
| 20 section_ = CreateFileMapping(platform_file(), NULL, PAGE_READWRITE, 0, | 21 section_ = CreateFileMapping(platform_file(), NULL, PAGE_READWRITE, 0, |
| 21 static_cast<DWORD>(size), NULL); | 22 static_cast<DWORD>(size), NULL); |
| 22 if (!section_) | 23 if (!section_) |
| 23 return NULL; | 24 return NULL; |
| 24 | 25 |
| 25 buffer_ = MapViewOfFile(section_, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size); | 26 buffer_ = MapViewOfFile(section_, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size); |
| 26 DCHECK(buffer_); | 27 DCHECK(buffer_); |
| 27 view_size_ = size; | 28 view_size_ = size; |
| 28 | 29 |
| 30 // Make sure we detect hardware failures reading the headers. | |
| 31 size_t temp_len = size ? size : 4096; | |
| 32 scoped_array<char> temp(new char[temp_len]); | |
| 33 if (!Read(temp.get(), temp_len, 0)) | |
| 34 return NULL; | |
|
gavinp
2011/08/16 17:00:45
Minor Nit: Although this is almost at the end of t
| |
| 35 | |
| 29 return buffer_; | 36 return buffer_; |
| 30 } | 37 } |
| 31 | 38 |
| 32 MappedFile::~MappedFile() { | 39 MappedFile::~MappedFile() { |
| 33 if (!init_) | 40 if (!init_) |
| 34 return; | 41 return; |
| 35 | 42 |
| 36 if (buffer_) { | 43 if (buffer_) { |
| 37 BOOL ret = UnmapViewOfFile(buffer_); | 44 BOOL ret = UnmapViewOfFile(buffer_); |
| 38 DCHECK(ret); | 45 DCHECK(ret); |
| 39 } | 46 } |
| 40 | 47 |
| 41 if (section_) | 48 if (section_) |
| 42 CloseHandle(section_); | 49 CloseHandle(section_); |
| 43 } | 50 } |
| 44 | 51 |
| 45 bool MappedFile::Load(const FileBlock* block) { | 52 bool MappedFile::Load(const FileBlock* block) { |
| 46 size_t offset = block->offset() + view_size_; | 53 size_t offset = block->offset() + view_size_; |
| 47 return Read(block->buffer(), block->size(), offset); | 54 return Read(block->buffer(), block->size(), offset); |
| 48 } | 55 } |
| 49 | 56 |
| 50 bool MappedFile::Store(const FileBlock* block) { | 57 bool MappedFile::Store(const FileBlock* block) { |
| 51 size_t offset = block->offset() + view_size_; | 58 size_t offset = block->offset() + view_size_; |
| 52 return Write(block->buffer(), block->size(), offset); | 59 return Write(block->buffer(), block->size(), offset); |
| 53 } | 60 } |
| 54 | 61 |
| 55 } // namespace disk_cache | 62 } // namespace disk_cache |
| OLD | NEW |