Reputation: 88
I'm trying to use the (fairly new) GetLogicalProcessorInformationEx function in Windows. The ReturnLength it gives isn't making sense.
The older GetLogicalProcessorInformation gives reasonable results...
ReturnLength = 0;
Result = GetLogicalProcessorInformation(NULL, &ReturnLength);
printf("GLPI (%d): %d %d\n",
Result,
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION),
ReturnLength);
Here's the output (2-core, 64-bit, Win7 box): GLPI (0): 32 416
In other words, the function will populate the buffer I pass with 416/32=13 SYSTEM_LOGICAL_PROCESSOR_INFORMATION structures.
For GetLogicalProcessorInformationEx, here's my call...
ReturnLength = 0;
Result = GetLogicalProcessorInformationEx(RelationProcessorCore,
NULL, &ReturnLength);
printf("GLPIX (%d): %d %d %d\n",
Result,
sizeof(PROCESSOR_RELATIONSHIP),
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX),
ReturnLength);
Here's the output (2-core, 64-bit, Win7 box): GLPIX (0): 40 80 96
The Microsoft docs (http://msdn.microsoft.com/en-us/library/windows/desktop/dd405488(v=vs.85).aspx) indicate that the function will return either PROCESSOR_RELATIONSHIP or SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX structures, depending on the value of the first argument. ReturnLength suggests it isn't going to return either, though - 96 isn't divisible by sizeof(PROCESSOR_RELATIONSHIP) or sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX).
I also tried RelationAll for the first argument, and that gave a ReturnLength of 768 - also not a multiple or 40 or 80.
Can anyone shed any light?
Upvotes: 3
Views: 702
Reputation: 942030
You'll need to trust what the function returns you. Necessarily so, the structures in the union have an unpredictable size. Particularly this member of PROCESSOR_RELATIONSHIP:
GROUP_AFFINITY GroupMask[ANYSIZE_ARRAY];
The ANYSIZE_ARRAY macro is the hint, that says that the size of the GroupMask array is variable and depends on the value of the GroupCount member. Using sizeof on the structure never gives you the correct size, it will be too low. Be sure to use the returned size to allocate the storage for the struct, like this:
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* buf =
(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(ReturnLength);
This pattern is otherwise common in C and the winapi.
Upvotes: 1