From 28bb2362489888bcb64dfe3da003cfe3f0f048c1 Mon Sep 17 00:00:00 2001 From: "John J. Camilleri" Date: Sun, 26 Sep 2021 15:45:34 +0200 Subject: [PATCH] Add deallocator functions to all classes --- src/runtime/python/expr.c | 127 +++++++++++++++++++++++------- src/runtime/python/pypgf.c | 2 +- src/runtime/python/transactions.c | 14 ++-- 3 files changed, 108 insertions(+), 35 deletions(-) diff --git a/src/runtime/python/expr.c b/src/runtime/python/expr.c index 2d5b67f43..c807b4bd2 100644 --- a/src/runtime/python/expr.c +++ b/src/runtime/python/expr.c @@ -57,13 +57,22 @@ Type_init(TypeObject *self, PyObject *args, PyObject *kwds) self->hypos = hypos; self->name = name; self->exprs = exprs; - Py_INCREF(hypos); - // Py_INCREF(name); - Py_INCREF(exprs); + Py_INCREF(self->hypos); + Py_INCREF(self->name); + Py_INCREF(self->exprs); return 0; } +static void +Type_dealloc(TypeObject *self) +{ + Py_XDECREF(self->hypos); + Py_XDECREF(self->name); + Py_XDECREF(self->exprs); + Py_TYPE(self)->tp_free(self); +} + static PyObject * Type_str(TypeObject *self) { @@ -128,7 +137,7 @@ PyTypeObject pgf_TypeType = { "pgf.Type", /*tp_name*/ sizeof(TypeObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor) Type_dealloc, /*tp_dealloc*/ + (destructor) Type_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -247,12 +256,21 @@ ExprAbs_init(ExprAbsObject *self, PyObject *args, PyObject *kwds) self->bind_type = bind_type; self->name = name; self->body = body; - Py_INCREF(bind_type); - Py_INCREF(name); - Py_INCREF(body); + Py_INCREF(self->bind_type); + Py_INCREF(self->name); + Py_INCREF(self->body); return 0; } +static void +ExprAbs_dealloc(ExprAbsObject *self) +{ + Py_XDECREF(self->bind_type); + Py_XDECREF(self->name); + Py_XDECREF(self->body); + Py_TYPE(self)->tp_free(self); +} + static PyObject * ExprAbs_richcompare(ExprAbsObject *e1, PyObject *p2, int op) { @@ -289,7 +307,7 @@ PyTypeObject pgf_ExprAbsType = { "pgf.ExprAbs", /*tp_name*/ sizeof(ExprAbsObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor)Expr_dealloc, /*tp_dealloc*/ + (destructor) ExprAbs_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -344,11 +362,19 @@ ExprApp_init(ExprAppObject *self, PyObject *args, PyObject *kwds) } self->fun = (ExprObject *)fun; self->arg = (ExprObject *)arg; - Py_INCREF(fun); - Py_INCREF(arg); + Py_INCREF(self->fun); + Py_INCREF(self->arg); return 0; } +static void +ExprApp_dealloc(ExprAppObject *self) +{ + Py_XDECREF(self->fun); + Py_XDECREF(self->arg); + Py_TYPE(self)->tp_free(self); +} + static PyObject * ExprApp_richcompare(ExprAppObject *e1, PyObject *p2, int op) { @@ -383,7 +409,7 @@ PyTypeObject pgf_ExprAppType = { "pgf.ExprApp", /*tp_name*/ sizeof(ExprAppObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor)Expr_dealloc, /*tp_dealloc*/ + (destructor) ExprApp_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -437,14 +463,21 @@ ExprLit_init(ExprLitObject *self, PyObject *args, PyObject *kwds) } if (PyLong_Check(lit) || PyFloat_Check(lit) || PyUnicode_Check(lit)) { self->lit = lit; - Py_INCREF(lit); + Py_INCREF(self->lit); return 0; } else { - PyErr_SetString(PyExc_TypeError, "invalid argument in ExprLit_init"); + PyErr_SetString(PyExc_TypeError, "a literal expression can be initialised with an integer, float, or string"); return -1; } } +static void +ExprLit_dealloc(ExprLitObject *self) +{ + Py_XDECREF(self->lit); + Py_TYPE(self)->tp_free(self); +} + static PyObject * ExprLit_richcompare(ExprLitObject *e1, PyObject *p2, int op) { @@ -493,7 +526,7 @@ PyTypeObject pgf_ExprLitType = { "pgf.ExprLit", /*tp_name*/ sizeof(ExprLitObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor)Expr_dealloc, /*tp_dealloc*/ + (destructor) ExprLit_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -547,17 +580,26 @@ ExprMeta_init(ExprMetaObject *self, PyObject *args, PyObject *kwds) } if (lit == NULL) { self->id = PyLong_FromLong(0); + Py_INCREF(self->id); return 0; } else if (PyLong_Check(lit)) { self->id = lit; - Py_INCREF(lit); + Py_INCREF(self->id); return 0; } else { - PyErr_SetString(PyExc_TypeError, "invalid argument in ExprMeta_init"); + // TODO check positive + PyErr_SetString(PyExc_TypeError, "a meta variable must be initialised with an integer"); return -1; } } +static void +ExprMeta_dealloc(ExprMetaObject *self) +{ + Py_XDECREF(self->id); + Py_TYPE(self)->tp_free(self); +} + static PyObject * ExprMeta_richcompare(ExprMetaObject *e1, PyObject *p2, int op) { @@ -590,7 +632,7 @@ PyTypeObject pgf_ExprMetaType = { "pgf.ExprMeta", /*tp_name*/ sizeof(ExprMetaObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor)Expr_dealloc, /*tp_dealloc*/ + (destructor) ExprMeta_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -643,10 +685,17 @@ ExprFun_init(ExprFunObject *self, PyObject *args, PyObject *kwds) return -1; } self->name = lit; - Py_INCREF(lit); + Py_INCREF(self->name); return 0; } +static void +ExprFun_dealloc(ExprFunObject *self) +{ + Py_XDECREF(self->name); + Py_TYPE(self)->tp_free(self); +} + static PyObject * ExprFun_richcompare(ExprFunObject *e1, PyObject *p2, int op) { @@ -679,7 +728,7 @@ PyTypeObject pgf_ExprFunType = { "pgf.ExprFun", /*tp_name*/ sizeof(ExprFunObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor)Expr_dealloc, /*tp_dealloc*/ + (destructor) ExprFun_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -733,17 +782,26 @@ ExprVar_init(ExprVarObject *self, PyObject *args, PyObject *kwds) } if (lit == NULL) { self->var = PyLong_FromLong(0); + Py_INCREF(self->var); return 0; } else if (PyLong_Check(lit)) { self->var = lit; - Py_INCREF(lit); + Py_INCREF(self->var); return 0; } else { - PyErr_SetString(PyExc_TypeError, "invalid argument in ExprVar_init"); + // TODO check positive + PyErr_SetString(PyExc_TypeError, "variable expression must be initialised with an integer"); return -1; } } +static void +ExprVar_dealloc(ExprVarObject *self) +{ + Py_XDECREF(self->var); + Py_TYPE(self)->tp_free(self); +} + static PyObject * ExprVar_richcompare(ExprVarObject *e1, PyObject *p2, int op) { @@ -776,7 +834,7 @@ PyTypeObject pgf_ExprVarType = { "pgf.ExprVar", /*tp_name*/ sizeof(ExprVarObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor)Expr_dealloc, /*tp_dealloc*/ + (destructor) ExprVar_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -831,11 +889,19 @@ ExprTyped_init(ExprTypedObject *self, PyObject *args, PyObject *kwds) } self->expr = expr; self->type = type; - Py_INCREF(expr); - Py_INCREF(type); + Py_INCREF(self->expr); + Py_INCREF(self->type); return 0; } +static void +ExprTyped_dealloc(ExprTypedObject *self) +{ + Py_XDECREF(self->expr); + Py_XDECREF(self->type); + Py_TYPE(self)->tp_free(self); +} + static PyObject * ExprTyped_richcompare(ExprTypedObject *e1, PyObject *p2, int op) { @@ -870,7 +936,7 @@ PyTypeObject pgf_ExprTypedType = { "pgf.ExprTyped", /*tp_name*/ sizeof(ExprTypedObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor)Expr_dealloc, /*tp_dealloc*/ + (destructor) ExprTyped_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -923,10 +989,17 @@ ExprImplArg_init(ExprImplArgObject *self, PyObject *args, PyObject *kwds) return -1; } self->expr = expr; - Py_INCREF(expr); + Py_INCREF(self->expr); return 0; } +static void +ExprImplArg_dealloc(ExprImplArgObject *self) +{ + Py_XDECREF(self->expr); + Py_TYPE(self)->tp_free(self); +} + static PyObject * ExprImplArg_richcompare(ExprImplArgObject *e1, PyObject *p2, int op) { @@ -959,7 +1032,7 @@ PyTypeObject pgf_ExprImplArgType = { "pgf.ExprImplArg", /*tp_name*/ sizeof(ExprImplArgObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor)Expr_dealloc, /*tp_dealloc*/ + (destructor) ExprImplArg_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c index 0ebc01e63..29e5c1a5f 100644 --- a/src/runtime/python/pypgf.c +++ b/src/runtime/python/pypgf.c @@ -15,7 +15,7 @@ PGF_dealloc(PGFObject *self) { if (self->db != NULL && self->revision != 0) pgf_free_revision(self->db, self->revision); - Py_TYPE(self)->tp_free((PyObject *)self); + Py_TYPE(self)->tp_free(self); } typedef struct { diff --git a/src/runtime/python/transactions.c b/src/runtime/python/transactions.c index 1ae788523..a51fcf37b 100644 --- a/src/runtime/python/transactions.c +++ b/src/runtime/python/transactions.c @@ -65,6 +65,12 @@ PGF_newTransaction(PGFObject *self, PyObject *args) // ---------------------------------------------------------------------------- +static void +Transaction_dealloc(TransactionObject *self) +{ + Py_TYPE(self)->tp_free(self); +} + static PyObject * Transaction_commit(TransactionObject *self, PyObject *args) { @@ -229,12 +235,6 @@ exit: return return_value; } -// static void -// Transaction_dealloc(PGFObject* self) -// { -// Py_TYPE(self)->tp_free((PyObject*)self); -// } - static PyGetSetDef Transaction_getseters[] = { {NULL} /* Sentinel */ }; @@ -277,7 +277,7 @@ PyTypeObject pgf_TransactionType = { "pgf.Transaction", /*tp_name*/ sizeof(TransactionObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - 0, //(destructor)Transaction_dealloc, /*tp_dealloc*/ + (destructor) Transaction_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/