Bumping manifests a=b2g-bump
[gecko.git] / mfbt / TaggedAnonymousMemory.cpp
bloba5f97900495c02802a7f912cb84a345c39007d11
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"
18 #include "mozilla/NullPtr.h"
20 // These constants are copied from <sys/prctl.h>, because the headers
21 // used for building may not have them even though the running kernel
22 // supports them.
23 #ifndef PR_SET_VMA
24 #define PR_SET_VMA 0x53564d41
25 #endif
26 #ifndef PR_SET_VMA_ANON_NAME
27 #define PR_SET_VMA_ANON_NAME 0
28 #endif
30 namespace mozilla {
32 // Returns 0 for success and -1 (with errno) for error.
33 static int
34 TagAnonymousMemoryAligned(const void* aPtr, size_t aLength, const char* aTag)
36 return prctl(PR_SET_VMA,
37 PR_SET_VMA_ANON_NAME,
38 reinterpret_cast<unsigned long>(aPtr),
39 aLength,
40 reinterpret_cast<unsigned long>(aTag));
43 // On some architectures, it's possible for the page size to be larger
44 // than the PAGE_SIZE we were compiled with. This computes the
45 // equivalent of PAGE_MASK.
46 static uintptr_t
47 GetPageMask()
49 static uintptr_t mask = 0;
51 if (mask == 0) {
52 uintptr_t pageSize = sysconf(_SC_PAGESIZE);
53 mask = ~(pageSize - 1);
54 MOZ_ASSERT((pageSize & (pageSize - 1)) == 0,
55 "Page size must be a power of 2!");
57 return mask;
60 } // namespace mozilla
62 int
63 MozTaggedMemoryIsSupported(void)
65 static int supported = -1;
67 if (supported == -1) {
68 // Tagging an empty range always "succeeds" if the feature is supported,
69 // regardless of the start pointer.
70 supported = mozilla::TagAnonymousMemoryAligned(nullptr, 0, nullptr) == 0;
72 return supported;
75 void
76 MozTagAnonymousMemory(const void* aPtr, size_t aLength, const char* aTag)
78 if (MozTaggedMemoryIsSupported()) {
79 // The kernel will round up the end of the range to the next page
80 // boundary if it's not aligned (comments indicate this behavior
81 // is based on that of madvise), but it will reject the request if
82 // the start is not aligned. We therefore round down the start
83 // address and adjust the length accordingly.
84 uintptr_t addr = reinterpret_cast<uintptr_t>(aPtr);
85 uintptr_t end = addr + aLength;
86 uintptr_t addrRounded = addr & mozilla::GetPageMask();
87 const void* ptrRounded = reinterpret_cast<const void*>(addrRounded);
89 mozilla::TagAnonymousMemoryAligned(ptrRounded, end - addrRounded, aTag);
93 void*
94 MozTaggedAnonymousMmap(void* aAddr, size_t aLength, int aProt, int aFlags,
95 int aFd, off_t aOffset, const char* aTag)
97 void* mapped = mmap(aAddr, aLength, aProt, aFlags, aFd, aOffset);
98 if (MozTaggedMemoryIsSupported() &&
99 (aFlags & MAP_ANONYMOUS) == MAP_ANONYMOUS &&
100 mapped != MAP_FAILED) {
101 mozilla::TagAnonymousMemoryAligned(mapped, aLength, aTag);
103 return mapped;
106 #endif // ANDROID