From: Ivan Maidanski Date: Tue, 1 Mar 2016 21:44:13 +0000 (+0300) Subject: Remove code duplication in GC_realloc X-Git-Tag: gc7_6_0~55 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6671b1833eee46984c4683c6da54887de314fb7c;p=gc Remove code duplication in GC_realloc * mallocx.c (GC_realloc): Avoid code duplication for shrink and grow cases. --- diff --git a/mallocx.c b/mallocx.c index f758bf8b..38fe3c47 100644 --- a/mallocx.c +++ b/mallocx.c @@ -86,8 +86,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 */ @@ -127,31 +128,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)