prometheus-client-mmap 0.7.0.beta39 → 0.7.0.beta40

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,266 @@
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__ */
@@ -0,0 +1,313 @@
1
+ #include "jsmn.h"
2
+
3
+ /**
4
+ * Allocates a fresh unused token from the token pull.
5
+ */
6
+ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
7
+ jsmntok_t *tokens, size_t num_tokens) {
8
+ jsmntok_t *tok;
9
+ if (parser->toknext >= num_tokens) {
10
+ return NULL;
11
+ }
12
+ tok = &tokens[parser->toknext++];
13
+ tok->start = tok->end = -1;
14
+ tok->size = 0;
15
+ #ifdef JSMN_PARENT_LINKS
16
+ tok->parent = -1;
17
+ #endif
18
+ return tok;
19
+ }
20
+
21
+ /**
22
+ * Fills token type and boundaries.
23
+ */
24
+ static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
25
+ int start, int end) {
26
+ token->type = type;
27
+ token->start = start;
28
+ token->end = end;
29
+ token->size = 0;
30
+ }
31
+
32
+ /**
33
+ * Fills next available token with JSON primitive.
34
+ */
35
+ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
36
+ size_t len, jsmntok_t *tokens, size_t num_tokens) {
37
+ jsmntok_t *token;
38
+ int start;
39
+
40
+ start = parser->pos;
41
+
42
+ for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
43
+ switch (js[parser->pos]) {
44
+ #ifndef JSMN_STRICT
45
+ /* In strict mode primitive must be followed by "," or "}" or "]" */
46
+ case ':':
47
+ #endif
48
+ case '\t' : case '\r' : case '\n' : case ' ' :
49
+ case ',' : case ']' : case '}' :
50
+ goto found;
51
+ }
52
+ if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
53
+ parser->pos = start;
54
+ return JSMN_ERROR_INVAL;
55
+ }
56
+ }
57
+ #ifdef JSMN_STRICT
58
+ /* In strict mode primitive must be followed by a comma/object/array */
59
+ parser->pos = start;
60
+ return JSMN_ERROR_PART;
61
+ #endif
62
+
63
+ found:
64
+ if (tokens == NULL) {
65
+ parser->pos--;
66
+ return 0;
67
+ }
68
+ token = jsmn_alloc_token(parser, tokens, num_tokens);
69
+ if (token == NULL) {
70
+ parser->pos = start;
71
+ return JSMN_ERROR_NOMEM;
72
+ }
73
+ jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
74
+ #ifdef JSMN_PARENT_LINKS
75
+ token->parent = parser->toksuper;
76
+ #endif
77
+ parser->pos--;
78
+ return 0;
79
+ }
80
+
81
+ /**
82
+ * Fills next token with JSON string.
83
+ */
84
+ static int jsmn_parse_string(jsmn_parser *parser, const char *js,
85
+ size_t len, jsmntok_t *tokens, size_t num_tokens) {
86
+ jsmntok_t *token;
87
+
88
+ int start = parser->pos;
89
+
90
+ parser->pos++;
91
+
92
+ /* Skip starting quote */
93
+ for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
94
+ char c = js[parser->pos];
95
+
96
+ /* Quote: end of string */
97
+ if (c == '\"') {
98
+ if (tokens == NULL) {
99
+ return 0;
100
+ }
101
+ token = jsmn_alloc_token(parser, tokens, num_tokens);
102
+ if (token == NULL) {
103
+ parser->pos = start;
104
+ return JSMN_ERROR_NOMEM;
105
+ }
106
+ jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
107
+ #ifdef JSMN_PARENT_LINKS
108
+ token->parent = parser->toksuper;
109
+ #endif
110
+ return 0;
111
+ }
112
+
113
+ /* Backslash: Quoted symbol expected */
114
+ if (c == '\\' && parser->pos + 1 < len) {
115
+ int i;
116
+ parser->pos++;
117
+ switch (js[parser->pos]) {
118
+ /* Allowed escaped symbols */
119
+ case '\"': case '/' : case '\\' : case 'b' :
120
+ case 'f' : case 'r' : case 'n' : case 't' :
121
+ break;
122
+ /* Allows escaped symbol \uXXXX */
123
+ case 'u':
124
+ parser->pos++;
125
+ for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
126
+ /* If it isn't a hex character we have an error */
127
+ if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
128
+ (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
129
+ (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
130
+ parser->pos = start;
131
+ return JSMN_ERROR_INVAL;
132
+ }
133
+ parser->pos++;
134
+ }
135
+ parser->pos--;
136
+ break;
137
+ /* Unexpected symbol */
138
+ default:
139
+ parser->pos = start;
140
+ return JSMN_ERROR_INVAL;
141
+ }
142
+ }
143
+ }
144
+ parser->pos = start;
145
+ return JSMN_ERROR_PART;
146
+ }
147
+
148
+ /**
149
+ * Parse JSON string and fill tokens.
150
+ */
151
+ int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
152
+ jsmntok_t *tokens, unsigned int num_tokens) {
153
+ int r;
154
+ int i;
155
+ jsmntok_t *token;
156
+ int count = parser->toknext;
157
+
158
+ for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
159
+ char c;
160
+ jsmntype_t type;
161
+
162
+ c = js[parser->pos];
163
+ switch (c) {
164
+ case '{': case '[':
165
+ count++;
166
+ if (tokens == NULL) {
167
+ break;
168
+ }
169
+ token = jsmn_alloc_token(parser, tokens, num_tokens);
170
+ if (token == NULL)
171
+ return JSMN_ERROR_NOMEM;
172
+ if (parser->toksuper != -1) {
173
+ tokens[parser->toksuper].size++;
174
+ #ifdef JSMN_PARENT_LINKS
175
+ token->parent = parser->toksuper;
176
+ #endif
177
+ }
178
+ token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
179
+ token->start = parser->pos;
180
+ parser->toksuper = parser->toknext - 1;
181
+ break;
182
+ case '}': case ']':
183
+ if (tokens == NULL)
184
+ break;
185
+ type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
186
+ #ifdef JSMN_PARENT_LINKS
187
+ if (parser->toknext < 1) {
188
+ return JSMN_ERROR_INVAL;
189
+ }
190
+ token = &tokens[parser->toknext - 1];
191
+ for (;;) {
192
+ if (token->start != -1 && token->end == -1) {
193
+ if (token->type != type) {
194
+ return JSMN_ERROR_INVAL;
195
+ }
196
+ token->end = parser->pos + 1;
197
+ parser->toksuper = token->parent;
198
+ break;
199
+ }
200
+ if (token->parent == -1) {
201
+ if(token->type != type || parser->toksuper == -1) {
202
+ return JSMN_ERROR_INVAL;
203
+ }
204
+ break;
205
+ }
206
+ token = &tokens[token->parent];
207
+ }
208
+ #else
209
+ for (i = parser->toknext - 1; i >= 0; i--) {
210
+ token = &tokens[i];
211
+ if (token->start != -1 && token->end == -1) {
212
+ if (token->type != type) {
213
+ return JSMN_ERROR_INVAL;
214
+ }
215
+ parser->toksuper = -1;
216
+ token->end = parser->pos + 1;
217
+ break;
218
+ }
219
+ }
220
+ /* Error if unmatched closing bracket */
221
+ if (i == -1) return JSMN_ERROR_INVAL;
222
+ for (; i >= 0; i--) {
223
+ token = &tokens[i];
224
+ if (token->start != -1 && token->end == -1) {
225
+ parser->toksuper = i;
226
+ break;
227
+ }
228
+ }
229
+ #endif
230
+ break;
231
+ case '\"':
232
+ r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
233
+ if (r < 0) return r;
234
+ count++;
235
+ if (parser->toksuper != -1 && tokens != NULL)
236
+ tokens[parser->toksuper].size++;
237
+ break;
238
+ case '\t' : case '\r' : case '\n' : case ' ':
239
+ break;
240
+ case ':':
241
+ parser->toksuper = parser->toknext - 1;
242
+ break;
243
+ case ',':
244
+ if (tokens != NULL && parser->toksuper != -1 &&
245
+ tokens[parser->toksuper].type != JSMN_ARRAY &&
246
+ tokens[parser->toksuper].type != JSMN_OBJECT) {
247
+ #ifdef JSMN_PARENT_LINKS
248
+ parser->toksuper = tokens[parser->toksuper].parent;
249
+ #else
250
+ for (i = parser->toknext - 1; i >= 0; i--) {
251
+ if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
252
+ if (tokens[i].start != -1 && tokens[i].end == -1) {
253
+ parser->toksuper = i;
254
+ break;
255
+ }
256
+ }
257
+ }
258
+ #endif
259
+ }
260
+ break;
261
+ #ifdef JSMN_STRICT
262
+ /* In strict mode primitives are: numbers and booleans */
263
+ case '-': case '0': case '1' : case '2': case '3' : case '4':
264
+ case '5': case '6': case '7' : case '8': case '9':
265
+ case 't': case 'f': case 'n' :
266
+ /* And they must not be keys of the object */
267
+ if (tokens != NULL && parser->toksuper != -1) {
268
+ jsmntok_t *t = &tokens[parser->toksuper];
269
+ if (t->type == JSMN_OBJECT ||
270
+ (t->type == JSMN_STRING && t->size != 0)) {
271
+ return JSMN_ERROR_INVAL;
272
+ }
273
+ }
274
+ #else
275
+ /* In non-strict mode every unquoted value is a primitive */
276
+ default:
277
+ #endif
278
+ r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
279
+ if (r < 0) return r;
280
+ count++;
281
+ if (parser->toksuper != -1 && tokens != NULL)
282
+ tokens[parser->toksuper].size++;
283
+ break;
284
+
285
+ #ifdef JSMN_STRICT
286
+ /* Unexpected char in strict mode */
287
+ default:
288
+ return JSMN_ERROR_INVAL;
289
+ #endif
290
+ }
291
+ }
292
+
293
+ if (tokens != NULL) {
294
+ for (i = parser->toknext - 1; i >= 0; i--) {
295
+ /* Unmatched opened object or array */
296
+ if (tokens[i].start != -1 && tokens[i].end == -1) {
297
+ return JSMN_ERROR_PART;
298
+ }
299
+ }
300
+ }
301
+
302
+ return count;
303
+ }
304
+
305
+ /**
306
+ * Creates a new parser based over a given buffer with an array of tokens
307
+ * available.
308
+ */
309
+ void jsmn_init(jsmn_parser *parser) {
310
+ parser->pos = 0;
311
+ parser->toknext = 0;
312
+ parser->toksuper = -1;
313
+ }