changeset 1448:6b3ea4d579d6

cdefer: Removed several memory holes, fixed comparison of Shares.
author Marcel Keller <mkeller@cs.au.dk>
date Mon, 19 Jul 2010 17:02:41 +0200
parents 2921ecae2a23
children d6bd88318fc6
files viff/cdefer.c
diffstat 1 files changed, 60 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/viff/cdefer.c	Fri Jul 16 14:35:19 2010 +0200
+++ b/viff/cdefer.c	Mon Jul 19 17:02:41 2010 +0200
@@ -942,12 +942,12 @@
     if(PyArg_UnpackTuple(args, "split_result", 2, 2, &result, &clone) < 0)
 	return NULL;
 
-    res = PyObject_CallMethodObjArgs(clone, PyString_FromString("callback"),
-			       result, NULL);
+    res = cdefer_Deferred__startRunCallbacks(clone, result);
 
     if (!res)
 	return NULL;
 
+    Py_DECREF(res);
     Py_INCREF(result);
     return result;
 }
@@ -957,7 +957,7 @@
 				METH_VARARGS, 0};
 
 static PyObject *cdefer_Share_clone(cdefer_Share *self, PyObject *noarg) {
-    PyObject *clone, *result, *split_result;
+    PyObject *clone, *result, *split_result, *arg;
 
     clone = PyObject_CallFunctionObjArgs((PyObject *)&cdefer_ShareType,
 					 self->runtime, self->field, NULL);
@@ -970,15 +970,24 @@
     if (!split_result)
 	return NULL;
 
+    arg = Py_BuildValue("(O)", clone);
+
+    if (!arg)
+	return NULL;
+
     Py_INCREF(Py_None);
     result = cdefer_Deferred__addCallbacks(&(self->deferred),
 					   split_result, Py_None,
-					   Py_BuildValue("(O)", clone), Py_None,
+					   arg, Py_None,
 					   Py_None, Py_None);
+    Py_DECREF(Py_None);
+    Py_DECREF(arg);
+    Py_DECREF(split_result);
 
     if (!result)
 	return NULL;
 
+    Py_DECREF(result);
     return clone;
 }
 
@@ -989,15 +998,19 @@
 
 #define SYM_BIN_OP(FUNCNAME, NBNAME, SHORTNAME)	\
 static PyObject *FUNCNAME(PyObject *self, PyObject *other) { \
+    PyObject* result; \
+    PyObject* name = PyString_FromString(#SHORTNAME); \
+    \
     if (Py_TYPE(self)->tp_as_number != NULL && \
 	Py_TYPE(self)->tp_as_number->NBNAME == FUNCNAME) \
-	return PyObject_CallMethodObjArgs(((cdefer_Share *)self)->runtime, \
-					  PyString_FromString(#SHORTNAME), \
-					  self, other, NULL); \
+	result = PyObject_CallMethodObjArgs(((cdefer_Share *)self)->runtime, \
+					    name, self, other, NULL); \
     else \
-	return PyObject_CallMethodObjArgs(((cdefer_Share *)other)->runtime, \
-					  PyString_FromString(#SHORTNAME), \
-					  other, self, NULL); \
+	result = PyObject_CallMethodObjArgs(((cdefer_Share *)other)->runtime, \
+					    name, other, self, NULL); \
+    \
+    Py_DECREF(name); \
+    return result; \
 }
 
 SYM_BIN_OP(cdefer_Share_add, nb_add, add)
@@ -1005,24 +1018,32 @@
 SYM_BIN_OP(cdefer_Share_xor, nb_xor, xor)
 
 static PyObject *cdefer_Share_sub(PyObject *self, PyObject *other) {
+    PyObject* result;
+    PyObject* name = PyString_FromString("sub");
+
     if (Py_TYPE(self)->tp_as_number != NULL &&
 	Py_TYPE(self)->tp_as_number->nb_subtract == cdefer_Share_sub)
-	return PyObject_CallMethodObjArgs(((cdefer_Share *)self)->runtime,
-					  PyString_FromString("sub"),
-					  self, other, NULL);
+	result =  PyObject_CallMethodObjArgs(((cdefer_Share *)self)->runtime,
+					     name, self, other, NULL);
     else
-	return PyObject_CallMethodObjArgs(((cdefer_Share *)other)->runtime,
-					  PyString_FromString("sub"),
-					  self, other, NULL);
+	result = PyObject_CallMethodObjArgs(((cdefer_Share *)other)->runtime,
+					    name, self, other, NULL);
+
+    Py_DECREF(name);
+    return result;
 }
 
 static PyObject *cdefer_Share_pow(PyObject *self, PyObject *other, PyObject *modulus) {
+    PyObject* result, *name;
+
     if (Py_TYPE(self)->tp_as_number != NULL &&
-	Py_TYPE(self)->tp_as_number->nb_power == cdefer_Share_pow)
-	return PyObject_CallMethodObjArgs(((cdefer_Share *)self)->runtime,
-					  PyString_FromString("pow"),
-					  self, other, NULL);
-    else {
+	Py_TYPE(self)->tp_as_number->nb_power == cdefer_Share_pow) {
+	name = PyString_FromString("pow");
+	result =  PyObject_CallMethodObjArgs(((cdefer_Share *)self)->runtime,
+					  name, self, other, NULL);
+	Py_DECREF(name);
+	return result;
+    } else {
 	Py_INCREF(Py_NotImplemented);
 	return Py_NotImplemented;
     }
@@ -1030,7 +1051,7 @@
 
 static PyObject *cdefer_Share_richcompare(PyObject *self,
 					  PyObject *other, int op) {
-    PyObject *first, *second, *tmp = NULL;
+    PyObject *first, *second, *tmp = NULL, *name = NULL, *one, *result;
 
     if (Py_TYPE(self)->tp_richcompare == cdefer_Share_richcompare) {
 	first = self;
@@ -1043,39 +1064,46 @@
     switch(op) {
     case Py_EQ:
     case Py_NE:
+	name = PyString_FromString("equal");
 	tmp = PyObject_CallMethodObjArgs(((cdefer_Share *)first)->runtime,
-					 PyString_FromString("equal"),
-					 first, second, NULL);
+					 name, first, second, NULL);
 	break;
 
     case Py_GE:
     case Py_LT:
+	name = PyString_FromString("greater_than_equal");
 	tmp = PyObject_CallMethodObjArgs(((cdefer_Share *)first)->runtime,
-					 PyString_FromString("greater_than_equal"),
-					 self, other, NULL);
+					 name, self, other, NULL);
 	break;
 
     case Py_LE:
     case Py_GT:
+	name = PyString_FromString("greater_than_equal");
 	tmp = PyObject_CallMethodObjArgs(((cdefer_Share *)first)->runtime,
-					 PyString_FromString("greater_than_equal"),
-					 other, self, NULL);
+					 name, other, self, NULL);
 	break;
     }
 
+    Py_XDECREF(name);
+
     switch(op) {
     case Py_EQ:
     case Py_GE:
-    case Py_GT:
+    case Py_LE:
 	return tmp;
 
     case Py_NE:
     case Py_LT:
-    case Py_LE:
+    case Py_GT:
 	if (!tmp)
 	    return NULL;
-	else
-	    return PyNumber_Subtract(PyInt_FromLong(1), tmp);
+	else {
+	    one = PyInt_FromLong(1);
+	    result = PyNumber_Subtract(one, tmp);
+	    Py_DECREF(one);
+	    Py_DECREF(tmp);
+	    return result;
+	}
     }
 
     Py_INCREF(Py_NotImplemented);