From 952c05db39a3956f0c7cf0d9505726efc115f031 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Wed, 2 Mar 2016 00:44:13 +0300 Subject: [PATCH] Remove code duplication in GC_realloc * mallocx.c (GC_realloc): Avoid code duplication for shrink and grow cases. --- mallocx.c | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/mallocx.c b/mallocx.c index f994a624..80118e72 100644 --- a/mallocx.c +++ b/mallocx.c @@ -77,8 +77,9 @@ GC_API void * GC_CALL GC_realloc(void * p, size_t lb) { struct hblk * h; hdr * hhdr; - size_t sz; /* Current size in bytes */ - size_t orig_sz; /* Original sz in bytes */ + void * result; + size_t sz; /* Current size in bytes */ + size_t orig_sz; /* Original sz in bytes */ int obj_kind; if (p == 0) return(GC_malloc(lb)); /* Required by ANSI */ @@ -118,31 +119,20 @@ GC_API void * GC_CALL GC_realloc(void * p, size_t lb) BZERO(((ptr_t)p) + lb, orig_sz - lb); } return(p); - } else { - /* shrink */ - void * result = - GC_generic_or_special_malloc((word)lb, obj_kind); - - if (result == 0) return(0); - /* Could also return original object. But this */ - /* gives the client warning of imminent disaster. */ - BCOPY(p, result, lb); -# ifndef IGNORE_FREE - GC_free(p); -# endif - return(result); } - } else { - /* grow */ - void * result = GC_generic_or_special_malloc((word)lb, obj_kind); - - if (result == 0) return(0); - BCOPY(p, result, sz); -# ifndef IGNORE_FREE - GC_free(p); -# endif - return(result); + /* shrink */ + sz = lb; + } + result = GC_generic_or_special_malloc((word)lb, obj_kind); + if (result != NULL) { + /* In case of shrink, it could also return original object. */ + /* But this gives the client warning of imminent disaster. */ + BCOPY(p, result, sz); +# ifndef IGNORE_FREE + GC_free(p); +# endif } + return result; } # if defined(REDIRECT_MALLOC) && !defined(REDIRECT_REALLOC) -- 2.40.0