Backed out 6 changesets (bug 1875528) for causing leakcheck failures. CLOSED TREE
[gecko.git] / ipc / glue / SharedMemory.h
blob818fad5e69adf6605f39c3d1766c72146ffd8cbf
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
10 #include "nsDebug.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.
21 namespace {
22 enum Rights { RightsNone = 0, RightsRead = 1 << 0, RightsWrite = 1 << 1 };
23 } // namespace
25 namespace mozilla {
27 namespace ipc {
28 class SharedMemory;
29 } // namespace ipc
31 namespace ipc {
33 class SharedMemory {
34 protected:
35 virtual ~SharedMemory() {
36 Unmapped();
37 Destroyed();
40 public:
41 enum OpenRights {
42 RightsReadOnly = RightsRead,
43 RightsReadWrite = RightsRead | RightsWrite,
46 size_t Size() const { return mMappedSize; }
48 virtual void* memory() const = 0;
50 virtual bool Create(size_t size) = 0;
51 virtual bool Map(size_t nBytes, void* fixed_address = nullptr) = 0;
52 virtual void Unmap() = 0;
54 virtual void CloseHandle() = 0;
56 virtual bool WriteHandle(IPC::MessageWriter* aWriter) = 0;
57 virtual bool ReadHandle(IPC::MessageReader* aReader) = 0;
59 void Protect(char* aAddr, size_t aSize, int aRights) {
60 char* memStart = reinterpret_cast<char*>(memory());
61 if (!memStart) MOZ_CRASH("SharedMemory region points at NULL!");
62 char* memEnd = memStart + Size();
64 char* protStart = aAddr;
65 if (!protStart) MOZ_CRASH("trying to Protect() a NULL region!");
66 char* protEnd = protStart + aSize;
68 if (!(memStart <= protStart && protEnd <= memEnd))
69 MOZ_CRASH("attempt to Protect() a region outside this SharedMemory");
71 // checks alignment etc.
72 SystemProtect(aAddr, aSize, aRights);
75 // bug 1168843, compositor thread may create shared memory instances that are
76 // destroyed by main thread on shutdown, so this must use thread-safe RC to
77 // avoid hitting assertion
78 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedMemory)
80 static void SystemProtect(char* aAddr, size_t aSize, int aRights);
81 [[nodiscard]] static bool SystemProtectFallible(char* aAddr, size_t aSize,
82 int aRights);
83 static size_t SystemPageSize();
84 static size_t PageAlignedSize(size_t aSize);
86 protected:
87 SharedMemory();
89 // Implementations should call these methods on shmem usage changes,
90 // but *only if* the OS-specific calls are known to have succeeded.
91 // The methods are expected to be called in the pattern
93 // Created (Mapped Unmapped)* Destroy
95 // but this isn't checked.
96 void Created(size_t aNBytes);
97 void Mapped(size_t aNBytes);
98 void Unmapped();
99 void Destroyed();
101 // The size of the shmem region requested in Create(), if
102 // successful. SharedMemory instances that are opened from a
103 // foreign handle have an alloc size of 0, even though they have
104 // access to the alloc-size information.
105 size_t mAllocSize;
106 // The size of the region mapped in Map(), if successful. All
107 // SharedMemorys that are mapped have a non-zero mapped size.
108 size_t mMappedSize;
111 template <typename HandleImpl>
112 class SharedMemoryCommon : public SharedMemory {
113 public:
114 typedef HandleImpl Handle;
116 virtual Handle CloneHandle() = 0;
117 virtual Handle TakeHandle() = 0;
118 virtual bool IsHandleValid(const Handle& aHandle) const = 0;
119 virtual bool SetHandle(Handle aHandle, OpenRights aRights) = 0;
121 virtual void CloseHandle() override { TakeHandle(); }
123 virtual bool WriteHandle(IPC::MessageWriter* aWriter) override {
124 Handle handle = CloneHandle();
125 if (!handle) {
126 return false;
128 IPC::WriteParam(aWriter, std::move(handle));
129 return true;
132 virtual bool ReadHandle(IPC::MessageReader* aReader) override {
133 Handle handle;
134 return IPC::ReadParam(aReader, &handle) && IsHandleValid(handle) &&
135 SetHandle(std::move(handle), RightsReadWrite);
139 } // namespace ipc
140 } // namespace mozilla
142 #endif // ifndef mozilla_ipc_SharedMemory_h