[svn:parrot] r39269 - in trunk: include/parrot src src/pmc

bacek at svn.parrot.org bacek at svn.parrot.org
Sat May 30 14:16:44 UTC 2009


Author: bacek
Date: Sat May 30 14:16:44 2009
New Revision: 39269
URL: https://trac.parrot.org/parrot/changeset/39269

Log:
[pmc][core] Add function Parrot_pmc_try_reuse.

It is reneralised version of C<reuse_or_new> from Intrger PMC. Will be
used in other PMCs to reduce GC pressure.

Modified:
   trunk/include/parrot/pmc.h
   trunk/src/pmc.c
   trunk/src/pmc/integer.pmc

Modified: trunk/include/parrot/pmc.h
==============================================================================
--- trunk/include/parrot/pmc.h	Sat May 30 13:36:21 2009	(r39268)
+++ trunk/include/parrot/pmc.h	Sat May 30 14:16:44 2009	(r39269)
@@ -49,6 +49,17 @@
         __attribute__nonnull__(1);
 
 PARROT_EXPORT
+PARROT_CANNOT_RETURN_NULL
+PARROT_WARN_UNUSED_RESULT
+PMC* Parrot_pmc_try_reuse(PARROT_INTERP,
+    ARGIN(PMC *self),
+    ARGIN_NULLOK(PMC * value),
+    ARGIN(PMC *dest))
+        __attribute__nonnull__(1)
+        __attribute__nonnull__(2)
+        __attribute__nonnull__(4);
+
+PARROT_EXPORT
 INTVAL PMC_is_null(SHIM_INTERP, ARGIN_NULLOK(const PMC *pmc));
 
 PARROT_EXPORT
@@ -133,6 +144,10 @@
     || PARROT_ASSERT_ARG(pmc)
 #define ASSERT_ARGS_Parrot_create_mro __attribute__unused__ int _ASSERT_ARGS_CHECK = \
        PARROT_ASSERT_ARG(interp)
+#define ASSERT_ARGS_Parrot_pmc_try_reuse __attribute__unused__ int _ASSERT_ARGS_CHECK = \
+       PARROT_ASSERT_ARG(interp) \
+    || PARROT_ASSERT_ARG(self) \
+    || PARROT_ASSERT_ARG(dest)
 #define ASSERT_ARGS_PMC_is_null __attribute__unused__ int _ASSERT_ARGS_CHECK = 0
 #define ASSERT_ARGS_pmc_new __attribute__unused__ int _ASSERT_ARGS_CHECK = \
        PARROT_ASSERT_ARG(interp)

Modified: trunk/src/pmc.c
==============================================================================
--- trunk/src/pmc.c	Sat May 30 13:36:21 2009	(r39268)
+++ trunk/src/pmc.c	Sat May 30 14:16:44 2009	(r39269)
@@ -225,6 +225,54 @@
     return pmc;
 }
 
