2009-12-04 10:45:15 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=8 et :
|
|
|
|
*/
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-12-04 10:45:15 -08:00
|
|
|
|
|
|
|
#ifndef mozilla_ipc_SharedMemory_h
|
|
|
|
#define mozilla_ipc_SharedMemory_h
|
|
|
|
|
|
|
|
#include "nsDebug.h"
|
2010-05-22 12:35:32 -07:00
|
|
|
#include "nsISupportsImpl.h" // NS_INLINE_DECL_REFCOUNTING
|
2009-12-04 10:45:15 -08:00
|
|
|
|
|
|
|
//
|
|
|
|
// This is a low-level wrapper around platform shared memory. Don't
|
|
|
|
// use it directly; use Shmem allocated through IPDL interfaces.
|
|
|
|
//
|
|
|
|
namespace {
|
|
|
|
enum Rights {
|
|
|
|
RightsNone = 0,
|
|
|
|
RightsRead = 1 << 0,
|
|
|
|
RightsWrite = 1 << 1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2010-04-15 22:29:16 -07:00
|
|
|
class SharedMemory
|
2009-12-04 10:45:15 -08:00
|
|
|
{
|
|
|
|
public:
|
2010-04-15 22:29:16 -07:00
|
|
|
enum SharedMemoryType {
|
|
|
|
TYPE_BASIC,
|
|
|
|
TYPE_SYSV,
|
|
|
|
TYPE_UNKNOWN
|
|
|
|
};
|
2009-12-04 10:45:15 -08:00
|
|
|
|
2010-11-05 00:17:07 -07:00
|
|
|
virtual ~SharedMemory() { Unmapped(); Destroyed(); }
|
2009-12-04 10:45:15 -08:00
|
|
|
|
2010-11-05 00:17:07 -07:00
|
|
|
size_t Size() const { return mMappedSize; }
|
2009-12-04 10:45:15 -08:00
|
|
|
|
2010-04-15 22:29:16 -07:00
|
|
|
virtual void* memory() const = 0;
|
2009-12-04 10:45:15 -08:00
|
|
|
|
2010-04-15 22:29:16 -07:00
|
|
|
virtual bool Create(size_t size) = 0;
|
|
|
|
virtual bool Map(size_t nBytes) = 0;
|
|
|
|
|
|
|
|
virtual SharedMemoryType Type() const = 0;
|
2009-12-04 10:45:15 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
Protect(char* aAddr, size_t aSize, int aRights)
|
|
|
|
{
|
|
|
|
char* memStart = reinterpret_cast<char*>(memory());
|
|
|
|
if (!memStart)
|
|
|
|
NS_RUNTIMEABORT("SharedMemory region points at NULL!");
|
|
|
|
char* memEnd = memStart + Size();
|
|
|
|
|
|
|
|
char* protStart = aAddr;
|
|
|
|
if (!protStart)
|
|
|
|
NS_RUNTIMEABORT("trying to Protect() a NULL region!");
|
|
|
|
char* protEnd = protStart + aSize;
|
|
|
|
|
|
|
|
if (!(memStart <= protStart
|
|
|
|
&& protEnd <= memEnd))
|
|
|
|
NS_RUNTIMEABORT("attempt to Protect() a region outside this SharedMemory");
|
|
|
|
|
|
|
|
// checks alignment etc.
|
|
|
|
SystemProtect(aAddr, aSize, aRights);
|
|
|
|
}
|
|
|
|
|
2010-05-22 12:35:32 -07:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(SharedMemory)
|
|
|
|
|
2009-12-04 10:45:15 -08:00
|
|
|
static void SystemProtect(char* aAddr, size_t aSize, int aRights);
|
|
|
|
static size_t SystemPageSize();
|
2010-11-05 00:17:07 -07:00
|
|
|
static size_t PageAlignedSize(size_t aSize);
|
2010-11-05 00:17:07 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
SharedMemory();
|
|
|
|
|
|
|
|
// Implementations should call these methods on shmem usage changes,
|
|
|
|
// but *only if* the OS-specific calls are known to have succeeded.
|
|
|
|
// The methods are expected to be called in the pattern
|
|
|
|
//
|
|
|
|
// Created (Mapped Unmapped)* Destroy
|
|
|
|
//
|
|
|
|
// but this isn't checked.
|
2010-11-05 00:17:07 -07:00
|
|
|
void Created(size_t aNBytes);
|
|
|
|
void Mapped(size_t aNBytes);
|
|
|
|
void Unmapped();
|
|
|
|
void Destroyed();
|
|
|
|
|
|
|
|
// The size of the shmem region requested in Create(), if
|
2010-11-05 00:21:30 -07:00
|
|
|
// successful. SharedMemory instances that are opened from a
|
|
|
|
// foreign handle have an alloc size of 0, even though they have
|
|
|
|
// access to the alloc-size information.
|
2010-11-05 00:17:07 -07:00
|
|
|
size_t mAllocSize;
|
|
|
|
// The size of the region mapped in Map(), if successful. All
|
|
|
|
// SharedMemorys that are mapped have a non-zero mapped size.
|
|
|
|
size_t mMappedSize;
|
2009-12-04 10:45:15 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ifndef mozilla_ipc_SharedMemory_h
|