]> git.ipfire.org Git - pakfire.git/commitdiff
Refactor how solutions are being created/freed
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 2 Jul 2021 16:04:36 +0000 (16:04 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 2 Jul 2021 16:04:36 +0000 (16:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/solution.h
src/libpakfire/problem.c
src/libpakfire/solution.c

index 4e36f73abb46a705637cc0635ee6eec8f19c03b2..561006e530ece0daad3941ebe7f2aaa63fa41aae 100644 (file)
@@ -27,7 +27,8 @@ struct pakfire_solution;
 
 #include <pakfire/problem.h>
 
-struct pakfire_solution* pakfire_solution_create(struct pakfire_problem* problem, Id id);
+int pakfire_solution_create(struct pakfire_solution** solution,
+       struct pakfire_problem* problem, Id id);
 struct pakfire_solution* pakfire_solution_ref(struct pakfire_solution* solution);
 struct pakfire_solution* pakfire_solution_unref(struct pakfire_solution* solution);
 
index 2e8ec982fdc852307bd491cf58e1e73a69ac2997..e929509030ed3fe7138e270e75d9bcecb7cdd793 100644 (file)
@@ -263,12 +263,13 @@ PAKFIRE_EXPORT struct pakfire_request* pakfire_problem_get_request(struct pakfir
 }
 
 PAKFIRE_EXPORT struct pakfire_solution* pakfire_problem_get_solutions(struct pakfire_problem* problem) {
+       struct pakfire_solution* s = NULL;
        struct pakfire_solution* ret = NULL;
        Solver* solver = pakfire_request_get_solver(problem->request);
 
        Id solution = 0;
        while ((solution = solver_next_solution(solver, problem->id, solution)) != 0) {
-               struct pakfire_solution* s = pakfire_solution_create(problem, solution);
+               pakfire_solution_create(&s, problem, solution);
 
                if (ret)
                        pakfire_solution_append(ret, s);
index 2459b8287aaab8d606e00ea1d9e0eb5a1887f345..841150f40ee9a3906308780eedfb33582daedf1a 100644 (file)
@@ -120,25 +120,24 @@ static void import_elements(struct pakfire_solution* solution) {
        pakfire_request_unref(request);
 }
 
-PAKFIRE_EXPORT struct pakfire_solution* pakfire_solution_create(struct pakfire_problem* problem, Id id) {
-       Pakfire pakfire = pakfire_problem_get_pakfire(problem);
+PAKFIRE_EXPORT int pakfire_solution_create(struct pakfire_solution** solution,
+               struct pakfire_problem* problem, Id id) {
+       struct pakfire_solution* s = calloc(1, sizeof(*s));
+       if (!s)
+               return 1;
 
-       struct pakfire_solution* solution = calloc(1, sizeof(*solution));
-       if (solution) {
-               DEBUG(pakfire, "Allocated Solution at %p\n", solution);
-               solution->pakfire = pakfire_ref(pakfire);
-               solution->nrefs = 1;
+       s->pakfire = pakfire_problem_get_pakfire(problem);
+       s->nrefs = 1;
 
-               solution->problem = pakfire_problem_ref(problem);
-               solution->id = id;
+       // Store a reference to the problem
+       s->problem = pakfire_problem_ref(problem);
+       s->id = id;
 
-               // Extract information from solver
-               import_elements(solution);
-       }
-
-       pakfire_unref(pakfire);
+       // Extract information from solver
+       import_elements(s);
 
-       return solution;
+       *solution = s;
+       return 0;
 }
 
 PAKFIRE_EXPORT struct pakfire_solution* pakfire_solution_ref(struct pakfire_solution* solution) {
@@ -148,7 +147,6 @@ PAKFIRE_EXPORT struct pakfire_solution* pakfire_solution_ref(struct pakfire_solu
 }
 
 static void pakfire_solution_free(struct pakfire_solution* solution) {
-       DEBUG(solution->pakfire, "Releasing Solution at %p\n", solution);
        pakfire_unref(solution->pakfire);
 
        if (solution->next)