/src/mozilla-central/ipc/glue/SharedMemory.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #include <math.h> |
8 | | |
9 | | #include "nsString.h" |
10 | | #include "nsIMemoryReporter.h" |
11 | | #include "mozilla/ipc/SharedMemory.h" |
12 | | #include "mozilla/Atomics.h" |
13 | | |
14 | | namespace mozilla { |
15 | | namespace ipc { |
16 | | |
17 | | static Atomic<size_t> gShmemAllocated; |
18 | | static Atomic<size_t> gShmemMapped; |
19 | | |
20 | | class ShmemReporter final : public nsIMemoryReporter |
21 | | { |
22 | 0 | ~ShmemReporter() {} |
23 | | |
24 | | public: |
25 | | NS_DECL_THREADSAFE_ISUPPORTS |
26 | | |
27 | | NS_IMETHOD |
28 | | CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData, |
29 | | bool aAnonymize) override |
30 | 0 | { |
31 | 0 | MOZ_COLLECT_REPORT( |
32 | 0 | "shmem-allocated", KIND_OTHER, UNITS_BYTES, gShmemAllocated, |
33 | 0 | "Memory shared with other processes that is accessible (but not " |
34 | 0 | "necessarily mapped)."); |
35 | 0 |
|
36 | 0 | MOZ_COLLECT_REPORT( |
37 | 0 | "shmem-mapped", KIND_OTHER, UNITS_BYTES, gShmemMapped, |
38 | 0 | "Memory shared with other processes that is mapped into the address " |
39 | 0 | "space."); |
40 | 0 |
|
41 | 0 | return NS_OK; |
42 | 0 | } |
43 | | }; |
44 | | |
45 | | NS_IMPL_ISUPPORTS(ShmemReporter, nsIMemoryReporter) |
46 | | |
47 | | SharedMemory::SharedMemory() |
48 | | : mAllocSize(0) |
49 | | , mMappedSize(0) |
50 | 0 | { |
51 | 0 | static Atomic<bool> registered; |
52 | 0 | if (registered.compareExchange(false, true)) { |
53 | 0 | RegisterStrongMemoryReporter(new ShmemReporter()); |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | /*static*/ size_t |
58 | | SharedMemory::PageAlignedSize(size_t aSize) |
59 | 0 | { |
60 | 0 | size_t pageSize = SystemPageSize(); |
61 | 0 | size_t nPagesNeeded = size_t(ceil(double(aSize) / double(pageSize))); |
62 | 0 | return pageSize * nPagesNeeded; |
63 | 0 | } |
64 | | |
65 | | void |
66 | | SharedMemory::Created(size_t aNBytes) |
67 | 0 | { |
68 | 0 | mAllocSize = aNBytes; |
69 | 0 | gShmemAllocated += mAllocSize; |
70 | 0 | } |
71 | | |
72 | | void |
73 | | SharedMemory::Mapped(size_t aNBytes) |
74 | 0 | { |
75 | 0 | mMappedSize = aNBytes; |
76 | 0 | gShmemMapped += mMappedSize; |
77 | 0 | } |
78 | | |
79 | | void |
80 | | SharedMemory::Unmapped() |
81 | 0 | { |
82 | 0 | MOZ_ASSERT(gShmemMapped >= mMappedSize, |
83 | 0 | "Can't unmap more than mapped"); |
84 | 0 | gShmemMapped -= mMappedSize; |
85 | 0 | mMappedSize = 0; |
86 | 0 | } |
87 | | |
88 | | /*static*/ void |
89 | | SharedMemory::Destroyed() |
90 | 0 | { |
91 | 0 | MOZ_ASSERT(gShmemAllocated >= mAllocSize, |
92 | 0 | "Can't destroy more than allocated"); |
93 | 0 | gShmemAllocated -= mAllocSize; |
94 | 0 | mAllocSize = 0; |
95 | 0 | } |
96 | | |
97 | | } // namespace ipc |
98 | | } // namespace mozilla |