Optimise windows version checking

Added GetNtMinorVersion() and IsAtleastWindows8p1() macros which get nt version info from the peb.
This commit is contained in:
Joshua Wierenga 2022-10-03 18:02:54 +11:00
parent 5d3ed30af1
commit 1e04918663
2 changed files with 11 additions and 2 deletions

View file

@ -9,6 +9,8 @@ bool32 GetVersionEx(struct NtOsVersionInfo *lpVersionInformation);
#if defined(__GCC_ASM_FLAG_OUTPUTS__) && !defined(__STRICT_ANSI__) #if defined(__GCC_ASM_FLAG_OUTPUTS__) && !defined(__STRICT_ANSI__)
#define IsAtLeastWindows10() (GetNtMajorVersion() >= 10) #define IsAtLeastWindows10() (GetNtMajorVersion() >= 10)
#define IsAtleastWindows8p1() \
(GetNtMajorVersion() > 6 || (GetNtMajorVersion() == 6 && GetNtMinorVersion() == 3))
#define GetNtMajorVersion() \ #define GetNtMajorVersion() \
({ \ ({ \
uintptr_t __x; \ uintptr_t __x; \
@ -17,6 +19,14 @@ bool32 GetVersionEx(struct NtOsVersionInfo *lpVersionInformation);
: "=q"(__x)); \ : "=q"(__x)); \
(unsigned char)__x; \ (unsigned char)__x; \
}) })
#define GetNtMinorVersion() \
({ \
uintptr_t __x; \
asm("mov\t%%gs:96,%q0\r\n" \
"mov\t284(%q0),%b0" \
: "=q"(__x)); \
(unsigned char)__x; \
})
#endif #endif
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_

View file

@ -7,7 +7,6 @@
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#include "libc/thread/tls.h" #include "libc/thread/tls.h"
#include "libc/nt/version.h" #include "libc/nt/version.h"
#include "libc/nt/enum/version.h"
#include "libc/runtime/stack.h" #include "libc/runtime/stack.h"
#include "libc/sysv/consts/ss.h" #include "libc/sysv/consts/ss.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
@ -39,7 +38,7 @@ COSMOPOLITAN_C_START_
#define _kMem(NORMAL, WIN7) \ #define _kMem(NORMAL, WIN7) \
(!IsWindows() || IsAtLeastWindows10() ? NORMAL : WIN7) (!IsWindows() || IsAtLeastWindows10() ? NORMAL : WIN7)
#define _kMemVista(NORMAL, WINVISTA) \ #define _kMemVista(NORMAL, WINVISTA) \
(!IsWindows() || NtGetVersion() >= kNtVersionWindows81 ? NORMAL : WINVISTA) (!IsWindows() || IsAtleastWindows8p1() ? NORMAL : WINVISTA)
struct MemoryInterval { struct MemoryInterval {
int x; int x;