]> code.delx.au - gnu-emacs/commitdiff
Prefer xpalloc to doubling buffers by hand
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 8 Nov 2015 07:52:17 +0000 (23:52 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 8 Nov 2015 07:52:57 +0000 (23:52 -0800)
* src/lread.c (grow_read_buffer): New function, which uses xpalloc.
(read1): Use it for simplicity.
* src/macros.c (store_kbd_macro_char):
* src/minibuf.c (read_minibuf_noninteractive):
* src/term.c (encode_terminal_code):
* src/xrdb.c (magic_db):
Prefer xpalloc to growing buffers by hand.
This doesn’t fix any bugs, but simplifies the code a bit.

src/lread.c
src/macros.c
src/minibuf.c
src/term.c
src/xrdb.c

index 7c891f9954f6794035fcf4fac82a7671c8f3f172..c4456f37f6dd1c18ef3a6c96951e74daaf0f7e5a 100644 (file)
@@ -2120,6 +2120,15 @@ read0 (Lisp_Object readcharfun)
 static ptrdiff_t read_buffer_size;
 static char *read_buffer;
 
+/* Grow the read buffer by at least MAX_MULTIBYTE_LENGTH bytes.  */
+
+static void
+grow_read_buffer (void)
+{
+  read_buffer = xpalloc (read_buffer, &read_buffer_size,
+                        MAX_MULTIBYTE_LENGTH, -1, 1);
+}
+
 /* Read a \-escape sequence, assuming we already read the `\'.
    If the escape sequence forces unibyte, return eight-bit char.  */
 
@@ -2985,10 +2994,7 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
            if (end - p < MAX_MULTIBYTE_LENGTH)
              {
                ptrdiff_t offset = p - read_buffer;
-               if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
-                 memory_full (SIZE_MAX);
-               read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
-               read_buffer_size *= 2;
+               grow_read_buffer ();
                p = read_buffer + offset;
                end = read_buffer + read_buffer_size;
              }
@@ -3119,10 +3125,7 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
              if (end - p < MAX_MULTIBYTE_LENGTH)
                {
                  ptrdiff_t offset = p - read_buffer;
-                 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
-                   memory_full (SIZE_MAX);
-                 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
-                 read_buffer_size *= 2;
+                 grow_read_buffer ();
                  p = read_buffer + offset;
                  end = read_buffer + read_buffer_size;
                }
@@ -3149,10 +3152,7 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
          if (p == end)
            {
              ptrdiff_t offset = p - read_buffer;
-             if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
-               memory_full (SIZE_MAX);
-             read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
-             read_buffer_size *= 2;
+             grow_read_buffer ();
              p = read_buffer + offset;
              end = read_buffer + read_buffer_size;
            }
index d963838069bf0929f2a482e8d5a2ceb9876db212..7c6ab2efc306542311198c1dfbdb28d620429948 100644 (file)
@@ -184,16 +184,11 @@ store_kbd_macro_char (Lisp_Object c)
     {
       if (kb->kbd_macro_ptr - kb->kbd_macro_buffer == kb->kbd_macro_bufsize)
        {
-         ptrdiff_t ptr_offset, end_offset, nbytes;
-
-         ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer;
-         end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer;
-         if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *kb->kbd_macro_buffer / 2
-             < kb->kbd_macro_bufsize)
-           memory_full (SIZE_MAX);
-         nbytes = kb->kbd_macro_bufsize * (2 * sizeof *kb->kbd_macro_buffer);
-         kb->kbd_macro_buffer = xrealloc (kb->kbd_macro_buffer, nbytes);
-         kb->kbd_macro_bufsize *= 2;
+         ptrdiff_t ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer;
+         ptrdiff_t end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer;
+         kb->kbd_macro_buffer = xpalloc (kb->kbd_macro_buffer,
+                                         &kb->kbd_macro_bufsize,
+                                         1, -1, sizeof *kb->kbd_macro_buffer);
          kb->kbd_macro_ptr = kb->kbd_macro_buffer + ptr_offset;
          kb->kbd_macro_end = kb->kbd_macro_buffer + end_offset;
        }
index 31b69461bde6f80a561813f0d94ccb8057a79ff1..727a70b166fe83b7f99a21d54c0cd480d0d3eea5 100644 (file)
@@ -229,12 +229,7 @@ read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
          if (hide_char)
            fprintf (stdout, "%c", hide_char);
          if (len == size)
-           {
-             if (STRING_BYTES_BOUND / 2 < size)
-               memory_full (SIZE_MAX);
-             size *= 2;
-             line = xrealloc (line, size);
-           }
+           line = xpalloc (line, &size, 1, -1, sizeof *line);
          line[len++] = c;
        }
     }
index 9b1e7cad4b2ddec192d5363a161b59bb9bc5c9c1..245712ecfc4aa36f41e40088299032d4d77957bf 100644 (file)
@@ -537,10 +537,10 @@ encode_terminal_code (struct glyph *src, int src_len,
   required = src_len;
   required *= MAX_MULTIBYTE_LENGTH;
   if (encode_terminal_src_size < required)
-    {
-      encode_terminal_src = xrealloc (encode_terminal_src, required);
-      encode_terminal_src_size = required;
-    }
+    encode_terminal_src = xpalloc (encode_terminal_src,
+                                  &encode_terminal_src_size,
+                                  required - encode_terminal_src_size,
+                                  -1, sizeof *encode_terminal_src);
 
   charset_list = coding_charset_list (coding);
 
index ce6e7d21edb15c9858a9f0f557382574d375473c..10bc76986e6b96379891d410367fbbbb3148321f 100644 (file)
@@ -177,12 +177,8 @@ magic_db (const char *string, ptrdiff_t string_len, const char *class,
 
       /* Do we have room for this component followed by a '\0'?  */
       if (path_size - path_len <= next_len)
-       {
-         if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
-           memory_full (SIZE_MAX);
-         path_size = (path_len + next_len + 1) * 2;
-         path = xrealloc (path, path_size);
-       }
+       path = xpalloc (path, &path_size, path_len - path_size + next_len + 1,
+                       -1, sizeof *path);
 
       memcpy (path + path_len, next, next_len);
       path_len += next_len;