virhash.h 6.2 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
 */

10
#pragma once
D
Daniel Veillard 已提交
11 12 13 14

/*
 * The hash table.
 */
15 16
typedef struct _virHashTable virHashTable;
typedef virHashTable *virHashTablePtr;
D
Daniel Veillard 已提交
17

J
Jiri Denemark 已提交
18 19 20
typedef struct _virHashAtomic virHashAtomic;
typedef virHashAtomic *virHashAtomicPtr;

D
Daniel Veillard 已提交
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 36 37 38 39 40 41
/**
 * virHashDataFreeSimple:
 * @payload:  the data in the hash
 * @name:  the name associated
 *
 * Callback to free data from a hash.
 */
typedef void (*virHashDataFreeSimple) (void *payload);

42 43 44
/**
 * virHashIterator:
 * @payload: the data in the hash
45
 * @name: the hash key
46 47 48
 * @data: user supplied data blob
 *
 * Callback to process a hash entry during iteration
49 50
 *
 * Returns -1 to stop the iteration, e.g. in case of an error
51
 */
52
typedef int (*virHashIterator) (void *payload, const void *name, void *data);
53
/**
54
 * virHashSearcher:
55
 * @payload: the data in the hash
56
 * @name: the hash key
57 58 59 60 61 62
 * @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
 */
63
typedef int (*virHashSearcher) (const void *payload, const void *name,
64
                                const void *data);
D
Daniel Veillard 已提交
65

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

/*
 * Add a new entry to the hash table.
 */
128
int virHashAddEntry(virHashTablePtr table,
129
                    const void *name, void *userdata);
130
int virHashUpdateEntry(virHashTablePtr table,
131
                       const void *name,
132
                       void *userdata);
J
Jiri Denemark 已提交
133 134 135
int virHashAtomicUpdate(virHashAtomicPtr table,
                        const void *name,
                        void *userdata);
D
Daniel Veillard 已提交
136 137 138 139

/*
 * Remove an entry from the hash table.
 */
140
int virHashRemoveEntry(virHashTablePtr table,
141
                       const void *name);
142

143 144 145 146 147
/*
 * Remove all entries from the hash table.
 */
ssize_t virHashRemoveAll(virHashTablePtr table);

D
Daniel Veillard 已提交
148 149 150
/*
 * Retrieve the userdata.
 */
E
Eric Blake 已提交
151
void *virHashLookup(const virHashTable *table, const void *name);
D
Daniel Veillard 已提交
152

153 154 155
/*
 * Retrieve & remove the userdata.
 */
156
void *virHashSteal(virHashTablePtr table, const void *name);
J
Jiri Denemark 已提交
157 158
void *virHashAtomicSteal(virHashAtomicPtr table,
                         const void *name);
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/*
 * 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 已提交
178 179
typedef int (*virHashKeyComparator)(const virHashKeyValuePair *,
                                    const virHashKeyValuePair *);
180 181
virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
                                       virHashKeyComparator compar);
182

183 184 185 186 187 188 189
/*
 * 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 已提交
190 191
bool virHashEqual(const virHashTable *table1,
                  const virHashTable *table2,
192 193 194
                  virHashValueComparator compar);


195 196 197
/*
 * Iterators
 */
198
int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
199
ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
E
Eric Blake 已提交
200
void *virHashSearch(const virHashTable *table, virHashSearcher iter,
201
                    const void *data, void **name);
202

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

206
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virHashTable, virHashFree);