Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
obey our coding standards and cosmetics
[simgrid.git] / src / xbt / mmalloc / mrealloc.c
index 589e412..753eda8 100644 (file)
@@ -1,6 +1,6 @@
 /* Change the size of a block allocated by `mmalloc'. */
 
-/* Copyright (c) 2010-2014. The SimGrid Team.
+/* Copyright (c) 2010-2018. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -22,7 +22,6 @@ void *mrealloc(xbt_mheap_t mdp, void *ptr, size_t size)
 {
   void *result;
   size_t blocks;
-  size_t oldlimit;
 
   /* Only keep real realloc, and reroute hidden malloc and free to the relevant functions */
   if (size == 0) {
@@ -37,9 +36,7 @@ void *mrealloc(xbt_mheap_t mdp, void *ptr, size_t size)
   if ((char *) ptr < (char *) mdp->heapbase || BLOCK(ptr) > mdp->heapsize) {
     printf("FIXME. Ouch, this pointer is not mine, refusing to proceed (another solution would be to malloc "
            "it instead of reallocing it, see source code)\n");
-    result = mmalloc(mdp, size);
     abort();
-    return result;
   }
 
   size_t requested_size = size; // The amount of memory requested by user, for real
@@ -72,11 +69,9 @@ void *mrealloc(xbt_mheap_t mdp, void *ptr, size_t size)
     if (size <= BLOCKSIZE / 2) { // Full block -> Fragment; no need to optimize for time
 
       result = mmalloc(mdp, size);
-      if (result != NULL) { // useless (mmalloc never returns NULL), but harmless
-        memcpy(result, ptr, requested_size);
-        mfree(mdp, ptr);
-        return (result);
-      }
+      memcpy(result, ptr, requested_size);
+      mfree(mdp, ptr);
+      return (result);
     }
 
     /* Full blocks -> Full blocks; see if we can hold it in place. */
@@ -92,8 +87,7 @@ void *mrealloc(xbt_mheap_t mdp, void *ptr, size_t size)
         mdp->heapinfo[it].busy_block.busy_size = 0;
       }
 
-      mdp->heapinfo[block + blocks].busy_block.size
-        = mdp->heapinfo[block].busy_block.size - blocks;
+      mdp->heapinfo[block + blocks].busy_block.size = mdp->heapinfo[block].busy_block.size - blocks;
       mfree(mdp, ADDRESS(block + blocks));
 
       mdp->heapinfo[block].busy_block.size = blocks;
@@ -115,7 +109,7 @@ void *mrealloc(xbt_mheap_t mdp, void *ptr, size_t size)
            action for obvious reasons. */
       blocks = mdp->heapinfo[block].busy_block.size;
       /* Prevent free from actually returning memory to the system.  */
-      oldlimit = mdp->heaplimit;
+      size_t oldlimit = mdp->heaplimit;
       mdp->heaplimit = 0;
       mfree(mdp, ptr);
       mdp->heaplimit = oldlimit;