Skip to content

Commit e26de23

Browse files
hsnovelslouken
hsnovel
authored andcommitted
add failsafe for querying cache line size on linux
1 parent 1512013 commit e26de23

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/cpuinfo/SDL_cpuinfo.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
#endif
8686
#endif
8787

88+
#if defined (__FreeBSD__)
89+
#include <sys/param.h>
90+
#endif
91+
8892
#if defined(__ANDROID__) && defined(__arm__) && !defined(HAVE_GETAUXVAL)
8993
#include <cpu-features.h>
9094
#endif
@@ -891,21 +895,37 @@ static const char *SDL_GetCPUName(void)
891895
int SDL_GetCPUCacheLineSize(void)
892896
{
893897
const char *cpuType = SDL_GetCPUType();
898+
int cacheline_size = SDL_CACHELINE_SIZE; /* initial guess */
894899
int a, b, c, d;
895900
(void)a;
896901
(void)b;
897902
(void)c;
898903
(void)d;
899904
if (SDL_strcmp(cpuType, "GenuineIntel") == 0 || SDL_strcmp(cpuType, "CentaurHauls") == 0 || SDL_strcmp(cpuType, " Shanghai ") == 0) {
900905
cpuid(0x00000001, a, b, c, d);
901-
return ((b >> 8) & 0xff) * 8;
906+
cacheline_size = ((b >> 8) & 0xff) * 8;
902907
} else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0 || SDL_strcmp(cpuType, "HygonGenuine") == 0) {
903908
cpuid(0x80000005, a, b, c, d);
904-
return c & 0xff;
909+
cacheline_size = c & 0xff;
905910
} else {
906-
/* Just make a guess here... */
907-
return SDL_CACHELINE_SIZE;
911+
#if defined(HAVE_SYSCONF) && defined(_SC_LEVEL1_DCACHE_LINESIZE)
912+
if ((cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)) > 0) {
913+
return cacheline_size;
914+
}
915+
#endif
916+
#if defined(__LINUX__)
917+
{
918+
FILE *f = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r");
919+
if (f) {
920+
fscanf(f, "%d", &cacheline_size);
921+
fclose(f);
922+
}
923+
}
924+
#elif defined(__FreeBSD__) && defined(CACHE_LINE_SIZE)
925+
cacheline_size = CACHE_LINE_SIZE;
926+
#endif
908927
}
928+
return cacheline_size;
909929
}
910930

911931
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;

0 commit comments

Comments
 (0)