1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/SharedMemory.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,111 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=8 et : 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#ifndef mozilla_ipc_SharedMemory_h 1.12 +#define mozilla_ipc_SharedMemory_h 1.13 + 1.14 +#include "nsDebug.h" 1.15 +#include "nsISupportsImpl.h" // NS_INLINE_DECL_REFCOUNTING 1.16 +#include "mozilla/Attributes.h" 1.17 + 1.18 +// 1.19 +// This is a low-level wrapper around platform shared memory. Don't 1.20 +// use it directly; use Shmem allocated through IPDL interfaces. 1.21 +// 1.22 +namespace { 1.23 +enum Rights { 1.24 + RightsNone = 0, 1.25 + RightsRead = 1 << 0, 1.26 + RightsWrite = 1 << 1 1.27 +}; 1.28 +} 1.29 + 1.30 +namespace mozilla { 1.31 +namespace ipc { 1.32 + 1.33 +class SharedMemory 1.34 +{ 1.35 +public: 1.36 + virtual ~SharedMemory() 1.37 + { 1.38 + MOZ_COUNT_DTOR(SharedMemory); 1.39 + Unmapped(); 1.40 + Destroyed(); 1.41 + } 1.42 + 1.43 + enum SharedMemoryType { 1.44 + TYPE_BASIC, 1.45 + TYPE_SYSV, 1.46 + TYPE_UNKNOWN 1.47 + }; 1.48 + 1.49 + size_t Size() const { return mMappedSize; } 1.50 + 1.51 + virtual void* memory() const = 0; 1.52 + 1.53 + virtual bool Create(size_t size) = 0; 1.54 + virtual bool Map(size_t nBytes) = 0; 1.55 + 1.56 + virtual SharedMemoryType Type() const = 0; 1.57 + 1.58 + void 1.59 + Protect(char* aAddr, size_t aSize, int aRights) 1.60 + { 1.61 + char* memStart = reinterpret_cast<char*>(memory()); 1.62 + if (!memStart) 1.63 + NS_RUNTIMEABORT("SharedMemory region points at NULL!"); 1.64 + char* memEnd = memStart + Size(); 1.65 + 1.66 + char* protStart = aAddr; 1.67 + if (!protStart) 1.68 + NS_RUNTIMEABORT("trying to Protect() a NULL region!"); 1.69 + char* protEnd = protStart + aSize; 1.70 + 1.71 + if (!(memStart <= protStart 1.72 + && protEnd <= memEnd)) 1.73 + NS_RUNTIMEABORT("attempt to Protect() a region outside this SharedMemory"); 1.74 + 1.75 + // checks alignment etc. 1.76 + SystemProtect(aAddr, aSize, aRights); 1.77 + } 1.78 + 1.79 + NS_INLINE_DECL_REFCOUNTING(SharedMemory) 1.80 + 1.81 + static void SystemProtect(char* aAddr, size_t aSize, int aRights); 1.82 + static size_t SystemPageSize(); 1.83 + static size_t PageAlignedSize(size_t aSize); 1.84 + 1.85 +protected: 1.86 + SharedMemory(); 1.87 + 1.88 + // Implementations should call these methods on shmem usage changes, 1.89 + // but *only if* the OS-specific calls are known to have succeeded. 1.90 + // The methods are expected to be called in the pattern 1.91 + // 1.92 + // Created (Mapped Unmapped)* Destroy 1.93 + // 1.94 + // but this isn't checked. 1.95 + void Created(size_t aNBytes); 1.96 + void Mapped(size_t aNBytes); 1.97 + void Unmapped(); 1.98 + void Destroyed(); 1.99 + 1.100 + // The size of the shmem region requested in Create(), if 1.101 + // successful. SharedMemory instances that are opened from a 1.102 + // foreign handle have an alloc size of 0, even though they have 1.103 + // access to the alloc-size information. 1.104 + size_t mAllocSize; 1.105 + // The size of the region mapped in Map(), if successful. All 1.106 + // SharedMemorys that are mapped have a non-zero mapped size. 1.107 + size_t mMappedSize; 1.108 +}; 1.109 + 1.110 +} // namespace ipc 1.111 +} // namespace mozilla 1.112 + 1.113 + 1.114 +#endif // ifndef mozilla_ipc_SharedMemory_h