Skip to content

Commit

Permalink
Get rid of dynamic stack allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed May 27, 2015
1 parent ee3bfe7 commit b5701b3
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pyeda/boolalg/exprnodemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,27 @@ _node2ast(struct BoolExpr *ex)
else {
int i, j;
PyObject *s;
PyObject *asts[ex->data.xs->length];

/* FIXME: magic number: 6 */
s = PyUnicode_FromString(ASTOPS[(int) (ex->kind - 6)]);
if (s == NULL)
return NULL;

PyObject **asts;

asts = (PyObject **) PyMem_Malloc(ex->data.xs->length * sizeof(PyObject *));
if (asts == NULL) {
Py_DECREF(s);
return NULL;
}

for (i = 0; i < ex->data.xs->length; ++i) {
asts[i] = _node2ast(ex->data.xs->items[i]);
if (asts[i] == NULL) {
Py_DECREF(s);
for (j = 0; j < i; ++j)
Py_DECREF(asts[j]);
PyMem_Free(asts);
return NULL;
}
}
Expand All @@ -179,12 +187,15 @@ _node2ast(struct BoolExpr *ex)
Py_DECREF(s);
for (i = 0; i < ex->data.xs->length; ++i)
Py_DECREF(asts[i]);
PyMem_Free(asts);
return NULL;
}

PyTuple_SET_ITEM(ast, 0, s);
for (i = 0; i < ex->data.xs->length; ++i)
PyTuple_SET_ITEM(ast, i+1, asts[i]);

PyMem_Free(asts);
}

return ast;
Expand Down Expand Up @@ -604,14 +615,19 @@ ExprNode_data(ExprNode *self)

if (IS_OP(self->ex)) {
int i, j;
ExprNode *nodes[self->ex->data.xs->length];
ExprNode **nodes;
PyObject *xs;

nodes = (ExprNode **) PyMem_Malloc(self->ex->data.xs->length * sizeof(ExprNode *));
if (nodes == NULL)
return NULL;

for (i = 0; i < self->ex->data.xs->length; ++i) {
nodes[i] = (ExprNode *) PyObject_CallObject((PyObject *) &ExprNode_T, NULL);
if (nodes[i] == NULL) {
for (j = 0; j < i; ++j)
Py_DECREF(nodes[j]);
PyMem_Free(nodes);
return NULL;
}
nodes[i]->ex = BoolExpr_IncRef(self->ex->data.xs->items[i]);
Expand All @@ -621,12 +637,15 @@ ExprNode_data(ExprNode *self)
if (xs == NULL) {
for (i = 0; i < self->ex->data.xs->length; ++i)
Py_DECREF(nodes[i]);
PyMem_Free(nodes);
return NULL;
}

for (i = 0; i < self->ex->data.xs->length; ++i)
PyTuple_SET_ITEM(xs, i, (PyObject *) nodes[i]);

PyMem_Free(nodes);

return xs;
}

Expand Down

0 comments on commit b5701b3

Please sign in to comment.