]> xenbits.xensource.com Git - libvirt.git/commitdiff
maint: avoid 'const fooPtr' in hashes
authorEric Blake <eblake@redhat.com>
Sat, 5 Oct 2013 02:30:35 +0000 (20:30 -0600)
committerEric Blake <eblake@redhat.com>
Mon, 14 Oct 2013 17:40:24 +0000 (11:40 -0600)
'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>
src/conf/nwfilter_params.c
src/nwfilter/nwfilter_ebiptables_driver.c
src/util/virhash.c
src/util/virhash.h
tests/virhashtest.c

index 7ae4a44296f329061ea1b819ced03648e63b95bf..c23de65763b476d92126ef822327de2abcbb7098 100644 (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
index 56c962be20e67770ca38113bc84791471b8f68e6..2fd8afb361e4a2fdfd06fda63228a2e230c5c58d 100644 (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 -
index 41c5920144e715fee5384d1ac94c5d95ad9bfa25..0857805e5cca45b61a235cc97df7db5a1f4dd455 100644 (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 = {
index 2c04f811f2b122f7b0a38e23ab18d21864aca42a..4de9a143e272e667dce54d05e49359d891e24905 100644 (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__ */
index 51197813f0401709c9c34e8207506aa308d15288..dbc0dbaaac9e696187692b01ec8c652af114c602 100644 (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);
 }