Commit 8a8613bc authored by Paul Eggert's avatar Paul Eggert
Browse files

Prefer xpalloc to doubling buffers by hand

* 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.
parent 6ea4ff5a
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
}
......
......@@ -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);
......
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment