changeset 54027:32c7f0e32819

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.
author Kim F. Storm <storm@cua.dk>
date Mon, 16 Feb 2004 23:13:13 +0000
parents 8fa7ff960e1c
children 77c2c4eceb79
files src/keyboard.c
diffstat 1 files changed, 29 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/keyboard.c	Mon Feb 16 23:11:58 2004 +0000
+++ b/src/keyboard.c	Mon Feb 16 23:13:13 2004 +0000
@@ -6556,10 +6556,13 @@
 
 #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 @@
 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 @@
 
       /* 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 @@
 	    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 @@
 	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 @@
   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.  */