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_SharedMemory_h
9 #define mozilla_ipc_SharedMemory_h
12 #include "nsISupportsImpl.h" // NS_INLINE_DECL_REFCOUNTING
13 #include "mozilla/Attributes.h"
16 // This is a low-level wrapper around platform shared memory. Don't
17 // use it directly; use Shmem allocated through IPDL interfaces.
38 virtual ~SharedMemory()
45 enum SharedMemoryType
{
51 size_t Size() const { return mMappedSize
; }
53 virtual void* memory() const = 0;
55 virtual bool Create(size_t size
) = 0;
56 virtual bool Map(size_t nBytes
) = 0;
58 virtual SharedMemoryType
Type() const = 0;
61 Protect(char* aAddr
, size_t aSize
, int aRights
)
63 char* memStart
= reinterpret_cast<char*>(memory());
65 NS_RUNTIMEABORT("SharedMemory region points at NULL!");
66 char* memEnd
= memStart
+ Size();
68 char* protStart
= aAddr
;
70 NS_RUNTIMEABORT("trying to Protect() a NULL region!");
71 char* protEnd
= protStart
+ aSize
;
73 if (!(memStart
<= protStart
74 && protEnd
<= memEnd
))
75 NS_RUNTIMEABORT("attempt to Protect() a region outside this SharedMemory");
77 // checks alignment etc.
78 SystemProtect(aAddr
, aSize
, aRights
);
81 NS_INLINE_DECL_REFCOUNTING(SharedMemory
)
83 static void SystemProtect(char* aAddr
, size_t aSize
, int aRights
);
84 static size_t SystemPageSize();
85 static size_t PageAlignedSize(size_t aSize
);
90 // Implementations should call these methods on shmem usage changes,
91 // but *only if* the OS-specific calls are known to have succeeded.
92 // The methods are expected to be called in the pattern
94 // Created (Mapped Unmapped)* Destroy
96 // but this isn't checked.
97 void Created(size_t aNBytes
);
98 void Mapped(size_t aNBytes
);
102 // The size of the shmem region requested in Create(), if
103 // successful. SharedMemory instances that are opened from a
104 // foreign handle have an alloc size of 0, even though they have
105 // access to the alloc-size information.
107 // The size of the region mapped in Map(), if successful. All
108 // SharedMemorys that are mapped have a non-zero mapped size.
113 } // namespace mozilla
116 #endif // ifndef mozilla_ipc_SharedMemory_h