Bug 1686838 [wpt PR 27194] - [webcodecs] Deprecate VideoFrame.destroy()., a=testonly
[gecko.git] / xpcom / base / MemoryInfo.cpp
blob40fd69570257e702bbba838a3ca3d77ac8e7b171
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"
11 #include <winbase.h>
13 namespace mozilla {
15 /* static */
16 MemoryInfo MemoryInfo::Get(const void* aPtr, size_t aSize) {
17 MemoryInfo result;
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;
23 while (ptr < end) {
24 MEMORY_BASIC_INFORMATION basicInfo;
25 if (!VirtualQuery(ptr, &basicInfo, sizeof(basicInfo))) {
26 break;
29 MOZ_ASSERT_IF(base, base == basicInfo.AllocationBase);
30 base = basicInfo.AllocationBase;
32 size_t regionSize =
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;
41 } else {
42 MOZ_ASSERT_UNREACHABLE("Unexpected region state");
44 result.mSize += regionSize;
45 ptr += 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
59 // are flags.
60 switch (basicInfo.AllocationProtect & 0xff) {
61 case PAGE_EXECUTE_WRITECOPY:
62 result.mPerms += Perm::CopyOnWrite;
63 [[fallthrough]];
64 case PAGE_EXECUTE_READWRITE:
65 result.mPerms += Perm::Write;
66 [[fallthrough]];
67 case PAGE_EXECUTE_READ:
68 result.mPerms += Perm::Read;
69 [[fallthrough]];
70 case PAGE_EXECUTE:
71 result.mPerms += Perm::Execute;
72 break;
74 case PAGE_WRITECOPY:
75 result.mPerms += Perm::CopyOnWrite;
76 [[fallthrough]];
77 case PAGE_READWRITE:
78 result.mPerms += Perm::Write;
79 [[fallthrough]];
80 case PAGE_READONLY:
81 result.mPerms += Perm::Read;
82 break;
84 default:
85 break;
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);
101 return result;
104 } // namespace mozilla