Remove code duplication in GC_realloc
authorIvan Maidanski <ivmai@mail.ru>
Tue, 1 Mar 2016 21:44:13 +0000 (00:44 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Tue, 1 Mar 2016 21:44:13 +0000 (00:44 +0300)
* mallocx.c (GC_realloc): Avoid code duplication for shrink and grow
cases.

mallocx.c

index f758bf8..38fe3c4 100644 (file)
--- 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)