ipc/glue/SharedMemory.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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 MOZ_FINAL : public nsIMemoryReporter
    21 {
    22 public:
    23   NS_DECL_THREADSAFE_ISUPPORTS
    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);
    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);
    41     return NS_OK;
    42   }
    43 };
    45 NS_IMPL_ISUPPORTS(ShmemReporter, nsIMemoryReporter)
    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 }
    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 }
    66 void
    67 SharedMemory::Created(size_t aNBytes)
    68 {
    69   mAllocSize = aNBytes;
    70   gShmemAllocated += mAllocSize;
    71 }
    73 void
    74 SharedMemory::Mapped(size_t aNBytes)
    75 {
    76   mMappedSize = aNBytes;
    77   gShmemMapped += mMappedSize;
    78 }
    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 }
    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 }
    98 } // namespace ipc
    99 } // namespace mozilla

mercurial