diff --git a/python/PyQt6/core/auto_generated/qgsapplication.sip.in b/python/PyQt6/core/auto_generated/qgsapplication.sip.in index 4be724f0bbac..b729b1ed4873 100644 --- a/python/PyQt6/core/auto_generated/qgsapplication.sip.in +++ b/python/PyQt6/core/auto_generated/qgsapplication.sip.in @@ -427,7 +427,7 @@ Returns a string name of the operating system QGIS is running on. .. seealso:: :py:func:`platform` %End - static int systemMemorySizeMb(); + static unsigned long systemMemorySizeMb(); %Docstring Returns the size of the system memory (RAM) in megabytes. diff --git a/python/core/auto_generated/qgsapplication.sip.in b/python/core/auto_generated/qgsapplication.sip.in index d4b8952d046d..601fbffafa65 100644 --- a/python/core/auto_generated/qgsapplication.sip.in +++ b/python/core/auto_generated/qgsapplication.sip.in @@ -427,7 +427,7 @@ Returns a string name of the operating system QGIS is running on. .. seealso:: :py:func:`platform` %End - static int systemMemorySizeMb(); + static unsigned long systemMemorySizeMb(); %Docstring Returns the size of the system memory (RAM) in megabytes. diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index 24158abb69da..71e24201326e 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -160,6 +160,11 @@ const QgsSettingsEntryStringList *QgsApplication::settingsSearchPathsForSVG = ne #include #endif +#if defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_NETBSD) +#include +#include +#endif + #define CONN_POOL_MAX_CONCURRENT_CONNS 4 QObject *ABISYM( QgsApplication::mFileOpenEventReceiver ) = nullptr; @@ -1422,8 +1427,9 @@ QString QgsApplication::osName() #endif } -int QgsApplication::systemMemorySizeMb() +unsigned long QgsApplication::systemMemorySizeMb() { + constexpr unsigned long megabyte = 1024 * 1024; #if defined(Q_OS_ANDROID) return -1; #elif defined(Q_OS_MAC) @@ -1434,22 +1440,33 @@ int QgsApplication::systemMemorySizeMb() memoryStatus.dwLength = sizeof( MEMORYSTATUSEX ); if ( GlobalMemoryStatusEx( &memoryStatus ) ) { - return memoryStatus.ullTotalPhys / ( 1024 * 1024 ); + return memoryStatus.ullTotalPhys / megabyte; } else { return -1; } #elif defined(Q_OS_LINUX) - constexpr int megabyte = 1024 * 1024; struct sysinfo si; sysinfo( &si ); return si.totalram / megabyte; -#elif defined(Q_OS_FREEBSD) - return -1; -#elif defined(Q_OS_OPENBSD) - return -1; -#elif defined(Q_OS_NETBSD) +#elif defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_NETBSD) + unsigned long physmem; + size_t size = sizeof( physmem ); +#ifdef HW_PHYSMEM64 + // NetBSD and OpenBSD have HW_PHYSMEM and HW_PHYSMEM64 + // Use 64 bit version + int mib[2] = {CTL_HW, HW_PHYSMEM64}; +#else + // For FreeBSD + int mib[2] = {CTL_HW, HW_PHYSMEM}; +#endif + + if ( sysctl( mib, 2, &physmem, &size, nullptr, 0 ) == 0 ) + { + return physmem / megabyte; + } + return -1; #elif defined(Q_OS_UNIX) return -1; diff --git a/src/core/qgsapplication.h b/src/core/qgsapplication.h index 71b07730a3d2..7d7454295726 100644 --- a/src/core/qgsapplication.h +++ b/src/core/qgsapplication.h @@ -475,7 +475,7 @@ class CORE_EXPORT QgsApplication : public QApplication * * \since QGIS 3.26 */ - static int systemMemorySizeMb(); + static unsigned long systemMemorySizeMb(); /** * Returns the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "external" (for external CLI scripts). diff --git a/src/core/qgsimagecache.cpp b/src/core/qgsimagecache.cpp index 1cef481339d0..7b9576240eb7 100644 --- a/src/core/qgsimagecache.cpp +++ b/src/core/qgsimagecache.cpp @@ -102,7 +102,7 @@ QgsImageCache::QgsImageCache( QObject *parent ) } else { - const int sysMemory = QgsApplication::systemMemorySizeMb(); + const unsigned long sysMemory = QgsApplication::systemMemorySizeMb(); if ( sysMemory > 0 ) { if ( sysMemory >= 32000 ) // 32 gb RAM (or more) = 500mb cache size