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_SharedMemorySysV_h michael@0: #define mozilla_ipc_SharedMemorySysV_h michael@0: michael@0: #if (defined(OS_LINUX) && !defined(ANDROID)) || defined(OS_BSD) michael@0: michael@0: // SysV shared memory isn't available on Windows, but we define the michael@0: // following macro so that #ifdefs are clearer (compared to #ifdef michael@0: // OS_LINUX). michael@0: #define MOZ_HAVE_SHAREDMEMORYSYSV michael@0: michael@0: #include "SharedMemory.h" michael@0: michael@0: #include "nsDebug.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include 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: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: michael@0: michael@0: class SharedMemorySysV : public SharedMemory michael@0: { michael@0: public: michael@0: typedef int Handle; michael@0: michael@0: SharedMemorySysV() : michael@0: mHandle(-1), michael@0: mData(nullptr) michael@0: { michael@0: } michael@0: michael@0: SharedMemorySysV(Handle aHandle) : michael@0: mHandle(aHandle), michael@0: mData(nullptr) michael@0: { michael@0: } michael@0: michael@0: virtual ~SharedMemorySysV() michael@0: { michael@0: shmdt(mData); michael@0: mHandle = -1; michael@0: mData = nullptr; michael@0: } michael@0: michael@0: virtual bool Create(size_t aNbytes) MOZ_OVERRIDE michael@0: { michael@0: int id = shmget(IPC_PRIVATE, aNbytes, IPC_CREAT | 0600); michael@0: if (id == -1) michael@0: return false; michael@0: michael@0: mHandle = id; michael@0: mAllocSize = aNbytes; michael@0: Created(aNbytes); michael@0: michael@0: return Map(aNbytes); michael@0: } michael@0: michael@0: virtual bool Map(size_t nBytes) MOZ_OVERRIDE michael@0: { michael@0: // already mapped michael@0: if (mData) michael@0: return true; michael@0: michael@0: if (!IsHandleValid(mHandle)) michael@0: return false; michael@0: michael@0: void* mem = shmat(mHandle, nullptr, 0); michael@0: if (mem == (void*) -1) { michael@0: char warning[256]; michael@0: ::snprintf(warning, sizeof(warning)-1, michael@0: "shmat(): %s (%d)\n", strerror(errno), errno); michael@0: michael@0: NS_WARNING(warning); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // Mark the handle as deleted so that, should this process go away, the michael@0: // segment is cleaned up. michael@0: shmctl(mHandle, IPC_RMID, 0); michael@0: michael@0: mData = mem; michael@0: michael@0: #ifdef DEBUG michael@0: struct shmid_ds info; michael@0: if (shmctl(mHandle, IPC_STAT, &info) < 0) michael@0: return false; michael@0: michael@0: NS_ABORT_IF_FALSE(nBytes <= info.shm_segsz, michael@0: "Segment doesn't have enough space!"); michael@0: #endif michael@0: michael@0: Mapped(nBytes); michael@0: return true; michael@0: } michael@0: michael@0: virtual void* memory() const MOZ_OVERRIDE michael@0: { michael@0: return mData; michael@0: } michael@0: michael@0: virtual SharedMemoryType Type() const MOZ_OVERRIDE michael@0: { michael@0: return TYPE_SYSV; michael@0: } michael@0: michael@0: Handle GetHandle() const michael@0: { michael@0: NS_ABORT_IF_FALSE(IsHandleValid(mHandle), "invalid handle"); michael@0: return mHandle; michael@0: } michael@0: michael@0: static Handle NULLHandle() michael@0: { michael@0: return -1; michael@0: } michael@0: michael@0: static bool IsHandleValid(Handle aHandle) michael@0: { michael@0: return aHandle != -1; michael@0: } michael@0: michael@0: private: michael@0: Handle mHandle; michael@0: void* mData; michael@0: }; michael@0: michael@0: } // namespace ipc michael@0: } // namespace mozilla michael@0: michael@0: #endif // OS_LINUX michael@0: michael@0: #endif // ifndef mozilla_ipc_SharedMemorySysV_h