diff --git a/src/problem.c b/src/problem.c index 66a9278f..89a39fca 100644 --- a/src/problem.c +++ b/src/problem.c @@ -26,24 +26,14 @@ #include #include -problem *new_problem(expr *objective, expr **constraints, int n_constraints, - bool verbose) +/* Store the objective and constraints in the problem, which holds one + reference on each. */ +static void set_objective_and_constraints(problem *prob, expr *objective, + expr **constraints, int n_constraints) { -#ifdef SP_TRACK_MEMORY - /* Start the high-water mark at the current live count: allocations made - before new_problem (e.g. in left_matmul) stay counted, and their frees - subtract from the live count as usual. */ - sp_memory_reset_peak(); -#endif - problem *prob = (problem *) sp_calloc(1, sizeof(problem)); - if (!prob) return NULL; - - /* objective */ prob->objective = objective; expr_retain(objective); - prob->n_vars = objective->n_vars; - /* constraints array */ prob->total_constraint_size = 0; prob->n_constraints = n_constraints; if (n_constraints > 0) @@ -56,6 +46,22 @@ problem *new_problem(expr *objective, expr **constraints, int n_constraints, expr_retain(constraints[i]); } } +} + +problem *new_problem(expr *objective, expr **constraints, int n_constraints, + bool verbose) +{ +#ifdef SP_TRACK_MEMORY + /* Start the high-water mark at the current live count: allocations made + before new_problem (e.g. in left_matmul) stay counted, and their frees + subtract from the live count as usual. */ + sp_memory_reset_peak(); +#endif + problem *prob = (problem *) sp_calloc(1, sizeof(problem)); + if (!prob) return NULL; + + prob->n_vars = objective->n_vars; + set_objective_and_constraints(prob, objective, constraints, n_constraints); /* allocation */ prob->constraint_values =