Commit f5c146f3 authored by Shlomi Fish's avatar Shlomi Fish
Browse files

[fc-solve] recycle instances instead of freeing.

This is better for preventing memory fragmentation and may also be a
little faster. Some methods were extracted, library calls avoided,
and the code cleaned up.

====

I hereby disclaim any implicit or explicit ownership of my changes in this
changeset, and put them under a multiple licence consisting of your choice of
one of more of:

- The CC0 / Public Domain - https://creativecommons.org/choose/zero/ .

- The MIT / Expat license - https://en.wikipedia.org/wiki/MIT_License

- Any licence in the 2018-Aug-27 popular licenses list of
https://opensource.org/licenses

Crediting me will be nice, but not mandatory, and you can change the licence
of the project without needing my permission.
parent 51fe91c5
......@@ -167,11 +167,7 @@ SolverInterface::ExitStatus FcSolveSolver::patsolve( int _max_positions )
switch (solver_ret)
{
case FCS_STATE_IS_NOT_SOLVEABLE:
if (solver_instance)
{
freecell_solver_user_free(solver_instance);
solver_instance = NULL;
}
make_solver_instance_ready();
return Solver::NoSolutionExists;
case FCS_STATE_WAS_SOLVED:
......@@ -179,15 +175,10 @@ SolverInterface::ExitStatus FcSolveSolver::patsolve( int _max_positions )
if (solver_instance)
{
m_winMoves.clear();
while (freecell_solver_user_get_moves_left(solver_instance))
fcs_move_t move;
while (!freecell_solver_user_get_next_move(solver_instance, &move))
{
fcs_move_t move;
MOVE new_move;
const int verdict = !freecell_solver_user_get_next_move(
solver_instance, &move)
;
Q_ASSERT (verdict);
new_move.is_fcs = true;
new_move.fcs = move;
......@@ -195,8 +186,7 @@ SolverInterface::ExitStatus FcSolveSolver::patsolve( int _max_positions )
m_winMoves.append( new_move );
}
freecell_solver_user_free(solver_instance);
solver_instance = NULL;
make_solver_instance_ready();
}
return Solver::SolutionExists;
}
......@@ -205,11 +195,7 @@ SolverInterface::ExitStatus FcSolveSolver::patsolve( int _max_positions )
return Solver::UnableToDetermineSolvability;
default:
if (solver_instance)
{
freecell_solver_user_free(solver_instance);
solver_instance = NULL;
}
make_solver_instance_ready();
return Solver::NoSolutionExists;
}
}
......@@ -268,6 +254,15 @@ void FcSolveSolver::unpack_cluster( unsigned int)
return;
}
void FcSolveSolver::make_solver_instance_ready()
{
if (solver_instance && (solver_ret != FCS_STATE_NOT_BEGAN_YET))
{
freecell_solver_user_recycle(solver_instance);
solver_ret = FCS_STATE_NOT_BEGAN_YET;
}
}
FcSolveSolver::~FcSolveSolver()
{
if (solver_instance)
......
......@@ -48,6 +48,8 @@ public:
int solver_ret;
// More than enough space for two decks.
char board_as_string[4 * 13 * 2 * 4 * 3];
protected:
void make_solver_instance_ready();
};
#endif // ABSTRACT_FC_SOLVE_SOLVER_H
......@@ -469,11 +469,7 @@ void FreecellSolver::translate_layout()
{
strcpy(board_as_string, deal->solverFormat().toLatin1());
if (solver_instance)
{
freecell_solver_user_recycle(solver_instance);
solver_ret = FCS_STATE_NOT_BEGAN_YET;
}
make_solver_instance_ready();
#if 0
/* Read the workspace. */
int total = 0;
......
......@@ -418,11 +418,7 @@ void SimonSolver::translate_layout()
{
strcpy(board_as_string, deal->solverFormat().toLatin1());
if (solver_instance)
{
freecell_solver_user_recycle(solver_instance);
solver_ret = FCS_STATE_NOT_BEGAN_YET;
}
make_solver_instance_ready();
#if 0
/* Read the workspace. */
int total = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment