1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "nsIMemoryReporter.h"
11 #include "mozilla/ipc/SharedMemory.h"
12 #include "mozilla/Atomics.h"
17 static Atomic
<size_t> gShmemAllocated
;
18 static Atomic
<size_t> gShmemMapped
;
20 class ShmemReporter final
: public nsIMemoryReporter
{
21 ~ShmemReporter() = default;
24 NS_DECL_THREADSAFE_ISUPPORTS
27 CollectReports(nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
28 bool aAnonymize
) override
{
30 "shmem-allocated", KIND_OTHER
, UNITS_BYTES
, gShmemAllocated
,
31 "Memory shared with other processes that is accessible (but not "
32 "necessarily mapped).");
35 "shmem-mapped", KIND_OTHER
, UNITS_BYTES
, gShmemMapped
,
36 "Memory shared with other processes that is mapped into the address "
43 NS_IMPL_ISUPPORTS(ShmemReporter
, nsIMemoryReporter
)
45 SharedMemory::SharedMemory() : mAllocSize(0), mMappedSize(0) {
46 static Atomic
<bool> registered
;
47 if (registered
.compareExchange(false, true)) {
48 RegisterStrongMemoryReporter(new ShmemReporter());
53 size_t SharedMemory::PageAlignedSize(size_t aSize
) {
54 size_t pageSize
= SystemPageSize();
55 size_t nPagesNeeded
= size_t(ceil(double(aSize
) / double(pageSize
)));
56 return pageSize
* nPagesNeeded
;
59 void SharedMemory::Created(size_t aNBytes
) {
61 gShmemAllocated
+= mAllocSize
;
64 void SharedMemory::Mapped(size_t aNBytes
) {
65 mMappedSize
= aNBytes
;
66 gShmemMapped
+= mMappedSize
;
69 void SharedMemory::Unmapped() {
70 MOZ_ASSERT(gShmemMapped
>= mMappedSize
, "Can't unmap more than mapped");
71 gShmemMapped
-= mMappedSize
;
76 void SharedMemory::Destroyed() {
77 MOZ_ASSERT(gShmemAllocated
>= mAllocSize
,
78 "Can't destroy more than allocated");
79 gShmemAllocated
-= mAllocSize
;
84 } // namespace mozilla