Skip to content

Commit

Permalink
Rename allocation function, use LuaJIT side allocation for solver
Browse files Browse the repository at this point in the history
  • Loading branch information
jkl1337 committed Mar 4, 2024
1 parent 534db83 commit 9b0f507
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
22 changes: 18 additions & 4 deletions ckiwi/ckiwi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ T* retain_unmanaged(T* p) {

extern "C" {

KiwiVar* kiwi_var_construct(const char* name) {
KiwiVar* kiwi_var_new(const char* name) {
return make_unmanaged<VariableData>(lk_likely(name) ? name : "");
}

Expand Down Expand Up @@ -208,7 +208,7 @@ void kiwi_expression_destroy(KiwiExpression* expr) {
}
}

KiwiConstraint* kiwi_constraint_construct(
KiwiConstraint* kiwi_constraint_new(
const KiwiExpression* lhs,
const KiwiExpression* rhs,
enum KiwiRelOp op,
Expand Down Expand Up @@ -293,15 +293,29 @@ struct KiwiSolver {
Solver solver;
};

KiwiSolver* kiwi_solver_construct(unsigned error_mask) {
void kiwi_solver_type_info(unsigned sz_align[2]) {
sz_align[0] = sizeof(KiwiSolver);
sz_align[1] = alignof(KiwiSolver);
}

KiwiSolver* kiwi_solver_new(unsigned error_mask) {
return new KiwiSolver {error_mask};
}

void kiwi_solver_destroy(KiwiSolver* s) {
void kiwi_solver_free(KiwiSolver* s) {
if (lk_likely(s))
delete s;
}

void kiwi_solver_init(KiwiSolver* s, unsigned error_mask) {
new (s) KiwiSolver {error_mask};
}

void kiwi_solver_destroy(KiwiSolver* s) {
if (lk_likely(s))
s->~KiwiSolver();
}

unsigned kiwi_solver_get_error_mask(const KiwiSolver* s) {
return lk_likely(s) ? s->error_mask : 0;
}
Expand Down
9 changes: 6 additions & 3 deletions ckiwi/ckiwi.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ typedef struct KiwiErr {
} KiwiErr;

struct KiwiSolver;
LJKIWI_EXP void kiwi_solver_type_info(unsigned sz_align[2]);

LJKIWI_EXP KiwiVar* kiwi_var_construct(const char* name);
LJKIWI_EXP KiwiVar* kiwi_var_new(const char* name);
LJKIWI_EXP void kiwi_var_release(KiwiVar* var);
LJKIWI_EXP void kiwi_var_retain(KiwiVar* var);

Expand All @@ -88,7 +89,7 @@ LJKIWI_EXP void kiwi_var_set_value(KiwiVar* var, double value);
LJKIWI_EXP void kiwi_expression_retain(KiwiExpression* expr);
LJKIWI_EXP void kiwi_expression_destroy(KiwiExpression* expr);

LJKIWI_EXP KiwiConstraint* kiwi_constraint_construct(
LJKIWI_EXP KiwiConstraint* kiwi_constraint_new(
const KiwiExpression* lhs,
const KiwiExpression* rhs,
enum KiwiRelOp op,
Expand All @@ -102,7 +103,9 @@ LJKIWI_EXP enum KiwiRelOp kiwi_constraint_op(const KiwiConstraint* c);
LJKIWI_EXP bool kiwi_constraint_violated(const KiwiConstraint* c);
LJKIWI_EXP int kiwi_constraint_expression(KiwiConstraint* c, KiwiExpression* out, int out_size);

LJKIWI_EXP KiwiSolver* kiwi_solver_construct(unsigned error_mask);
LJKIWI_EXP KiwiSolver* kiwi_solver_new(unsigned error_mask);
LJKIWI_EXP void kiwi_solver_free(KiwiSolver* s);
LJKIWI_EXP void kiwi_solver_init(KiwiSolver* s, unsigned error_mask);
LJKIWI_EXP void kiwi_solver_destroy(KiwiSolver* s);
LJKIWI_EXP unsigned kiwi_solver_get_error_mask(const KiwiSolver* s);
LJKIWI_EXP void kiwi_solver_set_error_mask(KiwiSolver* s, unsigned mask);
Expand Down
35 changes: 23 additions & 12 deletions kiwi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ do
end
kiwi.ljkiwi = ljkiwi

do
ffi.cdef("void kiwi_solver_type_info(unsigned sz_align[2]);")
local ti = ffi.new("unsigned[2]")
ljkiwi.kiwi_solver_type_info(ti)
ffi.cdef(
"typedef struct KiwiSolver { unsigned char b_[$]; } __attribute__((aligned($))) KiwiSolver;",
ti[0],
ti[1]
)
end

ffi.cdef([[
void free(void *);
typedef struct KiwiVar KiwiVar;
typedef struct KiwiConstraint KiwiConstraint;
typedef struct KiwiSolver KiwiSolver;]])
typedef struct KiwiConstraint KiwiConstraint;]])

ffi.cdef([[
enum KiwiErrKind {
Expand Down Expand Up @@ -66,7 +76,7 @@ typedef struct KiwiErr {
struct KiwiSolver;
KiwiVar* kiwi_var_construct(const char* name);
KiwiVar* kiwi_var_new(const char* name);
void kiwi_var_release(KiwiVar* var);
void kiwi_var_retain(KiwiVar* var);
Expand All @@ -78,7 +88,7 @@ void kiwi_var_set_value(KiwiVar* var, double value);
void kiwi_expression_retain(KiwiExpression* expr);
void kiwi_expression_destroy(KiwiExpression* expr);
KiwiConstraint* kiwi_constraint_construct(
KiwiConstraint* kiwi_constraint_new(
const KiwiExpression* lhs,
const KiwiExpression* rhs,
enum KiwiRelOp op,
Expand All @@ -92,7 +102,7 @@ enum KiwiRelOp kiwi_constraint_op(const KiwiConstraint* c);
bool kiwi_constraint_violated(const KiwiConstraint* c);
int kiwi_constraint_expression(KiwiConstraint* c, KiwiExpression* out, int out_size);
KiwiSolver* kiwi_solver_construct(unsigned error_mask);
void kiwi_solver_init(KiwiSolver* s, unsigned error_mask);
void kiwi_solver_destroy(KiwiSolver* s);
unsigned kiwi_solver_get_error_mask(const KiwiSolver* s);
void kiwi_solver_set_error_mask(KiwiSolver* s, unsigned mask);
Expand Down Expand Up @@ -317,7 +327,7 @@ local function rel(lhs, rhs, op, strength)
end

return ffi_gc(
ljkiwi.kiwi_constraint_construct(el, er, op, strength or REQUIRED),
ljkiwi.kiwi_constraint_new(el, er, op, strength or REQUIRED),
ljkiwi.kiwi_constraint_release
) --[[@as kiwi.Constraint]]
end
Expand Down Expand Up @@ -405,7 +415,7 @@ do
}

function Var_mt:__new(name)
return ffi_gc(ljkiwi.kiwi_var_construct(name), ljkiwi.kiwi_var_release)
return ffi_gc(ljkiwi.kiwi_var_new(name), ljkiwi.kiwi_var_release)
end

function Var_mt.__mul(a, b)
Expand Down Expand Up @@ -785,7 +795,7 @@ do

function Constraint_mt:__new(lhs, rhs, op, strength)
return ffi_gc(
ljkiwi.kiwi_constraint_construct(lhs, rhs, op or "EQ", strength or REQUIRED),
ljkiwi.kiwi_constraint_new(lhs, rhs, op or "EQ", strength or REQUIRED),
ljkiwi.kiwi_constraint_release
)
end
Expand Down Expand Up @@ -834,7 +844,7 @@ do
tmpexpr.term_count = 2

return ffi_gc(
ljkiwi.kiwi_constraint_construct(tmpexpr, nil, op or "EQ", strength or REQUIRED),
ljkiwi.kiwi_constraint_new(tmpexpr, nil, op or "EQ", strength or REQUIRED),
ljkiwi.kiwi_constraint_release
) --[[@as kiwi.Constraint]]
end
Expand Down Expand Up @@ -871,7 +881,7 @@ do
t.coefficient = 1.0

return ffi_gc(
ljkiwi.kiwi_constraint_construct(tmpexpr, nil, op or "EQ", strength or REQUIRED),
ljkiwi.kiwi_constraint_new(tmpexpr, nil, op or "EQ", strength or REQUIRED),
ljkiwi.kiwi_constraint_release
) --[[@as kiwi.Constraint]]
end
Expand Down Expand Up @@ -1153,8 +1163,9 @@ do
if type(error_mask) == "table" then
error_mask = kiwi.error_mask(error_mask)
end

return ffi_gc(ljkiwi.kiwi_solver_construct(error_mask or 0), ljkiwi.kiwi_solver_destroy) --[[@as kiwi.Constraint]]
local s = ffi_new(self)
ljkiwi.kiwi_solver_init(s, error_mask or 0)
return ffi_gc(s, ljkiwi.kiwi_solver_destroy)
end

local Solver = ffi.metatype(ffi.typeof("struct KiwiSolver"), Solver_mt) --[[@as kiwi.Solver]]
Expand Down

0 comments on commit 9b0f507

Please sign in to comment.