Author: miguel
Date: 2007-06-10 01:16:25 -0400 (Sun, 10 Jun 2007)
New Revision: 79106

Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/geometry.cpp
   trunk/moon/src/runtime.cpp
   trunk/moon/src/runtime.h
   trunk/moon/src/shape.cpp
Log:
2007-06-10  Miguel de Icaza  <[EMAIL PROTECTED]>

        * runtime.cpp, runtime.h (BasicArray): New base struct for
        PointArray and double array, puts the count field at the beginning
        and avoids an extra block to be allocated (count and values are
        now on a single contiguous block).

        Introduce a refcount for the arrays, so that we can release those
        properly.  Due to the nature of how we copied values before we
        had to leak the values, when the leak was plugged, we crashed.  

        The refcount is there just because it was easy, we could clone if
        we wanted, but also I would like to keep the refcount there to
        ensure that the array of doubles is aligned on an 8 byte
        boundary. 

        (double_array_new, point_array_new): New methods to create the
        structures. 

        (Value): Now free the arrays.



Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2007-06-10 03:25:07 UTC (rev 79105)
+++ trunk/moon/src/ChangeLog    2007-06-10 05:16:25 UTC (rev 79106)
@@ -1,3 +1,25 @@
+2007-06-10  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       * runtime.cpp, runtime.h (BasicArray): New base struct for
+       PointArray and double array, puts the count field at the beginning
+       and avoids an extra block to be allocated (count and values are
+       now on a single contiguous block).
+
+       Introduce a refcount for the arrays, so that we can release those
+       properly.  Due to the nature of how we copied values before we
+       had to leak the values, when the leak was plugged, we crashed.  
+
+       The refcount is there just because it was easy, we could clone if
+       we wanted, but also I would like to keep the refcount there to
+       ensure that the array of doubles is aligned on an 8 byte
+       boundary. 
+
+       (double_array_new, point_array_new): New methods to create the
+       structures. 
+
+       (Value): Now free the arrays.
+       
+
 2007-06-09  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * geometry.cpp|h: Add/complete PathFigureCollection and 

Modified: trunk/moon/src/geometry.cpp
===================================================================
--- trunk/moon/src/geometry.cpp 2007-06-10 03:25:07 UTC (rev 79105)
+++ trunk/moon/src/geometry.cpp 2007-06-10 05:16:25 UTC (rev 79106)
@@ -776,7 +776,7 @@
        }
 
        PointArray *pa = value->AsPointArray();
-       *count = pa->count;
+       *count = pa->basic.count;
        return pa->points;
 }
 
@@ -813,7 +813,7 @@
        }
 
        PointArray *pa = value->AsPointArray();
-       *count = pa->count;
+       *count = pa->basic.count;
        return pa->points;
 }
 
@@ -850,7 +850,7 @@
        }
 
        PointArray *pa = value->AsPointArray();
-       *count = pa->count;
+       *count = pa->basic.count;
        return pa->points;
 }
 

Modified: trunk/moon/src/runtime.cpp
===================================================================
--- trunk/moon/src/runtime.cpp  2007-06-10 03:25:07 UTC (rev 79105)
+++ trunk/moon/src/runtime.cpp  2007-06-10 05:16:25 UTC (rev 79106)
@@ -132,6 +132,27 @@
        return points;
 }
 
+DoubleArray *
+double_array_new (int count, double *values)
+{
+       DoubleArray *p = (DoubleArray *) g_malloc0 (sizeof (DoubleArray) + 
count * sizeof (double));
+       p->basic.count = count;
+       p->basic.refcount = 1;
+       memcpy (p->values, values, sizeof (double) * count);
+       return p;
+}
+
+PointArray *
+point_array_new (int count, Point *points)
+{
+       PointArray *p = (PointArray *) g_malloc0 (sizeof (PointArray) + count * 
sizeof (Point));
+       p->basic.count = count;
+       p->basic.refcount = 1;
+       memcpy (p->points, points, sizeof (Point) * count);
+       return p;
+};
+
+
 Rect
 rect_from_str (const char *s)
 {
@@ -191,6 +212,10 @@
        /* make a copy of the string instead of just the pointer */
        if (k == STRING)
                u.s = g_strdup (v.u.s);
+       else if (k == POINT_ARRAY)
+               u.point_array->basic.refcount++;
+       else if (k == DOUBLE_ARRAY)
+               u.double_array->basic.refcount++;
 }
 
 Value::Value (Kind k)
