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 // Some Linux kernels -- specifically, newer versions of Android and
8 // some B2G devices -- have a feature for assigning names to ranges of
9 // anonymous memory (i.e., memory that doesn't have a "name" in the
10 // form of an underlying mapped file). These names are reported in
11 // /proc/<pid>/smaps alongside system-level memory usage information
12 // such as Proportional Set Size (memory usage adjusted for sharing
13 // between processes), which allows reporting this information at a
14 // finer granularity than would otherwise be possible (e.g.,
15 // separating malloc() heap from JS heap).
17 // Existing memory can be tagged with MozTagAnonymousMemory(); it will
18 // tag the range of complete pages containing the given interval, so
19 // the results may be inexact if the range isn't page-aligned.
20 // MozTaggedAnonymousMmap() can be used like mmap() with an extra
21 // parameter, and will tag the returned memory if the mapping was
22 // successful (and if it was in fact anonymous).
24 // NOTE: The pointer given as the "tag" argument MUST remain valid as
25 // long as the mapping exists. The referenced string is read when
26 // /proc/<pid>/smaps or /proc/<pid>/maps is read, not when the tag is
27 // established, so freeing it or changing its contents will have
28 // unexpected results. Using a static string is probably best.
30 // Also note that this header can be used by both C and C++ code.
32 #ifndef mozilla_TaggedAnonymousMemory_h
33 #define mozilla_TaggedAnonymousMemory_h
40 # include <sys/types.h>
41 # include <sys/mman.h>
44 # include "mozilla/Types.h"
52 MFBT_API
void MozTagAnonymousMemory(const void* aPtr
, size_t aLength
,
55 MFBT_API
void* MozTaggedAnonymousMmap(void* aAddr
, size_t aLength
, int aProt
,
56 int aFlags
, int aFd
, off_t aOffset
,
59 MFBT_API
int MozTaggedMemoryIsSupported(void);
67 static inline void MozTagAnonymousMemory(const void* aPtr
, size_t aLength
,
70 static inline void* MozTaggedAnonymousMmap(void* aAddr
, size_t aLength
,
71 int aProt
, int aFlags
, int aFd
,
72 off_t aOffset
, const char* aTag
) {
74 MOZ_CRASH("We don't use this memory for WASI right now.");
77 return mmap(aAddr
, aLength
, aProt
, aFlags
, aFd
, aOffset
);
81 static inline int MozTaggedMemoryIsSupported(void) { return 0; }
87 #endif // mozilla_TaggedAnonymousMemory_h