|
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/. */ |
|
7 |
|
8 #ifndef mozilla_ipc_SharedMemory_h |
|
9 #define mozilla_ipc_SharedMemory_h |
|
10 |
|
11 #include "nsDebug.h" |
|
12 #include "nsISupportsImpl.h" // NS_INLINE_DECL_REFCOUNTING |
|
13 #include "mozilla/Attributes.h" |
|
14 |
|
15 // |
|
16 // This is a low-level wrapper around platform shared memory. Don't |
|
17 // use it directly; use Shmem allocated through IPDL interfaces. |
|
18 // |
|
19 namespace { |
|
20 enum Rights { |
|
21 RightsNone = 0, |
|
22 RightsRead = 1 << 0, |
|
23 RightsWrite = 1 << 1 |
|
24 }; |
|
25 } |
|
26 |
|
27 namespace mozilla { |
|
28 namespace ipc { |
|
29 |
|
30 class SharedMemory |
|
31 { |
|
32 public: |
|
33 virtual ~SharedMemory() |
|
34 { |
|
35 MOZ_COUNT_DTOR(SharedMemory); |
|
36 Unmapped(); |
|
37 Destroyed(); |
|
38 } |
|
39 |
|
40 enum SharedMemoryType { |
|
41 TYPE_BASIC, |
|
42 TYPE_SYSV, |
|
43 TYPE_UNKNOWN |
|
44 }; |
|
45 |
|
46 size_t Size() const { return mMappedSize; } |
|
47 |
|
48 virtual void* memory() const = 0; |
|
49 |
|
50 virtual bool Create(size_t size) = 0; |
|
51 virtual bool Map(size_t nBytes) = 0; |
|
52 |
|
53 virtual SharedMemoryType Type() const = 0; |
|
54 |
|
55 void |
|
56 Protect(char* aAddr, size_t aSize, int aRights) |
|
57 { |
|
58 char* memStart = reinterpret_cast<char*>(memory()); |
|
59 if (!memStart) |
|
60 NS_RUNTIMEABORT("SharedMemory region points at NULL!"); |
|
61 char* memEnd = memStart + Size(); |
|
62 |
|
63 char* protStart = aAddr; |
|
64 if (!protStart) |
|
65 NS_RUNTIMEABORT("trying to Protect() a NULL region!"); |
|
66 char* protEnd = protStart + aSize; |
|
67 |
|
68 if (!(memStart <= protStart |
|
69 && protEnd <= memEnd)) |
|
70 NS_RUNTIMEABORT("attempt to Protect() a region outside this SharedMemory"); |
|
71 |
|
72 // checks alignment etc. |
|
73 SystemProtect(aAddr, aSize, aRights); |
|
74 } |
|
75 |
|
76 NS_INLINE_DECL_REFCOUNTING(SharedMemory) |
|
77 |
|
78 static void SystemProtect(char* aAddr, size_t aSize, int aRights); |
|
79 static size_t SystemPageSize(); |
|
80 static size_t PageAlignedSize(size_t aSize); |
|
81 |
|
82 protected: |
|
83 SharedMemory(); |
|
84 |
|
85 // Implementations should call these methods on shmem usage changes, |
|
86 // but *only if* the OS-specific calls are known to have succeeded. |
|
87 // The methods are expected to be called in the pattern |
|
88 // |
|
89 // Created (Mapped Unmapped)* Destroy |
|
90 // |
|
91 // but this isn't checked. |
|
92 void Created(size_t aNBytes); |
|
93 void Mapped(size_t aNBytes); |
|
94 void Unmapped(); |
|
95 void Destroyed(); |
|
96 |
|
97 // The size of the shmem region requested in Create(), if |
|
98 // successful. SharedMemory instances that are opened from a |
|
99 // foreign handle have an alloc size of 0, even though they have |
|
100 // access to the alloc-size information. |
|
101 size_t mAllocSize; |
|
102 // The size of the region mapped in Map(), if successful. All |
|
103 // SharedMemorys that are mapped have a non-zero mapped size. |
|
104 size_t mMappedSize; |
|
105 }; |
|
106 |
|
107 } // namespace ipc |
|
108 } // namespace mozilla |
|
109 |
|
110 |
|
111 #endif // ifndef mozilla_ipc_SharedMemory_h |