Bumping manifests a=b2g-bump
[gecko.git] / ipc / glue / SharedMemory.h
blob47cd200a2b217b375a3d71af0732585f8942ba06
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
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
11 #include "nsDebug.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.
19 namespace {
20 enum Rights {
21 RightsNone = 0,
22 RightsRead = 1 << 0,
23 RightsWrite = 1 << 1
27 namespace mozilla {
29 namespace ipc {
30 class SharedMemory;
33 namespace ipc {
35 class SharedMemory
37 protected:
38 virtual ~SharedMemory()
40 Unmapped();
41 Destroyed();
44 public:
45 enum SharedMemoryType {
46 TYPE_BASIC,
47 TYPE_SYSV,
48 TYPE_UNKNOWN
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;
60 void
61 Protect(char* aAddr, size_t aSize, int aRights)
63 char* memStart = reinterpret_cast<char*>(memory());
64 if (!memStart)
65 NS_RUNTIMEABORT("SharedMemory region points at NULL!");
66 char* memEnd = memStart + Size();
68 char* protStart = aAddr;
69 if (!protStart)
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);
87 protected:
88 SharedMemory();
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);
99 void Unmapped();
100 void Destroyed();
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.
106 size_t mAllocSize;
107 // The size of the region mapped in Map(), if successful. All
108 // SharedMemorys that are mapped have a non-zero mapped size.
109 size_t mMappedSize;
112 } // namespace ipc
113 } // namespace mozilla
116 #endif // ifndef mozilla_ipc_SharedMemory_h