michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=8 et : michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_ipc_SharedMemory_h michael@0: #define mozilla_ipc_SharedMemory_h michael@0: michael@0: #include "nsDebug.h" michael@0: #include "nsISupportsImpl.h" // NS_INLINE_DECL_REFCOUNTING michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: // michael@0: // This is a low-level wrapper around platform shared memory. Don't michael@0: // use it directly; use Shmem allocated through IPDL interfaces. michael@0: // michael@0: namespace { michael@0: enum Rights { michael@0: RightsNone = 0, michael@0: RightsRead = 1 << 0, michael@0: RightsWrite = 1 << 1 michael@0: }; michael@0: } michael@0: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: michael@0: class SharedMemory michael@0: { michael@0: public: michael@0: virtual ~SharedMemory() michael@0: { michael@0: MOZ_COUNT_DTOR(SharedMemory); michael@0: Unmapped(); michael@0: Destroyed(); michael@0: } michael@0: michael@0: enum SharedMemoryType { michael@0: TYPE_BASIC, michael@0: TYPE_SYSV, michael@0: TYPE_UNKNOWN michael@0: }; michael@0: michael@0: size_t Size() const { return mMappedSize; } michael@0: michael@0: virtual void* memory() const = 0; michael@0: michael@0: virtual bool Create(size_t size) = 0; michael@0: virtual bool Map(size_t nBytes) = 0; michael@0: michael@0: virtual SharedMemoryType Type() const = 0; michael@0: michael@0: void michael@0: Protect(char* aAddr, size_t aSize, int aRights) michael@0: { michael@0: char* memStart = reinterpret_cast(memory()); michael@0: if (!memStart) michael@0: NS_RUNTIMEABORT("SharedMemory region points at NULL!"); michael@0: char* memEnd = memStart + Size(); michael@0: michael@0: char* protStart = aAddr; michael@0: if (!protStart) michael@0: NS_RUNTIMEABORT("trying to Protect() a NULL region!"); michael@0: char* protEnd = protStart + aSize; michael@0: michael@0: if (!(memStart <= protStart michael@0: && protEnd <= memEnd)) michael@0: NS_RUNTIMEABORT("attempt to Protect() a region outside this SharedMemory"); michael@0: michael@0: // checks alignment etc. michael@0: SystemProtect(aAddr, aSize, aRights); michael@0: } michael@0: michael@0: NS_INLINE_DECL_REFCOUNTING(SharedMemory) michael@0: michael@0: static void SystemProtect(char* aAddr, size_t aSize, int aRights); michael@0: static size_t SystemPageSize(); michael@0: static size_t PageAlignedSize(size_t aSize); michael@0: michael@0: protected: michael@0: SharedMemory(); michael@0: michael@0: // Implementations should call these methods on shmem usage changes, michael@0: // but *only if* the OS-specific calls are known to have succeeded. michael@0: // The methods are expected to be called in the pattern michael@0: // michael@0: // Created (Mapped Unmapped)* Destroy michael@0: // michael@0: // but this isn't checked. michael@0: void Created(size_t aNBytes); michael@0: void Mapped(size_t aNBytes); michael@0: void Unmapped(); michael@0: void Destroyed(); michael@0: michael@0: // The size of the shmem region requested in Create(), if michael@0: // successful. SharedMemory instances that are opened from a michael@0: // foreign handle have an alloc size of 0, even though they have michael@0: // access to the alloc-size information. michael@0: size_t mAllocSize; michael@0: // The size of the region mapped in Map(), if successful. All michael@0: // SharedMemorys that are mapped have a non-zero mapped size. michael@0: size_t mMappedSize; michael@0: }; michael@0: michael@0: } // namespace ipc michael@0: } // namespace mozilla michael@0: michael@0: michael@0: #endif // ifndef mozilla_ipc_SharedMemory_h