diff --git a/ckiwi/ckiwi.cpp b/ckiwi/ckiwi.cpp index 13596df..be6c013 100644 --- a/ckiwi/ckiwi.cpp +++ b/ckiwi/ckiwi.cpp @@ -1,10 +1,9 @@ -#include "ljkiwi.hpp" #include "ckiwi.h" #include -#include #include +#include #include #include @@ -38,15 +37,18 @@ const KiwiErr* new_error(const KiwiErr* base, const std::exception& ex) { static const constexpr KiwiErr kKiwiErrUnhandledCxxException { KiwiErrUnknown, - "An unhandled C++ exception occurred."}; + "An unhandled C++ exception occurred." +}; static const constexpr KiwiErr kKiwiErrNullObjectArg0 { KiwiErrNullObject, - "null object passed as argument #0 (self)"}; + "null object passed as argument #0 (self)" +}; static const constexpr KiwiErr kKiwiErrNullObjectArg1 { KiwiErrNullObject, - "null object passed as argument #1"}; + "null object passed as argument #1" +}; template const KiwiErr* wrap_err(F&& f) { @@ -55,42 +57,49 @@ const KiwiErr* wrap_err(F&& f) { } catch (const UnsatisfiableConstraint& ex) { static const constexpr KiwiErr err { KiwiErrUnsatisfiableConstraint, - "The constraint cannot be satisfied."}; + "The constraint cannot be satisfied." + }; return &err; } catch (const UnknownConstraint& ex) { static const constexpr KiwiErr err { KiwiErrUnknownConstraint, - "The constraint has not been added to the solver."}; + "The constraint has not been added to the solver." + }; return &err; } catch (const DuplicateConstraint& ex) { static const constexpr KiwiErr err { KiwiErrDuplicateConstraint, - "The constraint has already been added to the solver."}; + "The constraint has already been added to the solver." + }; return &err; } catch (const UnknownEditVariable& ex) { static const constexpr KiwiErr err { KiwiErrUnknownEditVariable, - "The edit variable has not been added to the solver."}; + "The edit variable has not been added to the solver." + }; return &err; } catch (const DuplicateEditVariable& ex) { static const constexpr KiwiErr err { KiwiErrDuplicateEditVariable, - "The edit variable has already been added to the solver."}; + "The edit variable has already been added to the solver." + }; return &err; } catch (const BadRequiredStrength& ex) { static const constexpr KiwiErr err { KiwiErrBadRequiredStrength, - "A required strength cannot be used in this context."}; + "A required strength cannot be used in this context." + }; return &err; } catch (const InternalSolverError& ex) { static const constexpr KiwiErr base { KiwiErrInternalSolverError, - "An internal solver error occurred."}; + "An internal solver error occurred." + }; return new_error(&base, ex); } catch (std::bad_alloc&) { static const constexpr KiwiErr err {KiwiErrAlloc, "A memory allocation failed."}; @@ -183,18 +192,19 @@ void kiwi_expression_retain(KiwiExpression* expr) { for (auto* t = expr->terms_; t != expr->terms_ + expr->term_count; ++t) { retain_unmanaged(t->var); } + expr->owner = expr; } void kiwi_expression_destroy(KiwiExpression* expr) { - if (lk_unlikely(!expr)) + if (lk_unlikely(!expr || !expr->owner)) return; - if (expr->owner) { - release_unmanaged(expr->owner); - } else { + if (expr->owner == expr) { for (auto* t = expr->terms_; t != expr->terms_ + expr->term_count; ++t) { release_unmanaged(t->var); } + } else { + release_unmanaged(static_cast(expr->owner)); } } @@ -209,7 +219,7 @@ KiwiConstraint* kiwi_constraint_construct( } std::vector terms; - terms.reserve(static_cast( + terms.reserve(static_cast( (lhs && lhs->term_count > 0 ? lhs->term_count : 0) + (rhs && rhs->term_count > 0 ? rhs->term_count : 0) )); diff --git a/ckiwi/ckiwi.h b/ckiwi/ckiwi.h index 2b5999f..7d3a201 100644 --- a/ckiwi/ckiwi.h +++ b/ckiwi/ckiwi.h @@ -56,7 +56,7 @@ typedef struct KiwiTerm { typedef struct KiwiExpression { double constant; int term_count; - KiwiConstraint* owner; + void* owner; #if defined(LJKIWI_LUAJIT_DEF) KiwiTerm terms_[?]; diff --git a/kiwi.lua b/kiwi.lua index 5c578aa..3163247 100644 --- a/kiwi.lua +++ b/kiwi.lua @@ -53,7 +53,7 @@ typedef struct KiwiTerm { typedef struct KiwiExpression { double constant; int term_count; - KiwiConstraint* owner; + void* owner; KiwiTerm terms_[?]; } KiwiExpression; @@ -227,7 +227,7 @@ local function new_expr_one(constant, var, coeff) dt.coefficient = coeff or 1.0 ret.constant = constant ret.term_count = 1 - ljkiwi.kiwi_var_retain(var) + ljkiwi.kiwi_expression_retain(ret) return ret end @@ -500,9 +500,11 @@ do end function Term_mt.__new(T, var, coefficient) - local t = ffi_new(T, var, coefficient or 1.0) + local t = ffi_gc(ffi_new(T), term_gc) --[[@as kiwi.Term]] ljkiwi.kiwi_var_retain(var) - return ffi_gc(t, term_gc) + t.var = var + t.coefficient = coefficient or 1.0 + return t end function Term_mt.__mul(a, b)