2005-11-30 13:36:58 +00:00
|
|
|
/*
|
2012-01-25 16:38:37 +00:00
|
|
|
* virhash.c: chained hash tables
|
2005-11-30 13:36:58 +00:00
|
|
|
*
|
|
|
|
* Reference: Your favorite introductory book on algorithms
|
|
|
|
*
|
2014-04-04 23:36:25 +00:00
|
|
|
* Copyright (C) 2005-2014 Red Hat, Inc.
|
2005-11-30 13:36:58 +00:00
|
|
|
* Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
|
2020-10-02 14:07:27 +00:00
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
|
2005-11-30 13:36:58 +00:00
|
|
|
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
|
|
|
|
*/
|
|
|
|
|
2008-01-29 18:15:54 +00:00
|
|
|
#include <config.h>
|
2007-12-05 21:40:15 +00:00
|
|
|
|
2008-11-04 22:30:33 +00:00
|
|
|
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-01-25 16:13:59 +00:00
|
|
|
#include "virhash.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-01-18 16:10:43 +00:00
|
|
|
#include "virhashcode.h"
|
|
|
|
#include "virrandom.h"
|
2015-07-02 12:21:27 +00:00
|
|
|
#include "virobject.h"
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2011-02-15 02:59:01 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.hash");
|
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2015-07-02 12:21:27 +00:00
|
|
|
struct _virHashAtomic {
|
|
|
|
virObjectLockable parent;
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *hash;
|
2015-07-02 12:21:27 +00:00
|
|
|
};
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virClass *virHashAtomicClass;
|
2015-07-02 12:21:27 +00:00
|
|
|
static void virHashAtomicDispose(void *obj);
|
|
|
|
|
|
|
|
static int virHashAtomicOnceInit(void)
|
|
|
|
{
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virHashAtomic, virClassForObjectLockable()))
|
2015-07-02 12:21:27 +00:00
|
|
|
return -1;
|
2018-04-17 15:42:33 +00:00
|
|
|
|
|
|
|
return 0;
|
2015-07-02 12:21:27 +00:00
|
|
|
}
|
2018-04-17 15:42:33 +00:00
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virHashAtomic);
|
2015-07-02 12:21:27 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
|
|
|
|
/**
|
2021-04-15 08:12:12 +00:00
|
|
|
* Our hash function uses a random seed to provide uncertainty from run to run
|
2020-10-22 09:14:00 +00:00
|
|
|
* to prevent pre-crafting of colliding hash keys.
|
|
|
|
*/
|
|
|
|
static uint32_t virHashTableSeed;
|
|
|
|
|
|
|
|
static int virHashTableSeedOnceInit(void)
|
|
|
|
{
|
|
|
|
virHashTableSeed = virRandomBits(32);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_ONCE_GLOBAL_INIT(virHashTableSeed);
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
virHashTableStringKey(const void *vkey)
|
Allow hash tables to use generic pointers as keys
Relax the restriction that the hash table key must be a string
by allowing an arbitrary hash code generator + comparison func
to be provided
* util/hash.c, util/hash.h: Allow any pointer as a key
* internal.h: Include stdbool.h as standard.
* conf/domain_conf.c, conf/domain_conf.c,
conf/nwfilter_params.c, nwfilter/nwfilter_gentech_driver.c,
nwfilter/nwfilter_gentech_driver.h, nwfilter/nwfilter_learnipaddr.c,
qemu/qemu_command.c, qemu/qemu_driver.c,
qemu/qemu_process.c, uml/uml_driver.c,
xen/xm_internal.c: s/char */void */ in hash callbacks
2011-02-22 15:11:59 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
const char *key = vkey;
|
|
|
|
|
|
|
|
return virHashCodeGen(key, strlen(key), virHashTableSeed);
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 16:17:16 +00:00
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/**
|
2020-10-20 16:17:16 +00:00
|
|
|
* virHashNew:
|
Allow hash tables to use generic pointers as keys
Relax the restriction that the hash table key must be a string
by allowing an arbitrary hash code generator + comparison func
to be provided
* util/hash.c, util/hash.h: Allow any pointer as a key
* internal.h: Include stdbool.h as standard.
* conf/domain_conf.c, conf/domain_conf.c,
conf/nwfilter_params.c, nwfilter/nwfilter_gentech_driver.c,
nwfilter/nwfilter_gentech_driver.h, nwfilter/nwfilter_learnipaddr.c,
qemu/qemu_command.c, qemu/qemu_driver.c,
qemu/qemu_process.c, uml/uml_driver.c,
xen/xm_internal.c: s/char */void */ in hash callbacks
2011-02-22 15:11:59 +00:00
|
|
|
* @dataFree: callback to free data
|
2005-11-30 13:36:58 +00:00
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Create a new GHashTable * for use with string-based keys.
|
2005-11-30 13:36:58 +00:00
|
|
|
*
|
2020-01-28 12:34:43 +00:00
|
|
|
* Returns the newly created object.
|
2005-11-30 13:36:58 +00:00
|
|
|
*/
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *
|
2020-10-20 16:17:16 +00:00
|
|
|
virHashNew(virHashDataFree dataFree)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
ignore_value(virHashTableSeedInitialize());
|
Allow hash tables to use generic pointers as keys
Relax the restriction that the hash table key must be a string
by allowing an arbitrary hash code generator + comparison func
to be provided
* util/hash.c, util/hash.h: Allow any pointer as a key
* internal.h: Include stdbool.h as standard.
* conf/domain_conf.c, conf/domain_conf.c,
conf/nwfilter_params.c, nwfilter/nwfilter_gentech_driver.c,
nwfilter/nwfilter_gentech_driver.h, nwfilter/nwfilter_learnipaddr.c,
qemu/qemu_command.c, qemu/qemu_driver.c,
qemu/qemu_process.c, uml/uml_driver.c,
xen/xm_internal.c: s/char */void */ in hash callbacks
2011-02-22 15:11:59 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
return g_hash_table_new_full(virHashTableStringKey, g_str_equal, g_free, dataFree);
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
Allow hash tables to use generic pointers as keys
Relax the restriction that the hash table key must be a string
by allowing an arbitrary hash code generator + comparison func
to be provided
* util/hash.c, util/hash.h: Allow any pointer as a key
* internal.h: Include stdbool.h as standard.
* conf/domain_conf.c, conf/domain_conf.c,
conf/nwfilter_params.c, nwfilter/nwfilter_gentech_driver.c,
nwfilter/nwfilter_gentech_driver.h, nwfilter/nwfilter_learnipaddr.c,
qemu/qemu_command.c, qemu/qemu_driver.c,
qemu/qemu_process.c, uml/uml_driver.c,
xen/xm_internal.c: s/char */void */ in hash callbacks
2011-02-22 15:11:59 +00:00
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virHashAtomic *
|
2020-10-20 16:28:21 +00:00
|
|
|
virHashAtomicNew(virHashDataFree dataFree)
|
2015-07-02 12:21:27 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virHashAtomic *hash;
|
2015-07-02 12:21:27 +00:00
|
|
|
|
|
|
|
if (virHashAtomicInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(hash = virObjectLockableNew(virHashAtomicClass)))
|
|
|
|
return NULL;
|
|
|
|
|
2020-10-20 16:28:21 +00:00
|
|
|
if (!(hash->hash = virHashNew(dataFree))) {
|
2015-07-02 12:21:27 +00:00
|
|
|
virObjectUnref(hash);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virHashAtomicDispose(void *obj)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virHashAtomic *hash = obj;
|
2015-07-02 12:21:27 +00:00
|
|
|
|
|
|
|
virHashFree(hash->hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virHashFree:
|
2005-11-30 13:36:58 +00:00
|
|
|
* @table: the hash table
|
|
|
|
*
|
|
|
|
* Free the hash @table and its contents. The userdata is
|
2011-02-18 21:30:24 +00:00
|
|
|
* deallocated with function provided at creation time.
|
2020-10-26 15:24:28 +00:00
|
|
|
*
|
|
|
|
* Deprecated: consider using g_hash_table_unref instead
|
2005-11-30 13:36:58 +00:00
|
|
|
*/
|
|
|
|
void
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashFree(GHashTable *table)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2005-11-30 13:36:58 +00:00
|
|
|
if (table == NULL)
|
2006-03-15 12:13:25 +00:00
|
|
|
return;
|
2011-04-12 17:12:12 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
g_hash_table_unref(table);
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-17 21:14:57 +00:00
|
|
|
/**
|
|
|
|
* virHashAddEntry:
|
|
|
|
* @table: the hash table
|
|
|
|
* @name: the name of the userdata
|
|
|
|
* @userdata: a pointer to the userdata
|
|
|
|
*
|
|
|
|
* Add the @userdata to the hash @table. This can later be retrieved
|
|
|
|
* by using @name. Duplicate entries generate errors.
|
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Deprecated: Consider using g_hash_table_insert insert. Note that
|
|
|
|
* g_hash_table_instead doesn't fail if entry exists. Also note that
|
|
|
|
* g_hash_table_insert doesn't copy the key.
|
|
|
|
*
|
2011-02-17 21:14:57 +00:00
|
|
|
* Returns 0 the addition succeeded and -1 in case of error.
|
|
|
|
*/
|
|
|
|
int
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashAddEntry(GHashTable *table, const char *name, void *userdata)
|
2011-02-17 21:14:57 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!table || !name)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (g_hash_table_contains(table, name)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Duplicate hash table key '%s'"), name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_hash_table_insert(table, g_strdup(name), userdata);
|
|
|
|
|
|
|
|
return 0;
|
2011-02-17 21:14:57 +00:00
|
|
|
}
|
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virHashUpdateEntry:
|
2005-11-30 13:36:58 +00:00
|
|
|
* @table: the hash table
|
|
|
|
* @name: the name of the userdata
|
|
|
|
* @userdata: a pointer to the userdata
|
|
|
|
*
|
|
|
|
* Add the @userdata to the hash @table. This can later be retrieved
|
|
|
|
* by using @name. Existing entry for this tuple
|
|
|
|
* will be removed and freed with @f if found.
|
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Deprecated: consider using g_hash_table_insert insert. Note that
|
|
|
|
* g_hash_table_insert doesn't copy the key.
|
|
|
|
*
|
2005-11-30 13:36:58 +00:00
|
|
|
* Returns 0 the addition succeeded and -1 in case of error.
|
|
|
|
*/
|
|
|
|
int
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashUpdateEntry(GHashTable *table, const char *name,
|
2011-02-22 15:11:59 +00:00
|
|
|
void *userdata)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!table || !name)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
g_hash_table_insert(table, g_strdup(name), userdata);
|
|
|
|
|
|
|
|
return 0;
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 12:21:27 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virHashAtomicUpdate(virHashAtomic *table,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name,
|
2015-07-02 12:21:27 +00:00
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
virObjectLock(table);
|
2020-10-22 09:14:00 +00:00
|
|
|
ret = virHashUpdateEntry(table->hash, name, userdata);
|
2015-07-02 12:21:27 +00:00
|
|
|
virObjectUnlock(table);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virHashLookup:
|
2005-11-30 13:36:58 +00:00
|
|
|
* @table: the hash table
|
|
|
|
* @name: the name of the userdata
|
|
|
|
*
|
Allow hash tables to use generic pointers as keys
Relax the restriction that the hash table key must be a string
by allowing an arbitrary hash code generator + comparison func
to be provided
* util/hash.c, util/hash.h: Allow any pointer as a key
* internal.h: Include stdbool.h as standard.
* conf/domain_conf.c, conf/domain_conf.c,
conf/nwfilter_params.c, nwfilter/nwfilter_gentech_driver.c,
nwfilter/nwfilter_gentech_driver.h, nwfilter/nwfilter_learnipaddr.c,
qemu/qemu_command.c, qemu/qemu_driver.c,
qemu/qemu_process.c, uml/uml_driver.c,
xen/xm_internal.c: s/char */void */ in hash callbacks
2011-02-22 15:11:59 +00:00
|
|
|
* Find the userdata specified by @name
|
2005-11-30 13:36:58 +00:00
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Deprecated: consider using g_hash_table_lookup instead
|
|
|
|
*
|
2012-08-22 18:29:18 +00:00
|
|
|
* Returns a pointer to the userdata
|
2005-11-30 13:36:58 +00:00
|
|
|
*/
|
|
|
|
void *
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashLookup(GHashTable *table,
|
2020-10-22 09:11:57 +00:00
|
|
|
const char *name)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!table || !name)
|
2011-04-12 17:12:12 +00:00
|
|
|
return NULL;
|
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
return g_hash_table_lookup(table, name);
|
2019-10-17 12:45:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virHashHasEntry:
|
|
|
|
* @table: the hash table
|
|
|
|
* @name: the name of the userdata
|
|
|
|
*
|
|
|
|
* Find whether entry specified by @name exists.
|
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Deprecated: consider using g_hash_table_contains instead
|
|
|
|
*
|
2019-10-17 12:45:47 +00:00
|
|
|
* Returns true if the entry exists and false otherwise
|
|
|
|
*/
|
|
|
|
bool
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashHasEntry(GHashTable *table,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name)
|
2019-10-17 12:45:47 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!table || !name)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return g_hash_table_contains(table, name);
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-22 15:11:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virHashSteal:
|
|
|
|
* @table: the hash table
|
|
|
|
* @name: the name of the userdata
|
|
|
|
*
|
|
|
|
* Find the userdata specified by @name
|
|
|
|
* and remove it from the hash without freeing it.
|
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Deprecated: consider using g_hash_table_steal_extended once we upgrade to
|
|
|
|
* glib 2.58
|
|
|
|
*
|
2012-08-22 18:29:18 +00:00
|
|
|
* Returns a pointer to the userdata
|
2011-02-22 15:11:59 +00:00
|
|
|
*/
|
2020-10-22 17:04:18 +00:00
|
|
|
void *virHashSteal(GHashTable *table, const char *name)
|
2011-02-22 15:11:59 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
g_autofree void *orig_name = NULL;
|
|
|
|
void *val = NULL;
|
|
|
|
|
|
|
|
if (!table || !name)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* we can replace this by g_hash_table_steal_extended with glib 2.58 */
|
|
|
|
if (!(g_hash_table_lookup_extended(table, name, &orig_name, &val)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
g_hash_table_steal(table, name);
|
|
|
|
|
|
|
|
return val;
|
2011-02-22 15:11:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 12:21:27 +00:00
|
|
|
void *
|
2021-03-11 07:16:13 +00:00
|
|
|
virHashAtomicSteal(virHashAtomic *table,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name)
|
2015-07-02 12:21:27 +00:00
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
virObjectLock(table);
|
|
|
|
data = virHashSteal(table->hash, name);
|
|
|
|
virObjectUnlock(table);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2011-02-22 15:11:59 +00:00
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virHashSize:
|
2005-11-30 13:36:58 +00:00
|
|
|
* @table: the hash table
|
|
|
|
*
|
|
|
|
* Query the number of elements installed in the hash @table.
|
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Deprecated: consider using g_hash_table_size instead
|
|
|
|
*
|
2005-11-30 13:36:58 +00:00
|
|
|
* Returns the number of elements in the hash table or
|
|
|
|
* -1 in case of error
|
|
|
|
*/
|
2012-01-25 15:55:00 +00:00
|
|
|
ssize_t
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashSize(GHashTable *table)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2005-11-30 13:36:58 +00:00
|
|
|
if (table == NULL)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2020-10-22 09:14:00 +00:00
|
|
|
|
|
|
|
return g_hash_table_size(table);
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-15 19:17:11 +00:00
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virHashRemoveEntry:
|
2005-11-30 13:36:58 +00:00
|
|
|
* @table: the hash table
|
|
|
|
* @name: the name of the userdata
|
|
|
|
*
|
|
|
|
* Find the userdata specified by the @name and remove
|
|
|
|
* it from the hash @table. Existing userdata for this tuple will be removed
|
|
|
|
* and freed with @f.
|
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Deprecated: consider using g_hash_table_remove
|
|
|
|
*
|
2005-11-30 13:36:58 +00:00
|
|
|
* Returns 0 if the removal succeeded and -1 in case of error or not found.
|
|
|
|
*/
|
|
|
|
int
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashRemoveEntry(GHashTable *table,
|
2020-10-22 09:14:00 +00:00
|
|
|
const char *name)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!table || !name)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
if (g_hash_table_remove(table, name))
|
|
|
|
return 0;
|
2011-04-12 17:12:12 +00:00
|
|
|
|
|
|
|
return -1;
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
2006-04-09 13:11:22 +00:00
|
|
|
|
2006-11-15 20:11:56 +00:00
|
|
|
|
|
|
|
/**
|
2020-10-26 14:30:10 +00:00
|
|
|
* virHashForEach, virHashForEachSorted, virHashForEachSafe
|
2006-11-15 20:11:56 +00:00
|
|
|
* @table: the hash table to process
|
|
|
|
* @iter: callback to process each element
|
2020-10-23 09:45:16 +00:00
|
|
|
* @opaque: opaque data to pass to the iterator
|
2006-11-15 20:11:56 +00:00
|
|
|
*
|
2020-10-26 14:28:08 +00:00
|
|
|
* Iterates over every element in the hash table, invoking the 'iter' callback.
|
|
|
|
*
|
|
|
|
* The elements are iterated in arbitrary order.
|
|
|
|
*
|
2020-10-23 07:49:36 +00:00
|
|
|
* virHashForEach prohibits @iter from modifying @table
|
|
|
|
*
|
|
|
|
* virHashForEachSafe allows the callback to remove the current
|
2020-10-26 14:28:08 +00:00
|
|
|
* element using virHashRemoveEntry but calling other virHash* functions is
|
|
|
|
* prohibited. Note that removing the entry invalidates @key and @payload in
|
|
|
|
* the callback.
|
|
|
|
*
|
|
|
|
* virHashForEachSorted iterates the elements in order by sorted key.
|
|
|
|
*
|
2020-10-26 14:30:10 +00:00
|
|
|
* virHashForEachSorted and virHashForEachSafe are more computationally
|
2020-10-26 14:28:08 +00:00
|
|
|
* expensive than virHashForEach.
|
|
|
|
*
|
2016-02-12 09:15:57 +00:00
|
|
|
* If @iter fails and returns a negative value, the evaluation is stopped and -1
|
|
|
|
* is returned.
|
2006-11-15 20:11:56 +00:00
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Deprecated: Consider using g_hash_table_foreach as replacement for
|
|
|
|
* virHashForEach, rewrite your code if it would require virHashForEachSafe.
|
|
|
|
*
|
2016-02-12 09:15:57 +00:00
|
|
|
* Returns 0 on success or -1 on failure.
|
2006-11-15 20:11:56 +00:00
|
|
|
*/
|
2016-02-12 09:15:57 +00:00
|
|
|
int
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashForEach(GHashTable *table, virHashIterator iter, void *opaque)
|
2011-03-07 14:07:01 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
GHashTableIter htitr;
|
|
|
|
void *key;
|
|
|
|
void *value;
|
2006-11-15 20:11:56 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!table || !iter)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2006-11-15 20:11:56 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
g_hash_table_iter_init(&htitr, table);
|
2011-04-12 15:58:22 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
while (g_hash_table_iter_next(&htitr, &key, &value)) {
|
|
|
|
if (iter(value, key, opaque) < 0)
|
|
|
|
return -1;
|
2006-11-15 20:11:56 +00:00
|
|
|
}
|
2011-03-07 14:07:01 +00:00
|
|
|
|
2019-10-21 18:19:04 +00:00
|
|
|
return 0;
|
2006-11-15 20:11:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 09:15:57 +00:00
|
|
|
|
2020-10-26 14:30:10 +00:00
|
|
|
int
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashForEachSafe(GHashTable *table,
|
2020-10-26 14:30:10 +00:00
|
|
|
virHashIterator iter,
|
|
|
|
void *opaque)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
g_autofree virHashKeyValuePair *items = virHashGetItems(table, NULL, false);
|
2020-10-26 14:30:10 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!items)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; items[i].key; i++) {
|
|
|
|
if (iter((void *)items[i].value, items[i].key, opaque) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-26 14:28:08 +00:00
|
|
|
int
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashForEachSorted(GHashTable *table,
|
2020-10-26 14:28:08 +00:00
|
|
|
virHashIterator iter,
|
|
|
|
void *opaque)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
g_autofree virHashKeyValuePair *items = virHashGetItems(table, NULL, true);
|
2020-10-26 14:28:08 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!items)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; items[i].key; i++) {
|
|
|
|
if (iter((void *)items[i].value, items[i].key, opaque) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
struct virHashSearcherWrapFuncData {
|
|
|
|
virHashSearcher iter;
|
|
|
|
const void *opaque;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
virHashSearcherWrapFunc(gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer opaque)
|
|
|
|
{
|
|
|
|
struct virHashSearcherWrapFuncData *data = opaque;
|
|
|
|
|
|
|
|
data->name = key;
|
|
|
|
|
|
|
|
return !!(data->iter(value, key, data->opaque));
|
|
|
|
}
|
|
|
|
|
2006-11-15 20:11:56 +00:00
|
|
|
/**
|
|
|
|
* virHashRemoveSet
|
|
|
|
* @table: the hash table to process
|
|
|
|
* @iter: callback to identify elements for removal
|
2020-10-23 09:45:16 +00:00
|
|
|
* @opaque: opaque data to pass to the iterator
|
2006-11-15 20:11:56 +00:00
|
|
|
*
|
|
|
|
* Iterates over all elements in the hash table, invoking the 'iter'
|
|
|
|
* callback. If the callback returns a non-zero value, the element
|
|
|
|
* will be removed from the hash table & its payload passed to the
|
2011-04-29 19:49:36 +00:00
|
|
|
* data freer callback registered at creation.
|
2006-11-15 20:11:56 +00:00
|
|
|
*
|
2020-10-26 15:24:28 +00:00
|
|
|
* Deprecated: consider using g_hash_table_foreach_remove instead
|
|
|
|
*
|
2006-11-15 20:11:56 +00:00
|
|
|
* Returns number of items removed on success, -1 on failure
|
|
|
|
*/
|
2012-01-25 15:55:00 +00:00
|
|
|
ssize_t
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashRemoveSet(GHashTable *table,
|
2012-01-25 15:55:00 +00:00
|
|
|
virHashSearcher iter,
|
2020-10-23 09:45:16 +00:00
|
|
|
const void *opaque)
|
2011-04-12 17:12:12 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
struct virHashSearcherWrapFuncData data = { iter, opaque, NULL };
|
2006-11-15 20:11:56 +00:00
|
|
|
|
|
|
|
if (table == NULL || iter == NULL)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2006-11-15 20:11:56 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
return g_hash_table_foreach_remove(table, virHashSearcherWrapFunc, &data);
|
2012-04-19 14:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virHashRemoveAll
|
|
|
|
* @table: the hash table to clear
|
|
|
|
*
|
|
|
|
* Free the hash @table's contents. The userdata is
|
|
|
|
* deallocated with the function provided at creation time.
|
2020-10-26 15:24:28 +00:00
|
|
|
*
|
|
|
|
* Deprecated: consider using g_hash_table_remove_all instead
|
2012-04-19 14:21:43 +00:00
|
|
|
*/
|
2020-10-21 12:57:36 +00:00
|
|
|
void
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashRemoveAll(GHashTable *table)
|
2012-04-19 14:21:43 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!table)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_hash_table_remove_all(table);
|
2012-04-19 14:21:43 +00:00
|
|
|
}
|
|
|
|
|
2006-11-15 20:11:56 +00:00
|
|
|
/**
|
|
|
|
* virHashSearch:
|
|
|
|
* @table: the hash table to search
|
|
|
|
* @iter: an iterator to identify the desired element
|
2020-10-23 09:45:16 +00:00
|
|
|
* @opaque: extra opaque information passed to the iter
|
2017-06-13 13:56:14 +00:00
|
|
|
* @name: the name of found user data, pass NULL to ignore
|
2006-11-15 20:11:56 +00:00
|
|
|
*
|
|
|
|
* Iterates over the hash table calling the 'iter' callback
|
|
|
|
* for each element. The first element for which the iter
|
|
|
|
* returns non-zero will be returned by this function.
|
2017-06-13 13:56:14 +00:00
|
|
|
* The elements are processed in a undefined order. Caller is
|
|
|
|
* responsible for freeing the @name.
|
2020-10-26 15:24:28 +00:00
|
|
|
*
|
|
|
|
* Deprecated: consider using g_hash_table_find instead
|
2006-11-15 20:11:56 +00:00
|
|
|
*/
|
2020-10-22 17:04:18 +00:00
|
|
|
void *virHashSearch(GHashTable *table,
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashSearcher iter,
|
2020-10-23 09:45:16 +00:00
|
|
|
const void *opaque,
|
2020-10-21 11:31:16 +00:00
|
|
|
char **name)
|
2011-04-12 17:12:12 +00:00
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
struct virHashSearcherWrapFuncData data = { iter, opaque, NULL };
|
|
|
|
void *ret;
|
maint: avoid 'const fooPtr' in hashes
'const fooPtr' is the same as 'foo * const' (the pointer won't
change, but it's contents can). But in general, if an interface
is trying to be const-correct, it should be using 'const foo *'
(the pointer is to data that can't be changed).
Fix up virhash to provide a const-correct interface: all actions
that don't modify the table take a const table. Note that in
one case (virHashSearch), we actually strip const away - we aren't
modifying the contents of the table, so much as associated data
for ensuring that the code uses the table correctly (if this were
C++, it would be a case for the 'mutable' keyword).
* src/util/virhash.h (virHashKeyComparator, virHashEqual): Use
intended type.
(virHashSize, virHashTableSize, virHashLookup, virHashSearch):
Make const-correct.
* src/util/virhash.c (virHashEqualData, virHashEqual)
(virHashLookup, virHashSize, virHashTableSize, virHashSearch)
(virHashComputeKey): Fix fallout.
* src/conf/nwfilter_params.c
(virNWFilterFormatParameterNameSorter): Likewise.
* src/nwfilter/nwfilter_ebiptables_driver.c
(ebiptablesFilterOrderSort): Likewise.
* tests/virhashtest.c (testHashGetItemsCompKey)
(testHashGetItemsCompValue): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-10-05 02:30:35 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!table || !iter)
|
2012-03-22 11:33:35 +00:00
|
|
|
return NULL;
|
2006-11-15 20:11:56 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!(ret = g_hash_table_find(table, virHashSearcherWrapFunc, &data)))
|
|
|
|
return NULL;
|
2011-11-18 16:58:17 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
if (name)
|
|
|
|
*name = g_strdup(data.name);
|
2011-11-18 16:58:17 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
return ret;
|
2011-11-18 16:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-22 15:38:53 +00:00
|
|
|
static int
|
|
|
|
virHashGetItemsKeySorter(const void *va,
|
|
|
|
const void *vb)
|
2011-11-18 16:58:17 +00:00
|
|
|
{
|
2020-10-22 15:38:53 +00:00
|
|
|
const virHashKeyValuePair *a = va;
|
|
|
|
const virHashKeyValuePair *b = vb;
|
2011-11-18 16:58:17 +00:00
|
|
|
|
2020-10-22 15:38:53 +00:00
|
|
|
return strcmp(a->key, b->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virHashKeyValuePair *
|
2020-10-22 17:04:18 +00:00
|
|
|
virHashGetItems(GHashTable *table,
|
2020-10-22 15:38:53 +00:00
|
|
|
size_t *nitems,
|
|
|
|
bool sortKeys)
|
|
|
|
{
|
2020-10-22 09:14:00 +00:00
|
|
|
virHashKeyValuePair *items;
|
2020-10-22 15:38:53 +00:00
|
|
|
size_t dummy;
|
2020-10-22 09:14:00 +00:00
|
|
|
GHashTableIter htitr;
|
|
|
|
void *key;
|
|
|
|
void *value;
|
|
|
|
size_t i = 0;
|
2011-11-18 16:58:17 +00:00
|
|
|
|
2020-10-22 15:38:53 +00:00
|
|
|
if (!nitems)
|
|
|
|
nitems = &dummy;
|
2011-11-18 16:58:17 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
if (!table)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*nitems = g_hash_table_size(table);
|
|
|
|
items = g_new0(virHashKeyValuePair, *nitems + 1);
|
2011-11-18 16:58:17 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
g_hash_table_iter_init(&htitr, table);
|
2011-11-18 16:58:17 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
while (g_hash_table_iter_next(&htitr, &key, &value)) {
|
|
|
|
items[i].key = key;
|
|
|
|
items[i].value = value;
|
|
|
|
i++;
|
|
|
|
}
|
2020-10-22 15:38:53 +00:00
|
|
|
|
|
|
|
if (sortKeys)
|
2020-10-22 09:14:00 +00:00
|
|
|
qsort(items, *nitems, sizeof(*items), virHashGetItemsKeySorter);
|
2020-10-22 15:38:53 +00:00
|
|
|
|
2020-10-22 09:14:00 +00:00
|
|
|
return items;
|
2011-11-18 16:58:17 +00:00
|
|
|
}
|
2012-01-23 20:35:54 +00:00
|
|
|
|
2020-10-22 15:38:53 +00:00
|
|
|
|
2012-01-23 20:35:54 +00:00
|
|
|
struct virHashEqualData
|
|
|
|
{
|
|
|
|
bool equal;
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *table2;
|
2012-01-23 20:35:54 +00:00
|
|
|
virHashValueComparator compar;
|
|
|
|
};
|
|
|
|
|
2020-10-21 11:31:16 +00:00
|
|
|
static int virHashEqualSearcher(const void *payload, const char *name,
|
2020-10-23 09:45:16 +00:00
|
|
|
const void *opaque)
|
2012-01-23 20:35:54 +00:00
|
|
|
{
|
2020-10-23 09:45:16 +00:00
|
|
|
struct virHashEqualData *vhed = (void *)opaque;
|
2012-01-23 20:35:54 +00:00
|
|
|
const void *value;
|
|
|
|
|
|
|
|
value = virHashLookup(vhed->table2, name);
|
|
|
|
if (!value ||
|
|
|
|
vhed->compar(value, payload) != 0) {
|
|
|
|
/* key is missing in 2nd table or values are different */
|
|
|
|
vhed->equal = false;
|
|
|
|
/* stop 'iteration' */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-22 17:04:18 +00:00
|
|
|
bool virHashEqual(GHashTable *table1,
|
|
|
|
GHashTable *table2,
|
2012-01-23 20:35:54 +00:00
|
|
|
virHashValueComparator compar)
|
|
|
|
{
|
|
|
|
struct virHashEqualData data = {
|
|
|
|
.equal = true,
|
|
|
|
.table2 = table2,
|
|
|
|
.compar = compar,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (table1 == table2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!table1 || !table2 ||
|
|
|
|
virHashSize(table1) != virHashSize(table2))
|
|
|
|
return false;
|
|
|
|
|
2017-06-13 13:56:14 +00:00
|
|
|
virHashSearch(table1, virHashEqualSearcher, &data, NULL);
|
2012-01-23 20:35:54 +00:00
|
|
|
|
|
|
|
return data.equal;
|
|
|
|
}
|