ipc/glue/SharedMemory.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial