[OS] Implement and expose to scripting APIs get_memory_info method instead of old get_free_static_memory.

This commit is contained in:
bruvzg 2023-04-03 11:46:29 +03:00
parent f178cad04a
commit 628f3b2f79
No known key found for this signature in database
GPG key ID: 7960FCF39844EC38
10 changed files with 275 additions and 4 deletions

View file

@ -463,6 +463,9 @@ def configure(env: "Environment"):
else:
env.Append(LINKFLAGS=["-T", "platform/linuxbsd/pck_embed.legacy.ld"])
if platform.system() == "FreeBSD":
env.Append(LINKFLAGS=["-lkvm"])
## Cross-compilation
# TODO: Support cross-compilation on architectures other than x86.
host_is_64_bit = sys.maxsize > 2**32

View file

@ -51,6 +51,7 @@
#include <direct.h>
#include <knownfolders.h>
#include <process.h>
#include <psapi.h>
#include <regstr.h>
#include <shlobj.h>
#include <wbemcli.h>
@ -683,6 +684,43 @@ static void _append_to_pipe(char *p_bytes, int p_size, String *r_pipe, Mutex *p_
}
}
Dictionary OS_Windows::get_memory_info() const {
Dictionary meminfo;
meminfo["physical"] = -1;
meminfo["free"] = -1;
meminfo["available"] = -1;
meminfo["stack"] = -1;
PERFORMANCE_INFORMATION pref_info;
pref_info.cb = sizeof(pref_info);
GetPerformanceInfo(&pref_info, sizeof(pref_info));
typedef void(WINAPI * PGetCurrentThreadStackLimits)(PULONG_PTR, PULONG_PTR);
PGetCurrentThreadStackLimits GetCurrentThreadStackLimits = (PGetCurrentThreadStackLimits)GetProcAddress(GetModuleHandleA("kernel32.dll"), "GetCurrentThreadStackLimits");
ULONG_PTR LowLimit = 0;
ULONG_PTR HighLimit = 0;
if (GetCurrentThreadStackLimits) {
GetCurrentThreadStackLimits(&LowLimit, &HighLimit);
}
if (pref_info.PhysicalTotal * pref_info.PageSize != 0) {
meminfo["physical"] = pref_info.PhysicalTotal * pref_info.PageSize;
}
if (pref_info.PhysicalAvailable * pref_info.PageSize != 0) {
meminfo["free"] = pref_info.PhysicalAvailable * pref_info.PageSize;
}
if (pref_info.CommitLimit * pref_info.PageSize != 0) {
meminfo["available"] = pref_info.CommitLimit * pref_info.PageSize;
}
if (HighLimit - LowLimit != 0) {
meminfo["stack"] = HighLimit - LowLimit;
}
return meminfo;
}
Error OS_Windows::execute(const String &p_path, const List<String> &p_arguments, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
String path = p_path.replace("/", "\\");
String command = _quote_command_line_argument(path);

View file

@ -178,6 +178,8 @@ public:
virtual void delay_usec(uint32_t p_usec) const override;
virtual uint64_t get_ticks_usec() const override;
virtual Dictionary get_memory_info() const override;
virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr, bool p_open_console = false) override;
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
virtual Error kill(const ProcessID &p_pid) override;