From 433da7a104035c2a39385464721af35f3558adf5 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Wed, 18 Feb 2015 01:15:33 +0100 Subject: [PATCH] Convert pkg_solved to kvec_t --- libpkg/Makefile.am | 1 + libpkg/pkg_cudf.c | 10 +++--- libpkg/pkg_jobs.c | 82 +++++++++++++++++++++++++++++------------- libpkg/pkg_solve.c | 6 ++-- libpkg/private/pkg.h | 1 + libpkg/private/pkg_jobs.h | 5 +-- libpkg/repo/binary/Makefile.am | 1 + 7 files changed, 71 insertions(+), 35 deletions(-) diff --git a/libpkg/Makefile.am b/libpkg/Makefile.am index 8849aae..2a4b10b 100644 --- a/libpkg/Makefile.am +++ b/libpkg/Makefile.am @@ -12,6 +12,7 @@ pkg_common_cflags= -I$(top_srcdir)/libpkg -I$(top_builddir)/libpkg \ -I$(top_srcdir)/external/sqlite \ -I$(top_srcdir)/external/include \ -I$(top_srcdir)/external/blake2 \ + -I$(top_srcdir)/external/libucl/klib \ -DPREFIX=\"$(prefix)\" \ -Wno-pointer-sign diff --git a/libpkg/pkg_cudf.c b/libpkg/pkg_cudf.c index 6a1cb0e..ab99271 100644 --- a/libpkg/pkg_cudf.c +++ b/libpkg/pkg_cudf.c @@ -328,7 +328,7 @@ cudf_strdup(const char *in) } static void -pkg_jobs_cudf_insert_res_job (struct pkg_solved **target, +pkg_jobs_cudf_insert_res_job(pkg_solved *target, struct pkg_job_universe_item *it_new, struct pkg_job_universe_item *it_old, int type) @@ -346,7 +346,7 @@ pkg_jobs_cudf_insert_res_job (struct pkg_solved **target, if (it_old != NULL) res->items[1] = it_old; - DL_APPEND(*target, res); + kv_push(struct pkg_solved *, *target, res); } struct pkg_cudf_entry { @@ -403,13 +403,13 @@ pkg_jobs_cudf_add_package(struct pkg_jobs *j, struct pkg_cudf_entry *entry) if (entry->installed && selected->pkg->type != PKG_INSTALLED) { pkg_debug(3, "pkg_cudf: schedule installation of %s(%d)", entry->uid, ver); - pkg_jobs_cudf_insert_res_job (&j->jobs, selected, NULL, PKG_SOLVED_INSTALL); + pkg_jobs_cudf_insert_res_job(&j->jobs, selected, NULL, PKG_SOLVED_INSTALL); j->count ++; } else if (!entry->installed && selected->pkg->type == PKG_INSTALLED) { pkg_debug(3, "pkg_cudf: schedule removing of %s(%d)", entry->uid, ver); - pkg_jobs_cudf_insert_res_job (&j->jobs, selected, NULL, PKG_SOLVED_DELETE); + pkg_jobs_cudf_insert_res_job(&j->jobs, selected, NULL, PKG_SOLVED_DELETE); j->count ++; } } @@ -426,7 +426,7 @@ pkg_jobs_cudf_add_package(struct pkg_jobs *j, struct pkg_cudf_entry *entry) assert(old != NULL); /* XXX: this is a hack due to iterators stupidity */ selected->pkg->old_version = old->pkg->version; - pkg_jobs_cudf_insert_res_job (&j->jobs, selected, old, PKG_SOLVED_UPGRADE); + pkg_jobs_cudf_insert_res_job(&j->jobs, selected, old, PKG_SOLVED_UPGRADE); j->count ++; } diff --git a/libpkg/pkg_jobs.c b/libpkg/pkg_jobs.c index 22cd50d..9ed2a25 100644 --- a/libpkg/pkg_jobs.c +++ b/libpkg/pkg_jobs.c @@ -94,6 +94,7 @@ pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db) (*j)->type = t; (*j)->solved = 0; (*j)->flags = PKG_FLAG_NONE; + kv_init((*j)->jobs); return (EPKG_OK); } @@ -160,6 +161,7 @@ void pkg_jobs_free(struct pkg_jobs *j) { struct pkg_job_request *req, *tmp; + struct pkg_solved *s; if (j == NULL) return; @@ -174,7 +176,13 @@ pkg_jobs_free(struct pkg_jobs *j) } pkg_jobs_universe_free(j->universe); - LL_FREE(j->jobs, free); + + while (kv_size(j->jobs)) { + s = kv_pop(j->jobs); + free(s); + } + kv_destroy(j->jobs); + HASH_FREE(j->patterns, pkg_jobs_pattern_free); free(j); } @@ -261,23 +269,25 @@ pkg_jobs_iter(struct pkg_jobs *jobs, void **iter, int *type) { struct pkg_solved *s; + unsigned int i; + assert(iter != NULL); - if (jobs->jobs == NULL) { + + i = (unsigned int)(uintptr_t)(*iter); + + if (kv_size(jobs->jobs) == 0) { return (false); } - if (*iter == NULL) { - s = jobs->jobs; - } - else if (*iter == jobs->jobs) { + if (i >= kv_size(jobs->jobs)) { return (false); } - else { - s = *iter; - } + s = kv_A(jobs->jobs, i); *new = s->items[0]->pkg; *old = s->items[1] ? s->items[1]->pkg : NULL; *type = s->type; - *iter = s->next ? s->next : jobs->jobs; + i++; + *iter = (void *)(uintptr_t)i; + return (true); } @@ -593,7 +603,7 @@ pkg_jobs_set_execute_priority(struct pkg_jobs *j, struct pkg_solved *solved) ts->items[0] = solved->items[1]; solved->items[1] = NULL; solved->type = PKG_SOLVED_INSTALL; - DL_APPEND(j->jobs, ts); + kv_push(struct pkg_solved *, j->jobs, ts); j->count ++; solved->already_deleted = true; pkg_debug(2, "split upgrade request for %s", @@ -616,8 +626,11 @@ pkg_jobs_set_execute_priority(struct pkg_jobs *j, struct pkg_solved *solved) } static int -pkg_jobs_sort_priority(struct pkg_solved *r1, struct pkg_solved *r2) +pkg_jobs_sort_priority(const void *e1, const void *e2) { + const struct pkg_solved *r1 = (const struct pkg_solved *)e1; + const struct pkg_solved *r2 = (const struct pkg_solved *)e2; + if (r1->items[0]->priority == r2->items[0]->priority) { if (r1->type == PKG_SOLVED_DELETE && r2->type != PKG_SOLVED_DELETE) return (-1); @@ -632,20 +645,23 @@ pkg_jobs_sort_priority(struct pkg_solved *r1, struct pkg_solved *r2) static void pkg_jobs_set_priorities(struct pkg_jobs *j) { + unsigned int i; struct pkg_solved *req; iter_again: - LL_FOREACH(j->jobs, req) { + for (i = 0; i < kv_size(j->jobs); i++) { + req = kv_A(j->jobs, i); req->items[0]->priority = 0; if (req->items[1] != NULL) req->items[1]->priority = 0; } - LL_FOREACH(j->jobs, req) { + for (i = 0; i < kv_size(j->jobs); i++) { + req = kv_A(j->jobs, i); if (pkg_jobs_set_execute_priority(j, req) == EPKG_CONFLICT) goto iter_again; } - DL_SORT(j->jobs, pkg_jobs_sort_priority); + qsort(j->jobs.a, kv_size(j->jobs), sizeof(struct pkg_solved), pkg_jobs_sort_priority); } @@ -1264,8 +1280,10 @@ pkg_jobs_set_deinstall_reasons(struct pkg_jobs *j) struct pkg_solved *sit; struct pkg_job_request *jreq; struct pkg *req_pkg, *pkg; + unsigned int i; - LL_FOREACH(j->jobs, sit) { + for (i = 0; i < kv_size(j->jobs); i++) { + sit = kv_A(j->jobs, i); jreq = pkg_jobs_find_deinstall_request(sit->items[0], j, 0); if (jreq != NULL && jreq->item->unit != sit->items[0]) { req_pkg = jreq->item->pkg; @@ -1605,6 +1623,7 @@ pkg_jobs_solve(struct pkg_jobs *j) const char *solver; FILE *spipe[2]; pid_t pchild; + unsigned int i; pkgdb_begin_solver(j->db); @@ -1699,7 +1718,8 @@ again: pkg_jobs_apply_replacements(j); /* Check if we need to fetch and re-run the solver */ - DL_FOREACH(j->jobs, job) { + for (i = 0; i < kv_size(j->jobs); i++) { + job = kv_A(j->jobs, i); struct pkg *p; p = job->items[0]->pkg; @@ -1721,8 +1741,10 @@ again: rc = pkg_jobs_check_conflicts(j); if (rc == EPKG_CONFLICT) { /* Cleanup results */ - LL_FREE(j->jobs, free); - j->jobs = NULL; + while (kv_size(j->jobs) > 0) { + job = kv_pop(j->jobs); + free(job); + } j->count = 0; has_conflicts = true; rc = pkg_jobs_solve(j); @@ -1831,6 +1853,7 @@ pkg_jobs_execute(struct pkg_jobs *j) struct pkg_manifest_key *keys = NULL; int flags = 0; int retcode = EPKG_FATAL; + unsigned int i; if (j->flags & PKG_FLAG_SKIP_INSTALL) return (EPKG_OK); @@ -1855,7 +1878,8 @@ pkg_jobs_execute(struct pkg_jobs *j) pkg_jobs_set_priorities(j); - DL_FOREACH(j->jobs, ps) { + for (i = 0; i < kv_size(j->jobs); i++) { + ps = kv_A(j->jobs, i); switch (ps->type) { case PKG_SOLVED_DELETE: case PKG_SOLVED_UPGRADE_REMOVE: @@ -1911,6 +1935,7 @@ pkg_jobs_apply(struct pkg_jobs *j) int rc; pkg_plugin_hook_t pre, post; bool has_conflicts = false; + struct pkg_solved *job; if (!j->solved) { pkg_emit_error("The jobs hasn't been solved"); @@ -1951,8 +1976,10 @@ pkg_jobs_apply(struct pkg_jobs *j) rc = pkg_jobs_check_conflicts(j); if (rc == EPKG_CONFLICT) { /* Cleanup results */ - LL_FREE(j->jobs, free); - j->jobs = NULL; + while (kv_size(j->jobs) > 0) { + job = kv_pop(j->jobs); + free(job); + } j->count = 0; has_conflicts = true; rc = pkg_jobs_solve(j); @@ -2010,6 +2037,7 @@ pkg_jobs_fetch(struct pkg_jobs *j) const char *cachedir = NULL; char cachedpath[MAXPATHLEN]; bool mirror = (j->flags & PKG_FLAG_FETCH_MIRROR) ? true : false; + unsigned int i; if (j->destdir == NULL || !mirror) @@ -2018,7 +2046,8 @@ pkg_jobs_fetch(struct pkg_jobs *j) cachedir = j->destdir; /* check for available size to fetch */ - DL_FOREACH(j->jobs, ps) { + for (i = 0; i < kv_size(j->jobs); i++) { + ps = kv_A(j->jobs, i); if (ps->type != PKG_SOLVED_DELETE && ps->type != PKG_SOLVED_UPGRADE_REMOVE) { p = ps->items[0]->pkg; if (p->type != PKG_REMOTE) @@ -2083,7 +2112,8 @@ pkg_jobs_fetch(struct pkg_jobs *j) return (EPKG_OK); /* don't download anything */ /* Fetch */ - DL_FOREACH(j->jobs, ps) { + for (i = 0; i < kv_size(j->jobs); i++) { + ps = kv_A(j->jobs, i); if (ps->type != PKG_SOLVED_DELETE && ps->type != PKG_SOLVED_UPGRADE_REMOVE) { p = ps->items[0]->pkg; @@ -2110,11 +2140,13 @@ pkg_jobs_check_conflicts(struct pkg_jobs *j) struct pkg_solved *ps; struct pkg *p = NULL; int ret = EPKG_OK, res, added = 0; + unsigned int i; pkg_emit_integritycheck_begin(); j->conflicts_registered = 0; - DL_FOREACH(j->jobs, ps) { + for (i = 0; i < kv_size(j->jobs); i++) { + ps = kv_A(j->jobs, i); if (ps->type == PKG_SOLVED_DELETE || ps->type == PKG_SOLVED_UPGRADE_REMOVE) { continue; } diff --git a/libpkg/pkg_solve.c b/libpkg/pkg_solve.c index d7bf4c2..c3f28ac 100644 --- a/libpkg/pkg_solve.c +++ b/libpkg/pkg_solve.c @@ -994,7 +994,7 @@ pkg_solve_insert_res_job (struct pkg_solve_variable *var, res->items[0] = add_var->unit; res->type = (j->type == PKG_JOBS_FETCH) ? PKG_SOLVED_FETCH : PKG_SOLVED_INSTALL; - DL_APPEND(j->jobs, res); + kv_push(struct pkg_solved *, j->jobs, res); pkg_debug(3, "pkg_solve: schedule installation of %s %s", add_var->uid, add_var->digest); } @@ -1003,7 +1003,7 @@ pkg_solve_insert_res_job (struct pkg_solve_variable *var, res->items[0] = add_var->unit; res->items[1] = del_var->unit; res->type = PKG_SOLVED_UPGRADE; - DL_APPEND(j->jobs, res); + kv_push(struct pkg_solved *, j->jobs, res); pkg_debug(3, "pkg_solve: schedule upgrade of %s from %s to %s", del_var->uid, del_var->digest, add_var->digest); } @@ -1027,7 +1027,7 @@ pkg_solve_insert_res_job (struct pkg_solve_variable *var, } res->items[0] = cur_var->unit; res->type = PKG_SOLVED_DELETE; - DL_APPEND(j->jobs, res); + kv_push(struct pkg_solved *, j->jobs, res); pkg_debug(3, "pkg_solve: schedule deletion of %s %s", cur_var->uid, cur_var->digest); j->count ++; diff --git a/libpkg/private/pkg.h b/libpkg/private/pkg.h index 8e594f6..7be37b0 100644 --- a/libpkg/private/pkg.h +++ b/libpkg/private/pkg.h @@ -44,6 +44,7 @@ #include #include #include +#include #include "private/utils.h" diff --git a/libpkg/private/pkg_jobs.h b/libpkg/private/pkg_jobs.h index a653a4c..b1b0e4f 100644 --- a/libpkg/private/pkg_jobs.h +++ b/libpkg/private/pkg_jobs.h @@ -66,9 +66,10 @@ struct pkg_solved { struct pkg_job_universe_item *items[2]; pkg_solved_t type; bool already_deleted; - struct pkg_solved *prev, *next; }; +typedef kvec_t(struct pkg_solved *) pkg_solved; + struct pkg_job_seen { struct pkg_job_universe_item *un; const char *digest; @@ -108,7 +109,7 @@ struct pkg_jobs { struct pkg_jobs_universe *universe; struct pkg_job_request *request_add; struct pkg_job_request *request_delete; - struct pkg_solved *jobs; + pkg_solved jobs; struct pkgdb *db; pkg_jobs_t type; pkg_flags flags; diff --git a/libpkg/repo/binary/Makefile.am b/libpkg/repo/binary/Makefile.am index a62bcad..6335639 100644 --- a/libpkg/repo/binary/Makefile.am +++ b/libpkg/repo/binary/Makefile.am @@ -4,6 +4,7 @@ pkg_common_cflags= -I$(top_srcdir)/libpkg -I$(top_builddir)/libpkg \ @LDNS_CFLAGS@ \ -I$(top_srcdir)/external/expat/lib \ -I$(top_srcdir)/external/libucl/include \ + -I$(top_srcdir)/external/libucl/klib \ -I$(top_srcdir)/external/uthash \ -I$(top_srcdir)/external/sqlite \ -Wno-pointer-sign -- 2.3.0