From 9940231136a3a88bce8439d20e19928dbbc2edd1 Mon Sep 17 00:00:00 2001 From: "Kim F. Storm" Date: Mon, 16 Feb 2004 23:13:13 +0000 Subject: [PATCH] Rework previous change; it didn't consider that the buf array was allocated on the stack. (prev_read): Remove variable. (read_avail_input_buf): New static event buffer array. (in_read_avail_input): New static variable to avoid re-entrancy. (read_avail_input): Change buf to pinter to read_avail_input_buf. Use in_read_avail_input to guard against re-entry. Do not initialize read_avail_input_buf here; instead assume it is always cleared on entry. To ensure that, we clear (just) the entries that were used before we return. (init_keyboard): Initialize read_avail_input_buf here. --- src/keyboard.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/keyboard.c b/src/keyboard.c index 3b9447f24b1..22610212c26 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -6556,10 +6556,13 @@ record_asynch_buffer_change () #ifndef VMS -/* This remembers the last number of characters read, so we could - avoid zeroing out the whole struct input_event buf and instead zero - out only its used slots. */ -static int prev_read = KBD_BUFFER_SIZE; +/* We make the read_avail_input buffer static to avoid zeroing out the + whole struct input_event buf on every call. */ +static struct input_event read_avail_input_buf[KBD_BUFFER_SIZE]; + +/* I don't know whether it is necessary, but make read_avail_input + re-entrant. */ +static int in_read_avail_input = 0; /* Read any terminal input already buffered up by the system into the kbd_buffer, but do not wait. @@ -6577,12 +6580,14 @@ static int read_avail_input (expected) int expected; { - struct input_event buf[KBD_BUFFER_SIZE]; + struct input_event *buf = read_avail_input_buf; register int i; int nread; - for (i = 0; i < prev_read; i++) - EVENT_INIT (buf[i]); + /* Trivial hack to make read_avail_input re-entrant. */ + if (in_read_avail_input) + return 0; + in_read_avail_input = 1; if (read_socket_hook) /* No need for FIONREAD or fcntl; just say don't wait. */ @@ -6597,12 +6602,12 @@ read_avail_input (expected) /* Determine how many characters we should *try* to read. */ #ifdef WINDOWSNT - return (prev_read = 0); + return (in_read_avail_input = 0); #else /* not WINDOWSNT */ #ifdef MSDOS n_to_read = dos_keysns (); if (n_to_read == 0) - return (prev_read = 0); + return (in_read_avail_input = 0); #else /* not MSDOS */ #ifdef FIONREAD /* Find out how much input is available. */ @@ -6620,7 +6625,7 @@ read_avail_input (expected) n_to_read = 0; } if (n_to_read == 0) - return (prev_read = 0); + return (in_read_avail_input = 0); if (n_to_read > sizeof cbuf) n_to_read = sizeof cbuf; #else /* no FIONREAD */ @@ -6711,7 +6716,12 @@ read_avail_input (expected) break; } - return (prev_read = nread); + /* Clear used events */ + for (i = 0; i < nread; i++) + EVENT_INIT (buf[i]); + + in_read_avail_input = 0; + return nread; } #endif /* not VMS */ @@ -10541,6 +10551,14 @@ init_keyboard () do_mouse_tracking = Qnil; #endif input_pending = 0; +#ifndef VMS + { + int i; + for (i = 0; i < KBD_BUFFER_SIZE; i++) + EVENT_INIT (read_avail_input_buf[i]); + } +#endif + /* This means that command_loop_1 won't try to select anything the first time through. */ -- 2.39.2