1 | /* $Id: RTSystemQueryTotalRam-win.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTSystemQueryTotalRam, windows ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/win/windows.h>
|
---|
32 | #include <iprt/system.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/errcore.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 | #include "internal-r3-win.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Global Variables *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | static bool volatile g_fInitialized = false;
|
---|
46 | typedef BOOL (WINAPI *PFNGLOBALMEMORYSTATUSEX)(LPMEMORYSTATUSEX);
|
---|
47 | static PFNGLOBALMEMORYSTATUSEX g_pfnGlobalMemoryStatusEx = NULL;
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * The GlobalMemoryStatusEx API is not available on older Windows version.
|
---|
52 | *
|
---|
53 | * @returns Pointer to GlobalMemoryStatusEx or NULL if not available.
|
---|
54 | */
|
---|
55 | DECLINLINE(PFNGLOBALMEMORYSTATUSEX) rtSystemWinGetExApi(void)
|
---|
56 | {
|
---|
57 | PFNGLOBALMEMORYSTATUSEX pfnEx;
|
---|
58 | if (g_fInitialized)
|
---|
59 | pfnEx = g_pfnGlobalMemoryStatusEx;
|
---|
60 | else
|
---|
61 | {
|
---|
62 | pfnEx = (PFNGLOBALMEMORYSTATUSEX)GetProcAddress(g_hModKernel32, "GlobalMemoryStatusEx");
|
---|
63 | g_pfnGlobalMemoryStatusEx = pfnEx;
|
---|
64 | g_fInitialized = true;
|
---|
65 | }
|
---|
66 | return pfnEx;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | RTDECL(int) RTSystemQueryTotalRam(uint64_t *pcb)
|
---|
71 | {
|
---|
72 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
73 |
|
---|
74 | int rc = VINF_SUCCESS;
|
---|
75 | PFNGLOBALMEMORYSTATUSEX pfnGlobalMemoryStatusEx = rtSystemWinGetExApi();
|
---|
76 | if (pfnGlobalMemoryStatusEx)
|
---|
77 | {
|
---|
78 | MEMORYSTATUSEX MemStatus;
|
---|
79 | MemStatus.dwLength = sizeof(MemStatus);
|
---|
80 | if (pfnGlobalMemoryStatusEx(&MemStatus))
|
---|
81 | *pcb = MemStatus.ullTotalPhys;
|
---|
82 | else
|
---|
83 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
84 | }
|
---|
85 | else
|
---|
86 | {
|
---|
87 | MEMORYSTATUS MemStatus;
|
---|
88 | RT_ZERO(MemStatus);
|
---|
89 | MemStatus.dwLength = sizeof(MemStatus);
|
---|
90 | GlobalMemoryStatus(&MemStatus);
|
---|
91 | *pcb = MemStatus.dwTotalPhys;
|
---|
92 | }
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | RTDECL(int) RTSystemQueryAvailableRam(uint64_t *pcb)
|
---|
98 | {
|
---|
99 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
100 |
|
---|
101 | int rc = VINF_SUCCESS;
|
---|
102 | PFNGLOBALMEMORYSTATUSEX pfnGlobalMemoryStatusEx = rtSystemWinGetExApi();
|
---|
103 | if (pfnGlobalMemoryStatusEx)
|
---|
104 | {
|
---|
105 | MEMORYSTATUSEX MemStatus;
|
---|
106 | MemStatus.dwLength = sizeof(MemStatus);
|
---|
107 | if (pfnGlobalMemoryStatusEx(&MemStatus))
|
---|
108 | *pcb = MemStatus.ullAvailPhys;
|
---|
109 | else
|
---|
110 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | MEMORYSTATUS MemStatus;
|
---|
115 | RT_ZERO(MemStatus);
|
---|
116 | MemStatus.dwLength = sizeof(MemStatus);
|
---|
117 | GlobalMemoryStatus(&MemStatus);
|
---|
118 | *pcb = MemStatus.dwAvailPhys;
|
---|
119 | }
|
---|
120 | return rc;
|
---|
121 | }
|
---|