virhash.h 6.0 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
 *
E
Eric Blake 已提交
6
 * Copyright (C) 2005-2014 Red Hat, Inc.
7
 * Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
D
Daniel Veillard 已提交
8 9
 *
 * Author: Bjorn Reese <bjorn.reese@systematic.dk>
10
 *         Daniel Veillard <veillard@redhat.com>
D
Daniel Veillard 已提交
11 12
 */

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

16 17
# include <stdint.h>

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

J
Jiri Denemark 已提交
24 25 26
typedef struct _virHashAtomic virHashAtomic;
typedef virHashAtomic *virHashAtomicPtr;

D
Daniel Veillard 已提交
27 28 29
/*
 * function types:
 */
30

D
Daniel Veillard 已提交
31
/**
32
 * virHashDataFree:
D
Daniel Veillard 已提交
33 34 35 36 37
 * @payload:  the data in the hash
 * @name:  the name associated
 *
 * Callback to free data from a hash.
 */
38
typedef void (*virHashDataFree) (void *payload, const void *name);
39 40 41
/**
 * virHashIterator:
 * @payload: the data in the hash
42
 * @name: the hash key
43 44 45 46
 * @data: user supplied data blob
 *
 * Callback to process a hash entry during iteration
 */
47
typedef void (*virHashIterator) (void *payload, const void *name, void *data);
48
/**
49
 * virHashSearcher:
50
 * @payload: the data in the hash
51
 * @name: the hash key
52 53 54 55 56 57
 * @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
 */
58
typedef int (*virHashSearcher) (const void *payload, const void *name,
59
                                const void *data);
D
Daniel Veillard 已提交
60

61 62 63
/**
 * virHashKeyCode:
 * @name: the hash key
64
 * @seed: random seed
65
 *
66 67
 * Compute the hash code corresponding to the key @name, using
 * @seed to perturb the hashing algorithm
68 69 70
 *
 * Returns the hash code
 */
71 72
typedef uint32_t (*virHashKeyCode)(const void *name,
                                   uint32_t seed);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/**
 * 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 已提交
102 103 104
/*
 * Constructor and destructor.
 */
105
virHashTablePtr virHashCreate(ssize_t size,
106
                              virHashDataFree dataFree);
J
Jiri Denemark 已提交
107 108
virHashAtomicPtr virHashAtomicNew(ssize_t size,
                                  virHashDataFree dataFree);
109
virHashTablePtr virHashCreateFull(ssize_t size,
110 111 112 113 114
                                  virHashDataFree dataFree,
                                  virHashKeyCode keyCode,
                                  virHashKeyEqual keyEqual,
                                  virHashKeyCopy keyCopy,
                                  virHashKeyFree keyFree);
115
void virHashFree(virHashTablePtr table);
E
Eric Blake 已提交
116 117
ssize_t virHashSize(const virHashTable *table);
ssize_t virHashTableSize(const virHashTable *table);
D
Daniel Veillard 已提交
118 119 120 121

/*
 * Add a new entry to the hash table.
 */
122
int virHashAddEntry(virHashTablePtr table,
123
                    const void *name, void *userdata);
124
int virHashUpdateEntry(virHashTablePtr table,
125
                       const void *name,
126
                       void *userdata);
J
Jiri Denemark 已提交
127 128 129
int virHashAtomicUpdate(virHashAtomicPtr table,
                        const void *name,
                        void *userdata);
D
Daniel Veillard 已提交
130 131 132 133

/*
 * Remove an entry from the hash table.
 */
134
int virHashRemoveEntry(virHashTablePtr table,
135
                       const void *name);
136

137 138 139 140 141
/*
 * Remove all entries from the hash table.
 */
ssize_t virHashRemoveAll(virHashTablePtr table);

D
Daniel Veillard 已提交
142 143 144
/*
 * Retrieve the userdata.
 */
E
Eric Blake 已提交
145
void *virHashLookup(const virHashTable *table, const void *name);
D
Daniel Veillard 已提交
146

147 148 149
/*
 * Retrieve & remove the userdata.
 */
150
void *virHashSteal(virHashTablePtr table, const void *name);
J
Jiri Denemark 已提交
151 152
void *virHashAtomicSteal(virHashAtomicPtr table,
                         const void *name);
153

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
/*
 * 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;
};
E
Eric Blake 已提交
172 173
typedef int (*virHashKeyComparator)(const virHashKeyValuePair *,
                                    const virHashKeyValuePair *);
174 175
virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
                                       virHashKeyComparator compar);
176

177 178 179 180 181 182 183
/*
 * 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);
E
Eric Blake 已提交
184 185
bool virHashEqual(const virHashTable *table1,
                  const virHashTable *table2,
186 187 188
                  virHashValueComparator compar);


189 190 191
/*
 * Iterators
 */
192 193
ssize_t virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
E
Eric Blake 已提交
194 195
void *virHashSearch(const virHashTable *table, virHashSearcher iter,
                    const void *data);
196

E
Eric Blake 已提交
197 198 199
/* Convenience for when VIR_FREE(value) is sufficient as a data freer.  */
void virHashValueFree(void *value, const void *name);

200
#endif                          /* ! __VIR_HASH_H__ */