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
|
|
|
|
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
|
|
|
|
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
|
|
|
|
*
|
2012-01-25 16:13:59 +00:00
|
|
|
* Author: Bjorn Reese <bjorn.reese@systematic.dk>
|
2006-04-09 13:11:22 +00:00
|
|
|
* Daniel Veillard <veillard@redhat.com>
|
2005-11-30 13:36:58 +00:00
|
|
|
*/
|
|
|
|
|
2008-01-29 18:15:54 +00:00
|
|
|
#include <config.h>
|
2007-12-05 21:40:15 +00:00
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
#include <string.h>
|
2005-12-07 09:33:00 +00:00
|
|
|
#include <stdlib.h>
|
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 18:06:53 +00:00
|
|
|
#include "viralloc.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"
|
2013-05-24 07:19:51 +00:00
|
|
|
#include "virstring.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
|
|
|
#define MAX_HASH_LEN 8
|
|
|
|
|
|
|
|
/* #define DEBUG_GROW */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A single entry in the hash table
|
|
|
|
*/
|
2005-12-05 11:16:07 +00:00
|
|
|
typedef struct _virHashEntry virHashEntry;
|
|
|
|
typedef virHashEntry *virHashEntryPtr;
|
|
|
|
struct _virHashEntry {
|
|
|
|
struct _virHashEntry *next;
|
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
|
|
|
void *name;
|
2005-11-30 13:36:58 +00:00
|
|
|
void *payload;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The entire hash table
|
|
|
|
*/
|
2005-12-05 11:16:07 +00:00
|
|
|
struct _virHashTable {
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashEntryPtr *table;
|
2012-01-18 16:10:43 +00:00
|
|
|
uint32_t seed;
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t size;
|
|
|
|
size_t nbElems;
|
2011-02-22 15:11:59 +00:00
|
|
|
virHashDataFree dataFree;
|
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
|
|
|
virHashKeyCode keyCode;
|
|
|
|
virHashKeyEqual keyEqual;
|
|
|
|
virHashKeyCopy keyCopy;
|
|
|
|
virHashKeyFree keyFree;
|
2005-11-30 13:36:58 +00:00
|
|
|
};
|
|
|
|
|
2015-07-02 12:21:27 +00:00
|
|
|
struct _virHashAtomic {
|
|
|
|
virObjectLockable parent;
|
|
|
|
virHashTablePtr hash;
|
|
|
|
};
|
|
|
|
|
|
|
|
static virClassPtr virHashAtomicClass;
|
|
|
|
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
|
|
|
|
2015-07-02 12:21:27 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virHashAtomic)
|
|
|
|
|
|
|
|
|
2012-01-18 16:10:43 +00:00
|
|
|
static uint32_t virHashStrCode(const void *name, uint32_t seed)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2012-01-18 16:10:43 +00:00
|
|
|
return virHashCodeGen(name, strlen(name), seed);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static bool virHashStrEqual(const void *namea, const void *nameb)
|
|
|
|
{
|
|
|
|
return STREQ(namea, nameb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *virHashStrCopy(const void *name)
|
|
|
|
{
|
2013-05-24 07:19:51 +00:00
|
|
|
char *ret;
|
|
|
|
ignore_value(VIR_STRDUP(ret, name));
|
|
|
|
return ret;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void virHashStrFree(void *name)
|
|
|
|
{
|
|
|
|
VIR_FREE(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-04 23:36:25 +00:00
|
|
|
void
|
|
|
|
virHashValueFree(void *value, const void *name ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
VIR_FREE(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-25 15:55:00 +00:00
|
|
|
static size_t
|
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
|
|
|
virHashComputeKey(const virHashTable *table, const void *name)
|
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
|
|
|
{
|
2012-01-18 16:10:43 +00:00
|
|
|
uint32_t value = table->keyCode(name, table->seed);
|
2012-03-22 11:33:35 +00:00
|
|
|
return value % table->size;
|
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
|
|
|
* virHashCreateFull:
|
2005-11-30 13:36:58 +00:00
|
|
|
* @size: the size of the hash table
|
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
|
|
|
|
* @keyCode: callback to compute hash code
|
|
|
|
* @keyEqual: callback to compare hash keys
|
|
|
|
* @keyCopy: callback to copy hash keys
|
|
|
|
* @keyFree: callback to free keys
|
2005-11-30 13:36:58 +00:00
|
|
|
*
|
2005-12-05 11:16:07 +00:00
|
|
|
* Create a new virHashTablePtr.
|
2005-11-30 13:36:58 +00:00
|
|
|
*
|
2011-10-10 20:02:06 +00:00
|
|
|
* Returns the newly created object, or NULL if an error occurred.
|
2005-11-30 13:36:58 +00:00
|
|
|
*/
|
2012-01-25 15:55:00 +00:00
|
|
|
virHashTablePtr virHashCreateFull(ssize_t size,
|
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
|
|
|
virHashDataFree dataFree,
|
|
|
|
virHashKeyCode keyCode,
|
|
|
|
virHashKeyEqual keyEqual,
|
|
|
|
virHashKeyCopy keyCopy,
|
|
|
|
virHashKeyFree keyFree)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2008-04-28 21:44:54 +00:00
|
|
|
virHashTablePtr table = NULL;
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
if (size <= 0)
|
|
|
|
size = 256;
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (VIR_ALLOC(table) < 0)
|
2008-04-28 21:44:54 +00:00
|
|
|
return NULL;
|
|
|
|
|
2012-01-18 16:10:43 +00:00
|
|
|
table->seed = virRandomBits(32);
|
2008-04-28 21:44:54 +00:00
|
|
|
table->size = size;
|
|
|
|
table->nbElems = 0;
|
2011-02-22 15:11:59 +00:00
|
|
|
table->dataFree = dataFree;
|
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
|
|
|
table->keyCode = keyCode;
|
|
|
|
table->keyEqual = keyEqual;
|
|
|
|
table->keyCopy = keyCopy;
|
|
|
|
table->keyFree = keyFree;
|
|
|
|
|
2008-04-28 21:44:54 +00:00
|
|
|
if (VIR_ALLOC_N(table->table, size) < 0) {
|
|
|
|
VIR_FREE(table);
|
|
|
|
return NULL;
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
2008-04-28 21:44:54 +00:00
|
|
|
|
|
|
|
return table;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* virHashCreate:
|
|
|
|
* @size: the size of the hash table
|
|
|
|
* @dataFree: callback to free data
|
|
|
|
*
|
|
|
|
* Create a new virHashTablePtr.
|
|
|
|
*
|
2011-04-12 17:12:12 +00:00
|
|
|
* Returns the newly created object, or NULL if an error occurred.
|
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
|
|
|
*/
|
2012-01-25 15:55:00 +00:00
|
|
|
virHashTablePtr virHashCreate(ssize_t size, virHashDataFree dataFree)
|
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
|
|
|
{
|
|
|
|
return virHashCreateFull(size,
|
|
|
|
dataFree,
|
|
|
|
virHashStrCode,
|
|
|
|
virHashStrEqual,
|
|
|
|
virHashStrCopy,
|
|
|
|
virHashStrFree);
|
|
|
|
}
|
|
|
|
|
2015-07-02 12:21:27 +00:00
|
|
|
|
|
|
|
virHashAtomicPtr
|
|
|
|
virHashAtomicNew(ssize_t size,
|
|
|
|
virHashDataFree dataFree)
|
|
|
|
{
|
|
|
|
virHashAtomicPtr hash;
|
|
|
|
|
|
|
|
if (virHashAtomicInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(hash = virObjectLockableNew(virHashAtomicClass)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(hash->hash = virHashCreate(size, dataFree))) {
|
|
|
|
virObjectUnref(hash);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virHashAtomicDispose(void *obj)
|
|
|
|
{
|
|
|
|
virHashAtomicPtr hash = obj;
|
|
|
|
|
|
|
|
virHashFree(hash->hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virHashGrow:
|
2005-11-30 13:36:58 +00:00
|
|
|
* @table: the hash table
|
|
|
|
* @size: the new size of the hash table
|
|
|
|
*
|
|
|
|
* resize the hash table
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success, -1 in case of failure
|
|
|
|
*/
|
|
|
|
static int
|
2012-01-25 15:55:00 +00:00
|
|
|
virHashGrow(virHashTablePtr table, size_t size)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t oldsize, i;
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashEntryPtr *oldtable;
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
#ifdef DEBUG_GROW
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t nbElem = 0;
|
2005-11-30 13:36:58 +00:00
|
|
|
#endif
|
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;
|
2005-11-30 13:36:58 +00:00
|
|
|
if (size < 8)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2005-11-30 13:36:58 +00:00
|
|
|
if (size > 8 * 2048)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
oldsize = table->size;
|
|
|
|
oldtable = table->table;
|
|
|
|
if (oldtable == NULL)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2008-04-28 21:44:54 +00:00
|
|
|
if (VIR_ALLOC_N(table->table, size) < 0) {
|
2006-03-15 12:13:25 +00:00
|
|
|
table->table = oldtable;
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
table->size = size;
|
|
|
|
|
|
|
|
for (i = 0; i < oldsize; i++) {
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashEntryPtr iter = oldtable[i];
|
2006-03-15 12:13:25 +00:00
|
|
|
while (iter) {
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashEntryPtr next = iter->next;
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t key = virHashComputeKey(table, iter->name);
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2011-04-12 17:12:12 +00:00
|
|
|
iter->next = table->table[key];
|
|
|
|
table->table[key] = iter;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_GROW
|
2006-03-15 12:13:25 +00:00
|
|
|
nbElem++;
|
2005-11-30 13:36:58 +00:00
|
|
|
#endif
|
2006-03-15 12:13:25 +00:00
|
|
|
iter = next;
|
|
|
|
}
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2008-04-28 21:44:54 +00:00
|
|
|
VIR_FREE(oldtable);
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_GROW
|
2015-10-27 18:14:01 +00:00
|
|
|
VIR_DEBUG("virHashGrow : from %d to %d, %ld elems", oldsize,
|
2011-02-17 23:33:12 +00:00
|
|
|
size, nbElem);
|
2005-11-30 13:36:58 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-22 11:33:35 +00:00
|
|
|
return 0;
|
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.
|
2005-11-30 13:36:58 +00:00
|
|
|
*/
|
|
|
|
void
|
2011-02-18 21:30:24 +00:00
|
|
|
virHashFree(virHashTablePtr table)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t i;
|
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
|
|
|
|
|
|
|
for (i = 0; i < table->size; i++) {
|
|
|
|
virHashEntryPtr iter = table->table[i];
|
|
|
|
while (iter) {
|
|
|
|
virHashEntryPtr next = iter->next;
|
|
|
|
|
|
|
|
if (table->dataFree)
|
|
|
|
table->dataFree(iter->payload, iter->name);
|
|
|
|
if (table->keyFree)
|
|
|
|
table->keyFree(iter->name);
|
|
|
|
VIR_FREE(iter);
|
|
|
|
iter = next;
|
2006-03-15 12:13:25 +00:00
|
|
|
}
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
2011-04-12 17:12:12 +00:00
|
|
|
|
2011-04-29 19:49:36 +00:00
|
|
|
VIR_FREE(table->table);
|
2008-04-28 21:44:54 +00:00
|
|
|
VIR_FREE(table);
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 21:14:57 +00:00
|
|
|
static int
|
2011-02-22 15:11:59 +00:00
|
|
|
virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
|
|
|
|
void *userdata,
|
2011-02-17 21:14:57 +00:00
|
|
|
bool is_update)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t key, len = 0;
|
2005-12-05 11:16:07 +00:00
|
|
|
virHashEntryPtr entry;
|
2015-08-17 19:07:40 +00:00
|
|
|
void *new_name;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
if ((table == NULL) || (name == NULL))
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
key = virHashComputeKey(table, name);
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2011-04-12 17:12:12 +00:00
|
|
|
/* Check for duplicate entry */
|
|
|
|
for (entry = table->table[key]; entry; entry = entry->next) {
|
|
|
|
if (table->keyEqual(entry->name, name)) {
|
|
|
|
if (is_update) {
|
|
|
|
if (table->dataFree)
|
|
|
|
table->dataFree(entry->payload, entry->name);
|
|
|
|
entry->payload = userdata;
|
|
|
|
return 0;
|
|
|
|
} else {
|
2015-08-17 19:01:07 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Duplicate key"));
|
2011-04-12 17:12:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-02-15 02:59:01 +00:00
|
|
|
}
|
2011-04-12 17:12:12 +00:00
|
|
|
len++;
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-12 17:12:12 +00:00
|
|
|
if (VIR_ALLOC(entry) < 0 || !(new_name = table->keyCopy(name))) {
|
|
|
|
VIR_FREE(entry);
|
|
|
|
return -1;
|
2011-02-14 05:36:06 +00:00
|
|
|
}
|
2011-04-12 17:12:12 +00:00
|
|
|
|
2011-02-14 05:36:06 +00:00
|
|
|
entry->name = new_name;
|
2005-11-30 13:36:58 +00:00
|
|
|
entry->payload = userdata;
|
2011-04-12 17:12:12 +00:00
|
|
|
entry->next = table->table[key];
|
|
|
|
table->table[key] = entry;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
table->nbElems++;
|
|
|
|
|
|
|
|
if (len > MAX_HASH_LEN)
|
2006-03-15 12:13:25 +00:00
|
|
|
virHashGrow(table, MAX_HASH_LEN * table->size);
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2011-04-12 17:12:12 +00:00
|
|
|
return 0;
|
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.
|
|
|
|
*
|
|
|
|
* Returns 0 the addition succeeded and -1 in case of error.
|
|
|
|
*/
|
|
|
|
int
|
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
|
|
|
virHashAddEntry(virHashTablePtr table, const void *name, void *userdata)
|
2011-02-17 21:14:57 +00:00
|
|
|
{
|
2011-02-22 15:11:59 +00:00
|
|
|
return virHashAddOrUpdateEntry(table, name, userdata, false);
|
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.
|
|
|
|
*
|
|
|
|
* Returns 0 the addition succeeded and -1 in case of error.
|
|
|
|
*/
|
|
|
|
int
|
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
|
|
|
virHashUpdateEntry(virHashTablePtr table, const void *name,
|
2011-02-22 15:11:59 +00:00
|
|
|
void *userdata)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2011-02-22 15:11:59 +00:00
|
|
|
return virHashAddOrUpdateEntry(table, name, userdata, true);
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 12:21:27 +00:00
|
|
|
int
|
|
|
|
virHashAtomicUpdate(virHashAtomicPtr table,
|
|
|
|
const void *name,
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
virObjectLock(table);
|
|
|
|
ret = virHashAddOrUpdateEntry(table->hash, name, userdata, true);
|
|
|
|
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
|
|
|
*
|
2012-08-22 18:29:18 +00:00
|
|
|
* Returns a pointer to the userdata
|
2005-11-30 13:36:58 +00:00
|
|
|
*/
|
|
|
|
void *
|
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
|
|
|
virHashLookup(const virHashTable *table, const void *name)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t key;
|
2005-12-05 11:16:07 +00:00
|
|
|
virHashEntryPtr entry;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2011-04-12 17:12:12 +00:00
|
|
|
if (!table || !name)
|
|
|
|
return NULL;
|
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
key = virHashComputeKey(table, name);
|
2011-04-12 17:12:12 +00:00
|
|
|
for (entry = table->table[key]; entry; entry = entry->next) {
|
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
|
|
|
if (table->keyEqual(entry->name, name))
|
2011-04-12 17:12:12 +00:00
|
|
|
return entry->payload;
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
2011-04-12 17:12:12 +00:00
|
|
|
return NULL;
|
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.
|
|
|
|
*
|
2012-08-22 18:29:18 +00:00
|
|
|
* Returns a pointer to the userdata
|
2011-02-22 15:11:59 +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
|
|
|
void *virHashSteal(virHashTablePtr table, const void *name)
|
2011-02-22 15:11:59 +00:00
|
|
|
{
|
|
|
|
void *data = virHashLookup(table, name);
|
|
|
|
if (data) {
|
|
|
|
virHashDataFree dataFree = table->dataFree;
|
|
|
|
table->dataFree = NULL;
|
|
|
|
virHashRemoveEntry(table, name);
|
|
|
|
table->dataFree = dataFree;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2015-07-02 12:21:27 +00:00
|
|
|
void *
|
|
|
|
virHashAtomicSteal(virHashAtomicPtr table,
|
|
|
|
const void *name)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* 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
|
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
|
|
|
virHashSize(const virHashTable *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;
|
|
|
|
return table->nbElems;
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-15 19:17:11 +00:00
|
|
|
/**
|
|
|
|
* virHashTableSize:
|
|
|
|
* @table: the hash table
|
|
|
|
*
|
|
|
|
* Query the size of the hash @table, i.e., number of buckets in the table.
|
|
|
|
*
|
|
|
|
* Returns the number of keys in the hash table or
|
|
|
|
* -1 in case of error
|
|
|
|
*/
|
2012-01-25 15:55:00 +00:00
|
|
|
ssize_t
|
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
|
|
|
virHashTableSize(const virHashTable *table)
|
2011-04-15 19:17:11 +00:00
|
|
|
{
|
|
|
|
if (table == NULL)
|
|
|
|
return -1;
|
|
|
|
return table->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* Returns 0 if the removal succeeded and -1 in case of error or not found.
|
|
|
|
*/
|
|
|
|
int
|
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
|
|
|
virHashRemoveEntry(virHashTablePtr table, const void *name)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2005-12-05 11:16:07 +00:00
|
|
|
virHashEntryPtr entry;
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashEntryPtr *nextptr;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
if (table == NULL || name == NULL)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2011-04-12 17:12:12 +00:00
|
|
|
nextptr = table->table + virHashComputeKey(table, name);
|
|
|
|
for (entry = *nextptr; entry; entry = entry->next) {
|
|
|
|
if (table->keyEqual(entry->name, name)) {
|
|
|
|
if (table->dataFree)
|
|
|
|
table->dataFree(entry->payload, entry->name);
|
|
|
|
if (table->keyFree)
|
|
|
|
table->keyFree(entry->name);
|
|
|
|
*nextptr = entry->next;
|
|
|
|
VIR_FREE(entry);
|
|
|
|
table->nbElems--;
|
|
|
|
return 0;
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
2011-04-12 17:12:12 +00:00
|
|
|
nextptr = &entry->next;
|
2005-11-30 13:36:58 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* virHashForEach
|
|
|
|
* @table: the hash table to process
|
|
|
|
* @iter: callback to process each element
|
|
|
|
* @data: opaque data to pass to the iterator
|
|
|
|
*
|
|
|
|
* Iterates over every element in the hash table, invoking the
|
2011-03-07 14:07:01 +00:00
|
|
|
* 'iter' callback. The callback is allowed to remove the current element
|
|
|
|
* using virHashRemoveEntry but calling other virHash* functions is prohibited.
|
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
|
|
|
*
|
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
|
2012-01-25 15:55:00 +00:00
|
|
|
virHashForEach(virHashTablePtr table, virHashIterator iter, void *data)
|
2011-03-07 14:07:01 +00:00
|
|
|
{
|
2016-02-12 09:15:57 +00:00
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
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
|
|
|
|
2013-05-21 07:58:16 +00:00
|
|
|
for (i = 0; i < table->size; i++) {
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashEntryPtr entry = table->table[i];
|
2006-11-15 20:11:56 +00:00
|
|
|
while (entry) {
|
2011-03-03 13:10:51 +00:00
|
|
|
virHashEntryPtr next = entry->next;
|
2016-02-12 09:15:57 +00:00
|
|
|
ret = iter(entry->payload, entry->name, data);
|
2011-04-12 15:58:22 +00:00
|
|
|
|
2016-02-12 09:15:57 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2011-03-03 13:10:51 +00:00
|
|
|
entry = next;
|
2006-11-15 20:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-07 14:07:01 +00:00
|
|
|
|
2016-02-12 09:15:57 +00:00
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
2006-11-15 20:11:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 09:15:57 +00:00
|
|
|
|
2006-11-15 20:11:56 +00:00
|
|
|
/**
|
|
|
|
* virHashRemoveSet
|
|
|
|
* @table: the hash table to process
|
|
|
|
* @iter: callback to identify elements for removal
|
|
|
|
* @data: opaque data to pass to the iterator
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* Returns number of items removed on success, -1 on failure
|
|
|
|
*/
|
2012-01-25 15:55:00 +00:00
|
|
|
ssize_t
|
|
|
|
virHashRemoveSet(virHashTablePtr table,
|
|
|
|
virHashSearcher iter,
|
|
|
|
const void *data)
|
2011-04-12 17:12:12 +00:00
|
|
|
{
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t i, count = 0;
|
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
|
|
|
|
2013-05-21 07:58:16 +00:00
|
|
|
for (i = 0; i < table->size; i++) {
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashEntryPtr *nextptr = table->table + i;
|
2006-11-15 20:11:56 +00:00
|
|
|
|
2011-04-12 17:12:12 +00:00
|
|
|
while (*nextptr) {
|
|
|
|
virHashEntryPtr entry = *nextptr;
|
|
|
|
if (!iter(entry->payload, entry->name, data)) {
|
2011-04-29 19:49:36 +00:00
|
|
|
nextptr = &entry->next;
|
2011-04-12 17:12:12 +00:00
|
|
|
} else {
|
2006-11-15 20:11:56 +00:00
|
|
|
count++;
|
2011-02-22 15:11:59 +00:00
|
|
|
if (table->dataFree)
|
|
|
|
table->dataFree(entry->payload, entry->name);
|
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
|
|
|
if (table->keyFree)
|
|
|
|
table->keyFree(entry->name);
|
2011-04-12 17:12:12 +00:00
|
|
|
*nextptr = entry->next;
|
|
|
|
VIR_FREE(entry);
|
2008-02-07 16:56:01 +00:00
|
|
|
table->nbElems--;
|
2006-11-15 20:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-07 14:07:01 +00:00
|
|
|
|
2011-04-12 17:12:12 +00:00
|
|
|
return count;
|
2006-11-15 20:11:56 +00:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:21:43 +00:00
|
|
|
static int
|
|
|
|
_virHashRemoveAllIter(const void *payload ATTRIBUTE_UNUSED,
|
|
|
|
const void *name ATTRIBUTE_UNUSED,
|
|
|
|
const void *data ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virHashRemoveAll
|
|
|
|
* @table: the hash table to clear
|
|
|
|
*
|
|
|
|
* Free the hash @table's contents. The userdata is
|
|
|
|
* deallocated with the function provided at creation time.
|
|
|
|
*
|
|
|
|
* Returns the number of items removed on success, -1 on failure
|
|
|
|
*/
|
|
|
|
ssize_t
|
|
|
|
virHashRemoveAll(virHashTablePtr table)
|
|
|
|
{
|
|
|
|
return virHashRemoveSet(table,
|
|
|
|
_virHashRemoveAllIter,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2006-11-15 20:11:56 +00:00
|
|
|
/**
|
|
|
|
* virHashSearch:
|
|
|
|
* @table: the hash table to search
|
|
|
|
* @iter: an iterator to identify the desired element
|
|
|
|
* @data: 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.
|
2006-11-15 20:11:56 +00:00
|
|
|
*/
|
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
|
|
|
void *virHashSearch(const virHashTable *ctable,
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashSearcher iter,
|
2017-06-13 13:56:14 +00:00
|
|
|
const void *data,
|
|
|
|
void **name)
|
2011-04-12 17:12:12 +00:00
|
|
|
{
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t i;
|
2006-11-15 20:11:56 +00:00
|
|
|
|
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
|
|
|
/* Cast away const for internal detection of misuse. */
|
|
|
|
virHashTablePtr table = (virHashTablePtr)ctable;
|
|
|
|
|
2006-11-15 20:11:56 +00:00
|
|
|
if (table == NULL || iter == NULL)
|
2012-03-22 11:33:35 +00:00
|
|
|
return NULL;
|
2006-11-15 20:11:56 +00:00
|
|
|
|
2013-05-21 07:58:16 +00:00
|
|
|
for (i = 0; i < table->size; i++) {
|
2011-04-12 17:12:12 +00:00
|
|
|
virHashEntryPtr entry;
|
|
|
|
for (entry = table->table[i]; entry; entry = entry->next) {
|
|
|
|
if (iter(entry->payload, entry->name, data)) {
|
2017-06-13 13:56:14 +00:00
|
|
|
if (name)
|
|
|
|
*name = table->keyCopy(entry->name);
|
2011-04-12 17:12:12 +00:00
|
|
|
return entry->payload;
|
2006-11-15 20:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-07 14:07:01 +00:00
|
|
|
|
2011-04-12 17:12:12 +00:00
|
|
|
return NULL;
|
2006-11-15 20:11:56 +00:00
|
|
|
}
|
2011-11-18 16:58:17 +00:00
|
|
|
|
|
|
|
struct getKeysIter
|
|
|
|
{
|
|
|
|
virHashKeyValuePair *sortArray;
|
2012-01-25 15:55:00 +00:00
|
|
|
size_t arrayIdx;
|
2011-11-18 16:58:17 +00:00
|
|
|
};
|
|
|
|
|
2016-02-12 09:03:50 +00:00
|
|
|
static int virHashGetKeysIterator(void *payload,
|
|
|
|
const void *key, void *data)
|
2011-11-18 16:58:17 +00:00
|
|
|
{
|
|
|
|
struct getKeysIter *iter = data;
|
|
|
|
|
|
|
|
iter->sortArray[iter->arrayIdx].key = key;
|
|
|
|
iter->sortArray[iter->arrayIdx].value = payload;
|
|
|
|
|
|
|
|
iter->arrayIdx++;
|
2016-02-12 09:03:50 +00:00
|
|
|
return 0;
|
2011-11-18 16:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef int (*qsort_comp)(const void *, const void *);
|
|
|
|
|
|
|
|
virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
|
|
|
|
virHashKeyComparator compar)
|
|
|
|
{
|
2012-01-25 15:55:00 +00:00
|
|
|
ssize_t numElems = virHashSize(table);
|
2011-11-18 16:58:17 +00:00
|
|
|
struct getKeysIter iter = {
|
|
|
|
.arrayIdx = 0,
|
|
|
|
.sortArray = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (numElems < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (VIR_ALLOC_N(iter.sortArray, numElems + 1))
|
2011-11-18 16:58:17 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
virHashForEach(table, virHashGetKeysIterator, &iter);
|
|
|
|
|
|
|
|
if (compar)
|
|
|
|
qsort(&iter.sortArray[0], numElems, sizeof(iter.sortArray[0]),
|
|
|
|
(qsort_comp)compar);
|
|
|
|
|
|
|
|
return iter.sortArray;
|
|
|
|
}
|
2012-01-23 20:35:54 +00:00
|
|
|
|
|
|
|
struct virHashEqualData
|
|
|
|
{
|
|
|
|
bool equal;
|
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
|
|
|
const virHashTable *table2;
|
2012-01-23 20:35:54 +00:00
|
|
|
virHashValueComparator compar;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int virHashEqualSearcher(const void *payload, const void *name,
|
|
|
|
const void *data)
|
|
|
|
{
|
|
|
|
struct virHashEqualData *vhed = (void *)data;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
bool virHashEqual(const virHashTable *table1,
|
|
|
|
const virHashTable *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;
|
|
|
|
}
|