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>
This commit is contained in:
Eric Blake 2013-10-04 20:30:35 -06:00
parent a5da542360
commit b43efdaa13
5 changed files with 33 additions and 29 deletions

View File

@ -1,7 +1,7 @@
/*
* nwfilter_params.c: parsing and data maintenance of filter parameters
*
* Copyright (C) 2011-2012 Red Hat, Inc.
* Copyright (C) 2011-2013 Red Hat, Inc.
* Copyright (C) 2010 IBM Corporation
*
* This library is free software; you can redistribute it and/or
@ -877,10 +877,10 @@ err_exit:
static int
virNWFilterFormatParameterNameSorter(const virHashKeyValuePairPtr a,
const virHashKeyValuePairPtr b)
virNWFilterFormatParameterNameSorter(const virHashKeyValuePair *a,
const virHashKeyValuePair *b)
{
return strcmp((const char *)a->key, (const char *)b->key);
return strcmp(a->key, b->key);
}
int

View File

@ -1,7 +1,7 @@
/*
* nwfilter_ebiptables_driver.c: driver for ebtables/iptables on tap devices
*
* Copyright (C) 2011-2012 Red Hat, Inc.
* Copyright (C) 2011-2013 Red Hat, Inc.
* Copyright (C) 2010-2012 IBM Corp.
* Copyright (C) 2010-2012 Stefan Berger
*
@ -3596,8 +3596,8 @@ ebiptablesRuleOrderSortPtr(const void *a, const void *b)
}
static int
ebiptablesFilterOrderSort(const virHashKeyValuePairPtr a,
const virHashKeyValuePairPtr b)
ebiptablesFilterOrderSort(const virHashKeyValuePair *a,
const virHashKeyValuePair *b)
{
/* elements' values has been limited to range [-1000, 1000] */
return *(virNWFilterChainPriority *)a->value -

View File

@ -3,7 +3,7 @@
*
* Reference: Your favorite introductory book on algorithms
*
* Copyright (C) 2005-2012 Red Hat, Inc.
* Copyright (C) 2005-2013 Red Hat, Inc.
* Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
*
* Permission to use, copy, modify, and distribute this software for any
@ -98,7 +98,7 @@ static void virHashStrFree(void *name)
static size_t
virHashComputeKey(virHashTablePtr table, const void *name)
virHashComputeKey(const virHashTable *table, const void *name)
{
uint32_t value = table->keyCode(name, table->seed);
return value % table->size;
@ -361,7 +361,7 @@ virHashUpdateEntry(virHashTablePtr table, const void *name,
* Returns a pointer to the userdata
*/
void *
virHashLookup(virHashTablePtr table, const void *name)
virHashLookup(const virHashTable *table, const void *name)
{
size_t key;
virHashEntryPtr entry;
@ -411,7 +411,7 @@ void *virHashSteal(virHashTablePtr table, const void *name)
* -1 in case of error
*/
ssize_t
virHashSize(virHashTablePtr table)
virHashSize(const virHashTable *table)
{
if (table == NULL)
return -1;
@ -428,7 +428,7 @@ virHashSize(virHashTablePtr table)
* -1 in case of error
*/
ssize_t
virHashTableSize(virHashTablePtr table)
virHashTableSize(const virHashTable *table)
{
if (table == NULL)
return -1;
@ -609,12 +609,15 @@ virHashRemoveAll(virHashTablePtr table)
* returns non-zero will be returned by this function.
* The elements are processed in a undefined order
*/
void *virHashSearch(virHashTablePtr table,
void *virHashSearch(const virHashTable *ctable,
virHashSearcher iter,
const void *data)
{
size_t i;
/* Cast away const for internal detection of misuse. */
virHashTablePtr table = (virHashTablePtr)ctable;
if (table == NULL || iter == NULL)
return NULL;
@ -683,7 +686,7 @@ virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
struct virHashEqualData
{
bool equal;
const virHashTablePtr table2;
const virHashTable *table2;
virHashValueComparator compar;
};
@ -704,8 +707,8 @@ static int virHashEqualSearcher(const void *payload, const void *name,
return 0;
}
bool virHashEqual(const virHashTablePtr table1,
const virHashTablePtr table2,
bool virHashEqual(const virHashTable *table1,
const virHashTable *table2,
virHashValueComparator compar)
{
struct virHashEqualData data = {

View File

@ -3,7 +3,7 @@
* Description: This module implements the hash table and allocation and
* deallocation of domains and connections
*
* Copyright (C) 2005-2012 Red Hat, Inc.
* Copyright (C) 2005-2013 Red Hat, Inc.
* Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
*
* Author: Bjorn Reese <bjorn.reese@systematic.dk>
@ -108,8 +108,8 @@ virHashTablePtr virHashCreateFull(ssize_t size,
virHashKeyCopy keyCopy,
virHashKeyFree keyFree);
void virHashFree(virHashTablePtr table);
ssize_t virHashSize(virHashTablePtr table);
ssize_t virHashTableSize(virHashTablePtr table);
ssize_t virHashSize(const virHashTable *table);
ssize_t virHashTableSize(const virHashTable *table);
/*
* Add a new entry to the hash table.
@ -134,7 +134,7 @@ ssize_t virHashRemoveAll(virHashTablePtr table);
/*
* Retrieve the userdata.
*/
void *virHashLookup(virHashTablePtr table, const void *name);
void *virHashLookup(const virHashTable *table, const void *name);
/*
* Retrieve & remove the userdata.
@ -159,8 +159,8 @@ struct _virHashKeyValuePair {
const void *key;
const void *value;
};
typedef int (*virHashKeyComparator)(const virHashKeyValuePairPtr,
const virHashKeyValuePairPtr);
typedef int (*virHashKeyComparator)(const virHashKeyValuePair *,
const virHashKeyValuePair *);
virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
virHashKeyComparator compar);
@ -171,8 +171,8 @@ virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
* of two keys.
*/
typedef int (*virHashValueComparator)(const void *value1, const void *value2);
bool virHashEqual(const virHashTablePtr table1,
const virHashTablePtr table2,
bool virHashEqual(const virHashTable *table1,
const virHashTable *table2,
virHashValueComparator compar);
@ -181,6 +181,7 @@ bool virHashEqual(const virHashTablePtr table1,
*/
ssize_t virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
void *virHashSearch(virHashTablePtr table, virHashSearcher iter, const void *data);
void *virHashSearch(const virHashTable *table, virHashSearcher iter,
const void *data);
#endif /* ! __VIR_HASH_H__ */

View File

@ -499,15 +499,15 @@ cleanup:
static int
testHashGetItemsCompKey(const virHashKeyValuePairPtr a,
const virHashKeyValuePairPtr b)
testHashGetItemsCompKey(const virHashKeyValuePair *a,
const virHashKeyValuePair *b)
{
return strcmp(a->key, b->key);
}
static int
testHashGetItemsCompValue(const virHashKeyValuePairPtr a,
const virHashKeyValuePairPtr b)
testHashGetItemsCompValue(const virHashKeyValuePair *a,
const virHashKeyValuePair *b)
{
return strcmp(a->value, b->value);
}