OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" | |
6 | |
7 #include <windows.h> | |
8 | |
9 #include <limits> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/sys_info.h" | |
13 #include "mojo/edk/embedder/platform_handle.h" | |
14 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
15 | |
16 namespace mojo { | |
17 namespace embedder { | |
18 | |
19 // SimplePlatformSharedBuffer -------------------------------------------------- | |
20 | |
21 bool SimplePlatformSharedBuffer::Init() { | |
22 DCHECK(!handle_.is_valid()); | |
23 | |
24 // TODO(vtl): Currently, we only support mapping up to 2^32-1 bytes. | |
25 if (static_cast<uint64_t>(num_bytes_) > | |
26 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())) { | |
27 return false; | |
28 } | |
29 | |
30 // IMPORTANT NOTE: Unnamed objects are NOT SECURABLE. Thus if we ever want to | |
31 // share read-only to other processes, we'll have to name our file mapping | |
32 // object. | |
33 // TODO(vtl): Unlike |base::SharedMemory|, we don't round up the size (to a | |
34 // multiple of 64 KB). This may cause problems with NaCl. Cross this bridge | |
35 // when we get there. crbug.com/210609 | |
36 handle_.reset(PlatformHandle( | |
37 CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, | |
38 static_cast<DWORD>(num_bytes_), nullptr))); | |
39 if (!handle_.is_valid()) { | |
40 PLOG(ERROR) << "CreateFileMapping"; | |
41 return false; | |
42 } | |
43 | |
44 return true; | |
45 } | |
46 | |
47 bool SimplePlatformSharedBuffer::InitFromPlatformHandle( | |
48 ScopedPlatformHandle platform_handle) { | |
49 DCHECK(!handle_.is_valid()); | |
50 | |
51 // TODO(vtl): Implement. | |
52 NOTIMPLEMENTED(); | |
53 return false; | |
54 } | |
55 | |
56 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapImpl( | |
57 size_t offset, | |
58 size_t length) { | |
59 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | |
60 size_t real_offset = offset - offset_rounding; | |
61 size_t real_length = length + offset_rounding; | |
62 | |
63 // 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. | |
65 DCHECK_LE(static_cast<uint64_t>(real_offset), | |
66 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())); | |
67 | |
68 void* real_base = | |
69 MapViewOfFile(handle_.get().handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, | |
70 static_cast<DWORD>(real_offset), real_length); | |
71 if (!real_base) { | |
72 PLOG(ERROR) << "MapViewOfFile"; | |
73 return nullptr; | |
74 } | |
75 | |
76 void* base = static_cast<char*>(real_base) + offset_rounding; | |
77 return make_scoped_ptr(new SimplePlatformSharedBufferMapping( | |
78 base, length, real_base, real_length)); | |
79 } | |
80 | |
81 // SimplePlatformSharedBufferMapping ------------------------------------------- | |
82 | |
83 void SimplePlatformSharedBufferMapping::Unmap() { | |
84 BOOL result = UnmapViewOfFile(real_base_); | |
85 PLOG_IF(ERROR, !result) << "UnmapViewOfFile"; | |
86 } | |
87 | |
88 } // namespace embedder | |
89 } // namespace mojo | |
OLD | NEW |