@@ -303,20 +328,28 @@
 {
        Init ();
        k = POINT_ARRAY;
-       u.point_array = new PointArray (points, count);
+       u.point_array = point_array_new (count, points);
 }
 
 Value::Value (double *values, int count)
 {
        Init ();
        k = DOUBLE_ARRAY;
-       u.double_array = new DoubleArray (values, count);
+       u.double_array = double_array_new (count, values);
 }
 
 Value::~Value ()
 {
        if (k == STRING)
                g_free (u.s);
+       else if (k == POINT_ARRAY){
+               if (--u.point_array->basic.refcount == 0)
+                       g_free (u.point_array);
+       }
+       else if (k == DOUBLE_ARRAY){
+               if (--u.double_array->basic.refcount == 0)
+                       g_free (u.double_array);
+       }
 }
 
 

Modified: trunk/moon/src/runtime.h
===================================================================
--- trunk/moon/src/runtime.h    2007-06-10 03:25:07 UTC (rev 79105)
+++ trunk/moon/src/runtime.h    2007-06-10 05:16:25 UTC (rev 79106)
@@ -59,28 +59,24 @@
        }
 };
 
+//
+// Arrays derive from this format
+//
+struct BasicArray {
+public:
+       uint32_t count;
+       uint32_t refcount;      // Double purpose: refcount and pad. 
+};
+
 Point point_from_str (const char *s);
 
 struct PointArray {
  public:
-       Point *points;
-       int count;
-
-       PointArray () : points (NULL), count (0) {}
-
-       PointArray (Point* points, int count)
-       {
-               this->points = new Point[count];
-               memcpy (this->points, points, sizeof (Point) * count);
-               this->count = count;
-       }
-
-       ~PointArray ()
-       {
-               delete points;
-       }
+       BasicArray basic;
+       Point points [0];
 };
 
+PointArray *point_array_new (int count, Point *points);
 Point* point_array_from_str (const char *s, int* count);
 
 // map to System.Windows.Rect
@@ -167,25 +163,12 @@
 
 struct DoubleArray {
  public:
-       double *values;
-       int count;
-
-       DoubleArray () : values (NULL), count (0) {}
-
-       DoubleArray (double* values, int count)
-       {
-               this->values = new double[count];
-               memcpy (this->values, values, sizeof (double) * count);
-               this->count = count;
-       }
-
-       ~DoubleArray ()
-       {
-               delete values;
-       }
+       BasicArray basic;
+       double values [0];
 };
 
-double* double_array_from_str (const char *s, int* count);
+double* double_array_from_str   (const char *s, int* count);
+DoubleArray *double_array_new   (int count, double *values);
 
 class Type {
 public:

Modified: trunk/moon/src/shape.cpp
===================================================================
--- trunk/moon/src/shape.cpp    2007-06-10 03:25:07 UTC (rev 79105)
+++ trunk/moon/src/shape.cpp    2007-06-10 05:16:25 UTC (rev 79106)
@@ -352,7 +352,7 @@
        }
 
        DoubleArray *da = value->AsDoubleArray();
-       *count = da->count;
+       *count = da->basic.count;
        return da->values;
 }
 
@@ -583,7 +583,7 @@
        }
 
        PointArray *pa = value->AsPointArray();
-       *count = pa->count;
+       *count = pa->basic.count;
        return pa->points;
 }
 
@@ -650,7 +650,7 @@
        }
 
        PointArray *pa = value->AsPointArray();
-       *count = pa->count;
+       *count = pa->basic.count;
        return pa->points;
 }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to