Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include <math.h> |
michael@0 | 8 | |
michael@0 | 9 | #include "nsString.h" |
michael@0 | 10 | #include "nsIMemoryReporter.h" |
michael@0 | 11 | #include "mozilla/ipc/SharedMemory.h" |
michael@0 | 12 | #include "mozilla/Atomics.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | namespace ipc { |
michael@0 | 16 | |
michael@0 | 17 | static Atomic<size_t> gShmemAllocated; |
michael@0 | 18 | static Atomic<size_t> gShmemMapped; |
michael@0 | 19 | |
michael@0 | 20 | class ShmemReporter MOZ_FINAL : public nsIMemoryReporter |
michael@0 | 21 | { |
michael@0 | 22 | public: |
michael@0 | 23 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 24 | |
michael@0 | 25 | NS_IMETHOD |
michael@0 | 26 | CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData) |
michael@0 | 27 | { |
michael@0 | 28 | nsresult rv; |
michael@0 | 29 | rv = MOZ_COLLECT_REPORT( |
michael@0 | 30 | "shmem-allocated", KIND_OTHER, UNITS_BYTES, gShmemAllocated, |
michael@0 | 31 | "Memory shared with other processes that is accessible (but not " |
michael@0 | 32 | "necessarily mapped)."); |
michael@0 | 33 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 34 | |
michael@0 | 35 | rv = MOZ_COLLECT_REPORT( |
michael@0 | 36 | "shmem-mapped", KIND_OTHER, UNITS_BYTES, gShmemMapped, |
michael@0 | 37 | "Memory shared with other processes that is mapped into the address " |
michael@0 | 38 | "space."); |
michael@0 | 39 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 40 | |
michael@0 | 41 | return NS_OK; |
michael@0 | 42 | } |
michael@0 | 43 | }; |
michael@0 | 44 | |
michael@0 | 45 | NS_IMPL_ISUPPORTS(ShmemReporter, nsIMemoryReporter) |
michael@0 | 46 | |
michael@0 | 47 | SharedMemory::SharedMemory() |
michael@0 | 48 | : mAllocSize(0) |
michael@0 | 49 | , mMappedSize(0) |
michael@0 | 50 | { |
michael@0 | 51 | MOZ_COUNT_CTOR(SharedMemory); |
michael@0 | 52 | static Atomic<bool> registered; |
michael@0 | 53 | if (registered.compareExchange(false, true)) { |
michael@0 | 54 | RegisterStrongMemoryReporter(new ShmemReporter()); |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | /*static*/ size_t |
michael@0 | 59 | SharedMemory::PageAlignedSize(size_t aSize) |
michael@0 | 60 | { |
michael@0 | 61 | size_t pageSize = SystemPageSize(); |
michael@0 | 62 | size_t nPagesNeeded = size_t(ceil(double(aSize) / double(pageSize))); |
michael@0 | 63 | return pageSize * nPagesNeeded; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | void |
michael@0 | 67 | SharedMemory::Created(size_t aNBytes) |
michael@0 | 68 | { |
michael@0 | 69 | mAllocSize = aNBytes; |
michael@0 | 70 | gShmemAllocated += mAllocSize; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | void |
michael@0 | 74 | SharedMemory::Mapped(size_t aNBytes) |
michael@0 | 75 | { |
michael@0 | 76 | mMappedSize = aNBytes; |
michael@0 | 77 | gShmemMapped += mMappedSize; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | void |
michael@0 | 81 | SharedMemory::Unmapped() |
michael@0 | 82 | { |
michael@0 | 83 | NS_ABORT_IF_FALSE(gShmemMapped >= mMappedSize, |
michael@0 | 84 | "Can't unmap more than mapped"); |
michael@0 | 85 | gShmemMapped -= mMappedSize; |
michael@0 | 86 | mMappedSize = 0; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | /*static*/ void |
michael@0 | 90 | SharedMemory::Destroyed() |
michael@0 | 91 | { |
michael@0 | 92 | NS_ABORT_IF_FALSE(gShmemAllocated >= mAllocSize, |
michael@0 | 93 | "Can't destroy more than allocated"); |
michael@0 | 94 | gShmemAllocated -= mAllocSize; |
michael@0 | 95 | mAllocSize = 0; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | } // namespace ipc |
michael@0 | 99 | } // namespace mozilla |