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 #include "mozilla/MemoryInfo.h"
9 #include "mozilla/DebugOnly.h"
16 MemoryInfo
MemoryInfo::Get(const void* aPtr
, size_t aSize
) {
19 result
.mStart
= uintptr_t(aPtr
);
20 const char* ptr
= reinterpret_cast<const char*>(aPtr
);
21 const char* end
= ptr
+ aSize
;
22 DebugOnly
<void*> base
= nullptr;
24 MEMORY_BASIC_INFORMATION basicInfo
;
25 if (!VirtualQuery(ptr
, &basicInfo
, sizeof(basicInfo
))) {
29 MOZ_ASSERT_IF(base
, base
== basicInfo
.AllocationBase
);
30 base
= basicInfo
.AllocationBase
;
33 std::min(size_t(basicInfo
.RegionSize
), size_t(end
- ptr
));
35 if (basicInfo
.State
== MEM_COMMIT
) {
36 result
.mCommitted
+= regionSize
;
37 } else if (basicInfo
.State
== MEM_RESERVE
) {
38 result
.mReserved
+= regionSize
;
39 } else if (basicInfo
.State
== MEM_FREE
) {
40 result
.mFree
+= regionSize
;
42 MOZ_ASSERT_UNREACHABLE("Unexpected region state");
44 result
.mSize
+= regionSize
;
47 if (result
.mType
.isEmpty()) {
48 if (basicInfo
.Type
& MEM_IMAGE
) {
49 result
.mType
+= PageType::Image
;
51 if (basicInfo
.Type
& MEM_MAPPED
) {
52 result
.mType
+= PageType::Mapped
;
54 if (basicInfo
.Type
& MEM_PRIVATE
) {
55 result
.mType
+= PageType::Private
;
58 // The first 8 bits of AllocationProtect are an enum. The remaining bits
60 switch (basicInfo
.AllocationProtect
& 0xff) {
61 case PAGE_EXECUTE_WRITECOPY
:
62 result
.mPerms
+= Perm::CopyOnWrite
;
64 case PAGE_EXECUTE_READWRITE
:
65 result
.mPerms
+= Perm::Write
;
67 case PAGE_EXECUTE_READ
:
68 result
.mPerms
+= Perm::Read
;
71 result
.mPerms
+= Perm::Execute
;
75 result
.mPerms
+= Perm::CopyOnWrite
;
78 result
.mPerms
+= Perm::Write
;
81 result
.mPerms
+= Perm::Read
;
88 if (basicInfo
.AllocationProtect
& PAGE_GUARD
) {
89 result
.mPerms
+= Perm::Guard
;
91 if (basicInfo
.AllocationProtect
& PAGE_NOCACHE
) {
92 result
.mPerms
+= Perm::NoCache
;
94 if (basicInfo
.AllocationProtect
& PAGE_WRITECOMBINE
) {
95 result
.mPerms
+= Perm::WriteCombine
;
100 result
.mEnd
= uintptr_t(ptr
);
104 } // namespace mozilla