OLD | NEW |
---|---|
1 // Copyright (c) 2011 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 <errno.h> | 7 #include <errno.h> |
8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "net/disk_cache/disk_cache.h" | 12 #include "net/disk_cache/disk_cache.h" |
13 | 13 |
14 namespace disk_cache { | 14 namespace disk_cache { |
15 | 15 |
16 void* MappedFile::Init(const FilePath& name, size_t size) { | 16 void* MappedFile::Init(const FilePath& name, size_t size) { |
17 DCHECK(!init_); | 17 DCHECK(!init_); |
18 if (init_ || !File::Init(name)) | 18 if (init_ || !File::Init(name)) |
19 return NULL; | 19 return NULL; |
20 | 20 |
21 if (!size) | 21 if (!size) |
22 size = GetLength(); | 22 size = GetLength(); |
23 | 23 |
24 buffer_ = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, | 24 buffer_ = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, |
25 platform_file(), 0); | 25 platform_file(), 0); |
26 init_ = true; | 26 init_ = true; |
27 view_size_ = size; | |
27 DCHECK(reinterpret_cast<intptr_t>(buffer_) != -1); | 28 DCHECK(reinterpret_cast<intptr_t>(buffer_) != -1); |
28 if (reinterpret_cast<intptr_t>(buffer_) == -1) | 29 if (reinterpret_cast<intptr_t>(buffer_) == -1) |
29 buffer_ = 0; | 30 buffer_ = 0; |
30 | 31 |
31 view_size_ = size; | 32 // Make sure we detect hardware failures reading the headers. |
33 scoped_array<char> temp(new char[size]); | |
gavinp
2012/06/01 12:00:34
Now there's another difference with windows. When
| |
34 if (!Read(temp.get(), size, 0)) | |
35 return NULL; | |
36 | |
32 return buffer_; | 37 return buffer_; |
33 } | 38 } |
34 | 39 |
35 bool MappedFile::Load(const FileBlock* block) { | 40 bool MappedFile::Load(const FileBlock* block) { |
36 size_t offset = block->offset() + view_size_; | 41 size_t offset = block->offset() + view_size_; |
37 return Read(block->buffer(), block->size(), offset); | 42 return Read(block->buffer(), block->size(), offset); |
38 } | 43 } |
39 | 44 |
40 bool MappedFile::Store(const FileBlock* block) { | 45 bool MappedFile::Store(const FileBlock* block) { |
41 size_t offset = block->offset() + view_size_; | 46 size_t offset = block->offset() + view_size_; |
42 return Write(block->buffer(), block->size(), offset); | 47 return Write(block->buffer(), block->size(), offset); |
43 } | 48 } |
44 | 49 |
45 MappedFile::~MappedFile() { | 50 MappedFile::~MappedFile() { |
46 if (!init_) | 51 if (!init_) |
47 return; | 52 return; |
48 | 53 |
49 if (buffer_) { | 54 if (buffer_) { |
50 int ret = munmap(buffer_, view_size_); | 55 int ret = munmap(buffer_, view_size_); |
51 DCHECK_EQ(0, ret); | 56 DCHECK_EQ(0, ret); |
52 } | 57 } |
53 } | 58 } |
54 | 59 |
55 } // namespace disk_cache | 60 } // namespace disk_cache |
OLD | NEW |