Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefetching buffer size for Windows console reads. #144

Merged
merged 2 commits into from
May 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions crossterm_winapi/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ use super::{is_true, Coord, Handle, HandleType, WindowPositions};
use std::io::{self, Error, Result};
use std::str;

use std::mem::zeroed;
use winapi::ctypes::c_void;
use winapi::shared::minwindef::DWORD;
use winapi::shared::ntdef::NULL;
use winapi::um::consoleapi::ReadConsoleInputW;
use winapi::um::consoleapi::{GetNumberOfConsoleInputEvents, ReadConsoleInputW, WriteConsoleW};
use winapi::um::{
consoleapi::WriteConsoleW,
wincon::{
FillConsoleOutputAttribute, FillConsoleOutputCharacterA, GetLargestConsoleWindowSize,
SetConsoleTextAttribute, SetConsoleWindowInfo, COORD, INPUT_RECORD, SMALL_RECT,
Expand Down Expand Up @@ -165,21 +163,27 @@ impl Console {
}

pub fn read_console_input(&self) -> Result<(u32, Vec<InputRecord>)> {
// Large buffers can overflow max heap size (?) and lead to errno 8
// "Not enough storage is available to process this command."
// It can be reproduced at Windows 7 i686 with `0x1000` buffer size.
let mut buf: [INPUT_RECORD; 0x800] = unsafe { zeroed() };
let mut buf_len: DWORD = 0;
if !is_true(unsafe { GetNumberOfConsoleInputEvents(*self.handle, &mut buf_len) }) {
return Err(Error::last_os_error());
}

// Fast-skipping all the code below if there is nothing to read at all
if buf_len == 0 {
return Ok((0, vec![]));
}

let mut buf: Vec<INPUT_RECORD> = Vec::with_capacity(buf_len as usize);
let mut size = 0;

if !is_true(unsafe {
ReadConsoleInputW(
*self.handle,
buf.as_mut_ptr(),
buf.len() as DWORD,
&mut size,
)
ReadConsoleInputW(*self.handle, buf.as_mut_ptr(), buf_len, &mut size)
}) {
return Err(Error::last_os_error());
} else {
unsafe {
buf.set_len(size as usize);
}
}

Ok((
Expand Down