use windows::Win32::System::SystemInformation::{
GetLogicalProcessorInformationEx, RelationGroup, SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX,
};
use grob::{GrowForSmallBinary, GrowableBuffer, RvIsError, StackBuffer, ToResult, WriteBuffer};
fn common(initial_buffer: &mut dyn WriteBuffer) -> Result<(), Box<dyn std::error::Error>> {
let grow_strategy = GrowForSmallBinary::new();
let mut growable_buffer = GrowableBuffer::<
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX,
*mut SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX,
>::new(initial_buffer, &grow_strategy);
loop {
let mut argument = growable_buffer.argument();
let rv = RvIsError::new(unsafe {
GetLogicalProcessorInformationEx(
RelationGroup,
Some(argument.pointer()),
argument.size(),
)
});
let fill_buffer_action = rv.to_result(&mut argument)?;
if argument.apply(fill_buffer_action) {
break;
}
}
let frozen_buffer = growable_buffer.freeze();
if let Some(p) = frozen_buffer.pointer() {
let r = unsafe { (*p).Relationship };
println!("Relationship = {:?}", r); println!("Size = {:?}", unsafe { (*p).Size });
println!("MaximumGroupCount = {:?}", unsafe {
(*p).Anonymous.Group.MaximumGroupCount
});
println!("ActiveGroupCount = {:?}", unsafe {
(*p).Anonymous.Group.ActiveGroupCount
});
println!("ActiveProcessorCount = {:?}", unsafe {
(*p).Anonymous.Group.GroupInfo[0].ActiveProcessorCount
});
println!("MaximumProcessorCount = {:?}", unsafe {
(*p).Anonymous.Group.GroupInfo[0].MaximumProcessorCount
});
println!();
}
Ok(())
}
fn just_heap_buffer() -> Result<(), Box<dyn std::error::Error>> {
let mut initial_buffer = StackBuffer::<0>::new();
common(&mut initial_buffer)
}
fn start_with_stack_buffer() -> Result<(), Box<dyn std::error::Error>> {
let mut initial_buffer = StackBuffer::<1024>::new();
common(&mut initial_buffer)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!();
start_with_stack_buffer()?;
println!();
just_heap_buffer()?;
println!();
println!();
Ok(())
}