Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(227)

Side by Side Diff: mojo/edk/embedder/simple_platform_shared_buffer_win.cc

Issue 1685513002: Replace Mojo SimplePlatformSharedMemory implementation with base::SharedMemory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix windows build. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <limits>
12
13 #include "base/logging.h"
14 #include "base/sys_info.h"
15 #include "mojo/edk/embedder/platform_handle.h"
16 #include "mojo/edk/embedder/scoped_platform_handle.h"
17
18 namespace mojo {
19 namespace edk {
20
21 // SimplePlatformSharedBuffer --------------------------------------------------
22
23 bool SimplePlatformSharedBuffer::Init() {
24 DCHECK(!handle_.is_valid());
25
26 // TODO(vtl): Currently, we only support mapping up to 2^32-1 bytes.
27 if (static_cast<uint64_t>(num_bytes_) >
28 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())) {
29 return false;
30 }
31
32 // IMPORTANT NOTE: Unnamed objects are NOT SECURABLE. Thus if we ever want to
33 // share read-only to other processes, we'll have to name our file mapping
34 // object.
35 // TODO(vtl): Unlike |base::SharedMemory|, we don't round up the size (to a
36 // multiple of 64 KB). This may cause problems with NaCl. Cross this bridge
37 // when we get there. crbug.com/210609
38 handle_.reset(PlatformHandle(
39 CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0,
40 static_cast<DWORD>(num_bytes_), nullptr)));
41 if (!handle_.is_valid()) {
42 PLOG(ERROR) << "CreateFileMapping";
43 return false;
44 }
45
46 return true;
47 }
48
49 bool SimplePlatformSharedBuffer::InitFromPlatformHandle(
50 ScopedPlatformHandle platform_handle) {
51 DCHECK(!handle_.is_valid());
52
53 handle_ = platform_handle.Pass();
54 return true;
55 }
56
57 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapImpl(
58 size_t offset,
59 size_t length) {
60 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity();
61 size_t real_offset = offset - offset_rounding;
62 size_t real_length = length + offset_rounding;
63
64 // This should hold (since we checked |num_bytes| versus the maximum value of
65 // |off_t| on creation, but it never hurts to be paranoid.
66 DCHECK_LE(static_cast<uint64_t>(real_offset),
67 static_cast<uint64_t>(std::numeric_limits<DWORD>::max()));
68
69 void* real_base =
70 MapViewOfFile(handle_.get().handle, FILE_MAP_READ | FILE_MAP_WRITE, 0,
71 static_cast<DWORD>(real_offset), real_length);
72 if (!real_base) {
73 PLOG(ERROR) << "MapViewOfFile";
74 return nullptr;
75 }
76
77 void* base = static_cast<char*>(real_base) + offset_rounding;
78 return make_scoped_ptr(new SimplePlatformSharedBufferMapping(
79 base, length, real_base, real_length));
80 }
81
82 // SimplePlatformSharedBufferMapping -------------------------------------------
83
84 void SimplePlatformSharedBufferMapping::Unmap() {
85 BOOL result = UnmapViewOfFile(real_base_);
86 PLOG_IF(ERROR, !result) << "UnmapViewOfFile";
87 }
88
89 } // namespace edk
90 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698