hash.h 5.1 KB
Newer Older
D
Daniel Veillard 已提交
1
/*
2 3 4
 * Summary: Chained hash tables and domain/connections handling
 * Description: This module implements the hash table and allocation and
 *              deallocation of domains and connections
D
Daniel Veillard 已提交
5
 *
6
 * Copy: Copyright (C) 2005, 2011 Red Hat, Inc.
D
Daniel Veillard 已提交
7 8
 *
 * Author: Bjorn Reese <bjorn.reese@systematic.dk>
9
 *         Daniel Veillard <veillard@redhat.com>
D
Daniel Veillard 已提交
10 11
 */

12
#ifndef __VIR_HASH_H__
13
# define __VIR_HASH_H__
D
Daniel Veillard 已提交
14 15 16 17

/*
 * The hash table.
 */
18 19
typedef struct _virHashTable virHashTable;
typedef virHashTable *virHashTablePtr;
D
Daniel Veillard 已提交
20 21 22 23

/*
 * function types:
 */
24

D
Daniel Veillard 已提交
25
/**
26
 * virHashDataFree:
D
Daniel Veillard 已提交
27 28 29 30 31
 * @payload:  the data in the hash
 * @name:  the name associated
 *
 * Callback to free data from a hash.
 */
32
typedef void (*virHashDataFree) (void *payload, const void *name);
33 34 35
/**
 * virHashIterator:
 * @payload: the data in the hash
36
 * @name: the hash key
37 38 39 40
 * @data: user supplied data blob
 *
 * Callback to process a hash entry during iteration
 */
41
typedef void (*virHashIterator) (void *payload, const void *name, void *data);
42
/**
43
 * virHashSearcher:
44
 * @payload: the data in the hash
45
 * @name: the hash key
46 47 48 49 50 51
 * @data: user supplied data blob
 *
 * Callback to identify hash entry desired
 * Returns 1 if the hash entry is desired, 0 to move
 * to next entry
 */
52
typedef int (*virHashSearcher) (const void *payload, const void *name,
53
                                const void *data);
D
Daniel Veillard 已提交
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/**
 * virHashKeyCode:
 * @name: the hash key
 *
 * Compute the hash code corresponding to the key @name
 *
 * Returns the hash code
 */
typedef unsigned long (*virHashKeyCode)(const void *name);
/**
 * virHashKeyEqual:
 * @namea: the first hash key
 * @nameb: the second hash key
 *
 * Compare two hash keys for equality
 *
 * Returns true if the keys are equal, false otherwise
 */
typedef bool (*virHashKeyEqual)(const void *namea, const void *nameb);
/**
 * virHashKeyCopy:
 * @name: the hash key
 *
 * Create a copy of the hash key, duplicating
 * memory allocation where applicable
 *
 * Returns a newly allocated copy of @name
 */
typedef void *(*virHashKeyCopy)(const void *name);
/**
 * virHashKeyFree:
 * @name: the hash key
 *
 * Free any memory associated with the hash
 * key @name
 */
typedef void (*virHashKeyFree)(void *name);

D
Daniel Veillard 已提交
93 94 95
/*
 * Constructor and destructor.
 */
96 97 98 99 100 101 102 103
virHashTablePtr virHashCreate(int size,
                              virHashDataFree dataFree);
virHashTablePtr virHashCreateFull(int size,
                                  virHashDataFree dataFree,
                                  virHashKeyCode keyCode,
                                  virHashKeyEqual keyEqual,
                                  virHashKeyCopy keyCopy,
                                  virHashKeyFree keyFree);
104
void virHashFree(virHashTablePtr table);
105
int virHashSize(virHashTablePtr table);
106
int virHashTableSize(virHashTablePtr table);
D
Daniel Veillard 已提交
107 108 109 110

/*
 * Add a new entry to the hash table.
 */
111
int virHashAddEntry(virHashTablePtr table,
112
                    const void *name, void *userdata);
113
int virHashUpdateEntry(virHashTablePtr table,
114
                       const void *name,
115
                       void *userdata);
D
Daniel Veillard 已提交
116 117 118 119

/*
 * Remove an entry from the hash table.
 */
120
int virHashRemoveEntry(virHashTablePtr table,
121
                       const void *name);
122

D
Daniel Veillard 已提交
123 124 125
/*
 * Retrieve the userdata.
 */
126
void *virHashLookup(virHashTablePtr table, const void *name);
D
Daniel Veillard 已提交
127

128 129 130
/*
 * Retrieve & remove the userdata.
 */
131
void *virHashSteal(virHashTablePtr table, const void *name);
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/*
 * 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;
};
typedef int (*virHashKeyComparator)(const virHashKeyValuePairPtr,
                                    const virHashKeyValuePairPtr);
virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
                                       virHashKeyComparator compar);
155

156 157 158 159 160 161 162 163 164 165 166 167
/*
 * 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);
bool virHashEqual(const virHashTablePtr table1,
                  const virHashTablePtr table2,
                  virHashValueComparator compar);


168 169 170
/*
 * Iterators
 */
171
int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
172
int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
173 174
void *virHashSearch(virHashTablePtr table, virHashSearcher iter, const void *data);

175
#endif                          /* ! __VIR_HASH_H__ */