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 * 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 * Copyright (C) 2010 IBM Corporation
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -877,10 +877,10 @@ err_exit:
static int static int
virNWFilterFormatParameterNameSorter(const virHashKeyValuePairPtr a, virNWFilterFormatParameterNameSorter(const virHashKeyValuePair *a,
const virHashKeyValuePairPtr b) const virHashKeyValuePair *b)
{ {
return strcmp((const char *)a->key, (const char *)b->key); return strcmp(a->key, b->key);
} }
int int

View File

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

View File

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

View File

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

View File

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