From 91731ec385b800431677f6a9da4815e24cbaaa37 Mon Sep 17 00:00:00 2001 From: Tatsuya Kinoshita Date: Thu, 4 Mar 2021 07:03:18 +0900 Subject: Prevent unneeded memory allocation in Strgrow --- Str.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Str.c b/Str.c index 0786ec1..ab8e4bd 100644 --- a/Str.c +++ b/Str.c @@ -288,13 +288,15 @@ Strgrow(Str x) if (x->length + 1 >= newlen) x->length = newlen - 2; } - x->ptr = GC_MALLOC_ATOMIC(newlen); - if (x->ptr == NULL) - exit(1); - x->area_size = newlen; - bcopy((void *)old, (void *)x->ptr, x->length); + if (x->area_size < newlen) { + x->ptr = GC_MALLOC_ATOMIC(newlen); + if (x->ptr == NULL) + exit(1); + x->area_size = newlen; + bcopy((void *)old, (void *)x->ptr, x->length); + GC_free(old); + } x->ptr[x->length] = '\0'; - GC_free(old); } Str -- cgit v1.2.3