Limit configurable CPU count to the number of detected CPU cores.

This commit is contained in:
Roman D 2023-08-21 22:24:13 +02:00
parent dfabad37d9
commit 908b8ca0b3
3 changed files with 10 additions and 4 deletions

View file

@ -1004,8 +1004,9 @@ int main(int argc, char *argv[]) {
return 1;
}
long int ncpus = strtol(argv[i + 1], NULL, 10);
if (ncpus < 0 || ncpus >= INT_MAX) {
PrintAndLogEx(ERR, _RED_("ERROR:") " invalid number of CPU cores: --ncpu " _YELLOW_("%s") "\n", argv[i + 1]);
const int detected_cpus = detect_num_CPUs();
if (ncpus < 0 || ncpus >= detected_cpus) {
PrintAndLogEx(ERR, _RED_("ERROR:") " invalid number of CPU cores: --ncpu " _YELLOW_("%s") " (available: %d)\n", argv[i + 1], detected_cpus);
return 1;
}
g_numCPUs = ncpus;

View file

@ -1079,12 +1079,16 @@ uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor) {
return result;
}
// determine number of logical CPU cores (use for multithreaded functions)
int num_CPUs(void) {
if (g_numCPUs > 0) {
return g_numCPUs;
}
return detect_num_CPUs();
}
// determine number of logical CPU cores (use for multithreaded functions)
int detect_num_CPUs(void) {
#if defined(_WIN32)
#include <sysinfoapi.h>
SYSTEM_INFO sysinfo;

View file

@ -131,7 +131,8 @@ void wiegand_add_parity_swapped(uint8_t *target, uint8_t *source, uint8_t length
uint32_t PackBits(uint8_t start, uint8_t len, const uint8_t *bits);
uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor);
int num_CPUs(void); // number of logical CPUs
int num_CPUs(void);
int detect_num_CPUs(void); // number of logical CPUs
void str_lower(char *s); // converts string to lower case
void str_upper(char *s); // converts string to UPPER case