Backed out changeset e4bf227a6224 (bug 1801501) for causing mochitest plain failures...
[gecko.git] / mfbt / TaggedAnonymousMemory.h
blobebd2c003185da58f3a80181ac317935219489f2c
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
35 #ifndef XP_WIN
37 # ifdef __wasi__
38 # include <stdlib.h>
39 # else
40 # include <sys/types.h>
41 # include <sys/mman.h>
42 # endif // __wasi__
44 # include "mozilla/Types.h"
46 # ifdef ANDROID
48 # ifdef __cplusplus
49 extern "C" {
50 # endif
52 MFBT_API void MozTagAnonymousMemory(const void* aPtr, size_t aLength,
53 const char* aTag);
55 MFBT_API void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength, int aProt,
56 int aFlags, int aFd, off_t aOffset,
57 const char* aTag);
59 MFBT_API int MozTaggedMemoryIsSupported(void);
61 # ifdef __cplusplus
62 } // extern "C"
63 # endif
65 # else // ANDROID
67 static inline void MozTagAnonymousMemory(const void* aPtr, size_t aLength,
68 const char* aTag) {}
70 static inline void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength,
71 int aProt, int aFlags, int aFd,
72 off_t aOffset, const char* aTag) {
73 # ifdef __wasi__
74 MOZ_CRASH("We don't use this memory for WASI right now.");
75 return nullptr;
76 # else
77 return mmap(aAddr, aLength, aProt, aFlags, aFd, aOffset);
78 # endif
81 static inline int MozTaggedMemoryIsSupported(void) { return 0; }
83 # endif // ANDROID
85 #endif // !XP_WIN
87 #endif // mozilla_TaggedAnonymousMemory_h