prometheus-client-mmap 0.7.0.beta42 → 0.7.0.beta43

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,267 @@
1
+ /*
2
+ * Copyright (c) 2016-2017 David Leeds <davidesleeds@gmail.com>
3
+ *
4
+ * Hashmap is free software; you can redistribute it and/or modify
5
+ * it under the terms of the MIT license. See LICENSE for details.
6
+ */
7
+
8
+ #ifndef __HASHMAP_H__
9
+ #define __HASHMAP_H__
10
+
11
+ #include <stddef.h>
12
+
13
+ /*
14
+ * Define HASHMAP_METRICS to compile in performance analysis
15
+ * functions for use in assessing hash function performance.
16
+ */
17
+ /* #define HASHMAP_METRICS */
18
+
19
+ /*
20
+ * Define HASHMAP_NOASSERT to compile out all assertions used internally.
21
+ */
22
+ /* #define HASHMAP_NOASSERT */
23
+
24
+ /*
25
+ * Macros to declare type-specific versions of hashmap_*() functions to
26
+ * allow compile-time type checking and avoid the need for type casting.
27
+ */
28
+ #define HASHMAP_FUNCS_DECLARE(name, key_type, data_type) \
29
+ data_type *name##_hashmap_put(struct hashmap *map, key_type *key, \
30
+ data_type *data); \
31
+ data_type *name##_hashmap_get(const struct hashmap *map, \
32
+ key_type *key); \
33
+ data_type *name##_hashmap_remove(struct hashmap *map, \
34
+ key_type *key); \
35
+ key_type *name##_hashmap_iter_get_key( \
36
+ const struct hashmap_iter *iter); \
37
+ data_type *name##_hashmap_iter_get_data( \
38
+ const struct hashmap_iter *iter); \
39
+ void name##_hashmap_iter_set_data(const struct hashmap_iter *iter, \
40
+ data_type *data); \
41
+ int name##_hashmap_foreach(const struct hashmap *map, \
42
+ int (*func)(key_type *, data_type *, void *), void *arg);
43
+
44
+ #define HASHMAP_FUNCS_CREATE(name, key_type, data_type) \
45
+ data_type *name##_hashmap_put(struct hashmap *map, key_type *key, \
46
+ data_type *data) \
47
+ { \
48
+ return (data_type *)hashmap_put(map, (const void *)key, \
49
+ (void *)data); \
50
+ } \
51
+ data_type *name##_hashmap_get(const struct hashmap *map, \
52
+ key_type *key) \
53
+ { \
54
+ return (data_type *)hashmap_get(map, (const void *)key); \
55
+ } \
56
+ data_type *name##_hashmap_remove(struct hashmap *map, \
57
+ key_type *key) \
58
+ { \
59
+ return (data_type *)hashmap_remove(map, (const void *)key); \
60
+ } \
61
+ key_type *name##_hashmap_iter_get_key( \
62
+ const struct hashmap_iter *iter) \
63
+ { \
64
+ return (key_type *)hashmap_iter_get_key(iter); \
65
+ } \
66
+ data_type *name##_hashmap_iter_get_data( \
67
+ const struct hashmap_iter *iter) \
68
+ { \
69
+ return (data_type *)hashmap_iter_get_data(iter); \
70
+ } \
71
+ void name##_hashmap_iter_set_data(const struct hashmap_iter *iter, \
72
+ data_type *data) \
73
+ { \
74
+ hashmap_iter_set_data(iter, (void *)data); \
75
+ } \
76
+ struct __##name##_hashmap_foreach_state { \
77
+ int (*func)(key_type *, data_type *, void *); \
78
+ void *arg; \
79
+ }; \
80
+ static inline int __##name##_hashmap_foreach_callback(const void *key, \
81
+ void *data, void *arg) \
82
+ { \
83
+ struct __##name##_hashmap_foreach_state *s = \
84
+ (struct __##name##_hashmap_foreach_state *)arg; \
85
+ return s->func((key_type *)key, (data_type *)data, s->arg); \
86
+ } \
87
+ int name##_hashmap_foreach(const struct hashmap *map, \
88
+ int (*func)(key_type *, data_type *, void *), void *arg) \
89
+ { \
90
+ struct __##name##_hashmap_foreach_state s = { func, arg }; \
91
+ return hashmap_foreach(map, \
92
+ __##name##_hashmap_foreach_callback, &s); \
93
+ }
94
+
95
+
96
+ struct hashmap_iter;
97
+ struct hashmap_entry;
98
+
99
+ /*
100
+ * The hashmap state structure.
101
+ */
102
+ struct hashmap {
103
+ size_t table_size_init;
104
+ size_t table_size;
105
+ size_t num_entries;
106
+ struct hashmap_entry *table;
107
+ size_t (*hash)(const void *);
108
+ int (*key_compare)(const void *, const void *);
109
+ void *(*key_alloc)(const void *);
110
+ void (*key_free)(void *);
111
+ };
112
+
113
+ /*
114
+ * Initialize an empty hashmap. A hash function and a key comparator are
115
+ * required.
116
+ *
117
+ * hash_func should return an even distribution of numbers between 0
118
+ * and SIZE_MAX varying on the key provided.
119
+ *
120
+ * key_compare_func should return 0 if the keys match, and non-zero otherwise.
121
+ *
122
+ * initial_size is optional, and may be set to the max number of entries
123
+ * expected to be put in the hash table. This is used as a hint to
124
+ * pre-allocate the hash table to the minimum size to avoid gratuitous rehashes.
125
+ * If initial_size 0, a default size will be used.
126
+ *
127
+ * Returns 0 on success and -errno on failure.
128
+ */
129
+ int hashmap_init(struct hashmap *map, size_t (*hash_func)(const void *),
130
+ int (*key_compare_func)(const void *, const void *),
131
+ size_t initial_size);
132
+
133
+ /*
134
+ * Free the hashmap and all associated memory.
135
+ */
136
+ void hashmap_destroy(struct hashmap *map);
137
+
138
+ /*
139
+ * Enable internal memory allocation and management of hash keys.
140
+ */
141
+ void hashmap_set_key_alloc_funcs(struct hashmap *map,
142
+ void *(*key_alloc_func)(const void *),
143
+ void (*key_free_func)(void *));
144
+
145
+ /*
146
+ * Add an entry to the hashmap. If an entry with a matching key already
147
+ * exists and has a data pointer associated with it, the existing data
148
+ * pointer is returned, instead of assigning the new value. Compare
149
+ * the return value with the data passed in to determine if a new entry was
150
+ * created. Returns NULL if memory allocation failed.
151
+ */
152
+ void *hashmap_put(struct hashmap *map, const void *key, void *data);
153
+
154
+ /*
155
+ * Return the data pointer, or NULL if no entry exists.
156
+ */
157
+ void *hashmap_get(const struct hashmap *map, const void *key);
158
+
159
+ /*
160
+ * Remove an entry with the specified key from the map.
161
+ * Returns the data pointer, or NULL, if no entry was found.
162
+ */
163
+ void *hashmap_remove(struct hashmap *map, const void *key);
164
+
165
+ /*
166
+ * Remove all entries.
167
+ */
168
+ void hashmap_clear(struct hashmap *map);
169
+
170
+ /*
171
+ * Remove all entries and reset the hash table to its initial size.
172
+ */
173
+ void hashmap_reset(struct hashmap *map);
174
+
175
+ /*
176
+ * Return the number of entries in the hash map.
177
+ */
178
+ size_t hashmap_size(const struct hashmap *map);
179
+
180
+ /*
181
+ * Get a new hashmap iterator. The iterator is an opaque
182
+ * pointer that may be used with hashmap_iter_*() functions.
183
+ * Hashmap iterators are INVALID after a put or remove operation is performed.
184
+ * hashmap_iter_remove() allows safe removal during iteration.
185
+ */
186
+ struct hashmap_iter *hashmap_iter(const struct hashmap *map);
187
+
188
+ /*
189
+ * Return an iterator to the next hashmap entry. Returns NULL if there are
190
+ * no more entries.
191
+ */
192
+ struct hashmap_iter *hashmap_iter_next(const struct hashmap *map,
193
+ const struct hashmap_iter *iter);
194
+
195
+ /*
196
+ * Remove the hashmap entry pointed to by this iterator and returns an
197
+ * iterator to the next entry. Returns NULL if there are no more entries.
198
+ */
199
+ struct hashmap_iter *hashmap_iter_remove(struct hashmap *map,
200
+ const struct hashmap_iter *iter);
201
+
202
+ /*
203
+ * Return the key of the entry pointed to by the iterator.
204
+ */
205
+ const void *hashmap_iter_get_key(const struct hashmap_iter *iter);
206
+
207
+ /*
208
+ * Return the data of the entry pointed to by the iterator.
209
+ */
210
+ void *hashmap_iter_get_data(const struct hashmap_iter *iter);
211
+
212
+ /*
213
+ * Set the data pointer of the entry pointed to by the iterator.
214
+ */
215
+ void hashmap_iter_set_data(const struct hashmap_iter *iter, void *data);
216
+
217
+ /*
218
+ * Invoke func for each entry in the hashmap. Unlike the hashmap_iter_*()
219
+ * interface, this function supports calls to hashmap_remove() during iteration.
220
+ * However, it is an error to put or remove an entry other than the current one,
221
+ * and doing so will immediately halt iteration and return an error.
222
+ * Iteration is stopped if func returns non-zero. Returns func's return
223
+ * value if it is < 0, otherwise, 0.
224
+ */
225
+ int hashmap_foreach(const struct hashmap *map,
226
+ int (*func)(const void *, void *, void *), void *arg);
227
+
228
+ /*
229
+ * Default hash function for string keys.
230
+ * This is an implementation of the well-documented Jenkins one-at-a-time
231
+ * hash function.
232
+ */
233
+ size_t hashmap_hash_string(const void *key);
234
+
235
+ /*
236
+ * Default key comparator function for string keys.
237
+ */
238
+ int hashmap_compare_string(const void *a, const void *b);
239
+
240
+ /*
241
+ * Default key allocation function for string keys. Use free() for the
242
+ * key_free_func.
243
+ */
244
+ void *hashmap_alloc_key_string(const void *key);
245
+
246
+
247
+ #ifdef HASHMAP_METRICS
248
+ /*
249
+ * Return the load factor.
250
+ */
251
+ double hashmap_load_factor(const struct hashmap *map);
252
+
253
+ /*
254
+ * Return the average number of collisions per entry.
255
+ */
256
+ double hashmap_collisions_mean(const struct hashmap *map);
257
+
258
+ /*
259
+ * Return the variance between entry collisions. The higher the variance,
260
+ * the more likely the hash function is poor and is resulting in clustering.
261
+ */
262
+ double hashmap_collisions_variance(const struct hashmap *map);
263
+ #endif
264
+
265
+
266
+ #endif /* __HASHMAP_H__ */
267
+
@@ -0,0 +1,22 @@
1
+ TOP_DIR := $(CURDIR)/..
2
+ SRC_DIR := $(TOP_DIR)/src
3
+
4
+ TARGET_EXEC := hashmap_test
5
+
6
+ SOURCES := $(SRC_DIR)/hashmap.c hashmap_test.c
7
+
8
+ CFLAGS += -O0 -g -ggdb -Wall -Wunused -Werror -I$(SRC_DIR) -DHASHMAP_METRICS
9
+
10
+ OBJS := $(SOURCES:%.c=%.o)
11
+
12
+
13
+ test: $(TARGET_EXEC)
14
+
15
+ $(TARGET_EXEC): $(OBJS)
16
+ @echo EXEC $@ ; \
17
+ $(CC) -o $@ $(OBJS)
18
+
19
+ clean:
20
+ rm -f $(OBJS) $(TARGET_EXEC)
21
+
22
+ .PHONY: test clean