Bug 1858509 add thread-safety annotations around MediaSourceDemuxer::mMonitor r=alwu
[gecko.git] / ipc / glue / SharedMemory.cpp
blob47617466580b38ae9a891efb789edaa27770f684
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/. */
7 #include <math.h>
9 #include "nsString.h"
10 #include "nsIMemoryReporter.h"
11 #include "mozilla/ipc/SharedMemory.h"
12 #include "mozilla/Atomics.h"
14 namespace mozilla {
15 namespace ipc {
17 static Atomic<size_t> gShmemAllocated;
18 static Atomic<size_t> gShmemMapped;
20 class ShmemReporter final : public nsIMemoryReporter {
21 ~ShmemReporter() = default;
23 public:
24 NS_DECL_THREADSAFE_ISUPPORTS
26 NS_IMETHOD
27 CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
28 bool aAnonymize) override {
29 MOZ_COLLECT_REPORT(
30 "shmem-allocated", KIND_OTHER, UNITS_BYTES, gShmemAllocated,
31 "Memory shared with other processes that is accessible (but not "
32 "necessarily mapped).");
34 MOZ_COLLECT_REPORT(
35 "shmem-mapped", KIND_OTHER, UNITS_BYTES, gShmemMapped,
36 "Memory shared with other processes that is mapped into the address "
37 "space.");
39 return NS_OK;
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());
52 /*static*/
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) {
60 mAllocSize = 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;
72 mMappedSize = 0;
75 /*static*/
76 void SharedMemory::Destroyed() {
77 MOZ_ASSERT(gShmemAllocated >= mAllocSize,
78 "Can't destroy more than allocated");
79 gShmemAllocated -= mAllocSize;
80 mAllocSize = 0;
83 } // namespace ipc
84 } // namespace mozilla