]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Merge realloc fixes from trunk to stable branch.
authorTom Hughes <tom@compton.nu>
Thu, 6 Oct 2005 09:06:59 +0000 (09:06 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 6 Oct 2005 09:06:59 +0000 (09:06 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/branches/VALGRIND_3_0_BRANCH@4876

helgrind/hg_main.c
massif/ms_main.c
memcheck/mac_malloc_wrappers.c

index 0b35dbe1f21106fb63b690e9e27d418deb39407a..cfed7ab041e10702f0816dfffd770592777a3441 100644 (file)
@@ -1964,7 +1964,6 @@ static void* hg_realloc ( ThreadId tid, void* p, SizeT new_size )
 {
    HG_Chunk  *hc;
    HG_Chunk **prev_chunks_next_ptr;
-   Int        i;
 
    /* First try and find the block. */
    hc = (HG_Chunk*)VG_(HT_get_node) ( hg_malloc_list, (UWord)p,
@@ -1992,22 +1991,23 @@ static void* hg_realloc ( ThreadId tid, void* p, SizeT new_size )
       /* Get new memory */
       p_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size);
 
-      /* First half kept and copied, second half new */
-      copy_address_range_state( (Addr)p, p_new, hc->size );
-      hg_new_mem_heap ( p_new+hc->size, new_size-hc->size,
-                        /*inited*/False );
+      if (p_new) {
+         /* First half kept and copied, second half new */
+         copy_address_range_state( (Addr)p, p_new, hc->size );
+         hg_new_mem_heap ( p_new+hc->size, new_size-hc->size,
+                           /*inited*/False );
 
-      /* Copy from old to new */
-      for (i = 0; i < hc->size; i++)
-         ((UChar*)p_new)[i] = ((UChar*)p)[i];
+         /* Copy from old to new */
+         VG_(memcpy)((void *)p_new, p, hc->size);
 
-      /* Free old memory */
-      die_and_free_mem ( tid, hc, prev_chunks_next_ptr );
+         /* Free old memory */
+         die_and_free_mem ( tid, hc, prev_chunks_next_ptr );
 
-      /* this has to be after die_and_free_mem, otherwise the
-         former succeeds in shorting out the new block, not the
-         old, in the case when both are on the same list.  */
-      add_HG_Chunk ( tid, p_new, new_size );
+         /* this has to be after die_and_free_mem, otherwise the
+            former succeeds in shorting out the new block, not the
+            old, in the case when both are on the same list.  */
+         add_HG_Chunk ( tid, p_new, new_size );
+      }
 
       return (void*)p_new;
    }  
index 95d1566169395f77c3770d0cd39cac3436331510..d340e36d65d834f4312930fd6d45b58dd37db906 100644 (file)
@@ -823,24 +823,28 @@ static void* ms_realloc ( ThreadId tid, void* p_old, SizeT new_size )
       // new size is bigger;  make new block, copy shared contents, free old
       p_new = VG_(cli_malloc)(VG_(clo_alignment), new_size);
 
-      for (i = 0; i < old_size; i++)
-         ((UChar*)p_new)[i] = ((UChar*)p_old)[i];
+      if (p_new) {
+         for (i = 0; i < old_size; i++)
+            ((UChar*)p_new)[i] = ((UChar*)p_old)[i];
 
-      VG_(cli_free)(p_old);
+         VG_(cli_free)(p_old);
+      }
    }
-   
-   old_where = hc->where;
-   new_where = get_XCon( tid, /*custom_malloc*/False);
 
-   // Update HP_Chunk
-   hc->data  = (Addr)p_new;
-   hc->size  = new_size;
-   hc->where = new_where;
+   if (p_new) {
+      old_where = hc->where;
+      new_where = get_XCon( tid, /*custom_malloc*/False);
 
-   // Update XPt curr_space fields
-   if (clo_heap) {
-      if (0 != old_size) update_XCon(old_where, -old_size);
-      if (0 != new_size) update_XCon(new_where,  new_size);
+      // Update HP_Chunk
+      hc->data  = (Addr)p_new;
+      hc->size  = new_size;
+      hc->where = new_where;
+
+      // Update XPt curr_space fields
+      if (clo_heap) {
+         if (0 != old_size) update_XCon(old_where, -old_size);
+         if (0 != new_size) update_XCon(new_where,  new_size);
+      }
    }
 
    // If block has moved, have to remove and reinsert in the malloclist
index 0d161d536977e45b7680a8b136b29cac5c3c3ef5..4b28d857a03c7084a075c4c3f01af5162a9eda9d 100644 (file)
@@ -421,25 +421,27 @@ void* MAC_(realloc) ( ThreadId tid, void* p, SizeT new_size )
       /* Get new memory */
       p_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size);
 
-      /* First half kept and copied, second half new, 
-         red zones as normal */
-      MAC_(ban_mem_heap) ( p_new-MAC_MALLOC_REDZONE_SZB, MAC_MALLOC_REDZONE_SZB );
-      MAC_(copy_mem_heap)( (Addr)p, p_new, mc->size );
-      MAC_(new_mem_heap) ( p_new+mc->size, new_size-mc->size, /*inited*/False );
-      MAC_(ban_mem_heap) ( p_new+new_size, MAC_MALLOC_REDZONE_SZB );
-
-      /* Copy from old to new */
-      for (i = 0; i < mc->size; i++)
-         ((UChar*)p_new)[i] = ((UChar*)p)[i];
-
-      /* Free old memory */
-      die_and_free_mem ( tid, mc, prev_chunks_next_ptr, MAC_MALLOC_REDZONE_SZB );
-
-      /* this has to be after die_and_free_mem, otherwise the
-         former succeeds in shorting out the new block, not the
-         old, in the case when both are on the same list.  */
-      add_MAC_Chunk ( tid, p_new, new_size, 
-                           MAC_AllocMalloc, MAC_(malloc_list) );
+      if (p_new) {
+         /* First half kept and copied, second half new, 
+            red zones as normal */
+         MAC_(ban_mem_heap) ( p_new-MAC_MALLOC_REDZONE_SZB, MAC_MALLOC_REDZONE_SZB );
+         MAC_(copy_mem_heap)( (Addr)p, p_new, mc->size );
+         MAC_(new_mem_heap) ( p_new+mc->size, new_size-mc->size, /*inited*/False );
+         MAC_(ban_mem_heap) ( p_new+new_size, MAC_MALLOC_REDZONE_SZB );
+
+         /* Copy from old to new */
+         for (i = 0; i < mc->size; i++)
+            ((UChar*)p_new)[i] = ((UChar*)p)[i];
+
+         /* Free old memory */
+         die_and_free_mem ( tid, mc, prev_chunks_next_ptr, MAC_MALLOC_REDZONE_SZB );
+
+         /* this has to be after die_and_free_mem, otherwise the
+            former succeeds in shorting out the new block, not the
+            old, in the case when both are on the same list.  */
+         add_MAC_Chunk ( tid, p_new, new_size, 
+                         MAC_AllocMalloc, MAC_(malloc_list) );
+      }
 
       VGP_POPCC(VgpCliMalloc);
       return (void*)p_new;