Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
refacto: convert pkgdb's internal list to vectors
Baptiste Daroussin committed 1 year ago
commit 2729f5b591f82d2c735d48dd7b2c444beee1e972
parent 9d2555e
4 files changed +58 -100
modified libpkg/pkgdb.c
@@ -846,7 +846,7 @@ pkgdb_open_repos(struct pkgdb *db, const char *reponame)
			/* We need read only access here */
			if (r->ops->open(r, R_OK) == EPKG_OK) {
				r->ops->init(r);
-
				tll_push_front(db->repos, r);
+
				vec_push(&db->repos, r);
			} else
				pkg_emit_error("Repository %s cannot be opened."
				    " 'pkg update' required", r->name);
@@ -1146,7 +1146,7 @@ pkgdb_close(struct pkgdb *db)

	if (db->sqlite != NULL) {

-
		tll_free_and_free(db->repos, pkgdb_free_repo);
+
		vec_free_and_free(&db->repos, pkgdb_free_repo);

		if (!sqlite3_db_readonly(db->sqlite, "main"))
			pkg_plugins_hook_run(PKG_PLUGIN_HOOK_PKGDB_CLOSE_RW, NULL, db);
@@ -2884,14 +2884,14 @@ pkgdb_stats(struct pkgdb *db, pkg_stats_t type)
	case PKG_STATS_REMOTE_UNIQUE:
	case PKG_STATS_REMOTE_COUNT:
	case PKG_STATS_REMOTE_SIZE:
-
		tll_foreach(db->repos, rit) {
-
			if (rit->item->ops->stat != NULL)
-
				stats += rit->item->ops->stat(rit->item, type);
+
		vec_foreach(db->repos, i) {
+
			if (db->repos.d[i]->ops->stat != NULL)
+
				stats += db->repos.d[i]->ops->stat(db->repos.d[i], type);
		}
		return (stats);
		break;
	case PKG_STATS_REMOTE_REPOS:
-
		return (tll_length(db->repos));
+
		return (vec_len(&db->repos));
		break;
	}

modified libpkg/pkgdb_iterator.c
@@ -1118,16 +1118,15 @@ pkgdb_it_next(struct pkgdb_it *it, struct pkg **pkg_p, unsigned flags)
			return (r);
	}

-
	if (tll_length(it->remote) != 0) {
-
		if (it->opq_it == NULL)
-
			it->opq_it = it->remote.head;
-
		__typeof__(*(it->remote).head) *lit = it->opq_it;
-
		rit = lit->item;
+
	if (vec_len(&it->remote) != 0) {
+
		if (it->remote_pos >= it->remote.len)
+
			it->remote_pos = 0;
+
		struct pkg_repo_it *rit = it->remote.d[it->remote_pos];
		ret = rit->ops->next(rit, pkg_p, flags);
		if (ret != EPKG_OK) {
-
			if (it->opq_it == it->remote.tail)
+
			if (it->remote_pos == it->remote.len -1 )
				return (EPKG_END);
-
			it->opq_it = lit->next;
+
			it->remote_pos++;
			return (pkgdb_it_next(it, pkg_p, flags));
		}

@@ -1182,8 +1181,8 @@ pkgdb_it_reset(struct pkgdb_it *it)
	if (it->local != NULL) {
		pkgdb_sqlite_it_reset(it->local);
	}
-
	tll_foreach(it->remote, cur) {
-
		cur->item->ops->reset(cur->item);
+
	vec_foreach(it->remote, i) {
+
		it->remote.d[i]->ops->reset(it->remote.d[i]);
	}
}

@@ -1197,7 +1196,7 @@ pkgdb_it_free(struct pkgdb_it *it)
		pkgdb_sqlite_it_free(it->local);
		free(it->local);
	}
-
	tll_free_and_free(it->remote, remote_free);
+
	vec_free_and_free(&it->remote, remote_free);

	free(it);
}
@@ -1221,7 +1220,7 @@ pkgdb_it_new_sqlite(struct pkgdb *db, sqlite3_stmt *s, int type, short flags)

	it->local->flags = flags;
	it->local->finished = 0;
-
	it->opq_it = it->remote.head;
