Chromium Code Reviews| 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 <errno.h> | 7 #include <errno.h> |
| 8 #include <string.h> | |
|
rvargas (doing something else)
2014/07/07 23:30:32
(and this should not be needed)
Mostyn Bramley-Moore
2014/07/08 08:07:19
Done.
| |
| 8 #include <sys/mman.h> | 9 #include <sys/mman.h> |
| 9 | 10 |
| 10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "net/disk_cache/disk_cache.h" | 13 #include "net/disk_cache/disk_cache.h" |
| 13 | 14 |
| 14 namespace disk_cache { | 15 namespace disk_cache { |
| 15 | 16 |
| 16 void* MappedFile::Init(const base::FilePath& name, size_t size) { | 17 void* MappedFile::Init(const base::FilePath& name, size_t size) { |
| 17 DCHECK(!init_); | 18 DCHECK(!init_); |
| 18 if (init_ || !File::Init(name)) | 19 if (init_ || !File::Init(name)) |
| 19 return NULL; | 20 return NULL; |
| 20 | 21 |
| 21 size_t temp_len = size ? size : 4096; | 22 size_t temp_len = size ? size : 4096; |
| 22 if (!size) | 23 if (!size) |
| 23 size = GetLength(); | 24 size = GetLength(); |
| 24 | 25 |
| 25 buffer_ = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, | 26 buffer_ = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 26 platform_file(), 0); | 27 platform_file(), 0); |
| 27 init_ = true; | 28 init_ = true; |
| 28 view_size_ = size; | 29 view_size_ = size; |
| 29 DCHECK(reinterpret_cast<intptr_t>(buffer_) != -1); | 30 DLOG_IF(FATAL, buffer_ == MAP_FAILED) << "Failed to mmap " << name.value() |
| 30 if (reinterpret_cast<intptr_t>(buffer_) == -1) | 31 << ": " << strerror(errno); |
|
rvargas (doing something else)
2014/07/07 23:30:32
nit: use DPLOG_IF instead
Mostyn Bramley-Moore
2014/07/08 08:07:19
I hadn't noticed the P logging macros before- nice
| |
| 32 if (buffer_ == MAP_FAILED) | |
| 31 buffer_ = 0; | 33 buffer_ = 0; |
| 32 | 34 |
| 33 // Make sure we detect hardware failures reading the headers. | 35 // Make sure we detect hardware failures reading the headers. |
| 34 scoped_ptr<char[]> temp(new char[temp_len]); | 36 scoped_ptr<char[]> temp(new char[temp_len]); |
| 35 if (!Read(temp.get(), temp_len, 0)) | 37 if (!Read(temp.get(), temp_len, 0)) |
| 36 return NULL; | 38 return NULL; |
| 37 | 39 |
| 38 return buffer_; | 40 return buffer_; |
| 39 } | 41 } |
| 40 | 42 |
| 41 void MappedFile::Flush() { | 43 void MappedFile::Flush() { |
| 42 } | 44 } |
| 43 | 45 |
| 44 MappedFile::~MappedFile() { | 46 MappedFile::~MappedFile() { |
| 45 if (!init_) | 47 if (!init_) |
| 46 return; | 48 return; |
| 47 | 49 |
| 48 if (buffer_) { | 50 if (buffer_) { |
| 49 int ret = munmap(buffer_, view_size_); | 51 int ret = munmap(buffer_, view_size_); |
| 50 DCHECK_EQ(0, ret); | 52 DCHECK_EQ(0, ret); |
| 51 } | 53 } |
| 52 } | 54 } |
| 53 | 55 |
| 54 } // namespace disk_cache | 56 } // namespace disk_cache |
| OLD | NEW |