1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=8 et :
3 : */
4 : /* ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is Mozilla IPC.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * The Mozilla Foundation
21 : * Portions created by the Initial Developer are Copyright (C) 2009-2010
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef mozilla_ipc_SharedMemory_h
41 : #define mozilla_ipc_SharedMemory_h
42 :
43 : #include "nsDebug.h"
44 : #include "nsISupportsImpl.h" // NS_INLINE_DECL_REFCOUNTING
45 :
46 : //
47 : // This is a low-level wrapper around platform shared memory. Don't
48 : // use it directly; use Shmem allocated through IPDL interfaces.
49 : //
50 : namespace {
51 : enum Rights {
52 : RightsNone = 0,
53 : RightsRead = 1 << 0,
54 : RightsWrite = 1 << 1
55 : };
56 : }
57 :
58 : namespace mozilla {
59 : namespace ipc {
60 :
61 : class SharedMemory
62 : {
63 : public:
64 : enum SharedMemoryType {
65 : TYPE_BASIC,
66 : TYPE_SYSV,
67 : TYPE_UNKNOWN
68 : };
69 :
70 0 : virtual ~SharedMemory() { Unmapped(); Destroyed(); }
71 :
72 0 : size_t Size() const { return mMappedSize; }
73 :
74 : virtual void* memory() const = 0;
75 :
76 : virtual bool Create(size_t size) = 0;
77 : virtual bool Map(size_t nBytes) = 0;
78 :
79 : virtual SharedMemoryType Type() const = 0;
80 :
81 : void
82 0 : Protect(char* aAddr, size_t aSize, int aRights)
83 : {
84 0 : char* memStart = reinterpret_cast<char*>(memory());
85 0 : if (!memStart)
86 0 : NS_RUNTIMEABORT("SharedMemory region points at NULL!");
87 0 : char* memEnd = memStart + Size();
88 :
89 0 : char* protStart = aAddr;
90 0 : if (!protStart)
91 0 : NS_RUNTIMEABORT("trying to Protect() a NULL region!");
92 0 : char* protEnd = protStart + aSize;
93 :
94 0 : if (!(memStart <= protStart
95 0 : && protEnd <= memEnd))
96 0 : NS_RUNTIMEABORT("attempt to Protect() a region outside this SharedMemory");
97 :
98 : // checks alignment etc.
99 0 : SystemProtect(aAddr, aSize, aRights);
100 0 : }
101 :
102 0 : NS_INLINE_DECL_REFCOUNTING(SharedMemory)
103 :
104 : static void SystemProtect(char* aAddr, size_t aSize, int aRights);
105 : static size_t SystemPageSize();
106 : static size_t PageAlignedSize(size_t aSize);
107 :
108 : protected:
109 : SharedMemory();
110 :
111 : // Implementations should call these methods on shmem usage changes,
112 : // but *only if* the OS-specific calls are known to have succeeded.
113 : // The methods are expected to be called in the pattern
114 : //
115 : // Created (Mapped Unmapped)* Destroy
116 : //
117 : // but this isn't checked.
118 : void Created(size_t aNBytes);
119 : void Mapped(size_t aNBytes);
120 : void Unmapped();
121 : void Destroyed();
122 :
123 : // The size of the shmem region requested in Create(), if
124 : // successful. SharedMemory instances that are opened from a
125 : // foreign handle have an alloc size of 0, even though they have
126 : // access to the alloc-size information.
127 : size_t mAllocSize;
128 : // The size of the region mapped in Map(), if successful. All
129 : // SharedMemorys that are mapped have a non-zero mapped size.
130 : size_t mMappedSize;
131 : };
132 :
133 : } // namespace ipc
134 : } // namespace mozilla
135 :
136 :
137 : #endif // ifndef mozilla_ipc_SharedMemory_h
|