2005-11-30 13:36:58 +00:00
|
|
|
/*
|
2006-04-09 13:11:22 +00:00
|
|
|
* Summary: Chained hash tables and domain/connections handling
|
|
|
|
* Description: This module implements the hash table and allocation and
|
|
|
|
* deallocation of domains and connections
|
2005-11-30 13:36:58 +00:00
|
|
|
*
|
2014-04-04 23:36:25 +00:00
|
|
|
* Copyright (C) 2005-2014 Red Hat, Inc.
|
2012-01-25 16:13:59 +00:00
|
|
|
* Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
|
2005-11-30 13:36:58 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-18 16:12:59 +00:00
|
|
|
#pragma once
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The hash table.
|
|
|
|
*/
|
2006-04-09 13:11:22 +00:00
|
|
|
typedef struct _virHashTable virHashTable;
|
|
|
|
typedef virHashTable *virHashTablePtr;
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2015-07-02 12:21:27 +00:00
|
|
|
typedef struct _virHashAtomic virHashAtomic;
|
|
|
|
typedef virHashAtomic *virHashAtomicPtr;
|
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/*
|
|
|
|
* function types:
|
|
|
|
*/
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/**
|
2011-02-22 15:11:59 +00:00
|
|
|
* virHashDataFree:
|
2005-11-30 13:36:58 +00:00
|
|
|
* @payload: the data in the hash
|
|
|
|
*
|
|
|
|
* Callback to free data from a hash.
|
|
|
|
*/
|
2019-11-21 19:27:58 +00:00
|
|
|
typedef void (*virHashDataFree) (void *payload);
|
2019-10-09 13:26:37 +00:00
|
|
|
|
2006-11-15 20:11:56 +00:00
|
|
|
/**
|
|
|
|
* virHashIterator:
|
|
|
|
* @payload: the data in the hash
|
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
|
|
|
* @name: the hash key
|
2020-10-23 09:45:16 +00:00
|
|
|
* @opaque: user supplied data blob
|
2006-11-15 20:11:56 +00:00
|
|
|
*
|
|
|
|
* Callback to process a hash entry during iteration
|
2016-02-12 09:03:50 +00:00
|
|
|
*
|
|
|
|
* Returns -1 to stop the iteration, e.g. in case of an error
|
2006-11-15 20:11:56 +00:00
|
|
|
*/
|
2020-10-23 09:45:16 +00:00
|
|
|
typedef int (*virHashIterator) (void *payload, const char *name, void *opaque);
|
2006-11-15 20:11:56 +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
|
|
|
* virHashSearcher:
|
2006-11-15 20:11:56 +00:00
|
|
|
* @payload: the data in the hash
|
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
|
|
|
* @name: the hash key
|
2020-10-23 09:45:16 +00:00
|
|
|
* @opaque: user supplied data blob
|
2006-11-15 20:11:56 +00:00
|
|
|
*
|
|
|
|
* Callback to identify hash entry desired
|
|
|
|
* Returns 1 if the hash entry is desired, 0 to move
|
|
|
|
* to next entry
|
|
|
|
*/
|
2020-10-21 11:31:16 +00:00
|
|
|
typedef int (*virHashSearcher) (const void *payload, const char *name,
|
2020-10-23 09:45:16 +00:00
|
|
|
const void *opaque);
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor and destructor.
|
|
|
|
*/
|
2019-11-21 19:27:58 +00:00
|
|
|
virHashTablePtr virHashNew(virHashDataFree dataFree);
|
2020-10-20 16:28:21 +00:00
|
|
|
virHashAtomicPtr virHashAtomicNew(virHashDataFree dataFree);
|
2011-02-18 21:30:24 +00:00
|
|
|
void virHashFree(virHashTablePtr table);
|
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
|
|
|
ssize_t virHashSize(const virHashTable *table);
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new entry to the hash table.
|
|
|
|
*/
|
2006-04-09 13:11:22 +00:00
|
|
|
int virHashAddEntry(virHashTablePtr table,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name, void *userdata);
|
2006-04-09 13:11:22 +00:00
|
|
|
int virHashUpdateEntry(virHashTablePtr table,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name,
|
2011-02-22 15:11:59 +00:00
|
|
|
void *userdata);
|
2015-07-02 12:21:27 +00:00
|
|
|
int virHashAtomicUpdate(virHashAtomicPtr table,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name,
|
2015-07-02 12:21:27 +00:00
|
|
|
void *userdata);
|
2005-11-30 13:36:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove an entry from the hash table.
|
|
|
|
*/
|
2006-04-09 13:11:22 +00:00
|
|
|
int virHashRemoveEntry(virHashTablePtr table,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name);
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2012-04-19 14:21:43 +00:00
|
|
|
/*
|
|
|
|
* Remove all entries from the hash table.
|
|
|
|
*/
|
2020-10-21 12:57:36 +00:00
|
|
|
void virHashRemoveAll(virHashTablePtr table);
|
2012-04-19 14:21:43 +00:00
|
|
|
|
2005-11-30 13:36:58 +00:00
|
|
|
/*
|
|
|
|
* Retrieve the userdata.
|
|
|
|
*/
|
2020-10-21 11:31:16 +00:00
|
|
|
void *virHashLookup(const virHashTable *table, const char *name);
|
|
|
|
bool virHashHasEntry(const virHashTable *table, const char *name);
|
2005-11-30 13:36:58 +00:00
|
|
|
|
2011-02-22 15:11:59 +00:00
|
|
|
/*
|
|
|
|
* Retrieve & remove the userdata.
|
|
|
|
*/
|
2020-10-21 11:31:16 +00:00
|
|
|
void *virHashSteal(virHashTablePtr table, const char *name);
|
2015-07-02 12:21:27 +00:00
|
|
|
void *virHashAtomicSteal(virHashAtomicPtr table,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name);
|
2011-02-22 15:11:59 +00:00
|
|
|
|
2011-11-18 16:58:17 +00:00
|
|
|
/*
|
|
|
|
* Get the hash table's key/value pairs and have them optionally sorted.
|
|
|
|
* The returned array contains virHashSize() elements. Additionally,
|
|
|
|
* an empty element has been added to the end of the array (with key == NULL)
|
|
|
|
* to indicate the end of the array.
|
|
|
|
* The key/value pairs are only valid as long as the underlying hash
|
|
|
|
* table is not modified, i.e., no keys are removed or inserted, and
|
|
|
|
* the hash table is not deleted.
|
|
|
|
* The caller must only free the returned array using VIR_FREE().
|
|
|
|
* The caller must make copies of all returned keys and values if they are
|
|
|
|
* to be used somewhere else.
|
|
|
|
*/
|
|
|
|
typedef struct _virHashKeyValuePair virHashKeyValuePair;
|
|
|
|
typedef virHashKeyValuePair *virHashKeyValuePairPtr;
|
|
|
|
struct _virHashKeyValuePair {
|
|
|
|
const void *key;
|
|
|
|
const void *value;
|
|
|
|
};
|
|
|
|
virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
|
2020-10-22 15:38:53 +00:00
|
|
|
size_t *nitems,
|
|
|
|
bool sortedKeys);
|
2006-11-15 20:11:56 +00:00
|
|
|
|
2012-01-23 20:35:54 +00:00
|
|
|
/*
|
|
|
|
* Compare two tables for equality: the lookup of a key's value in
|
|
|
|
* both tables must result in an equivalent value.
|
|
|
|
* The caller must pass in a comparator function for comparing the values
|
|
|
|
* of two keys.
|
|
|
|
*/
|
|
|
|
typedef int (*virHashValueComparator)(const void *value1, const void *value2);
|
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);
|
|
|
|
|
|
|
|
|
2006-11-15 20:11:56 +00:00
|
|
|
/*
|
|
|
|
* Iterators
|
|
|
|
*/
|
2020-10-23 09:45:16 +00:00
|
|
|
int virHashForEach(virHashTablePtr table, virHashIterator iter, void *opaque);
|
2020-10-26 14:30:10 +00:00
|
|
|
int virHashForEachSafe(virHashTablePtr table, virHashIterator iter, void *opaque);
|
2020-10-26 14:28:08 +00:00
|
|
|
int virHashForEachSorted(virHashTablePtr table, virHashIterator iter, void *opaque);
|
2020-10-23 09:45:16 +00:00
|
|
|
ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *opaque);
|
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 *table, virHashSearcher iter,
|
2020-10-23 09:45:16 +00:00
|
|
|
const void *opaque, char **name);
|
2006-11-15 20:11:56 +00:00
|
|
|
|
2019-10-15 12:47:50 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virHashTable, virHashFree);
|