Backed out 2 changesets (bug 1121297) for Cpp bustage on a CLOSED TREE

Backed out changeset 28960bb167ef (bug 1121297)
Backed out changeset 005cf05954e7 (bug 1121297)

--HG--
rename : memory/volatile/VolatileBuffer.h => memory/mozalloc/VolatileBuffer.h
rename : memory/volatile/VolatileBufferAshmem.cpp => memory/mozalloc/VolatileBufferAshmem.cpp
rename : memory/volatile/VolatileBufferFallback.cpp => memory/mozalloc/VolatileBufferFallback.cpp
rename : memory/volatile/VolatileBufferOSX.cpp => memory/mozalloc/VolatileBufferOSX.cpp
rename : memory/volatile/VolatileBufferWindows.cpp => memory/mozalloc/VolatileBufferWindows.cpp
rename : memory/volatile/tests/TestVolatileBuffer.cpp => memory/mozalloc/tests/TestVolatileBuffer.cpp
rename : memory/volatile/tests/moz.build => memory/mozalloc/tests/moz.build
This commit is contained in:
Wes Kocher 2015-01-16 15:21:25 -08:00
parent cb8c3ea750
commit 5f582f7ec0
11 changed files with 180 additions and 195 deletions

View File

@ -6,7 +6,6 @@
#define mozalloc_VolatileBuffer_h
#include "mozilla/mozalloc.h"
#include "mozilla/Mutex.h"
#include "mozilla/RefPtr.h"
#include "mozilla/MemoryReporting.h"
@ -37,19 +36,19 @@
* When a buffer is purged, some or all of the buffer is zeroed out. This
* API cannot tell which parts of the buffer were lost.
*
* VolatileBuffer and VolatileBufferPtr are threadsafe.
* VolatileBuffer is not thread safe. Do not use VolatileBufferPtrs on
* different threads.
*/
namespace mozilla {
class VolatileBuffer
class MOZALLOC_EXPORT VolatileBuffer : public RefCounted<VolatileBuffer>
{
friend class VolatileBufferPtr_base;
public:
MOZ_DECLARE_REFCOUNTED_TYPENAME(VolatileBuffer)
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VolatileBuffer)
VolatileBuffer();
~VolatileBuffer();
/* aAlignment must be a multiple of the pointer size */
bool Init(size_t aSize, size_t aAlignment = sizeof(void*));
@ -63,15 +62,6 @@ protected:
void Unlock();
private:
~VolatileBuffer();
/**
* Protects mLockCount, mFirstLock, and changes to the volatility of our
* buffer. Other member variables are read-only except in Init() and the
* destructor.
*/
Mutex mMutex;
void* mBuf;
size_t mSize;
int mLockCount;

View File

@ -22,8 +22,7 @@ extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size);
namespace mozilla {
VolatileBuffer::VolatileBuffer()
: mMutex("VolatileBuffer")
, mBuf(nullptr)
: mBuf(nullptr)
, mSize(0)
, mLockCount(0)
, mFd(-1)
@ -73,8 +72,6 @@ heap_alloc:
VolatileBuffer::~VolatileBuffer()
{
MOZ_ASSERT(mLockCount == 0, "Being destroyed with non-zero lock count?");
if (OnHeap()) {
free(mBuf);
} else {
@ -86,8 +83,6 @@ VolatileBuffer::~VolatileBuffer()
bool
VolatileBuffer::Lock(void** aBuf)
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer");
*aBuf = mBuf;
@ -103,8 +98,6 @@ VolatileBuffer::Lock(void** aBuf)
void
VolatileBuffer::Unlock()
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mLockCount > 0, "VolatileBuffer unlocked too many times!");
if (--mLockCount || OnHeap()) {
return;

View File

@ -13,8 +13,7 @@ int posix_memalign(void** memptr, size_t alignment, size_t size);
namespace mozilla {
VolatileBuffer::VolatileBuffer()
: mMutex("VolatileBuffer")
, mBuf(nullptr)
: mBuf(nullptr)
, mSize(0)
, mLockCount(0)
{
@ -28,13 +27,9 @@ bool VolatileBuffer::Init(size_t aSize, size_t aAlignment)
mSize = aSize;
#if defined(MOZ_MEMORY)
if (posix_memalign(&mBuf, aAlignment, aSize) != 0) {
return false;
}
posix_memalign(&mBuf, aAlignment, aSize);
#elif defined(HAVE_POSIX_MEMALIGN)
if (moz_posix_memalign(&mBuf, aAlignment, aSize) != 0) {
return false;
}
(void)moz_posix_memalign(&mBuf, aAlignment, aSize);
#else
#error "No memalign implementation found"
#endif
@ -43,16 +38,12 @@ bool VolatileBuffer::Init(size_t aSize, size_t aAlignment)
VolatileBuffer::~VolatileBuffer()
{
MOZ_ASSERT(mLockCount == 0, "Being destroyed with non-zero lock count?");
free(mBuf);
}
bool
VolatileBuffer::Lock(void** aBuf)
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer");
*aBuf = mBuf;
@ -64,8 +55,6 @@ VolatileBuffer::Lock(void** aBuf)
void
VolatileBuffer::Unlock()
{
MutexAutoLock lock(mMutex);
mLockCount--;
MOZ_ASSERT(mLockCount >= 0, "VolatileBuffer unlocked too many times!");
}

View File

@ -16,8 +16,7 @@
namespace mozilla {
VolatileBuffer::VolatileBuffer()
: mMutex("VolatileBuffer")
, mBuf(nullptr)
: mBuf(nullptr)
, mSize(0)
, mLockCount(0)
, mHeap(false)
@ -54,8 +53,6 @@ heap_alloc:
VolatileBuffer::~VolatileBuffer()
{
MOZ_ASSERT(mLockCount == 0, "Being destroyed with non-zero lock count?");
if (OnHeap()) {
free(mBuf);
} else {
@ -66,8 +63,6 @@ VolatileBuffer::~VolatileBuffer()
bool
VolatileBuffer::Lock(void** aBuf)
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer");
*aBuf = mBuf;
@ -87,8 +82,6 @@ VolatileBuffer::Lock(void** aBuf)
void
VolatileBuffer::Unlock()
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mLockCount > 0, "VolatileBuffer unlocked too many times!");
if (--mLockCount || OnHeap()) {
return;

View File

@ -2,6 +2,10 @@
* 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/. */
#if defined(XP_WIN)
# define MOZALLOC_EXPORT __declspec(dllexport)
#endif
#include "VolatileBuffer.h"
#include "mozilla/Assertions.h"
#include "mozilla/mozalloc.h"
@ -22,8 +26,7 @@ extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size);
namespace mozilla {
VolatileBuffer::VolatileBuffer()
: mMutex("VolatileBuffer")
, mBuf(nullptr)
: mBuf(nullptr)
, mSize(0)
, mLockCount(0)
, mHeap(false)
@ -69,8 +72,6 @@ heap_alloc:
VolatileBuffer::~VolatileBuffer()
{
MOZ_ASSERT(mLockCount == 0, "Being destroyed with non-zero lock count?");
if (OnHeap()) {
#ifdef MOZ_MEMORY
free(mBuf);
@ -85,8 +86,6 @@ VolatileBuffer::~VolatileBuffer()
bool
VolatileBuffer::Lock(void** aBuf)
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer");
*aBuf = mBuf;
@ -112,8 +111,6 @@ VolatileBuffer::Lock(void** aBuf)
void
VolatileBuffer::Unlock()
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mLockCount > 0, "VolatileBuffer unlocked too many times!");
if (--mLockCount || OnHeap()) {
return;

View File

@ -10,6 +10,7 @@ EXPORTS.mozilla += [
'mozalloc.h',
'mozalloc_abort.h',
'mozalloc_oom.h',
'VolatileBuffer.h',
]
if CONFIG['MOZ_MSVC_STL_WRAP__RAISE'] or CONFIG['MOZ_MSVC_STL_WRAP__Throw']:
@ -39,6 +40,23 @@ UNIFIED_SOURCES += [
'mozalloc_oom.cpp',
]
if CONFIG['OS_TARGET'] == 'Android':
UNIFIED_SOURCES += [
'VolatileBufferAshmem.cpp',
]
elif CONFIG['OS_TARGET'] == 'Darwin':
UNIFIED_SOURCES += [
'VolatileBufferOSX.cpp',
]
elif CONFIG['OS_TARGET'] == 'WINNT':
UNIFIED_SOURCES += [
'VolatileBufferWindows.cpp',
]
else:
UNIFIED_SOURCES += [
'VolatileBufferFallback.cpp',
]
if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
Library('mozalloc')
else:
@ -48,6 +66,8 @@ else:
# The strndup declaration in string.h is in an ifdef __USE_GNU section
DEFINES['_GNU_SOURCE'] = True
TEST_DIRS += ['tests']
GENERATED_INCLUDES += ['/xpcom']
DISABLE_STL_WRAPPING = True

View File

@ -0,0 +1,143 @@
/* 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/. */
#include "TestHarness.h"
#include "mozilla/VolatileBuffer.h"
#include <string.h>
#if defined(ANDROID)
#include <fcntl.h>
#include <linux/ashmem.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#elif defined(XP_DARWIN)
#include <mach/mach.h>
#endif
using namespace mozilla;
int main(int argc, char **argv)
{
ScopedXPCOM xpcom("VolatileBufferTests");
if (xpcom.failed()) {
return 1;
}
RefPtr<VolatileBuffer> heapbuf = new VolatileBuffer();
if (!heapbuf || !heapbuf->Init(512)) {
fail("Failed to initialize VolatileBuffer");
return 1;
}
{
VolatileBufferPtr<char> ptr(heapbuf);
if (ptr.WasBufferPurged()) {
fail("Buffer was immediately purged after initialization");
return 1;
}
if (!ptr) {
fail("Didn't get a pointer");
return 1;
}
}
RefPtr<VolatileBuffer> buf = new VolatileBuffer();
if (!buf || !buf->Init(16384)) {
fail("Failed to initialize VolatileBuffer");
return 1;
}
const char teststr[] = "foobar";
{
VolatileBufferPtr<char> ptr(buf);
if (ptr.WasBufferPurged()) {
fail("Buffer should not be purged immediately after initialization");
return 1;
}
if (!ptr) {
fail("Didn't get a pointer");
return 1;
}
{
VolatileBufferPtr<char> ptr2(buf);
if (ptr2.WasBufferPurged()) {
fail("Failed to Lock buffer again while currently locked");
return 1;
}
if (!ptr2) {
fail("Didn't get a pointer on the second lock");
return 1;
}
strcpy(ptr2, teststr);
}
}
{
VolatileBufferPtr<char> ptr(buf);
if (ptr.WasBufferPurged()) {
fail("Buffer was immediately purged after unlock");
return 1;
}
if (strcmp(ptr, teststr)) {
fail("Buffer failed to retain data after unlock");
return 1;
}
}
// Test purging if we know how to
#if defined(MOZ_WIDGET_GONK)
// This also works on Android, but we need root.
int fd = open("/" ASHMEM_NAME_DEF, O_RDWR);
if (fd < 0) {
fail("Failed to open ashmem device");
return 1;
}
if (ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL) < 0) {
fail("Failed to purge ashmem caches");
return 1;
}
#elif defined(XP_DARWIN)
int state;
vm_purgable_control(mach_task_self(), (vm_address_t)NULL,
VM_PURGABLE_PURGE_ALL, &state);
#else
return 0;
#endif
if (!buf->NonHeapSizeOfExcludingThis()) {
fail("Buffer should not be allocated on heap");
return 1;
}
{
VolatileBufferPtr<char> ptr(buf);
if (!ptr.WasBufferPurged()) {
fail("Buffer should not be unpurged after forced purge");
return 1;
}
if (!strcmp(ptr, teststr)) {
fail("Purge did not actually purge data");
return 1;
}
}
{
VolatileBufferPtr<char> ptr(buf);
if (ptr.WasBufferPurged()) {
fail("Buffer still purged after lock");
return 1;
}
}
return 0;
}

View File

@ -4,10 +4,6 @@
# 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/.
UNIFIED_SOURCES = [
'TestVolatileBuffer.cpp',
]
FINAL_LIBRARY = 'xul-gtest'
FAIL_ON_WARNINGS = True
GeckoCppUnitTests([
'TestVolatileBuffer',
])

View File

@ -1,33 +0,0 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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/.
NO_VISIBILITY_FLAGS = True
EXPORTS.mozilla += [
'VolatileBuffer.h',
]
if CONFIG['OS_TARGET'] == 'Android':
UNIFIED_SOURCES += [
'VolatileBufferAshmem.cpp',
]
elif CONFIG['OS_TARGET'] == 'Darwin':
UNIFIED_SOURCES += [
'VolatileBufferOSX.cpp',
]
elif CONFIG['OS_TARGET'] == 'WINNT':
UNIFIED_SOURCES += [
'VolatileBufferWindows.cpp',
]
else:
UNIFIED_SOURCES += [
'VolatileBufferFallback.cpp',
]
FINAL_LIBRARY = 'xul'
TEST_DIRS += ['tests']
FAIL_ON_WARNINGS = True

View File

@ -1,102 +0,0 @@
/* 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/. */
#include "gtest/gtest.h"
#include "mozilla/VolatileBuffer.h"
#include <string.h>
#if defined(ANDROID)
#include <fcntl.h>
#include <linux/ashmem.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#elif defined(XP_DARWIN)
#include <mach/mach.h>
#endif
using namespace mozilla;
TEST(VolatileBufferTest, HeapVolatileBuffersWork)
{
RefPtr<VolatileBuffer> heapbuf = new VolatileBuffer();
ASSERT_TRUE(heapbuf) << "Failed to create VolatileBuffer";
ASSERT_TRUE(heapbuf->Init(512)) << "Failed to initialize VolatileBuffer";
VolatileBufferPtr<char> ptr(heapbuf);
EXPECT_FALSE(ptr.WasBufferPurged())
<< "Buffer should not be purged immediately after initialization";
EXPECT_TRUE(ptr) << "Couldn't get pointer from VolatileBufferPtr";
}
TEST(VolatileBufferTest, RealVolatileBuffersWork)
{
RefPtr<VolatileBuffer> buf = new VolatileBuffer();
ASSERT_TRUE(buf) << "Failed to create VolatileBuffer";
ASSERT_TRUE(buf->Init(16384)) << "Failed to initialize VolatileBuffer";
const char teststr[] = "foobar";
{
VolatileBufferPtr<char> ptr(buf);
EXPECT_FALSE(ptr.WasBufferPurged())
<< "Buffer should not be purged immediately after initialization";
EXPECT_TRUE(ptr) << "Couldn't get pointer from VolatileBufferPtr";
{
VolatileBufferPtr<char> ptr2(buf);
EXPECT_FALSE(ptr.WasBufferPurged())
<< "Failed to lock buffer again while currently locked";
ASSERT_TRUE(ptr2) << "Didn't get a pointer on the second lock";
strcpy(ptr2, teststr);
}
}
{
VolatileBufferPtr<char> ptr(buf);
EXPECT_FALSE(ptr.WasBufferPurged())
<< "Buffer was immediately purged after unlock";
EXPECT_STREQ(ptr, teststr) << "Buffer failed to retain data after unlock";
}
// Test purging if we know how to
#if defined(MOZ_WIDGET_GONK)
// This also works on Android, but we need root.
int fd = open("/" ASHMEM_NAME_DEF, O_RDWR);
ASSERT_GE(fd, 0) << "Failed to open ashmem device";
ASSERT_GE(ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL), 0)
<< "Failed to purge ashmem caches";
#elif defined(XP_DARWIN)
int state;
vm_purgable_control(mach_task_self(), (vm_address_t)NULL,
VM_PURGABLE_PURGE_ALL, &state);
#else
return;
#endif
EXPECT_GT(buf->NonHeapSizeOfExcludingThis(), 0ul)
<< "Buffer should not be allocated on heap";
{
VolatileBufferPtr<char> ptr(buf);
EXPECT_TRUE(ptr.WasBufferPurged())
<< "Buffer should not be unpurged after forced purge";
EXPECT_STRNE(ptr, teststr) << "Purge did not actually purge data";
}
{
VolatileBufferPtr<char> ptr(buf);
EXPECT_FALSE(ptr.WasBufferPurged()) << "Buffer still purged after lock";
}
}

View File

@ -44,7 +44,6 @@ if not CONFIG['LIBXUL_SDK']:
DIRS += [
'mozglue',
'memory/mozalloc',
'memory/volatile',
]
if not CONFIG['JS_STANDALONE']: