Skip to content

Commit

Permalink
Lots of miscellaneous changes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed May 30, 2015
1 parent 3fc5c84 commit c69ea04
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 206 deletions.
20 changes: 7 additions & 13 deletions extension/boolexpr/argset.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ BoolExprOrAndArgSet_New(BoolExprKind kind)
{
struct BoolExprOrAndArgSet *argset;

argset = (struct BoolExprOrAndArgSet *) malloc(sizeof(struct BoolExprOrAndArgSet));
argset = malloc(sizeof(struct BoolExprOrAndArgSet));
if (argset == NULL)
return NULL; // LCOV_EXCL_LINE

argset->kind = kind;
argset->min = true;
argset->max = false;

argset->xs = BoolExprSet_New();
if (argset->xs == NULL) {
free(argset); // LCOV_EXCL_LINE
Expand All @@ -37,7 +36,6 @@ void
BoolExprOrAndArgSet_Del(struct BoolExprOrAndArgSet *argset)
{
BoolExprSet_Del(argset->xs);

free(argset);
}

Expand Down Expand Up @@ -88,12 +86,11 @@ BoolExprXorArgSet_New(bool parity)
{
struct BoolExprXorArgSet *argset;

argset = (struct BoolExprXorArgSet *) malloc(sizeof(struct BoolExprXorArgSet));
argset = malloc(sizeof(struct BoolExprXorArgSet));
if (argset == NULL)
return NULL; // LCOV_EXCL_LINE

argset->parity = parity;

argset->xs = BoolExprSet_New();
if (argset->xs == NULL) {
free(argset); // LCOV_EXCL_LINE
Expand All @@ -108,7 +105,6 @@ void
BoolExprXorArgSet_Del(struct BoolExprXorArgSet *argset)
{
BoolExprSet_Del(argset->xs);

free(argset);
}

Expand All @@ -132,9 +128,9 @@ BoolExprXorArgSet_Insert(struct BoolExprXorArgSet *argset, struct BoolExpr *key)
/* Xnor(x, y, z, ~z) = Xor(x, y) */
if (IS_LIT(key) || IS_NOT(key)) {
struct BoolExpr *temp = Not(key);
bool inset = BoolExprSet_Contains(argset->xs, temp);
bool flip = BoolExprSet_Contains(argset->xs, temp);
BoolExpr_DecRef(temp);
if (inset) {
if (flip) {
BoolExprSet_Remove(argset->xs, temp);
argset->parity ^= true;
return true;
Expand Down Expand Up @@ -171,13 +167,12 @@ BoolExprEqArgSet_New(void)
{
struct BoolExprEqArgSet *argset;

argset = (struct BoolExprEqArgSet *) malloc(sizeof(struct BoolExprEqArgSet));
argset = malloc(sizeof(struct BoolExprEqArgSet));
if (argset == NULL)
return NULL; // LCOV_EXCL_LINE

argset->zero = false;
argset->one = false;

argset->xs = BoolExprSet_New();
if (argset->xs == NULL) {
free(argset); // LCOV_EXCL_LINE
Expand All @@ -192,7 +187,6 @@ void
BoolExprEqArgSet_Del(struct BoolExprEqArgSet *argset)
{
BoolExprSet_Del(argset->xs);

free(argset);
}

Expand Down Expand Up @@ -220,9 +214,9 @@ BoolExprEqArgSet_Insert(struct BoolExprEqArgSet *argset, struct BoolExpr *key)
/* Equal(~x, x) = 0 */
if (IS_LIT(key) || IS_NOT(key)) {
struct BoolExpr *temp = Not(key);
bool inset = BoolExprSet_Contains(argset->xs, temp);
bool contradict = BoolExprSet_Contains(argset->xs, temp);
BoolExpr_DecRef(temp);
if (inset) {
if (contradict) {
argset->zero = true;
argset->one = true;
BoolExprSet_Clear(argset->xs);
Expand Down
8 changes: 3 additions & 5 deletions extension/boolexpr/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ BoolExprArray_New(size_t length, struct BoolExpr **items)
{
struct BoolExprArray *array;

array = (struct BoolExprArray *) malloc(sizeof(struct BoolExprArray));
array = malloc(sizeof(struct BoolExprArray));
if (array == NULL)
return NULL; // LCOV_EXCL_LINE

array->items = (struct BoolExpr **) malloc(length * sizeof(struct BoolExpr *));
array->length = length;
array->items = malloc(length * sizeof(struct BoolExpr *));
if (array->items == NULL) {
free(array); // LCOV_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}

array->length = length;

for (size_t i = 0; i < length; ++i)
array->items[i] = BoolExpr_IncRef(items[i]);

Expand All @@ -39,7 +38,6 @@ BoolExprArray_Del(struct BoolExprArray *array)
{
for (size_t i = 0; i < array->length; ++i)
BoolExpr_DecRef(array->items[i]);

free(array->items);
free(array);
}
Expand Down
12 changes: 6 additions & 6 deletions extension/boolexpr/array2.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ BoolExprArray2_New(size_t length, size_t *lengths, struct BoolExpr ***items)
{
struct BoolExprArray2 *array2;

array2 = (struct BoolExprArray2 *) malloc(sizeof(struct BoolExprArray2));
array2 = malloc(sizeof(struct BoolExprArray2));
if (array2 == NULL)
return NULL; // LCOV_EXCL_LINE

array2->items = (struct BoolExprArray **) malloc(length * sizeof(struct BoolExprArray *));
array2->length = length;
array2->items = malloc(length * sizeof(struct BoolExprArray *));
if (array2->items == NULL) {
free(array2); // LCOV_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}

array2->length = length;

for (size_t i = 0; i < length; ++i) {
array2->items[i] = BoolExprArray_New(lengths[i], items[i]);
/* LCOV_EXCL_START */
if (array2->items[i] == NULL) {
for (size_t j = 0; j < i; ++j)
BoolExprArray_Del(array2->items[j]);
free(array2->items);
free(array2);
return NULL;
}
/* LCOV_EXCL_STOP */
Expand All @@ -51,7 +52,6 @@ BoolExprArray2_Del(struct BoolExprArray2 *array2)
{
for (size_t i = 0; i < array2->length; ++i)
BoolExprArray_Del(array2->items[i]);

free(array2->items);
free(array2);
}
Expand Down Expand Up @@ -79,7 +79,7 @@ _multiply(struct BoolExprArray *a, struct BoolExprArray *b, BoolExprKind kind)
struct BoolExpr **items;
struct BoolExprArray *prod;

items = (struct BoolExpr **) malloc(length * sizeof(struct BoolExpr *));
items = malloc(length * sizeof(struct BoolExpr *));
if (items == NULL)
return NULL; // LCOV_EXCL_LINE

Expand Down
22 changes: 7 additions & 15 deletions extension/boolexpr/binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
struct BoolExpr * _op_new(BoolExprKind kind, size_t n, struct BoolExpr **xs);

/* util.c */
void _free_xs(int n, struct BoolExpr **xs);
struct BoolExpr * _op_transform(struct BoolExpr *op, struct BoolExpr * (*fn)(struct BoolExpr *));


Expand Down Expand Up @@ -67,34 +68,25 @@ _eq_binify(struct BoolExpr *op)
struct BoolExpr *y;

length = (op->data.xs->length * (op->data.xs->length - 1)) >> 1;
xs = (struct BoolExpr **) malloc(length * sizeof(struct BoolExpr *));
xs = malloc(length * sizeof(struct BoolExpr *));

for (size_t i = 0, index = 0; i < (op->data.xs->length - 1); ++i) {
for (size_t j = i+1; j < op->data.xs->length; ++j, ++index) {
xs[index] = EqualN(2, op->data.xs->items[i], op->data.xs->items[j]);
if (xs[index] == NULL) {
/* LCOV_EXCL_START */
for (size_t k = 0; k < index; ++k)
BoolExpr_DecRef(xs[k]);
free(xs);
return NULL;
/* LCOV_EXCL_STOP */
_free_xs(index, xs); // LCOV_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}
}
}

temp = And(length, xs);
if (temp == NULL) {
/* LCOV_EXCL_START */
for (size_t i = 0; i < length; ++i)
BoolExpr_DecRef(xs[i]);
free(xs);
/* LCOV_EXCL_STOP */
_free_xs(length, xs); // LCOV_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}

for (size_t i = 0; i < length; ++i)
BoolExpr_DecRef(xs[i]);
free(xs);
_free_xs(length, xs);

CHECK_NULL_1(y, _commutative_binify(temp), temp);
BoolExpr_DecRef(temp);
Expand Down
24 changes: 11 additions & 13 deletions extension/boolexpr/boolexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ BoolExprIter_New(struct BoolExpr *ex)
{
struct BoolExprIter *it;

it = (struct BoolExprIter *) malloc(sizeof(struct BoolExprIter));
it = malloc(sizeof(struct BoolExprIter));
if (it == NULL)
return NULL; // LCOV_EXCL_LINE

Expand Down Expand Up @@ -99,7 +99,7 @@ struct BoolExpr Illogical = {1, ILLOGICAL, NNF | SIMPLE, {.pcval=0}};
struct BoolExpr * IDENTITY[16] = {
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
&Zero, &One, &Zero, &One,
&Zero, &One, &Zero, NULL,
NULL, NULL, NULL, NULL,
};

Expand All @@ -116,7 +116,7 @@ _lit_new(struct BoolExprVector *lits, long uniqid)
{
struct BoolExpr *lit;

lit = (struct BoolExpr *) malloc(sizeof(struct BoolExpr));
lit = malloc(sizeof(struct BoolExpr));
if (lit == NULL)
return NULL; // LCOV_EXCL_LINE

Expand All @@ -142,20 +142,19 @@ _op_new(BoolExprKind kind, size_t n, struct BoolExpr **xs)
{
struct BoolExpr *op;

op = (struct BoolExpr *) malloc(sizeof(struct BoolExpr));
op = malloc(sizeof(struct BoolExpr));
if (op == NULL)
return NULL; // LCOV_EXCL_LINE

op->refcount = 1;
op->kind = kind;
op->flags = (BoolExprFlags) 0;
op->data.xs = BoolExprArray_New(n, xs);
if (op->data.xs == NULL) {
free(op); // LCOV_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}

op->refcount = 1;
op->kind = kind;
op->flags = (BoolExprFlags) 0;

return op;
}

Expand Down Expand Up @@ -184,12 +183,10 @@ _op_del(struct BoolExpr *op)
struct BoolExpr *
Literal(struct BoolExprVector *lits, long uniqid)
{
size_t index;
size_t index = _uniqid2index(uniqid);
struct BoolExpr *lit;

index = _uniqid2index(uniqid);

lit = index >= lits->length ? (struct BoolExpr *) NULL : lits->items[index];
lit = (index >= lits->length) ? (struct BoolExpr *) NULL : lits->items[index];
if (lit == (struct BoolExpr *) NULL) {
CHECK_NULL(lit, _lit_new(lits, uniqid));
BoolExprVector_Insert(lits, index, lit);
Expand Down Expand Up @@ -268,7 +265,7 @@ Equal(size_t n, struct BoolExpr **xs)
{
/* Equal() <=> Equal(0) <=> Equal(1) <=> 1 */
if (n <= 1)
return BoolExpr_IncRef(IDENTITY[OP_EQ]);
return BoolExpr_IncRef(&One);

return _op_new(OP_EQ, n, xs);
}
Expand Down Expand Up @@ -491,6 +488,7 @@ BoolExpr_DecRef(struct BoolExpr * ex)
assert(ex->refcount > 0);

ex->refcount -= 1;

if (ex->refcount == 0) {
/* Constant refcount must never reach zero */
assert(!IS_CONST(ex));
Expand Down

0 comments on commit c69ea04

Please sign in to comment.