cpuinfo: Added SDL_GetSystemPageSize.

Fixes #14201.
This commit is contained in:
Ryan C. Gordon
2025-10-10 17:40:51 -04:00
parent 9d635fe641
commit f7d5bb3c7a
7 changed files with 85 additions and 0 deletions

View File

@@ -1213,6 +1213,65 @@ int SDL_GetSystemRAM(void)
return SDL_SystemRAM;
}
static int SDL_SystemPageSize = -1;
int SDL_GetSystemPageSize(void)
{
if (SDL_SystemPageSize == -1) {
#ifdef SDL_PLATFORM_SYSTEM_PAGE_SIZE_PRIVATE // consoles will define this in a platform-specific internal header.
SDL_SystemPageSize = SDL_PLATFORM_SYSTEM_PAGE_SIZE_PRIVATE;
#endif
#ifdef SDL_PLATFORM_3DS
SDL_SystemPageSize = 4096; // It's an ARM11 CPU; I assume this is 4K.
#endif
#ifdef SDL_PLATFORM_VITA
SDL_SystemPageSize = 4096; // It's an ARMv7 CPU; I assume this is 4K.
#endif
#ifdef SDL_PLATFORM_PS2
SDL_SystemPageSize = 4096; // It's a MIPS R5900 CPU; I assume this is 4K.
#endif
#if defined(HAVE_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
if (SDL_SystemPageSize <= 0) {
#if defined(_SC_PAGE_SIZE)
SDL_SystemPageSize = sysconf(_SC_PAGE_SIZE);
#else
SDL_SystemPageSize = sysconf(_SC_PAGESIZE);
#endif
}
#endif
#if defined(HAVE_SYSCTLBYNAME) && defined(HW_PAGESIZE)
if (SDL_SystemPageSize <= 0) {
// NetBSD, OpenBSD, FreeBSD, Darwin...everything agrees to use HW_PAGESIZE.
int mib[2] = { CTL_HW, HW_PAGESIZE };
int pagesize = 0;
size_t len = sizeof(pagesize);
if (sysctl(mib, 2, &pagesize, &len, NULL, 0) == 0) {
SDL_SystemPageSize = pagesize;
}
}
#endif
#ifdef HAVE_GETPAGESIZE
if (SDL_SystemPageSize <= 0) {
SDL_SystemPageSize = getpagesize();
}
#endif
#if defined(SDL_PLATFORM_WINDOWS)
if (SDL_SystemPageSize <= 0) {
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
SDL_SystemPageSize = (int) sysinfo.dwPageSize;
}
#endif
if (SDL_SystemPageSize < 0) { // in case we got a weird result somewhere, or no better information, force it to 0.
SDL_SystemPageSize = 0; // unknown page size, sorry.
}
}
return SDL_SystemPageSize;
}
size_t SDL_GetSIMDAlignment(void)
{
if (SDL_SIMDAlignment == 0xFFFFFFFF) {

View File

@@ -1264,6 +1264,7 @@ SDL3_0.0.0 {
SDL_LoadPNG;
SDL_SavePNG_IO;
SDL_SavePNG;
SDL_GetSystemPageSize;
# extra symbols go here (don't modify this line)
local: *;
};

View File

@@ -1290,3 +1290,4 @@
#define SDL_LoadPNG SDL_LoadPNG_REAL
#define SDL_SavePNG_IO SDL_SavePNG_IO_REAL
#define SDL_SavePNG SDL_SavePNG_REAL
#define SDL_GetSystemPageSize SDL_GetSystemPageSize_REAL

View File

@@ -1298,3 +1298,4 @@ SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadPNG_IO,(SDL_IOStream *a,bool b),(a,b),retur
SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadPNG,(const char *a),(a),return)
SDL_DYNAPI_PROC(bool,SDL_SavePNG_IO,(SDL_Surface *a,SDL_IOStream *b,bool c),(a,b,c),return)
SDL_DYNAPI_PROC(bool,SDL_SavePNG,(SDL_Surface *a,const char *b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_GetSystemPageSize,(void),(),return)