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/blockfile/mapped_file.h" | 5 #include "net/disk_cache/blockfile/mapped_file.h" |
6 | 6 |
| 7 #include <memory> |
| 8 |
7 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "net/disk_cache/disk_cache.h" | 11 #include "net/disk_cache/disk_cache.h" |
11 | 12 |
12 namespace disk_cache { | 13 namespace disk_cache { |
13 | 14 |
14 void* MappedFile::Init(const base::FilePath& name, size_t size) { | 15 void* MappedFile::Init(const base::FilePath& name, size_t size) { |
15 DCHECK(!init_); | 16 DCHECK(!init_); |
16 if (init_ || !File::Init(name)) | 17 if (init_ || !File::Init(name)) |
17 return NULL; | 18 return NULL; |
18 | 19 |
19 buffer_ = NULL; | 20 buffer_ = NULL; |
20 init_ = true; | 21 init_ = true; |
21 section_ = CreateFileMapping(platform_file(), NULL, PAGE_READWRITE, 0, | 22 section_ = CreateFileMapping(platform_file(), NULL, PAGE_READWRITE, 0, |
22 static_cast<DWORD>(size), NULL); | 23 static_cast<DWORD>(size), NULL); |
23 if (!section_) | 24 if (!section_) |
24 return NULL; | 25 return NULL; |
25 | 26 |
26 buffer_ = MapViewOfFile(section_, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size); | 27 buffer_ = MapViewOfFile(section_, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size); |
27 DCHECK(buffer_); | 28 DCHECK(buffer_); |
28 view_size_ = size; | 29 view_size_ = size; |
29 | 30 |
30 // Make sure we detect hardware failures reading the headers. | 31 // Make sure we detect hardware failures reading the headers. |
31 size_t temp_len = size ? size : 4096; | 32 size_t temp_len = size ? size : 4096; |
32 scoped_ptr<char[]> temp(new char[temp_len]); | 33 std::unique_ptr<char[]> temp(new char[temp_len]); |
33 if (!Read(temp.get(), temp_len, 0)) | 34 if (!Read(temp.get(), temp_len, 0)) |
34 return NULL; | 35 return NULL; |
35 | 36 |
36 return buffer_; | 37 return buffer_; |
37 } | 38 } |
38 | 39 |
39 MappedFile::~MappedFile() { | 40 MappedFile::~MappedFile() { |
40 if (!init_) | 41 if (!init_) |
41 return; | 42 return; |
42 | 43 |
43 if (buffer_) { | 44 if (buffer_) { |
44 BOOL ret = UnmapViewOfFile(buffer_); | 45 BOOL ret = UnmapViewOfFile(buffer_); |
45 DCHECK(ret); | 46 DCHECK(ret); |
46 } | 47 } |
47 | 48 |
48 if (section_) | 49 if (section_) |
49 CloseHandle(section_); | 50 CloseHandle(section_); |
50 } | 51 } |
51 | 52 |
52 void MappedFile::Flush() { | 53 void MappedFile::Flush() { |
53 } | 54 } |
54 | 55 |
55 } // namespace disk_cache | 56 } // namespace disk_cache |
OLD | NEW |