|
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 MOZ_FINAL : public nsIMemoryReporter |
|
21 { |
|
22 public: |
|
23 NS_DECL_THREADSAFE_ISUPPORTS |
|
24 |
|
25 NS_IMETHOD |
|
26 CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData) |
|
27 { |
|
28 nsresult rv; |
|
29 rv = 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)."); |
|
33 NS_ENSURE_SUCCESS(rv, rv); |
|
34 |
|
35 rv = MOZ_COLLECT_REPORT( |
|
36 "shmem-mapped", KIND_OTHER, UNITS_BYTES, gShmemMapped, |
|
37 "Memory shared with other processes that is mapped into the address " |
|
38 "space."); |
|
39 NS_ENSURE_SUCCESS(rv, rv); |
|
40 |
|
41 return NS_OK; |
|
42 } |
|
43 }; |
|
44 |
|
45 NS_IMPL_ISUPPORTS(ShmemReporter, nsIMemoryReporter) |
|
46 |
|
47 SharedMemory::SharedMemory() |
|
48 : mAllocSize(0) |
|
49 , mMappedSize(0) |
|
50 { |
|
51 MOZ_COUNT_CTOR(SharedMemory); |
|
52 static Atomic<bool> registered; |
|
53 if (registered.compareExchange(false, true)) { |
|
54 RegisterStrongMemoryReporter(new ShmemReporter()); |
|
55 } |
|
56 } |
|
57 |
|
58 /*static*/ size_t |
|
59 SharedMemory::PageAlignedSize(size_t aSize) |
|
60 { |
|
61 size_t pageSize = SystemPageSize(); |
|
62 size_t nPagesNeeded = size_t(ceil(double(aSize) / double(pageSize))); |
|
63 return pageSize * nPagesNeeded; |
|
64 } |
|
65 |
|
66 void |
|
67 SharedMemory::Created(size_t aNBytes) |
|
68 { |
|
69 mAllocSize = aNBytes; |
|
70 gShmemAllocated += mAllocSize; |
|
71 } |
|
72 |
|
73 void |
|
74 SharedMemory::Mapped(size_t aNBytes) |
|
75 { |
|
76 mMappedSize = aNBytes; |
|
77 gShmemMapped += mMappedSize; |
|
78 } |
|
79 |
|
80 void |
|
81 SharedMemory::Unmapped() |
|
82 { |
|
83 NS_ABORT_IF_FALSE(gShmemMapped >= mMappedSize, |
|
84 "Can't unmap more than mapped"); |
|
85 gShmemMapped -= mMappedSize; |
|
86 mMappedSize = 0; |
|
87 } |
|
88 |
|
89 /*static*/ void |
|
90 SharedMemory::Destroyed() |
|
91 { |
|
92 NS_ABORT_IF_FALSE(gShmemAllocated >= mAllocSize, |
|
93 "Can't destroy more than allocated"); |
|
94 gShmemAllocated -= mAllocSize; |
|
95 mAllocSize = 0; |
|
96 } |
|
97 |
|
98 } // namespace ipc |
|
99 } // namespace mozilla |