18
18
#define WIN32_LEAN_AND_MEAN
19
19
#define NOMINMAX
20
20
#include < windows.h>
21
+ #include < intrin.h>
21
22
#include < fcntl.h>
22
23
#include < io.h>
23
24
#define CP_UTF8 65001
@@ -53,14 +54,13 @@ int32_t get_num_physical_cores() {
53
54
return num_physical_cores;
54
55
}
55
56
#elif defined(_WIN32)
56
- SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buffer = nullptr ;
57
- DWORD length = 0 ;
58
-
59
57
// Call GetLogicalProcessorInformationEx with a nullptr buffer to get the required buffer length
58
+ DWORD length = 0 ;
60
59
GetLogicalProcessorInformationEx (RelationAll, nullptr , &length);
61
60
62
61
// Allocate memory for the buffer
63
- buffer = reinterpret_cast <SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>(new char [length]);
62
+ std::unique_ptr<char []> buffer_ptr (new char [length]);
63
+ char * buffer = buffer_ptr.get ();
64
64
65
65
// Things to count
66
66
unsigned int physical_cores = 0 ;
@@ -71,11 +71,14 @@ int32_t get_num_physical_cores() {
71
71
unsigned int logical_efficiency_cores = 0 ;
72
72
73
73
// Call GetLogicalProcessorInformationEx again with the allocated buffer
74
- if (GetLogicalProcessorInformationEx (RelationAll, buffer, &length)) {
74
+ if (GetLogicalProcessorInformationEx (
75
+ RelationAll,
76
+ reinterpret_cast <SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>(buffer),
77
+ &length)) {
75
78
DWORD offset = 0 ;
76
79
77
80
while (offset < length) {
78
- auto info = reinterpret_cast <SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>(reinterpret_cast < char *>( buffer) + offset);
81
+ auto info = reinterpret_cast <SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>(buffer + offset);
79
82
80
83
if (info->Relationship == RelationProcessorCore) {
81
84
physical_cores += info->Processor .GroupCount ;
@@ -111,8 +114,6 @@ int32_t get_num_physical_cores() {
111
114
fprintf (stderr, " Failed to get processor information. Error: %u\n " , GetLastError ());
112
115
}
113
116
114
- delete[] buffer;
115
-
116
117
if (physical_performance_cores > 0 ) {
117
118
return static_cast <int32_t >(physical_performance_cores);
118
119
} else if (physical_cores > 0 ) {
0 commit comments