+
	it->remote_pos = 0;

	return (it);
}
@@ -1241,7 +1240,7 @@ pkgdb_it_new_repo(struct pkgdb *db)
void
pkgdb_it_repo_attach(struct pkgdb_it *it, struct pkg_repo_it *rit)
{
-
	tll_push_front(it->remote, rit);
+
	vec_push(&it->remote, rit);
}

int
@@ -1267,10 +1266,10 @@ pkgdb_ensure_loaded(struct pkgdb *db, struct pkg *pkg, unsigned flags)
	if (pkg->type == PKG_INSTALLED)
		return (pkgdb_ensure_loaded_sqlite(db->sqlite, pkg, flags));

-
	tll_foreach(db->repos, cur) {
-
		if (cur->item == pkg->repo) {
-
			if (cur->item->ops->ensure_loaded) {
-
				return (cur->item->ops->ensure_loaded(cur->item,
+
	vec_foreach(db->repos, i) {
+
		if (db->repos.d[i] == pkg->repo) {
+
			if (db->repos.d[i]->ops->ensure_loaded) {
+
				return (db->repos.d[i]->ops->ensure_loaded(db->repos.d[i],
				    pkg, flags));
			}
		}
modified libpkg/pkgdb_query.c
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2011-2022 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2025 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * Copyright (c) 2011 Will Andrews <will@FreeBSD.org>
 * Copyright (c) 2011 Philippe Pepiot <phil@philpep.org>
@@ -10,28 +10,8 @@
 * Copyright (c) 2013-2014 Vsevolod Stakhov <vsevolod@FreeBSD.org>
 * Copyright (c) 2023 Serenity Cyber Security, LLC
 *                    Author: Gleb Popov <arrowd@FreeBSD.org>
-
 * All rights reserved.
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer
-
 *    in this position and unchanged.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
-
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
-
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "pkg/vec.h"
@@ -423,12 +403,12 @@ pkgdb_repo_query_cond2(struct pkgdb *db, const char *cond, const char *pattern,
	if (it == NULL)
		return (NULL);

-
	tll_foreach(db->repos, cur) {
-
		if (consider_this_repo(repos, cur->item->name)) {
+
	vec_foreach(db->repos, i) {
+
		if (consider_this_repo(repos, db->repos.d[i]->name)) {
			if (pattern != NULL && *pattern == '@')
-
				rit = cur->item->ops->groupquery(cur->item, pattern + 1, match);
+
				rit = db->repos.d[i]->ops->groupquery(db->repos.d[i], pattern + 1, match);
			else
-
				rit = cur->item->ops->query(cur->item, cond, pattern, match);
+
				rit = db->repos.d[i]->ops->query(db->repos.d[i], cond, pattern, match);
			if (rit != NULL)
				pkgdb_it_repo_attach(it, rit);
		}
@@ -459,10 +439,10 @@ pkgdb_repo_shlib_require(struct pkgdb *db, const char *require, c_charv_t *repos
	if (it == NULL)
		return (NULL);

-
	tll_foreach(db->repos, cur) {
-
		if (consider_this_repo(repos, cur->item->name)) {
-
			if (cur->item->ops->shlib_required != NULL) {
-
				rit = cur->item->ops->shlib_required(cur->item, require);
+
	vec_foreach(db->repos, i) {
+
		if (consider_this_repo(repos, db->repos.d[i]->name)) {
+
			if (db->repos.d[i]->ops->shlib_required != NULL) {
+
				rit = db->repos.d[i]->ops->shlib_required(db->repos.d[i], require);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
@@ -482,10 +462,10 @@ pkgdb_repo_shlib_provide(struct pkgdb *db, const char *require, c_charv_t *repos
	if (it == NULL)
		return (NULL);

-
	tll_foreach(db->repos, cur) {
-
		if (consider_this_repo(repos, cur->item->name)) {
-
			if (cur->item->ops->shlib_required != NULL) {
-
				rit = cur->item->ops->shlib_provided(cur->item, require);
+
	vec_foreach(db->repos, i) {
+
		if (consider_this_repo(repos, db->repos.d[i]->name)) {
+
			if (db->repos.d[i]->ops->shlib_required != NULL) {
+
				rit = db->repos.d[i]->ops->shlib_provided(db->repos.d[i], require);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
@@ -505,10 +485,10 @@ pkgdb_repo_require(struct pkgdb *db, const char *require, c_charv_t *repo)
	if (it == NULL)
		return (NULL);

-
	tll_foreach(db->repos, cur) {
-
		if (consider_this_repo(repo, cur->item->name)) {
-
			if (cur->item->ops->required != NULL) {
-
				rit = cur->item->ops->required(cur->item, require);
+
	vec_foreach(db->repos, i) {
+
		if (consider_this_repo(repo, db->repos.d[i]->name)) {
+
			if (db->repos.d[i]->ops->required != NULL) {
+
				rit = db->repos.d[i]->ops->required(db->repos.d[i], require);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
@@ -528,10 +508,10 @@ pkgdb_repo_provide(struct pkgdb *db, const char *require, c_charv_t *repo)
	if (it == NULL)
		return (NULL);

-
	tll_foreach(db->repos, cur) {
-
		if (consider_this_repo(repo, cur->item->name)) {
-
			if (cur->item->ops->required != NULL) {
-
				rit = cur->item->ops->provided(cur->item, require);
+
	vec_foreach(db->repos, i) {
+
		if (consider_this_repo(repo, db->repos.d[i]->name)) {
+
			if (db->repos.d[i]->ops->required != NULL) {
+
				rit = db->repos.d[i]->ops->provided(db->repos.d[i], require);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
@@ -569,16 +549,16 @@ pkgdb_repo_search2(struct pkgdb *db, const char *pattern, match_t match,
	if (it == NULL)
		return (NULL);

-
	tll_foreach(db->repos, cur) {
-
		if (consider_this_repo(repos, cur->item->name)) {
-
			if (cur->item->ops->search != NULL) {
-
				rit = cur->item->ops->search(cur->item, pattern, match,
+
	vec_foreach(db->repos, i) {
+
		if (consider_this_repo(repos, db->repos.d[i]->name)) {
+
			if (db->repos.d[i]->ops->search != NULL) {
+
				rit = db->repos.d[i]->ops->search(db->repos.d[i], pattern, match,
					field, sort);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
-
			if (cur->item->ops->groupsearch != NULL) {
-
				rit = cur->item->ops->groupsearch(cur->item, pattern, match, field);
+
			if (db->repos.d[i]->ops->groupsearch != NULL) {
+
				rit = db->repos.d[i]->ops->groupsearch(db->repos.d[i], pattern, match, field);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
@@ -617,10 +597,10 @@ pkgdb_all_search2(struct pkgdb *db, const char *pattern, match_t match,

	it = pkgdb_query(db, pattern, match);

-
	tll_foreach(db->repos, cur) {
-
		if (consider_this_repo(repos, cur->item->name)) {
-
			if (cur->item->ops->search != NULL) {
-
				rit = cur->item->ops->search(cur->item, pattern, match,
+
	vec_foreach(db->repos, i) {
+
		if (consider_this_repo(repos, db->repos.d[i]->name)) {
+
			if (db->repos.d[i]->ops->search != NULL) {
+
				rit = db->repos.d[i]->ops->search(db->repos.d[i], pattern, match,
					field, sort);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
modified libpkg/private/pkgdb.h
@@ -1,30 +1,9 @@
/*-
-
 * Copyright (c) 2011-2022 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2025 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * Copyright (c) 2023 Serenity Cyber Security, LLC
 *                    Author: Gleb Popov <arrowd@FreeBSD.org>
-
 * All rights reserved.
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer
-
 *    in this position and unchanged.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
-
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
-
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _PKGDB_H
@@ -33,13 +12,13 @@
#include "pkg.h"

#include <sqlite3.h>
-
#include <tllist.h>
+
#include "pkg/vec.h"

struct pkgdb {
	sqlite3		*sqlite;
	bool		 prstmt_initialized;

-
	tll(struct pkg_repo *) repos;
+
	vec_t(struct pkg_repo *) repos;
};

struct pkgdb_sqlite_it {
@@ -54,8 +33,8 @@ struct pkg_repo_it;

struct pkgdb_it {
	struct pkgdb *db;
-
	tll(struct pkg_repo_it *) remote;
-
	void *opq_it;
+
	vec_t(struct pkg_repo_it *) remote;
+
	size_t remote_pos;
	struct pkgdb_sqlite_it *local;
};