1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/SharedMemory.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,99 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include <math.h> 1.11 + 1.12 +#include "nsString.h" 1.13 +#include "nsIMemoryReporter.h" 1.14 +#include "mozilla/ipc/SharedMemory.h" 1.15 +#include "mozilla/Atomics.h" 1.16 + 1.17 +namespace mozilla { 1.18 +namespace ipc { 1.19 + 1.20 +static Atomic<size_t> gShmemAllocated; 1.21 +static Atomic<size_t> gShmemMapped; 1.22 + 1.23 +class ShmemReporter MOZ_FINAL : public nsIMemoryReporter 1.24 +{ 1.25 +public: 1.26 + NS_DECL_THREADSAFE_ISUPPORTS 1.27 + 1.28 + NS_IMETHOD 1.29 + CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData) 1.30 + { 1.31 + nsresult rv; 1.32 + rv = MOZ_COLLECT_REPORT( 1.33 + "shmem-allocated", KIND_OTHER, UNITS_BYTES, gShmemAllocated, 1.34 + "Memory shared with other processes that is accessible (but not " 1.35 + "necessarily mapped)."); 1.36 + NS_ENSURE_SUCCESS(rv, rv); 1.37 + 1.38 + rv = MOZ_COLLECT_REPORT( 1.39 + "shmem-mapped", KIND_OTHER, UNITS_BYTES, gShmemMapped, 1.40 + "Memory shared with other processes that is mapped into the address " 1.41 + "space."); 1.42 + NS_ENSURE_SUCCESS(rv, rv); 1.43 + 1.44 + return NS_OK; 1.45 + } 1.46 +}; 1.47 + 1.48 +NS_IMPL_ISUPPORTS(ShmemReporter, nsIMemoryReporter) 1.49 + 1.50 +SharedMemory::SharedMemory() 1.51 + : mAllocSize(0) 1.52 + , mMappedSize(0) 1.53 +{ 1.54 + MOZ_COUNT_CTOR(SharedMemory); 1.55 + static Atomic<bool> registered; 1.56 + if (registered.compareExchange(false, true)) { 1.57 + RegisterStrongMemoryReporter(new ShmemReporter()); 1.58 + } 1.59 +} 1.60 + 1.61 +/*static*/ size_t 1.62 +SharedMemory::PageAlignedSize(size_t aSize) 1.63 +{ 1.64 + size_t pageSize = SystemPageSize(); 1.65 + size_t nPagesNeeded = size_t(ceil(double(aSize) / double(pageSize))); 1.66 + return pageSize * nPagesNeeded; 1.67 +} 1.68 + 1.69 +void 1.70 +SharedMemory::Created(size_t aNBytes) 1.71 +{ 1.72 + mAllocSize = aNBytes; 1.73 + gShmemAllocated += mAllocSize; 1.74 +} 1.75 + 1.76 +void 1.77 +SharedMemory::Mapped(size_t aNBytes) 1.78 +{ 1.79 + mMappedSize = aNBytes; 1.80 + gShmemMapped += mMappedSize; 1.81 +} 1.82 + 1.83 +void 1.84 +SharedMemory::Unmapped() 1.85 +{ 1.86 + NS_ABORT_IF_FALSE(gShmemMapped >= mMappedSize, 1.87 + "Can't unmap more than mapped"); 1.88 + gShmemMapped -= mMappedSize; 1.89 + mMappedSize = 0; 1.90 +} 1.91 + 1.92 +/*static*/ void 1.93 +SharedMemory::Destroyed() 1.94 +{ 1.95 + NS_ABORT_IF_FALSE(gShmemAllocated >= mAllocSize, 1.96 + "Can't destroy more than allocated"); 1.97 + gShmemAllocated -= mAllocSize; 1.98 + mAllocSize = 0; 1.99 +} 1.100 + 1.101 +} // namespace ipc 1.102 +} // namespace mozilla