Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / mfbt / TaggedAnonymousMemory.h
blob7ca5e60c9d0827eedebc020ee4cc8c2a55ef0510
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 // Linux kernels since 5.17 have a feature for assigning names to
8 // ranges of anonymous memory (i.e., memory that doesn't have a "name"
9 // in the form of an underlying mapped file). These names are
10 // reported in /proc/<pid>/smaps alongside system-level memory usage
11 // information such as Proportional Set Size (memory usage adjusted
12 // for sharing between processes), which allows reporting this
13 // information at a finer granularity than would otherwise be possible
14 // (e.g., separating malloc() heap from JS heap).
16 // Existing memory can be tagged with MozTagAnonymousMemory(); it will
17 // tag the range of complete pages containing the given interval, so
18 // the results may be inexact if the range isn't page-aligned.
19 // MozTaggedAnonymousMmap() can be used like mmap() with an extra
20 // parameter, and will tag the returned memory if the mapping was
21 // successful (and if it was in fact anonymous).
23 // NOTE: The pointer given as the "tag" argument MUST remain valid as
24 // long as the mapping exists. The referenced string is read when
25 // /proc/<pid>/smaps or /proc/<pid>/maps is read, not when the tag is
26 // established, so freeing it or changing its contents will have
27 // unexpected results. Using a static string is probably best.
29 // Also note that this header can be used by both C and C++ code.
31 #ifndef mozilla_TaggedAnonymousMemory_h
32 #define mozilla_TaggedAnonymousMemory_h
34 #ifndef XP_WIN
36 # ifdef __wasi__
37 # include <stdlib.h>
38 # else
39 # include <sys/types.h>
40 # include <sys/mman.h>
41 # endif // __wasi__
43 # include "mozilla/Types.h"
45 # ifdef XP_LINUX
47 # ifdef __cplusplus
48 extern "C" {
49 # endif
51 MFBT_API void MozTagAnonymousMemory(const void* aPtr, size_t aLength,
52 const char* aTag);
54 MFBT_API void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength, int aProt,
55 int aFlags, int aFd, off_t aOffset,
56 const char* aTag);
58 # ifdef __cplusplus
59 } // extern "C"
60 # endif
62 # else // XP_LINUX
64 static inline void MozTagAnonymousMemory(const void* aPtr, size_t aLength,
65 const char* aTag) {}
67 static inline void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength,
68 int aProt, int aFlags, int aFd,
69 off_t aOffset, const char* aTag) {
70 # ifdef __wasi__
71 MOZ_CRASH("We don't use this memory for WASI right now.");
72 return nullptr;
73 # else
74 return mmap(aAddr, aLength, aProt, aFlags, aFd, aOffset);
75 # endif
78 # endif // XP_LINUX
80 #endif // !XP_WIN
82 #endif // mozilla_TaggedAnonymousMemory_h