Skip to content

Commit

Permalink
Slightly more robust resource management
Browse files Browse the repository at this point in the history
  • Loading branch information
jkl1337 committed Feb 26, 2024
1 parent 94a8bdc commit c35cea6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
44 changes: 27 additions & 17 deletions ckiwi/ckiwi.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "ljkiwi.hpp"
#include "ckiwi.h"

#include <kiwi/kiwi.h>

#include <cstdlib>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>

Expand Down Expand Up @@ -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<typename F>
const KiwiErr* wrap_err(F&& f) {
Expand All @@ -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."};
Expand Down Expand Up @@ -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<ConstraintData*>(expr->owner));
}
}

Expand All @@ -209,7 +219,7 @@ KiwiConstraint* kiwi_constraint_construct(
}

std::vector<Term> terms;
terms.reserve(static_cast<std::size_t>(
terms.reserve(static_cast<decltype(terms)::size_type>(
(lhs && lhs->term_count > 0 ? lhs->term_count : 0)
+ (rhs && rhs->term_count > 0 ? rhs->term_count : 0)
));
Expand Down
2 changes: 1 addition & 1 deletion ckiwi/ckiwi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_[?];
Expand Down
10 changes: 6 additions & 4 deletions kiwi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ typedef struct KiwiTerm {
typedef struct KiwiExpression {
double constant;
int term_count;
KiwiConstraint* owner;
void* owner;
KiwiTerm terms_[?];
} KiwiExpression;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c35cea6

Please sign in to comment.