From f5fdf917bd76506fcaed6a05cb4f73982833580b Mon Sep 17 00:00:00 2001 From: Eidolon Date: Sat, 11 Jul 2026 13:48:56 -0500 Subject: [PATCH] Support >4GB sys mem in memfree for linux, win32, macOS sys mem counts Also includes memory counts for the extended allocation functionality --- src/android/i_system.c | 10 +++---- src/core/memory.cpp | 3 +++ src/core/memory.h | 4 +++ src/dedicated/i_system.c | 56 +++++++++++++++++++++++++++++++++------- src/sdl/i_system.cpp | 54 +++++++++++++++++++++++++++++++------- src/z_zone.cpp | 10 +++++++ src/z_zone.h | 1 + 7 files changed, 113 insertions(+), 25 deletions(-) diff --git a/src/android/i_system.c b/src/android/i_system.c index ef10df5b5..c0f7cc11c 100644 --- a/src/android/i_system.c +++ b/src/android/i_system.c @@ -34,8 +34,8 @@ size_t I_GetFreeMem(size_t *total) /* return si.freeram; */ char buf[1024]; char *memTag; - UINT32 freeKBytes; - UINT32 totalKBytes; + size_t freeKBytes; + size_t totalKBytes; INT32 n; INT32 meminfo_fd = -1; @@ -46,7 +46,7 @@ size_t I_GetFreeMem(size_t *total) if (n < 0) { // Error - *total = 0L; + *total = 0; return 0; } @@ -54,7 +54,7 @@ size_t I_GetFreeMem(size_t *total) if (NULL == (memTag = strstr(buf, MEMTOTAL))) { // Error - *total = 0L; + *total = 0; return 0; } @@ -64,7 +64,7 @@ size_t I_GetFreeMem(size_t *total) if (NULL == (memTag = strstr(buf, MEMFREE))) { // Error - *total = 0L; + *total = 0; return 0; } diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 9e591e78c..81ba79d5d 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -114,6 +114,7 @@ void* PoolAllocator::allocate() FreeBlock* ret = head_; head_ = head_->next; + allocated_blocks_++; return ret; } @@ -125,6 +126,7 @@ void PoolAllocator::deallocate(void* p) FreeBlock* block = reinterpret_cast(p); block->next = head_; head_ = block; + allocated_blocks_--; } void PoolAllocator::release() @@ -144,6 +146,7 @@ void PoolAllocator::release() first_chunk_ = nullptr; head_ = nullptr; + allocated_blocks_ = 0; } static LinearMemory g_frame_memory {4 * 1024 * 1024}; diff --git a/src/core/memory.h b/src/core/memory.h index 77e7206b5..4aec917d7 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -37,6 +37,7 @@ class PoolAllocator final FreeBlock* head_; size_t block_size_; size_t blocks_; + size_t allocated_blocks_; int32_t tag_; ChunkFooter* allocate_chunk(); @@ -47,6 +48,7 @@ public: , head_(nullptr) , block_size_(block_size) , blocks_(blocks) + , allocated_blocks_(0) , tag_(tag) {} PoolAllocator(const PoolAllocator&) = delete; @@ -59,6 +61,8 @@ public: void* allocate(); void deallocate(void* p); constexpr size_t block_size() const noexcept { return block_size_; }; + constexpr size_t allocated_blocks() const noexcept { return allocated_blocks_; }; + constexpr size_t allocated_bytes() const noexcept { return allocated_blocks_ * block_size_; }; void release(); }; diff --git a/src/dedicated/i_system.c b/src/dedicated/i_system.c index fb3cc2731..fc38ffdef 100644 --- a/src/dedicated/i_system.c +++ b/src/dedicated/i_system.c @@ -68,6 +68,12 @@ typedef LPVOID (WINAPI *p_MapViewOfFile) (HANDLE, DWORD, DWORD, DWORD, SIZE_T); #include #if defined (__linux__) #include +#elif defined(__APPLE__) +#include +#include +#include +#include +#include #else #include #include @@ -2009,13 +2015,13 @@ size_t I_GetFreeMem(size_t *total) *total = 32 << 20; return 32 << 20; #elif defined (_WIN32) - MEMORYSTATUS info; + MEMORYSTATUSEX info; - info.dwLength = sizeof (MEMORYSTATUS); - GlobalMemoryStatus( &info ); + info.dwLength = sizeof (MEMORYSTATUSEX); + GlobalMemoryStatusEx( &info ); if (total) - *total = (size_t)info.dwTotalPhys; - return (size_t)info.dwAvailPhys; + *total = (size_t)info.ullTotalPhys; + return (size_t)info.ullAvailPhys; #elif defined (__OS2__) UINT32 pr_arena; @@ -2048,7 +2054,7 @@ size_t I_GetFreeMem(size_t *total) { // Error if (total) - *total = 0L; + *total = 0; return 0; } @@ -2057,12 +2063,12 @@ size_t I_GetFreeMem(size_t *total) { // Error if (total) - *total = 0L; + *total = 0; return 0; } memTag += sizeof (MEMTOTAL); - totalKBytes = (size_t)atoi(memTag); + totalKBytes = strtoul(memTag, NULL, 10); if ((memTag = strstr(buf, MEMAVAILABLE)) == NULL) { @@ -2076,7 +2082,7 @@ size_t I_GetFreeMem(size_t *total) { // Error if (total) - *total = 0L; + *total = 0; return 0; } freeKBytes = MemAvailable; @@ -2084,16 +2090,46 @@ size_t I_GetFreeMem(size_t *total) else { memTag += sizeof (MEMAVAILABLE); - freeKBytes = atoi(memTag); + freeKBytes = strtoul(memTag, NULL, 10); } if (total) *total = totalKBytes << 10; return freeKBytes << 10; +#elif defined(__APPLE__) + /* macOS */ + mach_port_t host = mach_host_self(); + kern_return_t kr; + mach_msg_type_number_t count; + vm_size_t v_page_size; + struct vm_statistics64 vm_stats; + uint64_t total_mem, free_mem; + size_t size; + + size = sizeof(total_mem); + if (sysctlbyname("hw.memsize", &total_mem, &size, NULL, 0) < 0) + total_mem = 0; + + kr = host_page_size(host, &v_page_size); + if (kr != KERN_SUCCESS) + v_page_size = 4096; + + count = HOST_VM_INFO64_COUNT; + kr = host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm_stats, &count); + if (kr == KERN_SUCCESS) + free_mem = (uint64_t)(vm_stats.free_count + vm_stats.inactive_count) * v_page_size; + else + free_mem = 0; + + if (total) + *total = (size_t)total_mem; + return (size_t)free_mem; #else // Guess 48 MB. if (total) + { *total = 48<<20; + } return 48<<20; #endif } diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 4e9de24c2..fb4f0dcd5 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -83,6 +83,12 @@ typedef LPVOID (WINAPI *p_MapViewOfFile) (HANDLE, DWORD, DWORD, DWORD, SIZE_T); #if defined (__unix__) || defined(__APPLE__) || (defined (UNIXCOMMON) && !defined (__HAIKU__)) #if defined (__linux__) #include +#elif defined(__APPLE__) +#include +#include +#include +#include +#include #else #include #include @@ -2635,13 +2641,13 @@ size_t I_GetFreeMem(size_t *total) *total = 32 << 20; return 32 << 20; #elif defined (_WIN32) - MEMORYSTATUS info; + MEMORYSTATUSEX info; - info.dwLength = sizeof (MEMORYSTATUS); - GlobalMemoryStatus( &info ); + info.dwLength = sizeof (MEMORYSTATUSEX); + GlobalMemoryStatusEx( &info ); if (total) - *total = (size_t)info.dwTotalPhys; - return (size_t)info.dwAvailPhys; + *total = (size_t)info.ullTotalPhys; + return (size_t)info.ullAvailPhys; #elif defined (__OS2__) UINT32 pr_arena; @@ -2674,7 +2680,7 @@ size_t I_GetFreeMem(size_t *total) { // Error if (total) - *total = 0L; + *total = 0; return 0; } @@ -2683,12 +2689,12 @@ size_t I_GetFreeMem(size_t *total) { // Error if (total) - *total = 0L; + *total = 0; return 0; } memTag += sizeof (MEMTOTAL); - totalKBytes = (size_t)atoi(memTag); + totalKBytes = strtoul(memTag, NULL, 10); if ((memTag = strstr(buf, MEMAVAILABLE)) == NULL) { @@ -2702,7 +2708,7 @@ size_t I_GetFreeMem(size_t *total) { // Error if (total) - *total = 0L; + *total = 0; return 0; } freeKBytes = MemAvailable; @@ -2710,12 +2716,40 @@ size_t I_GetFreeMem(size_t *total) else { memTag += sizeof (MEMAVAILABLE); - freeKBytes = atoi(memTag); + freeKBytes = strtoul(memTag, NULL, 10); } if (total) *total = totalKBytes << 10; return freeKBytes << 10; +#elif defined(__APPLE__) + /* macOS */ + mach_port_t host = mach_host_self(); + kern_return_t kr; + mach_msg_type_number_t count; + vm_size_t v_page_size; + struct vm_statistics64 vm_stats; + uint64_t total_mem, free_mem; + size_t size; + + size = sizeof(total_mem); + if (sysctlbyname("hw.memsize", &total_mem, &size, NULL, 0) < 0) + total_mem = 0; + + kr = host_page_size(host, &v_page_size); + if (kr != KERN_SUCCESS) + v_page_size = 4096; + + count = HOST_VM_INFO64_COUNT; + kr = host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm_stats, &count); + if (kr == KERN_SUCCESS) + free_mem = (uint64_t)(vm_stats.free_count + vm_stats.inactive_count) * v_page_size; + else + free_mem = 0; + + if (total) + *total = (size_t)total_mem; + return (size_t)free_mem; #else // Guess 48 MB. if (total) diff --git a/src/z_zone.cpp b/src/z_zone.cpp index f25f99e43..f688dd831 100644 --- a/src/z_zone.cpp +++ b/src/z_zone.cpp @@ -114,6 +114,7 @@ void Z_Init(void) head.next = head.prev = &head; memfree = I_GetFreeMem(&total)>>20; + CONS_Printf("System memory: %sMB - Free: %sMB\n", sizeu1(total>>20), sizeu2(memfree)); // Note: This allocates memory. Watch out. @@ -745,6 +746,7 @@ static void Command_Memfree_f(void) CONS_Printf(M_GetText("HUD graphics : %7s KB\n"), sizeu1(Z_TagUsage(PU_HUDGFX)>>10)); CONS_Printf(M_GetText("Locked cache : %7s KB\n"), sizeu1(Z_TagUsage(PU_CACHE)>>10)); CONS_Printf(M_GetText("Level : %7s KB\n"), sizeu1(Z_TagUsage(PU_LEVEL)>>10)); + CONS_Printf(M_GetText("Level (pooled) : %7s KB\n"), sizeu1(Z_LevelPoolUsage()>>10)); CONS_Printf(M_GetText("Special thinker : %7s KB\n"), sizeu1(Z_TagUsage(PU_LEVSPEC)>>10)); CONS_Printf(M_GetText("All purgable : %7s KB\n"), sizeu1(Z_TagsUsage(PU_PURGELEVEL, INT32_MAX)>>10)); @@ -804,6 +806,14 @@ char *Z_StrDup(const char *s) return strcpy((char*)ZZ_Alloc(strlen(s) + 1), s); } +size_t Z_LevelPoolUsage(void) +{ + return g_level_large_pool.allocated_bytes() + + g_level_med_pool.allocated_bytes() + + g_level_small_pool.allocated_bytes() + + g_level_tiny_pool.allocated_bytes(); +} + void* Z_LevelPoolMalloc(size_t size) { void* p = nullptr; diff --git a/src/z_zone.h b/src/z_zone.h index 38f00c48c..847e47291 100644 --- a/src/z_zone.h +++ b/src/z_zone.h @@ -167,6 +167,7 @@ char *Z_StrDup(const char *in); // // Specialty allocation functions // +size_t Z_LevelPoolUsage(void); void *Z_LevelPoolMalloc(size_t size); void *Z_LevelPoolCalloc(size_t size); void Z_LevelPoolFree(void *p, size_t size);