Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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_SharedMemorySysV_h |
michael@0 | 9 | #define mozilla_ipc_SharedMemorySysV_h |
michael@0 | 10 | |
michael@0 | 11 | #if (defined(OS_LINUX) && !defined(ANDROID)) || defined(OS_BSD) |
michael@0 | 12 | |
michael@0 | 13 | // SysV shared memory isn't available on Windows, but we define the |
michael@0 | 14 | // following macro so that #ifdefs are clearer (compared to #ifdef |
michael@0 | 15 | // OS_LINUX). |
michael@0 | 16 | #define MOZ_HAVE_SHAREDMEMORYSYSV |
michael@0 | 17 | |
michael@0 | 18 | #include "SharedMemory.h" |
michael@0 | 19 | |
michael@0 | 20 | #include "nsDebug.h" |
michael@0 | 21 | |
michael@0 | 22 | #include <errno.h> |
michael@0 | 23 | #include <fcntl.h> |
michael@0 | 24 | #include <stdio.h> |
michael@0 | 25 | #include <string.h> |
michael@0 | 26 | #include <sys/ipc.h> |
michael@0 | 27 | #include <sys/shm.h> |
michael@0 | 28 | |
michael@0 | 29 | // |
michael@0 | 30 | // This is a low-level wrapper around platform shared memory. Don't |
michael@0 | 31 | // use it directly; use Shmem allocated through IPDL interfaces. |
michael@0 | 32 | // |
michael@0 | 33 | |
michael@0 | 34 | namespace mozilla { |
michael@0 | 35 | namespace ipc { |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | class SharedMemorySysV : public SharedMemory |
michael@0 | 39 | { |
michael@0 | 40 | public: |
michael@0 | 41 | typedef int Handle; |
michael@0 | 42 | |
michael@0 | 43 | SharedMemorySysV() : |
michael@0 | 44 | mHandle(-1), |
michael@0 | 45 | mData(nullptr) |
michael@0 | 46 | { |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | SharedMemorySysV(Handle aHandle) : |
michael@0 | 50 | mHandle(aHandle), |
michael@0 | 51 | mData(nullptr) |
michael@0 | 52 | { |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | virtual ~SharedMemorySysV() |
michael@0 | 56 | { |
michael@0 | 57 | shmdt(mData); |
michael@0 | 58 | mHandle = -1; |
michael@0 | 59 | mData = nullptr; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | virtual bool Create(size_t aNbytes) MOZ_OVERRIDE |
michael@0 | 63 | { |
michael@0 | 64 | int id = shmget(IPC_PRIVATE, aNbytes, IPC_CREAT | 0600); |
michael@0 | 65 | if (id == -1) |
michael@0 | 66 | return false; |
michael@0 | 67 | |
michael@0 | 68 | mHandle = id; |
michael@0 | 69 | mAllocSize = aNbytes; |
michael@0 | 70 | Created(aNbytes); |
michael@0 | 71 | |
michael@0 | 72 | return Map(aNbytes); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | virtual bool Map(size_t nBytes) MOZ_OVERRIDE |
michael@0 | 76 | { |
michael@0 | 77 | // already mapped |
michael@0 | 78 | if (mData) |
michael@0 | 79 | return true; |
michael@0 | 80 | |
michael@0 | 81 | if (!IsHandleValid(mHandle)) |
michael@0 | 82 | return false; |
michael@0 | 83 | |
michael@0 | 84 | void* mem = shmat(mHandle, nullptr, 0); |
michael@0 | 85 | if (mem == (void*) -1) { |
michael@0 | 86 | char warning[256]; |
michael@0 | 87 | ::snprintf(warning, sizeof(warning)-1, |
michael@0 | 88 | "shmat(): %s (%d)\n", strerror(errno), errno); |
michael@0 | 89 | |
michael@0 | 90 | NS_WARNING(warning); |
michael@0 | 91 | |
michael@0 | 92 | return false; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // Mark the handle as deleted so that, should this process go away, the |
michael@0 | 96 | // segment is cleaned up. |
michael@0 | 97 | shmctl(mHandle, IPC_RMID, 0); |
michael@0 | 98 | |
michael@0 | 99 | mData = mem; |
michael@0 | 100 | |
michael@0 | 101 | #ifdef DEBUG |
michael@0 | 102 | struct shmid_ds info; |
michael@0 | 103 | if (shmctl(mHandle, IPC_STAT, &info) < 0) |
michael@0 | 104 | return false; |
michael@0 | 105 | |
michael@0 | 106 | NS_ABORT_IF_FALSE(nBytes <= info.shm_segsz, |
michael@0 | 107 | "Segment doesn't have enough space!"); |
michael@0 | 108 | #endif |
michael@0 | 109 | |
michael@0 | 110 | Mapped(nBytes); |
michael@0 | 111 | return true; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | virtual void* memory() const MOZ_OVERRIDE |
michael@0 | 115 | { |
michael@0 | 116 | return mData; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | virtual SharedMemoryType Type() const MOZ_OVERRIDE |
michael@0 | 120 | { |
michael@0 | 121 | return TYPE_SYSV; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | Handle GetHandle() const |
michael@0 | 125 | { |
michael@0 | 126 | NS_ABORT_IF_FALSE(IsHandleValid(mHandle), "invalid handle"); |
michael@0 | 127 | return mHandle; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | static Handle NULLHandle() |
michael@0 | 131 | { |
michael@0 | 132 | return -1; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | static bool IsHandleValid(Handle aHandle) |
michael@0 | 136 | { |
michael@0 | 137 | return aHandle != -1; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | private: |
michael@0 | 141 | Handle mHandle; |
michael@0 | 142 | void* mData; |
michael@0 | 143 | }; |
michael@0 | 144 | |
michael@0 | 145 | } // namespace ipc |
michael@0 | 146 | } // namespace mozilla |
michael@0 | 147 | |
michael@0 | 148 | #endif // OS_LINUX |
michael@0 | 149 | |
michael@0 | 150 | #endif // ifndef mozilla_ipc_SharedMemorySysV_h |