1 | /* $Id: systemmem-darwin.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTSystemQueryTotalRam, darwin ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2017 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/system.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <unistd.h>
|
---|
39 | #include <sys/sysctl.h>
|
---|
40 | #include <sys/stat.h>
|
---|
41 | #include <mach/mach.h>
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 | RTDECL(int) RTSystemQueryTotalRam(uint64_t *pcb)
|
---|
46 | {
|
---|
47 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
48 |
|
---|
49 | int aiMib[2];
|
---|
50 | aiMib[0] = CTL_HW;
|
---|
51 | aiMib[1] = HW_MEMSIZE;
|
---|
52 | uint64_t cbPhysMem = UINT64_MAX;
|
---|
53 | size_t cb = sizeof(cbPhysMem);
|
---|
54 | int rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cbPhysMem, &cb, NULL, 0);
|
---|
55 | if (rc == 0)
|
---|
56 | {
|
---|
57 | *pcb = cbPhysMem;
|
---|
58 | return VINF_SUCCESS;
|
---|
59 | }
|
---|
60 | return RTErrConvertFromErrno(errno);
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | RTDECL(int) RTSystemQueryAvailableRam(uint64_t *pcb)
|
---|
65 | {
|
---|
66 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
67 |
|
---|
68 | static mach_port_t volatile s_hSelfPort = 0;
|
---|
69 | mach_port_t hSelfPort = s_hSelfPort;
|
---|
70 | if (hSelfPort == 0)
|
---|
71 | s_hSelfPort = hSelfPort = mach_host_self();
|
---|
72 |
|
---|
73 | vm_statistics_data_t VmStats;
|
---|
74 | mach_msg_type_number_t cItems = sizeof(VmStats) / sizeof(natural_t);
|
---|
75 |
|
---|
76 | kern_return_t krc = host_statistics(hSelfPort, HOST_VM_INFO, (host_info_t)&VmStats, &cItems);
|
---|
77 | if (krc == KERN_SUCCESS)
|
---|
78 | {
|
---|
79 | uint64_t cPages = VmStats.inactive_count;
|
---|
80 | cPages += VmStats.free_count;
|
---|
81 | *pcb = cPages * PAGE_SIZE;
|
---|
82 | return VINF_SUCCESS;
|
---|
83 | }
|
---|
84 | return RTErrConvertFromDarwin(krc);
|
---|
85 | }
|
---|
86 |
|
---|