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;
54 virtual void Unmap() = 0;
56 virtual void CloseHandle() = 0;
58 virtual SharedMemoryType
Type() const = 0;
60 virtual bool WriteHandle(IPC::MessageWriter
* aWriter
) = 0;
61 virtual bool ReadHandle(IPC::MessageReader
* aReader
) = 0;
63 void Protect(char* aAddr
, size_t aSize
, int aRights
) {
64 char* memStart
= reinterpret_cast<char*>(memory());
65 if (!memStart
) MOZ_CRASH("SharedMemory region points at NULL!");
66 char* memEnd
= memStart
+ Size();
68 char* protStart
= aAddr
;
69 if (!protStart
) MOZ_CRASH("trying to Protect() a NULL region!");
70 char* protEnd
= protStart
+ aSize
;
72 if (!(memStart
<= protStart
&& protEnd
<= memEnd
))
73 MOZ_CRASH("attempt to Protect() a region outside this SharedMemory");
75 // checks alignment etc.
76 SystemProtect(aAddr
, aSize
, aRights
);
79 // bug 1168843, compositor thread may create shared memory instances that are
80 // destroyed by main thread on shutdown, so this must use thread-safe RC to
81 // avoid hitting assertion
82 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedMemory
)
84 static void SystemProtect(char* aAddr
, size_t aSize
, int aRights
);
85 [[nodiscard
]] static bool SystemProtectFallible(char* aAddr
, size_t aSize
,
87 static size_t SystemPageSize();
88 static size_t PageAlignedSize(size_t aSize
);
93 // Implementations should call these methods on shmem usage changes,
94 // but *only if* the OS-specific calls are known to have succeeded.
95 // The methods are expected to be called in the pattern
97 // Created (Mapped Unmapped)* Destroy
99 // but this isn't checked.
100 void Created(size_t aNBytes
);
101 void Mapped(size_t aNBytes
);
105 // The size of the shmem region requested in Create(), if
106 // successful. SharedMemory instances that are opened from a
107 // foreign handle have an alloc size of 0, even though they have
108 // access to the alloc-size information.
110 // The size of the region mapped in Map(), if successful. All
111 // SharedMemorys that are mapped have a non-zero mapped size.
115 template <typename HandleImpl
>
116 class SharedMemoryCommon
: public SharedMemory
{
118 typedef HandleImpl Handle
;
120 virtual Handle
CloneHandle() = 0;
121 virtual bool IsHandleValid(const Handle
& aHandle
) const = 0;
122 virtual bool SetHandle(Handle aHandle
, OpenRights aRights
) = 0;
124 virtual bool WriteHandle(IPC::MessageWriter
* aWriter
) override
{
125 Handle handle
= CloneHandle();
129 IPC::WriteParam(aWriter
, std::move(handle
));
133 virtual bool ReadHandle(IPC::MessageReader
* aReader
) override
{
135 return IPC::ReadParam(aReader
, &handle
) && IsHandleValid(handle
) &&
136 SetHandle(std::move(handle
), RightsReadWrite
);
141 } // namespace mozilla
143 #endif // ifndef mozilla_ipc_SharedMemory_h