Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / mfbt / TaggedAnonymousMemory.cpp
blob07262b01ae914671b5115a4e3dc6642a9e688c62
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 #ifdef ANDROID
9 # include "mozilla/TaggedAnonymousMemory.h"
11 # include <sys/types.h>
12 # include <sys/mman.h>
13 # include <sys/prctl.h>
14 # include <sys/syscall.h>
15 # include <unistd.h>
17 # include "mozilla/Assertions.h"
19 // These constants are copied from <sys/prctl.h>, because the headers
20 // used for building may not have them even though the running kernel
21 // supports them.
22 # ifndef PR_SET_VMA
23 # define PR_SET_VMA 0x53564d41
24 # endif
25 # ifndef PR_SET_VMA_ANON_NAME
26 # define PR_SET_VMA_ANON_NAME 0
27 # endif
29 namespace mozilla {
31 // Returns 0 for success and -1 (with errno) for error.
32 static int TagAnonymousMemoryAligned(const void* aPtr, size_t aLength,
33 const char* aTag) {
34 return prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME,
35 reinterpret_cast<unsigned long>(aPtr), aLength,
36 reinterpret_cast<unsigned long>(aTag));
39 // On some architectures, it's possible for the page size to be larger
40 // than the PAGE_SIZE we were compiled with. This computes the
41 // equivalent of PAGE_MASK.
42 static uintptr_t GetPageMask() {
43 static uintptr_t mask = 0;
45 if (mask == 0) {
46 uintptr_t pageSize = sysconf(_SC_PAGESIZE);
47 mask = ~(pageSize - 1);
48 MOZ_ASSERT((pageSize & (pageSize - 1)) == 0,
49 "Page size must be a power of 2!");
51 return mask;
54 } // namespace mozilla
56 int MozTaggedMemoryIsSupported(void) {
57 static int supported = -1;
59 if (supported == -1) {
60 // Tagging an empty range always "succeeds" if the feature is supported,
61 // regardless of the start pointer.
62 supported = mozilla::TagAnonymousMemoryAligned(nullptr, 0, nullptr) == 0;
64 return supported;
67 void MozTagAnonymousMemory(const void* aPtr, size_t aLength, const char* aTag) {
68 if (MozTaggedMemoryIsSupported()) {
69 // The kernel will round up the end of the range to the next page
70 // boundary if it's not aligned (comments indicate this behavior
71 // is based on that of madvise), but it will reject the request if
72 // the start is not aligned. We therefore round down the start
73 // address and adjust the length accordingly.
74 uintptr_t addr = reinterpret_cast<uintptr_t>(aPtr);
75 uintptr_t end = addr + aLength;
76 uintptr_t addrRounded = addr & mozilla::GetPageMask();
77 const void* ptrRounded = reinterpret_cast<const void*>(addrRounded);
79 mozilla::TagAnonymousMemoryAligned(ptrRounded, end - addrRounded, aTag);
83 void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength, int aProt, int aFlags,
84 int aFd, off_t aOffset, const char* aTag) {
85 void* mapped = mmap(aAddr, aLength, aProt, aFlags, aFd, aOffset);
86 if (MozTaggedMemoryIsSupported() &&
87 (aFlags & MAP_ANONYMOUS) == MAP_ANONYMOUS && mapped != MAP_FAILED) {
88 mozilla::TagAnonymousMemoryAligned(mapped, aLength, aTag);
90 return mapped;
93 #endif // ANDROID