1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_ipc_SharedMemory_h
8 #define mozilla_ipc_SharedMemory_h
11 #include "nsISupportsImpl.h" // NS_INLINE_DECL_REFCOUNTING
12 #include "mozilla/Attributes.h"
14 #include "base/process.h"
15 #include "chrome/common/ipc_message_utils.h"
18 // This is a low-level wrapper around platform shared memory. Don't
19 // use it directly; use Shmem allocated through IPDL interfaces.
22 enum Rights
{ RightsNone
= 0, RightsRead
= 1 << 0, RightsWrite
= 1 << 1 };
35 virtual ~SharedMemory() {
41 enum SharedMemoryType
{ TYPE_BASIC
, TYPE_UNKNOWN
};
44 RightsReadOnly
= RightsRead
,
45 RightsReadWrite
= RightsRead
| RightsWrite
,
48 size_t Size() const { return mMappedSize
; }
50 virtual void* memory() const = 0;
52 virtual bool Create(size_t size
) = 0;
53 virtual bool Map(size_t nBytes
, void* fixed_address
= nullptr) = 0;
55 virtual void CloseHandle() = 0;
57 virtual SharedMemoryType
Type() const = 0;
59 virtual bool ShareHandle(base::ProcessId aProcessId
,
60 IPC::Message
* aMessage
) = 0;
61 virtual bool ReadHandle(const IPC::Message
* aMessage
,
62 PickleIterator
* aIter
) = 0;
64 void Protect(char* aAddr
, size_t aSize
, int aRights
) {
65 char* memStart
= reinterpret_cast<char*>(memory());
66 if (!memStart
) MOZ_CRASH("SharedMemory region points at NULL!");
67 char* memEnd
= memStart
+ Size();
69 char* protStart
= aAddr
;
70 if (!protStart
) MOZ_CRASH("trying to Protect() a NULL region!");
71 char* protEnd
= protStart
+ aSize
;
73 if (!(memStart
<= protStart
&& protEnd
<= memEnd
))
74 MOZ_CRASH("attempt to Protect() a region outside this SharedMemory");
76 // checks alignment etc.
77 SystemProtect(aAddr
, aSize
, aRights
);
80 // bug 1168843, compositor thread may create shared memory instances that are
81 // destroyed by main thread on shutdown, so this must use thread-safe RC to
82 // avoid hitting assertion
83 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedMemory
)
85 static void SystemProtect(char* aAddr
, size_t aSize
, int aRights
);
86 static MOZ_MUST_USE
bool SystemProtectFallible(char* aAddr
, size_t aSize
,
88 static size_t SystemPageSize();
89 static size_t PageAlignedSize(size_t aSize
);
94 // Implementations should call these methods on shmem usage changes,
95 // but *only if* the OS-specific calls are known to have succeeded.
96 // The methods are expected to be called in the pattern
98 // Created (Mapped Unmapped)* Destroy
100 // but this isn't checked.
101 void Created(size_t aNBytes
);
102 void Mapped(size_t aNBytes
);
106 // The size of the shmem region requested in Create(), if
107 // successful. SharedMemory instances that are opened from a
108 // foreign handle have an alloc size of 0, even though they have
109 // access to the alloc-size information.
111 // The size of the region mapped in Map(), if successful. All
112 // SharedMemorys that are mapped have a non-zero mapped size.
116 template <typename HandleImpl
>
117 class SharedMemoryCommon
: public SharedMemory
{
119 typedef HandleImpl Handle
;
121 virtual bool ShareToProcess(base::ProcessId aProcessId
, Handle
* aHandle
) = 0;
122 virtual bool IsHandleValid(const Handle
& aHandle
) const = 0;
123 virtual bool SetHandle(const Handle
& aHandle
, OpenRights aRights
) = 0;
125 virtual bool ShareHandle(base::ProcessId aProcessId
,
126 IPC::Message
* aMessage
) override
{
128 if (!ShareToProcess(aProcessId
, &handle
)) {
131 IPC::WriteParam(aMessage
, handle
);
135 virtual bool ReadHandle(const IPC::Message
* aMessage
,
136 PickleIterator
* aIter
) override
{
138 return IPC::ReadParam(aMessage
, aIter
, &handle
) && IsHandleValid(handle
) &&
139 SetHandle(handle
, RightsReadWrite
);
144 } // namespace mozilla
146 #endif // ifndef mozilla_ipc_SharedMemory_h