From e6bf2bcafc41319a22945e98f50906149527ff51 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Mon, 10 Apr 2023 13:17:26 -0700 Subject: move psutil_virtual_mem() in a new mem.c file --- psutil/_psutil_windows.c | 30 +----------------------------- psutil/arch/windows/mem.c | 35 +++++++++++++++++++++++++++++++++++ psutil/arch/windows/mem.h | 9 +++++++++ setup.py | 1 + 4 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 psutil/arch/windows/mem.c create mode 100644 psutil/arch/windows/mem.h diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 6e51c449..969c5713 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -33,6 +33,7 @@ #include "arch/windows/process_handles.h" #include "arch/windows/disk.h" #include "arch/windows/cpu.h" +#include "arch/windows/mem.h" #include "arch/windows/net.h" #include "arch/windows/services.h" #include "arch/windows/socks.h" @@ -603,35 +604,6 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) { return Py_BuildValue("I", wsCounters.NumberOfPrivatePages); } - -/* - * Return a Python integer indicating the total amount of physical memory - * in bytes. - */ -static PyObject * -psutil_virtual_mem(PyObject *self, PyObject *args) { - unsigned long long totalPhys, availPhys, totalSys, availSys, pageSize; - PERFORMANCE_INFORMATION perfInfo; - - if (! GetPerformanceInfo(&perfInfo, sizeof(PERFORMANCE_INFORMATION))) { - PyErr_SetFromWindowsErr(0); - return NULL; - } - // values are size_t, widen (if needed) to long long - pageSize = perfInfo.PageSize; - totalPhys = perfInfo.PhysicalTotal * pageSize; - availPhys = perfInfo.PhysicalAvailable * pageSize; - totalSys = perfInfo.CommitLimit * pageSize; - availSys = totalSys - perfInfo.CommitTotal * pageSize; - return Py_BuildValue( - "(LLLL)", - totalPhys, - availPhys, - totalSys, - availSys); -} - - /* * Return process current working directory as a Python string. */ diff --git a/psutil/arch/windows/mem.c b/psutil/arch/windows/mem.c new file mode 100644 index 00000000..8b678254 --- /dev/null +++ b/psutil/arch/windows/mem.c @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include +#include +#include + +#include "../../_psutil_common.h" + + +PyObject * +psutil_virtual_mem(PyObject *self, PyObject *args) { + unsigned long long totalPhys, availPhys, totalSys, availSys, pageSize; + PERFORMANCE_INFORMATION perfInfo; + + if (! GetPerformanceInfo(&perfInfo, sizeof(PERFORMANCE_INFORMATION))) { + PyErr_SetFromWindowsErr(0); + return NULL; + } + // values are size_t, widen (if needed) to long long + pageSize = perfInfo.PageSize; + totalPhys = perfInfo.PhysicalTotal * pageSize; + availPhys = perfInfo.PhysicalAvailable * pageSize; + totalSys = perfInfo.CommitLimit * pageSize; + availSys = totalSys - perfInfo.CommitTotal * pageSize; + return Py_BuildValue( + "(LLLL)", + totalPhys, + availPhys, + totalSys, + availSys); +} diff --git a/psutil/arch/windows/mem.h b/psutil/arch/windows/mem.h new file mode 100644 index 00000000..871cd64e --- /dev/null +++ b/psutil/arch/windows/mem.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include + +PyObject *psutil_virtual_mem(PyObject* self, PyObject* args); diff --git a/setup.py b/setup.py index 4e6f6f17..a4f719b2 100755 --- a/setup.py +++ b/setup.py @@ -214,6 +214,7 @@ if WINDOWS: 'psutil/arch/windows/process_info.c', 'psutil/arch/windows/process_handles.c', 'psutil/arch/windows/disk.c', + 'psutil/arch/windows/mem.c', 'psutil/arch/windows/net.c', 'psutil/arch/windows/cpu.c', 'psutil/arch/windows/security.c', -- cgit v1.2.1