Avoid malloc/free pairs in emit_static_object

* src/comp.c (emit_static_object): Avoid an malloc/free of
a 1 KiB buffer; just put it on the stack.  Use strnlen+mempcpy
instead of strncpy as there is no need to zero-fill buff.
Use int for values that must fit in int since we are
passing them to gcc_jit_context_new_rvalue_from_int.
This commit is contained in:
Paul Eggert 2026-05-17 22:17:47 -07:00
parent f5c3ddd9ad
commit b9e20e3995

View file

@ -2783,16 +2783,17 @@ emit_static_object (const char *name, Lisp_Object obj)
<https://gcc.gnu.org/ml/jit/2019-q3/msg00013.html>.
Adjust if possible to reduce the number of function calls. */
size_t chunk_size = NILP (Fcomp_libgccjit_version ()) ? 200 : 1024;
char *buff = xmalloc (chunk_size);
char buff[1024];
int chunk_size = NILP (Fcomp_libgccjit_version ()) ? 200 : sizeof buff;
for (ptrdiff_t i = 0; i < len;)
{
strncpy (buff, p, chunk_size);
buff[chunk_size - 1] = 0;
uintptr_t l = strlen (buff);
int l = strnlen (p, chunk_size - 1);
if (l != 0)
{
char *buff_end = mempcpy (buff, p, l);
*buff_end = '\0';
p += l;
i += l;
@ -2836,7 +2837,6 @@ emit_static_object (const char *name, Lisp_Object obj)
NULL));
}
}
xfree (buff);
gcc_jit_block_add_assignment (
block,