1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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
16 #define MOZ_HAVE_SHAREDMEMORYSYSV
18 #include "SharedMemory.h"
30 // This is a low-level wrapper around platform shared memory. Don't
31 // use it directly; use Shmem allocated through IPDL interfaces.
38 class SharedMemorySysV
: public SharedMemory
49 SharedMemorySysV(Handle aHandle
) :
55 virtual ~SharedMemorySysV()
62 virtual bool Create(size_t aNbytes
) MOZ_OVERRIDE
64 int id
= shmget(IPC_PRIVATE
, aNbytes
, IPC_CREAT
| 0600);
75 virtual bool Map(size_t nBytes
) MOZ_OVERRIDE
81 if (!IsHandleValid(mHandle
))
84 void* mem
= shmat(mHandle
, nullptr, 0);
85 if (mem
== (void*) -1) {
87 ::snprintf(warning
, sizeof(warning
)-1,
88 "shmat(): %s (%d)\n", strerror(errno
), errno
);
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);
102 struct shmid_ds info
;
103 if (shmctl(mHandle
, IPC_STAT
, &info
) < 0)
106 NS_ABORT_IF_FALSE(nBytes
<= info
.shm_segsz
,
107 "Segment doesn't have enough space!");
114 virtual void* memory() const MOZ_OVERRIDE
119 virtual SharedMemoryType
Type() const MOZ_OVERRIDE
124 Handle
GetHandle() const
126 NS_ABORT_IF_FALSE(IsHandleValid(mHandle
), "invalid handle");
130 static Handle
NULLHandle()
135 static bool IsHandleValid(Handle aHandle
)
137 return aHandle
!= -1;
146 } // namespace mozilla
150 #endif // ifndef mozilla_ipc_SharedMemorySysV_h