OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/system/raw_shared_buffer.h" | 5 #include "mojo/system/raw_shared_buffer.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
13 #include "mojo/embedder/platform_handle.h" | 13 #include "mojo/embedder/platform_handle.h" |
14 #include "mojo/embedder/scoped_platform_handle.h" | 14 #include "mojo/embedder/scoped_platform_handle.h" |
15 | 15 |
16 namespace mojo { | 16 namespace mojo { |
17 namespace system { | 17 namespace system { |
18 | 18 |
19 // RawSharedBuffer::Mapping ---------------------------------------------------- | |
20 | |
21 void RawSharedBuffer::Mapping::Unmap() { | |
22 BOOL result = UnmapViewOfFile(real_base_); | |
23 PLOG_IF(ERROR, !result) << "UnmapViewOfFile"; | |
24 } | |
25 | |
26 // RawSharedBuffer ------------------------------------------------------------- | 19 // RawSharedBuffer ------------------------------------------------------------- |
27 | 20 |
28 bool RawSharedBuffer::InitNoLock() { | 21 bool RawSharedBuffer::InitNoLock() { |
29 DCHECK(!handle_.is_valid()); | 22 DCHECK(!handle_.is_valid()); |
30 | 23 |
31 // TODO(vtl): Currently, we only support mapping up to 2^32-1 bytes. | 24 // TODO(vtl): Currently, we only support mapping up to 2^32-1 bytes. |
32 if (static_cast<uint64_t>(num_bytes_) > | 25 if (static_cast<uint64_t>(num_bytes_) > |
33 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())) { | 26 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())) { |
34 return false; | 27 return false; |
35 } | 28 } |
36 | 29 |
37 // IMPORTANT NOTE: Unnamed objects are NOT SECURABLE. Thus if we ever want to | 30 // IMPORTANT NOTE: Unnamed objects are NOT SECURABLE. Thus if we ever want to |
38 // share read-only to other processes, we'll have to name our file mapping | 31 // share read-only to other processes, we'll have to name our file mapping |
39 // object. | 32 // object. |
40 // TODO(vtl): Unlike |base::SharedMemory|, we don't round up the size (to a | 33 // TODO(vtl): Unlike |base::SharedMemory|, we don't round up the size (to a |
41 // multiple of 64 KB). This may cause problems with NaCl. Cross this bridge | 34 // multiple of 64 KB). This may cause problems with NaCl. Cross this bridge |
42 // when we get there. crbug.com/210609 | 35 // when we get there. crbug.com/210609 |
43 handle_.reset(embedder::PlatformHandle(CreateFileMapping( | 36 handle_.reset(embedder::PlatformHandle(CreateFileMapping( |
44 INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, | 37 INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, |
45 static_cast<DWORD>(num_bytes_), NULL))); | 38 static_cast<DWORD>(num_bytes_), NULL))); |
46 if (!handle_.is_valid()) { | 39 if (!handle_.is_valid()) { |
47 PLOG(ERROR) << "CreateFileMapping"; | 40 PLOG(ERROR) << "CreateFileMapping"; |
48 return false; | 41 return false; |
49 } | 42 } |
50 | 43 |
51 return true; | 44 return true; |
52 } | 45 } |
53 | 46 |
54 scoped_ptr<RawSharedBuffer::Mapping> RawSharedBuffer::MapImplNoLock( | 47 scoped_ptr<RawSharedBufferMapping> RawSharedBuffer::MapImplNoLock( |
55 size_t offset, | 48 size_t offset, |
56 size_t length) { | 49 size_t length) { |
57 lock_.AssertAcquired(); | 50 lock_.AssertAcquired(); |
58 | 51 |
59 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | 52 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); |
60 size_t real_offset = offset - offset_rounding; | 53 size_t real_offset = offset - offset_rounding; |
61 size_t real_length = length + offset_rounding; | 54 size_t real_length = length + offset_rounding; |
62 | 55 |
63 // This should hold (since we checked |num_bytes| versus the maximum value of | 56 // This should hold (since we checked |num_bytes| versus the maximum value of |
64 // |off_t| on creation, but it never hurts to be paranoid. | 57 // |off_t| on creation, but it never hurts to be paranoid. |
65 DCHECK_LE(static_cast<uint64_t>(real_offset), | 58 DCHECK_LE(static_cast<uint64_t>(real_offset), |
66 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())); | 59 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())); |
67 | 60 |
68 void* real_base = MapViewOfFile( | 61 void* real_base = MapViewOfFile( |
69 handle_.get().handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, | 62 handle_.get().handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, |
70 static_cast<DWORD>(real_offset), real_length); | 63 static_cast<DWORD>(real_offset), real_length); |
71 if (!real_base) { | 64 if (!real_base) { |
72 PLOG(ERROR) << "MapViewOfFile"; | 65 PLOG(ERROR) << "MapViewOfFile"; |
73 return scoped_ptr<Mapping>(); | 66 return scoped_ptr<RawSharedBufferMapping>(); |
74 } | 67 } |
75 | 68 |
76 void* base = static_cast<char*>(real_base) + offset_rounding; | 69 void* base = static_cast<char*>(real_base) + offset_rounding; |
77 return make_scoped_ptr(new Mapping(base, length, real_base, real_length)); | 70 return make_scoped_ptr( |
| 71 new RawSharedBufferMapping(base, length, real_base, real_length)); |
| 72 } |
| 73 |
| 74 // RawSharedBufferMapping ------------------------------------------------------ |
| 75 |
| 76 void RawSharedBufferMapping::Unmap() { |
| 77 BOOL result = UnmapViewOfFile(real_base_); |
| 78 PLOG_IF(ERROR, !result) << "UnmapViewOfFile"; |
78 } | 79 } |
79 | 80 |
80 } // namespace system | 81 } // namespace system |
81 } // namespace mojo | 82 } // namespace mojo |
OLD | NEW |