Skip to content

Commit

Permalink
Use GetLogicalProcessorCount() to count CPUs on Windows.
Browse files Browse the repository at this point in the history
Bump version to 0.7.
  • Loading branch information
samr7 committed Jul 8, 2011
1 parent 03f4e05 commit d40577e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ Version 0.6, released July 8 2011:
- Fix calculation of difficulty for multiple prefixes
- When prefixes overlap, output the discarded prefix as
well as the existing prefix with which it overlaps

Version 0.7, released July 8 2011:
- Use GetLogicalProcessorInformation() to count CPUs on Windows,
because GetActiveProcessorCount() is Windows 7 and newer.
6 changes: 2 additions & 4 deletions vanitygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1969,17 +1969,14 @@ read_file(FILE *fp, char ***result, int *rescount)
return ret;
}

#if !defined(_WIN32)
int
count_processors(void)
{
FILE *fp;
char buf[512];
int count = 0;

#if defined(_WIN32)
return GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
#endif

fp = fopen("/proc/cpuinfo", "r");
if (!fp)
return -1;
Expand All @@ -1991,6 +1988,7 @@ count_processors(void)
fclose(fp);
return count;
}
#endif

int
start_threads(void *(*func)(void *), void *arg, int nthreads)
Expand Down
50 changes: 50 additions & 0 deletions winglue.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,56 @@
#define INLINE
#define snprintf _snprintf


int
count_processors(void)
{
typedef BOOL (WINAPI *LPFN_GLPI)(
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
LPFN_GLPI glpi;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL, ptr;
DWORD size = 0, count = 0, pos = 0, i, ret;

glpi = (LPFN_GLPI) GetProcAddress(GetModuleHandle(TEXT("kernel32")),
"GetLogicalProcessorInformation");
if (!glpi)
return -1;

while (1) {
ret = glpi(buffer, &size);
if (ret)
break;
if (buffer)
free(buffer);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return -1;
buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION) malloc(size);
if (!buffer)
return -1;
}

for (ptr = buffer;
(pos + sizeof(*ptr)) <= size;
ptr++, pos += sizeof(*ptr)) {
switch (ptr->Relationship) {
case RelationProcessorCore:
for (i = ptr->ProcessorMask; i != 0; i >>= 1) {
if (i & 1)
count++;
}
count++;
break;
default:
break;
}
}

if (buffer)
free(buffer);
return count;
}


/*
* struct timeval compatibility for Win32
*/
Expand Down

0 comments on commit d40577e

Please sign in to comment.