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"
17 MemoryInfo
MemoryInfo::Get(const void* aPtr
, size_t aSize
) {
20 result
.mStart
= uintptr_t(aPtr
);
21 const char* ptr
= reinterpret_cast<const char*>(aPtr
);
22 const char* end
= ptr
+ aSize
;
23 DebugOnly
<void*> base
= nullptr;
25 MEMORY_BASIC_INFORMATION basicInfo
;
26 if (!VirtualQuery(ptr
, &basicInfo
, sizeof(basicInfo
))) {
30 MOZ_ASSERT_IF(base
, base
== basicInfo
.AllocationBase
);
31 base
= basicInfo
.AllocationBase
;
34 std::min(size_t(basicInfo
.RegionSize
), size_t(end
- ptr
));
36 if (basicInfo
.State
== MEM_COMMIT
) {
37 result
.mCommitted
+= regionSize
;
38 } else if (basicInfo
.State
== MEM_RESERVE
) {
39 result
.mReserved
+= regionSize
;
40 } else if (basicInfo
.State
== MEM_FREE
) {
41 result
.mFree
+= regionSize
;
43 MOZ_ASSERT_UNREACHABLE("Unexpected region state");
45 result
.mSize
+= regionSize
;
48 if (result
.mType
.isEmpty()) {
49 if (basicInfo
.Type
& MEM_IMAGE
) {
50 result
.mType
+= PageType::Image
;
52 if (basicInfo
.Type
& MEM_MAPPED
) {
53 result
.mType
+= PageType::Mapped
;
55 if (basicInfo
.Type
& MEM_PRIVATE
) {
56 result
.mType
+= PageType::Private
;
59 // The first 8 bits of AllocationProtect are an enum. The remaining bits
61 switch (basicInfo
.AllocationProtect
& 0xff) {
62 case PAGE_EXECUTE_WRITECOPY
:
63 result
.mPerms
+= Perm::CopyOnWrite
;
65 case PAGE_EXECUTE_READWRITE
:
66 result
.mPerms
+= Perm::Write
;
68 case PAGE_EXECUTE_READ
:
69 result
.mPerms
+= Perm::Read
;
72 result
.mPerms
+= Perm::Execute
;
76 result
.mPerms
+= Perm::CopyOnWrite
;
79 result
.mPerms
+= Perm::Write
;
82 result
.mPerms
+= Perm::Read
;
89 if (basicInfo
.AllocationProtect
& PAGE_GUARD
) {
90 result
.mPerms
+= Perm::Guard
;
92 if (basicInfo
.AllocationProtect
& PAGE_NOCACHE
) {
93 result
.mPerms
+= Perm::NoCache
;
95 if (basicInfo
.AllocationProtect
& PAGE_WRITECOMBINE
) {
96 result
.mPerms
+= Perm::WriteCombine
;
101 result
.mEnd
= uintptr_t(ptr
);
105 } // namespace mozilla