Author: chromatic
Date: Fri Jun  1 21:11:19 2007
New Revision: 18757

Modified:
   trunk/include/parrot/memory.h
   trunk/src/gc/memory.c
   trunk/src/mmd.c

Log:
[MMD] Initialize MMD tables when resizing them (Mehmet Yavuz Selim Soyturk, RT 
#43105).

Also adds mem_sys_realloc_zeroed() macro.

Modified: trunk/include/parrot/memory.h
==============================================================================
--- trunk/include/parrot/memory.h       (original)
+++ trunk/include/parrot/memory.h       Fri Jun  1 21:11:19 2007
@@ -19,6 +19,10 @@
 
 PARROT_API void *mem__sys_realloc(void *, size_t);
 #define mem_sys_realloc(x,y) (assert(x!=NULL), mem__sys_realloc(x,y))
+
+PARROT_API void *mem__sys_realloc_zeroed(void *, size_t, size_t);
+#define mem_sys_realloc_zeroed(x,y,z) (assert(x!=NULL), 
mem__sys_realloc_zeroed(x,y,z))
+
 PARROT_API void mem_sys_free(void *);
 
 void *mem__internal_allocate(size_t, const char *, int);

Modified: trunk/src/gc/memory.c
==============================================================================
--- trunk/src/gc/memory.c       (original)
+++ trunk/src/gc/memory.c       Fri Jun  1 21:11:19 2007
@@ -22,6 +22,7 @@
 */
 
 #include "parrot/parrot.h"
+#include "parrot/memory.h"
 
 /* for PANIC */
 #define interp NULL
@@ -125,6 +126,38 @@
     return ptr;
 }
 
+
+/*
+
+=item C<void *
+mem_sys_realloc_zeroed(void *from, size_t size, size_t old_size)>
+
+Resize a chunk of system memory. Fill the newly allocated space with zeroes.
+
+=cut
+
+*/
+
+void *
+mem__sys_realloc_zeroed(void *from, size_t size, size_t old_size)
+{
+    void *ptr;
+#ifdef DETAIL_MEMORY_DEBUG
+    fprintf(stderr, "Freed %p (realloc -- %i bytes)\n", from, size);
+#endif
+    ptr = realloc(from, size);
+#ifdef DETAIL_MEMORY_DEBUG
+    fprintf(stderr, "Allocated %i at %p\n", size, ptr);
+#endif
+    if (!ptr)
+         PANIC("Out of mem");
+
+    if (size > old_size)
+        memset((char*)ptr + old_size, 0, size - old_size);
+
+    return ptr;
+}
+
 void *
 mem__internal_realloc(void *from, size_t size, const char *file, int line)
 {

Modified: trunk/src/mmd.c
==============================================================================
--- trunk/src/mmd.c     (original)
+++ trunk/src/mmd.c     Fri Jun  1 21:11:19 2007
@@ -585,10 +585,8 @@
 
     /* First, fill in the whole new table with the default function
        pointer. We only really need to do the new part, but... */
-    new_table = (funcptr_t *)mem_sys_allocate(sizeof (funcptr_t) * y * new_x);
-    for (i = 0; i < y * new_x; i++) {
-        new_table[i] = NULL;
-    }
+    new_table = (funcptr_t *)mem_sys_allocate_zeroed(sizeof (funcptr_t) *
+                                                     y * new_x);
 
     /* Then copy the old table over. We have to do this row by row,
        because the rows in the old and new tables are different
@@ -623,17 +621,19 @@
 static void
 mmd_expand_y(Interp *interp, INTVAL func_nr, INTVAL new_y)
 {
-    UINTVAL    new_size;
+    UINTVAL           new_size, old_size;
     MMD_table * const table = interp->binop_mmd_funcs + func_nr;
 
     assert(table->x);
 
+    old_size = sizeof (funcptr_t) * table->x * table->y;
     new_size = sizeof (funcptr_t) * table->x * new_y;
 
     if (table->mmd_funcs)
-        table->mmd_funcs = (funcptr_t *)mem_sys_realloc(table->mmd_funcs, 
new_size);
+        table->mmd_funcs = (funcptr_t *)mem_sys_realloc_zeroed(
+            table->mmd_funcs, new_size, old_size);
     else
-        table->mmd_funcs = (funcptr_t *)mem_sys_allocate(new_size);
+        table->mmd_funcs = (funcptr_t *)mem_sys_allocate_zeroed(new_size);
 
     table->y = new_y;
 }

Reply via email to