1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef mozilla_ipc_Shmem_h
9 #define mozilla_ipc_Shmem_h
11 #include "mozilla/Attributes.h"
13 #include "base/basictypes.h"
14 #include "base/process.h"
18 #include "nsAutoPtr.h"
20 #include "ipc/IPCMessageUtils.h"
21 #include "mozilla/ipc/SharedMemory.h"
24 * |Shmem| is one agent in the IPDL shared memory scheme. The way it
27 * (1) C++ code calls, say, |parentActor->AllocShmem(size)|
29 * (2) IPDL-generated code creates a |mozilla::ipc::SharedMemory|
30 * wrapping the bare OS shmem primitives. The code then adds the new
31 * SharedMemory to the set of shmem segments being managed by IPDL.
33 * (3) IPDL-generated code "shares" the new SharedMemory to the child
34 * process, and then sends a special asynchronous IPC message to the
35 * child notifying it of the creation of the segment. (What this
36 * means is OS specific.)
38 * (4a) The child receives the special IPC message, and using the
39 * |SharedMemory{SysV,Basic}::Handle| it was passed, creates a
40 * |mozilla::ipc::SharedMemory| in the child
43 * (4b) After sending the "shmem-created" IPC message, IPDL-generated
44 * code in the parent returns a |mozilla::ipc::Shmem| back to the C++
45 * caller of |parentActor->AllocShmem()|. The |Shmem| is a "weak
46 * reference" to the underlying |SharedMemory|, which is managed by
47 * IPDL-generated code. C++ consumers of |Shmem| can't get at the
48 * underlying |SharedMemory|.
50 * If parent code wants to give access rights to the Shmem to the
51 * child, it does so by sending its |Shmem| to the child, in an IPDL
52 * message. The parent's |Shmem| then "dies", i.e. becomes
53 * inaccessible. This process could be compared to passing a
54 * "shmem-access baton" between parent and child.
59 class ShadowLayerForwarder
;
66 friend struct IPC::ParamTraits
<mozilla::ipc::Shmem
>;
68 // For ShadowLayerForwarder::CheckSurfaceDescriptor
69 friend class mozilla::layers::ShadowLayerForwarder
;
74 // Low-level wrapper around platform shmem primitives.
75 typedef mozilla::ipc::SharedMemory SharedMemory
;
76 typedef SharedMemory::SharedMemoryType SharedMemoryType
;
77 struct IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
{};
87 Shmem(const Shmem
& aOther
) :
88 mSegment(aOther
.mSegment
),
96 Shmem(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
,
97 SharedMemory
* aSegment
, id_t aId
) :
99 mData(aSegment
->memory()),
103 mSize
= static_cast<size_t>(*PtrToSize(mSegment
));
106 Shmem(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
,
107 SharedMemory
* aSegment
, id_t aId
);
112 // Shmem only holds a "weak ref" to the actual segment, which is
113 // owned by IPDL. So there's nothing interesting to be done here
114 forget(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead());
117 Shmem
& operator=(const Shmem
& aRhs
)
119 mSegment
= aRhs
.mSegment
;
126 bool operator==(const Shmem
& aRhs
) const
128 // need to compare IDs because of AdoptShmem(); two Shmems might
129 // refer to the same segment but with different IDs for different
130 // protocol trees. (NB: it's possible for this method to
131 // spuriously return true if AdoptShmem() gives the same ID for
132 // two protocol trees, but I don't think that can cause any
133 // problems since the Shmems really would be indistinguishable.)
134 return mSegment
== aRhs
.mSegment
&& mId
== aRhs
.mId
;
137 // Returns whether this Shmem is writable by you, and thus whether you can
138 // transfer writability to another actor.
142 return mSegment
!= nullptr;
145 // Returns whether this Shmem is readable by you, and thus whether you can
146 // transfer readability to another actor.
150 return mSegment
!= nullptr;
153 // Return a pointer to the user-visible data segment.
161 return reinterpret_cast<T
*>(mData
);
164 // Return the size of the segment as requested when this shmem
165 // segment was allocated, in units of T. The underlying mapping may
166 // actually be larger because of page alignment and private data,
167 // but this isn't exposed to clients.
175 return mSize
/ sizeof(T
);
178 int GetSysVID() const;
180 // These shouldn't be used directly, use the IPDL interface instead.
181 id_t
Id(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
) const {
185 SharedMemory
* Segment(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
) const {
190 void RevokeRights(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
)
194 void RevokeRights(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
);
197 void forget(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
)
205 static already_AddRefed
<Shmem::SharedMemory
>
206 Alloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
,
208 SharedMemoryType aType
,
210 bool aProtect
=false);
212 // Prepare this to be shared with |aProcess|. Return an IPC message
213 // that contains enough information for the other process to map
214 // this segment in OpenExisting() below. Return a new message if
215 // successful (owned by the caller), nullptr if not.
217 ShareTo(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
,
218 base::ProcessHandle aProcess
,
221 // Stop sharing this with |aProcess|. Return an IPC message that
222 // contains enough information for the other process to unmap this
223 // segment. Return a new message if successful (owned by the
224 // caller), nullptr if not.
226 UnshareFrom(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
,
227 base::ProcessHandle aProcess
,
230 // Return a SharedMemory instance in this process using the
231 // descriptor shared to us by the process that created the
232 // underlying OS shmem resource. The contents of the descriptor
233 // depend on the type of SharedMemory that was passed to us.
234 static already_AddRefed
<SharedMemory
>
235 OpenExisting(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
,
236 const IPC::Message
& aDescriptor
,
238 bool aProtect
=false);
241 Dealloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead
,
242 SharedMemory
* aSegment
);
246 void AssertAligned() const
248 if (0 != (mSize
% sizeof(T
)))
249 NS_RUNTIMEABORT("shmem is not T-aligned");
253 void AssertInvariants() const
257 PtrToSize(SharedMemory
* aSegment
)
260 reinterpret_cast<char*>(aSegment
->memory()) + aSegment
->Size();
261 return reinterpret_cast<uint32_t*>(endOfSegment
- sizeof(uint32_t));
265 void AssertInvariants() const;
268 SharedMemory
* MOZ_NON_OWNING_REF mSegment
;
276 } // namespace mozilla
282 struct ParamTraits
<mozilla::ipc::Shmem
>
284 typedef mozilla::ipc::Shmem paramType
;
286 // NB: Read()/Write() look creepy in that Shmems have a pointer
287 // member, but IPDL internally uses mId to properly initialize a
290 static void Write(Message
* aMsg
, const paramType
& aParam
)
292 WriteParam(aMsg
, aParam
.mId
);
295 static bool Read(const Message
* aMsg
, void** aIter
, paramType
* aResult
)
298 if (!ReadParam(aMsg
, aIter
, &id
))
304 static void Log(const paramType
& aParam
, std::wstring
* aLog
)
306 aLog
->append(L
"(shmem segment)");
314 #endif // ifndef mozilla_ipc_Shmem_h