virhash.h 5.3 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 7
 * Copyright (C) 2005-2012 Red Hat, Inc.
 * 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 24 25 26

/*
 * function types:
 */
27

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

58 59 60
/**
 * virHashKeyCode:
 * @name: the hash key
61
 * @seed: random seed
62
 *
63 64
 * Compute the hash code corresponding to the key @name, using
 * @seed to perturb the hashing algorithm
65 66 67
 *
 * Returns the hash code
 */
68 69
typedef uint32_t (*virHashKeyCode)(const void *name,
                                   uint32_t seed);
70 71 72 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
/**
 * 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 已提交
99 100 101
/*
 * Constructor and destructor.
 */
102
virHashTablePtr virHashCreate(ssize_t size,
103
                              virHashDataFree dataFree);
104
virHashTablePtr virHashCreateFull(ssize_t size,
105 106 107 108 109
                                  virHashDataFree dataFree,
                                  virHashKeyCode keyCode,
                                  virHashKeyEqual keyEqual,
                                  virHashKeyCopy keyCopy,
                                  virHashKeyFree keyFree);
110
void virHashFree(virHashTablePtr table);
111 112
ssize_t virHashSize(virHashTablePtr table);
ssize_t virHashTableSize(virHashTablePtr table);
D
Daniel Veillard 已提交
113 114 115 116

/*
 * Add a new entry to the hash table.
 */
117
int virHashAddEntry(virHashTablePtr table,
118
                    const void *name, void *userdata);
119
int virHashUpdateEntry(virHashTablePtr table,
120
                       const void *name,
121
                       void *userdata);
D
Daniel Veillard 已提交
122 123 124 125

/*
 * Remove an entry from the hash table.
 */
126
int virHashRemoveEntry(virHashTablePtr table,
127
                       const void *name);
128

D
Daniel Veillard 已提交
129 130 131
/*
 * Retrieve the userdata.
 */
132
void *virHashLookup(virHashTablePtr table, const void *name);
D
Daniel Veillard 已提交
133

134 135 136
/*
 * Retrieve & remove the userdata.
 */
137
void *virHashSteal(virHashTablePtr table, const void *name);
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
/*
 * 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);
161

162 163 164 165 166 167 168 169 170 171 172 173
/*
 * 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);


174 175 176
/*
 * Iterators
 */
177 178
ssize_t virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
179 180
void *virHashSearch(virHashTablePtr table, virHashSearcher iter, const void *data);

181
#endif                          /* ! __VIR_HASH_H__ */