+
+/*
+=item C<PMC* Parrot_pmc_try_reuse(PARROT_INTERP, PMC *self, PMC * value, PMC
+*dest)>
+
+Try to reuse dest PMC if possible. We are going to kill C<dest> anyway. So try
+to reuse it before.
+
+TODO Write nice POD about sideeffects here.
+
+=cut
+*/
+
+PARROT_EXPORT
+PARROT_CANNOT_RETURN_NULL
+PARROT_WARN_UNUSED_RESULT
+PMC*
+Parrot_pmc_try_reuse(PARROT_INTERP, ARGIN(PMC *self), ARGIN_NULLOK(PMC * value),
+    ARGIN(PMC *dest))
+{
+    ASSERT_ARGS(Parrot_pmc_try_reuse)
+    /* Can't reuse dest because we'll lost value */
+    if (dest == value)
+        return pmc_new(interp, VTABLE_type(interp, self));
+
+    /* Can't reuse Null */
+    if (PMC_IS_NULL(dest))
+        return pmc_new(interp, VTABLE_type(interp, self));
+
+    /* Can't reuse read-only variables */
+    if (dest->vtable->flags & VTABLE_IS_READONLY_FLAG)
+        return pmc_new(interp, VTABLE_type(interp, self));
+
+    /* It's safe in this case. Caller want to replace us anyway */
+    if (dest == self)
+        return dest;
+
+    /* Morph to self type is required */
+    {
+        INTVAL type = VTABLE_type(interp, self);
+        if (dest->vtable->base_type != type)
+            pmc_reuse(interp, dest, type, 0);
+    }
+
+    return dest;
+}
+
+
 /*
 
 =item C<static void check_pmc_reuse_flags(PARROT_INTERP, UINTVAL srcflags,

Modified: trunk/src/pmc/integer.pmc
==============================================================================
--- trunk/src/pmc/integer.pmc	Sat May 30 13:36:21 2009	(r39268)
+++ trunk/src/pmc/integer.pmc	Sat May 30 14:16:44 2009	(r39269)
@@ -43,36 +43,6 @@
     return self;
 }
 
-/* Try to reuse dest PMC if possible */
-static PMC*
-reuse_or_new(PARROT_INTERP, PMC *self, PMC * value, PMC *dest)
-{
-    /* Can't reuse dest because we'll lost value */
-    if (dest == value)
-        return pmc_new(interp, VTABLE_type(interp, self));
-
-    /* Can't reuse Null */
-    if (PMC_IS_NULL(dest))
-        return pmc_new(interp, VTABLE_type(interp, self));
-
-    /* Can't reuse read-only variables */
-    if (dest->vtable->flags & VTABLE_IS_READONLY_FLAG)
-        return pmc_new(interp, VTABLE_type(interp, self));
-
-    /* It's safe in this case. Caller want to replace us anyway */
-    if (dest == self)
-        return dest;
-
-    /* Morph to Integer is required */
-    {
-        INTVAL type = VTABLE_type(interp, self);
-        if (dest->vtable->base_type != type)
-            pmc_reuse(interp, dest, type, 0);
-    }
-
-    return dest;
-}
-
 
 pmclass Integer extends scalar provides integer provides scalar {
     ATTR INTVAL iv; /* the value of this Integer */
@@ -378,7 +348,7 @@
         const INTVAL c = a + b;
 
         if ((c^a) >= 0 || (c^b) >= 0) {
-            dest = reuse_or_new(INTERP, SELF, value, dest);
+            dest = Parrot_pmc_try_reuse(INTERP, SELF, value, dest);
 
             /* need this for e.g. Undef PMC */
             VTABLE_set_integer_native(INTERP, dest, c);
@@ -395,7 +365,7 @@
 
     MULTI PMC *add(Complex value, PMC *dest) {
         const INTVAL a = SELF.get_integer();
-        dest           = reuse_or_new(INTERP, SELF, value, dest);
+        dest           = pmc_new(INTERP, VTABLE_type(INTERP, value));
 
         VTABLE_set_number_native(INTERP, dest,
                 a + VTABLE_get_number_keyed_int(INTERP, value, 0));
@@ -415,7 +385,7 @@
 
 
     MULTI PMC *add(DEFAULT value, PMC *dest) {
-        dest = reuse_or_new(INTERP, SELF, value, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, value, dest);
 
         VTABLE_set_number_native(INTERP, dest,
                 SELF.get_integer() + VTABLE_get_number(INTERP, value));
@@ -428,7 +398,7 @@
         const INTVAL c = a + b;
 
         if ((c^a) >= 0 || (c^b) >= 0) {
-            dest = reuse_or_new(INTERP, SELF, NULL, dest);
+            dest = Parrot_pmc_try_reuse(INTERP, SELF, NULL, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -517,7 +487,7 @@
         const INTVAL c = a - b;
 
         if ((c^a) >= 0 || (c^~b) >= 0) {
-            dest = reuse_or_new(INTERP, SELF, value, dest);
+            dest = Parrot_pmc_try_reuse(INTERP, SELF, value, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -533,7 +503,7 @@
 
     MULTI PMC *subtract(Complex value, PMC *dest) {
         const INTVAL a = SELF.get_integer();
-        dest           = reuse_or_new(INTERP, SELF, value, dest);
+        dest           = pmc_new(INTERP, VTABLE_type(INTERP, value));
 
         VTABLE_set_number_native(INTERP, dest,
                 a - VTABLE_get_number_keyed_int(INTERP, value, 0));
@@ -553,7 +523,7 @@
 
 
     MULTI PMC *subtract(DEFAULT value, PMC *dest) {
-        dest = reuse_or_new(INTERP, SELF, value, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, value, dest);
 
         VTABLE_set_number_native(INTERP, dest,
                 SELF.get_integer() - VTABLE_get_number(INTERP, value));
@@ -576,7 +546,7 @@
         const INTVAL c = a - b;
 
         if ((c^a) >= 0 || (c^~b) >= 0) {
-            dest = reuse_or_new(INTERP, SELF, NULL, dest);
+            dest = Parrot_pmc_try_reuse(INTERP, SELF, NULL, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -675,7 +645,7 @@
         const double cf = (double)a * (double)b;
 
         if ((double) c == cf) {
-            dest = reuse_or_new(INTERP, SELF, value, dest);
+            dest = Parrot_pmc_try_reuse(INTERP, SELF, value, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -706,7 +676,7 @@
 
     MULTI PMC *multiply(DEFAULT value, PMC *dest) {
         const FLOATVAL valf = VTABLE_get_number(INTERP, value);
-        dest                = reuse_or_new(INTERP, SELF, value, dest);
+        dest                = Parrot_pmc_try_reuse(INTERP, SELF, value, dest);
 
         VTABLE_set_number_native(INTERP, dest, SELF.get_number() * valf);
         return dest;
@@ -719,7 +689,7 @@
         const double cf = (double)a * (double)b;
 
         if ((double) c == cf) {
-            dest = reuse_or_new(INTERP, SELF, NULL, dest);
+            dest = Parrot_pmc_try_reuse(INTERP, SELF, NULL, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -824,7 +794,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "float division by zero");
 
-        dest = reuse_or_new(INTERP, SELF, value, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, value, dest);
         VTABLE_set_number_native(INTERP, dest, SELF.get_number() / d);
         return dest;
     }
@@ -886,7 +856,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "float division by zero");
 
-        dest = reuse_or_new(INTERP, SELF, value, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, value, dest);
 
         f = floor(SELF.get_number() / d);
         VTABLE_set_integer_native(INTERP, dest, (INTVAL)f);
@@ -901,7 +871,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "float division by zero");
 
-        dest = reuse_or_new(INTERP, SELF, NULL, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, NULL, dest);
 
         f = floor(SELF.get_number() / value);
         VTABLE_set_integer_native(INTERP, dest, (INTVAL)f);
@@ -916,7 +886,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "float division by zero");
 
-        dest = reuse_or_new(INTERP, SELF, NULL, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, NULL, dest);
 
         f = floor(SELF.get_number() / value);
         VTABLE_set_integer_native(INTERP, dest, (INTVAL)f);
@@ -1007,7 +977,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "int modulus by zero");
 
-        dest = reuse_or_new(INTERP, SELF, value, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, value, dest);
 
         VTABLE_set_integer_native(INTERP, dest,
                 intval_mod(SELF.get_integer(), d));
@@ -1020,7 +990,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "int modulus by zero");
 
-        dest = reuse_or_new(INTERP, SELF, NULL, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, NULL, dest);
 
         VTABLE_set_integer_native(INTERP, dest,
                 intval_mod(SELF.get_integer(), value));
@@ -1033,7 +1003,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "int modulus by zero");
 
-        dest = reuse_or_new(INTERP, SELF, NULL, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, NULL, dest);
 
         VTABLE_set_integer_native(INTERP, dest,
                 intval_mod(SELF.get_integer(), (INTVAL)value));
@@ -1152,7 +1122,7 @@
             }
         }
 
-        dest = reuse_or_new(INTERP, SELF, NULL, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, NULL, dest);
 
         VTABLE_set_integer_native(INTERP, dest, r);
         return dest;
@@ -1339,7 +1309,7 @@
         const INTVAL a = abs(SELF.get_integer());
 
         /* RT #46635 overflow for -maxint */
-        dest = reuse_or_new(INTERP, SELF, NULL, dest);
+        dest = Parrot_pmc_try_reuse(INTERP, SELF, NULL, dest);
 
         VTABLE_set_integer_native(INTERP, dest, a);
         return dest;


More information about the parrot-commits mailing list