diff --git a/libguile/r6rs-ports.c b/libguile/r6rs-ports.c index ffa1e1b2b..034564cfd 100644 --- a/libguile/r6rs-ports.c +++ b/libguile/r6rs-ports.c @@ -1,4 +1,4 @@ -/* Copyright 2009-2011,2013-2015,2018-2019,2023,2024 +/* Copyright 2009-2011,2013-2015,2018-2019,2023,2024,2025 Free Software Foundation, Inc. This file is part of Guile. @@ -515,6 +515,26 @@ SCM_DEFINE (scm_unget_bytevector, "unget-bytevector", 2, 2, 0, #undef FUNC_NAME + + + +static void* +grow_byte_buffer (void *p, size_t old_size, size_t new_size) +{ + void *new_p = scm_gc_malloc_pointerless (new_size, NULL); + memcpy (new_p, p, old_size); + return new_p; +} + +static void* +shrink_byte_buffer (void *p, size_t old_size, size_t new_size) +{ + void *new_p = scm_gc_malloc_pointerless (new_size, NULL); + memcpy (new_p, p, new_size); + return new_p; +} + + /* Bytevector output port. */ @@ -581,8 +601,7 @@ bytevector_output_port_buffer_grow (scm_t_bytevector_output_port_buffer *buf, if (INT_ADD_OVERFLOW (buf->total_len, buf->total_len)) scm_num_overflow ("bytevector_output_port_buffer_grow"); new_size = MAX (min_size, buf->total_len * 2); - new_buf = scm_gc_realloc ((void *) buf->buffer, buf->total_len, - new_size, SCM_GC_BYTEVECTOR_OUTPUT_PORT); + new_buf = grow_byte_buffer (buf->buffer, buf->total_len, new_size); } else { @@ -696,10 +715,9 @@ SCM_SMOB_APPLY (bytevector_output_port_procedure, { if (result_buf.total_len > result_buf.len) /* Shrink the buffer. */ - result_buf.buffer = scm_gc_realloc ((void *) result_buf.buffer, - result_buf.total_len, - result_buf.len, - SCM_GC_BYTEVECTOR_OUTPUT_PORT); + result_buf.buffer = shrink_byte_buffer (result_buf.buffer, + result_buf.total_len, + result_buf.len); bv = scm_c_take_gc_bytevector ((signed char *) result_buf.buffer, result_buf.len, SCM_BOOL_F);