Bumping manifests a=b2g-bump
[gecko.git] / ipc / glue / SharedMemorySysV.h
blob695425e35d90792ff079f812ccac7e96a776a51a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef mozilla_ipc_SharedMemorySysV_h
9 #define mozilla_ipc_SharedMemorySysV_h
11 #if (defined(OS_LINUX) && !defined(ANDROID)) || defined(OS_BSD)
13 // SysV shared memory isn't available on Windows, but we define the
14 // following macro so that #ifdefs are clearer (compared to #ifdef
15 // OS_LINUX).
16 #define MOZ_HAVE_SHAREDMEMORYSYSV
18 #include "SharedMemory.h"
20 #include "nsDebug.h"
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/ipc.h>
27 #include <sys/shm.h>
30 // This is a low-level wrapper around platform shared memory. Don't
31 // use it directly; use Shmem allocated through IPDL interfaces.
34 namespace mozilla {
35 namespace ipc {
38 class SharedMemorySysV : public SharedMemory
40 public:
41 typedef int Handle;
43 SharedMemorySysV() :
44 mHandle(-1),
45 mData(nullptr)
49 SharedMemorySysV(Handle aHandle) :
50 mHandle(aHandle),
51 mData(nullptr)
55 virtual ~SharedMemorySysV()
57 shmdt(mData);
58 mHandle = -1;
59 mData = nullptr;
62 virtual bool Create(size_t aNbytes) MOZ_OVERRIDE
64 int id = shmget(IPC_PRIVATE, aNbytes, IPC_CREAT | 0600);
65 if (id == -1)
66 return false;
68 mHandle = id;
69 mAllocSize = aNbytes;
70 Created(aNbytes);
72 return Map(aNbytes);
75 virtual bool Map(size_t nBytes) MOZ_OVERRIDE
77 // already mapped
78 if (mData)
79 return true;
81 if (!IsHandleValid(mHandle))
82 return false;
84 void* mem = shmat(mHandle, nullptr, 0);
85 if (mem == (void*) -1) {
86 char warning[256];
87 ::snprintf(warning, sizeof(warning)-1,
88 "shmat(): %s (%d)\n", strerror(errno), errno);
90 NS_WARNING(warning);
92 return false;
95 // Mark the handle as deleted so that, should this process go away, the
96 // segment is cleaned up.
97 shmctl(mHandle, IPC_RMID, 0);
99 mData = mem;
101 #ifdef DEBUG
102 struct shmid_ds info;
103 if (shmctl(mHandle, IPC_STAT, &info) < 0)
104 return false;
106 NS_ABORT_IF_FALSE(nBytes <= info.shm_segsz,
107 "Segment doesn't have enough space!");
108 #endif
110 Mapped(nBytes);
111 return true;
114 virtual void* memory() const MOZ_OVERRIDE
116 return mData;
119 virtual SharedMemoryType Type() const MOZ_OVERRIDE
121 return TYPE_SYSV;
124 Handle GetHandle() const
126 NS_ABORT_IF_FALSE(IsHandleValid(mHandle), "invalid handle");
127 return mHandle;
130 static Handle NULLHandle()
132 return -1;
135 static bool IsHandleValid(Handle aHandle)
137 return aHandle != -1;
140 private:
141 Handle mHandle;
142 void* mData;
145 } // namespace ipc
146 } // namespace mozilla
148 #endif // OS_LINUX
150 #endif // ifndef mozilla_ipc_SharedMemorySysV_h