aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVRKernel/Src/Kernel/OVR_SharedMemory.h
blob: dd155f7b0415a55ffba76189ec4a99d23c75afd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/************************************************************************************

PublicHeader:   OVR
Filename    :   OVR_SharedMemory.h
Content     :   Inter-process shared memory subsystem
Created     :   June 1, 2014
Notes       : 

Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
you may not use the Oculus VR Rift SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#ifndef OVR_SharedMemory_h
#define OVR_SharedMemory_h

#include "OVR_Types.h"
#include "OVR_RefCount.h"
#include "OVR_Allocator.h"
#include "OVR_System.h"
#include "OVR_String.h"


namespace OVR {

class SharedMemoryInternalBase; // Opaque


// SharedMemory
// Note: Safe when used between 32-bit and 64-bit processes
class SharedMemory : public RefCountBase<SharedMemory>
{
    friend class SharedMemoryFactory;

    OVR_NON_COPYABLE(SharedMemory);

public:
    // Only constructed by the SharedMemory Factory
    SharedMemory(int size, void* data, const String& name, SharedMemoryInternalBase* pInternal);
    // Call close when it goes out of scope
    ~SharedMemory();

    // Modes for opening a new shared memory region
    enum OpenMode
    {
        // Note: On Windows, Create* requires Administrator priviledges or running as a Service.
        OpenMode_CreateOnly,        // Must not already exist
        OpenMode_OpenOnly,            // Must already exist
        OpenMode_CreateOrOpen        // May exist or not
    };

    // Local access restrictions
    enum AccessMode
    {
        AccessMode_ReadOnly,        // Acquire read-only access
        AccessMode_ReadWrite,        // Acquire read or write access
    };

    // Remote access restrictions
    enum RemoteMode
    {
        RemoteMode_ReadOnly,        // Other processes will need to open in read-only mode
        RemoteMode_ReadWrite        // Other processes can open in read-write mode
    };

    // Modes for opening a new shared memory region
    struct OpenParameters
    {
        OpenParameters() :
            globalName(NULL),
            minSizeBytes(0),
            openMode(SharedMemory::OpenMode_CreateOrOpen),
            remoteMode(SharedMemory::RemoteMode_ReadWrite),
            accessMode(SharedMemory::AccessMode_ReadWrite)
        {
        }

        // Creation parameters
        const char*              globalName;   // Name of the shared memory region
        int                      minSizeBytes; // Minimum number of bytes to request
        SharedMemory::OpenMode   openMode;     // Creating the file or opening the file?
        SharedMemory::RemoteMode remoteMode;   // When creating, what access should other processes get?
        SharedMemory::AccessMode accessMode;   // When opening/creating, what access should this process get?
    };

public:
    // Returns the size of the shared memory region
    int GetSizeI() const
    {
        return Size;
    }

    // Returns the process-local pointer to the shared memory region
    // Note: This may be different on different processes
    void* GetData() const
    {
        return Data;
    }

    // Returns the name of the shared memory region
    String GetName()
    {
        return Name;
    }

protected:
    int Size;        // How many shared bytes are shared at the pointer address?
    void* Data;        // Pointer to the shared memory region.
    String Name;    // Name that can be used to access this region

    // Hidden implementation class for OS-specific behavior
    SharedMemoryInternalBase* Internal;

    // Close and cleanup the shared memory region
    // Note: This is called on destruction
    void Close();

public:
    static void SetFakeSharedMemory(bool enabled);
    static bool IsFakingSharedMemory();
};


// SharedMemoryFactory
class SharedMemoryFactory : public NewOverrideBase, public SystemSingletonBase<SharedMemoryFactory>
{
    OVR_DECLARE_SINGLETON(SharedMemoryFactory);

public:
    // Construct a SharedMemory object.
    // Note: The new object is reference-counted so it should be stored with Ptr<>.  Initial reference count is 1.
    Ptr<SharedMemory> Open(const SharedMemory::OpenParameters&);
};


// A shared object
// Its constructor will be called when creating a writer
// Its destructor will not be called
template<class SharedType>
class ISharedObject : public RefCountBase<ISharedObject<SharedType> >
{
public:
    static const int RegionSize = (int)sizeof(SharedType);

protected:
    Ptr<SharedMemory> pSharedMemory;

    bool Open(const char* name, bool readOnly)
    {
        // Configure open parameters based on read-only mode
        SharedMemory::OpenParameters params;

        // FIXME: This is a hack.  We currently need to allow clients to open this for read-write even
        // though they only need read-only access.  This is because in the first 0.4 release the
        // LocklessUpdater class technically writes to it (increments by 0) to read from the space.
        // This was quickly corrected in 0.4.1 and we are waiting for the right time to disallow write
        // access when everyone upgrades to 0.4.1+.
        //params.remoteMode = SharedMemory::RemoteMode_ReadOnly;
        params.remoteMode   = SharedMemory::RemoteMode_ReadWrite;
        params.globalName   = name;
        params.accessMode   = readOnly ? SharedMemory::AccessMode_ReadOnly : SharedMemory::AccessMode_ReadWrite;
        params.minSizeBytes = RegionSize;
        params.openMode     = readOnly ? SharedMemory::OpenMode_OpenOnly : SharedMemory::OpenMode_CreateOrOpen;

        // Attempt to open the shared memory file
        pSharedMemory = SharedMemoryFactory::GetInstance()->Open(params);

        // If it was not able to be opened,
        if (pSharedMemory && pSharedMemory->GetSizeI() >= RegionSize && pSharedMemory->GetData())
        {
            // If writing,
            if (!readOnly)
            {
                // Construct the object also
                Construct<SharedType>(pSharedMemory->GetData());
            }

            return true;
        }

        return false;
    }

    SharedType* Get() const
    {
        if (!pSharedMemory)
        {
            return NULL;
        }

        void* data = pSharedMemory->GetData();
        if (!data)
        {
            return NULL;
        }

        return reinterpret_cast<SharedType*>(data);
    }

public:
    String GetName() const
    {
        return pSharedMemory ? pSharedMemory->GetName() : "";
    }
};

// Writer specialized shared object: Ctor will be called on Open()
template<class SharedType>
class SharedObjectWriter : public ISharedObject<SharedType>
{
public:
    OVR_FORCE_INLINE bool Open(const char* name)
    {
        return ISharedObject<SharedType>::Open(name, false);
    }
    OVR_FORCE_INLINE SharedType* Get()
    {
        return ISharedObject<SharedType>::Get();
    }
};

// Reader specialized shared object: Ctor will not be called
template<class SharedType>
class SharedObjectReader : public ISharedObject<SharedType>
{
public:
    OVR_FORCE_INLINE bool Open(const char* name)
    {
        return ISharedObject<SharedType>::Open(name, true);
    }
    OVR_FORCE_INLINE const SharedType* Get() const
    {
        return ISharedObject<SharedType>::Get();
    }
};


} // namespace OVR

#endif // OVR_SharedMemory_h