1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=8 et :
3 : */
4 : /* ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at:
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is Mozilla Code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * The Mozilla Foundation
21 : * Portions created by the Initial Developer are Copyright (C) 2010
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Chris Jones <jones.chris.g@gmail.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include <math.h>
42 :
43 : #include "nsString.h"
44 : #include "nsIMemoryReporter.h"
45 : #include "mozilla/ipc/SharedMemory.h"
46 :
47 : namespace mozilla {
48 : namespace ipc {
49 :
50 : static PRInt64 gShmemAllocated;
51 : static PRInt64 gShmemMapped;
52 0 : static PRInt64 GetShmemAllocated() { return gShmemAllocated; }
53 0 : static PRInt64 GetShmemMapped() { return gShmemMapped; }
54 :
55 0 : NS_MEMORY_REPORTER_IMPLEMENT(ShmemAllocated,
56 : "shmem-allocated",
57 : KIND_OTHER,
58 : UNITS_BYTES,
59 : GetShmemAllocated,
60 : "Memory shared with other processes that is accessible (but not "
61 0 : "necessarily mapped).")
62 0 : NS_MEMORY_REPORTER_IMPLEMENT(ShmemMapped,
63 : "shmem-mapped",
64 : KIND_OTHER,
65 : UNITS_BYTES,
66 : GetShmemMapped,
67 0 : "Memory shared with other processes that is mapped into the address space.")
68 :
69 0 : SharedMemory::SharedMemory()
70 : : mAllocSize(0)
71 0 : , mMappedSize(0)
72 : {
73 : // NB: SharedMemory is main-thread-only at the moment, but that may
74 : // change soon
75 : static bool registered;
76 0 : if (!registered) {
77 0 : NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(ShmemAllocated));
78 0 : NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(ShmemMapped));
79 0 : registered = true;
80 : }
81 0 : }
82 :
83 : /*static*/ size_t
84 0 : SharedMemory::PageAlignedSize(size_t aSize)
85 : {
86 0 : size_t pageSize = SystemPageSize();
87 0 : size_t nPagesNeeded = size_t(ceil(double(aSize) / double(pageSize)));
88 0 : return pageSize * nPagesNeeded;
89 : }
90 :
91 : void
92 0 : SharedMemory::Created(size_t aNBytes)
93 : {
94 0 : mAllocSize = aNBytes;
95 0 : gShmemAllocated += mAllocSize;
96 0 : }
97 :
98 : void
99 0 : SharedMemory::Mapped(size_t aNBytes)
100 : {
101 0 : mMappedSize = aNBytes;
102 0 : gShmemMapped += mMappedSize;
103 0 : }
104 :
105 : void
106 0 : SharedMemory::Unmapped()
107 : {
108 0 : NS_ABORT_IF_FALSE(gShmemMapped >= PRInt64(mMappedSize),
109 : "Can't unmap more than mapped");
110 0 : gShmemMapped -= mMappedSize;
111 0 : mMappedSize = 0;
112 0 : }
113 :
114 : /*static*/ void
115 0 : SharedMemory::Destroyed()
116 : {
117 0 : NS_ABORT_IF_FALSE(gShmemAllocated >= PRInt64(mAllocSize),
118 : "Can't destroy more than allocated");
119 0 : gShmemAllocated -= mAllocSize;
120 0 : mAllocSize = 0;
121 0 : }
122 :
123 : } // namespace ipc
124 : } // namespace mozilla
|