michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "nsString.h" michael@0: #include "nsIMemoryReporter.h" michael@0: #include "mozilla/ipc/SharedMemory.h" michael@0: #include "mozilla/Atomics.h" michael@0: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: michael@0: static Atomic gShmemAllocated; michael@0: static Atomic gShmemMapped; michael@0: michael@0: class ShmemReporter MOZ_FINAL : public nsIMemoryReporter michael@0: { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: NS_IMETHOD michael@0: CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData) michael@0: { michael@0: nsresult rv; michael@0: rv = MOZ_COLLECT_REPORT( michael@0: "shmem-allocated", KIND_OTHER, UNITS_BYTES, gShmemAllocated, michael@0: "Memory shared with other processes that is accessible (but not " michael@0: "necessarily mapped)."); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = MOZ_COLLECT_REPORT( michael@0: "shmem-mapped", KIND_OTHER, UNITS_BYTES, gShmemMapped, michael@0: "Memory shared with other processes that is mapped into the address " michael@0: "space."); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(ShmemReporter, nsIMemoryReporter) michael@0: michael@0: SharedMemory::SharedMemory() michael@0: : mAllocSize(0) michael@0: , mMappedSize(0) michael@0: { michael@0: MOZ_COUNT_CTOR(SharedMemory); michael@0: static Atomic registered; michael@0: if (registered.compareExchange(false, true)) { michael@0: RegisterStrongMemoryReporter(new ShmemReporter()); michael@0: } michael@0: } michael@0: michael@0: /*static*/ size_t michael@0: SharedMemory::PageAlignedSize(size_t aSize) michael@0: { michael@0: size_t pageSize = SystemPageSize(); michael@0: size_t nPagesNeeded = size_t(ceil(double(aSize) / double(pageSize))); michael@0: return pageSize * nPagesNeeded; michael@0: } michael@0: michael@0: void michael@0: SharedMemory::Created(size_t aNBytes) michael@0: { michael@0: mAllocSize = aNBytes; michael@0: gShmemAllocated += mAllocSize; michael@0: } michael@0: michael@0: void michael@0: SharedMemory::Mapped(size_t aNBytes) michael@0: { michael@0: mMappedSize = aNBytes; michael@0: gShmemMapped += mMappedSize; michael@0: } michael@0: michael@0: void michael@0: SharedMemory::Unmapped() michael@0: { michael@0: NS_ABORT_IF_FALSE(gShmemMapped >= mMappedSize, michael@0: "Can't unmap more than mapped"); michael@0: gShmemMapped -= mMappedSize; michael@0: mMappedSize = 0; michael@0: } michael@0: michael@0: /*static*/ void michael@0: SharedMemory::Destroyed() michael@0: { michael@0: NS_ABORT_IF_FALSE(gShmemAllocated >= mAllocSize, michael@0: "Can't destroy more than allocated"); michael@0: gShmemAllocated -= mAllocSize; michael@0: mAllocSize = 0; michael@0: } michael@0: michael@0: } // namespace ipc michael@0: } // namespace mozilla