File: enum.c

package info (click to toggle)
babeltrace 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,636 kB
  • ctags: 3,016
  • sloc: ansic: 24,798; sh: 11,915; yacc: 2,318; makefile: 377; lex: 142
file content (470 lines) | stat: -rw-r--r-- 13,015 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * enum.c
 *
 * BabelTrace - Enumeration Type
 *
 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
 *
 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <babeltrace/compiler.h>
#include <babeltrace/format.h>
#include <babeltrace/types.h>
#include <stdint.h>
#include <glib.h>

#if (__LONG_MAX__ == 2147483647L)
#define WORD_SIZE	32
#elif (__LONG_MAX__ == 9223372036854775807L)
#define WORD_SIZE	64
#else
#error "Unknown long size."
#endif

static
struct bt_definition *_enum_definition_new(struct bt_declaration *declaration,
					struct definition_scope *parent_scope,
					GQuark field_name, int index,
					const char *root_name);
static
void _enum_definition_free(struct bt_definition *definition);

static
void enum_range_set_free(void *ptr)
{
	g_array_unref(ptr);
}

#if (WORD_SIZE == 32)
static inline
gpointer get_uint_v(uint64_t *v)
{
	return v;
}

static inline
gpointer get_int_v(int64_t *v)
{
	return v;
}

static
guint enum_val_hash(gconstpointer key)
{
	int64_t ukey = *(const int64_t *)key;

	return (guint)ukey ^ (guint)(ukey >> 32);
}

static
gboolean enum_val_equal(gconstpointer a, gconstpointer b)
{
	int64_t ua = *(const int64_t *)a;
	int64_t ub = *(const int64_t *)b;

	return ua == ub;
}

static
void enum_val_free(void *ptr)
{
	g_free(ptr);
}
#else  /* WORD_SIZE != 32 */
static inline
gpointer get_uint_v(uint64_t *v)
{
	return (gpointer) *v;
}

static inline
gpointer get_int_v(int64_t *v)
{
	return (gpointer) *v;
}

static
guint enum_val_hash(gconstpointer key)
{
	return g_direct_hash(key);
}

static
gboolean enum_val_equal(gconstpointer a, gconstpointer b)
{
	return g_direct_equal(a, b);
}

static
void enum_val_free(void *ptr)
{
}
#endif /* WORD_SIZE != 32 */

/*
 * Returns a GArray or NULL.
 * Caller must release the GArray with g_array_unref().
 */
GArray *bt_enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
			       uint64_t v)
{
	struct enum_range_to_quark *iter;
	GArray *qs, *ranges = NULL;

	/* Single values lookup */
	qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set,
				 get_uint_v(&v));

	/* Range lookup */
	bt_list_for_each_entry(iter, &enum_declaration->table.range_to_quark, node) {
		if (iter->range.start._unsigned > v || iter->range.end._unsigned < v)
			continue;
		if (!ranges) {
			size_t qs_len = 0;

			if (qs)
				qs_len = qs->len;
			ranges = g_array_sized_new(FALSE, TRUE,
					sizeof(GQuark),
					qs_len + 1);
			g_array_set_size(ranges, qs_len + 1);
			if (qs)
				memcpy(ranges->data, qs->data,
				       sizeof(GQuark) * qs_len);
			g_array_index(ranges, GQuark, qs_len) = iter->quark;
		} else {
			size_t qs_len = ranges->len;

			g_array_set_size(ranges, qs_len + 1);
			g_array_index(ranges, GQuark, qs_len) = iter->quark;
		}
	}
	if (!ranges) {
		if (!qs)
			return NULL;
		ranges = qs;
		g_array_ref(ranges);
	}
	return ranges;
}

/*
 * Returns a GArray or NULL.
 * Caller must release the GArray with g_array_unref().
 */
GArray *bt_enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
			      int64_t v)
{
	struct enum_range_to_quark *iter;
	GArray *qs, *ranges = NULL;

	/* Single values lookup */
	qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set,
				 get_int_v(&v));

	/* Range lookup */
	bt_list_for_each_entry(iter, &enum_declaration->table.range_to_quark, node) {
		if (iter->range.start._signed > v || iter->range.end._signed < v)
			continue;
		if (!ranges) {
			size_t qs_len = 0;

			if (qs)
				qs_len = qs->len;
			ranges = g_array_sized_new(FALSE, TRUE,
					sizeof(GQuark),
					qs_len + 1);
			g_array_set_size(ranges, qs_len + 1);
			if (qs)
				memcpy(ranges->data, qs->data,
				       sizeof(GQuark) * qs_len);
			g_array_index(ranges, GQuark, qs_len) = iter->quark;
		} else {
			size_t qs_len = ranges->len;

			g_array_set_size(ranges, qs_len + 1);
			g_array_index(ranges, GQuark, qs_len) = iter->quark;
		}
	}
	if (!ranges) {
		if (!qs)
			return NULL;
		ranges = qs;
		g_array_ref(ranges);
	}
	return ranges;
}

static
void bt_enum_unsigned_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
			 uint64_t v, GQuark q)
{
	uint64_t *valuep;
	GArray *array;

	array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set,
				    get_uint_v(&v));
	if (!array) {
		array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
		g_array_set_size(array, 1);
		g_array_index(array, GQuark, array->len - 1) = q;
#if (WORD_SIZE == 32)
		valuep = g_new(uint64_t, 1);
		*valuep = v;
#else  /* WORD_SIZE != 32 */
		valuep = get_uint_v(&v);
#endif /* WORD_SIZE != 32 */
		g_hash_table_insert(enum_declaration->table.value_to_quark_set, valuep, array);
	} else {
		g_array_set_size(array, array->len + 1);
		g_array_index(array, GQuark, array->len - 1) = q;
	}
}

static
void bt_enum_signed_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
			int64_t v, GQuark q)
{
	int64_t *valuep;
	GArray *array;

	array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set,
				    get_int_v(&v));
	if (!array) {
		array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
		g_array_set_size(array, 1);
		g_array_index(array, GQuark, array->len - 1) = q;
#if (WORD_SIZE == 32)
		valuep = g_new(int64_t, 1);
		*valuep = v;
#else  /* WORD_SIZE != 32 */
		valuep = get_int_v(&v);
#endif /* WORD_SIZE != 32 */
		g_hash_table_insert(enum_declaration->table.value_to_quark_set, valuep, array);
	} else {
		g_array_set_size(array, array->len + 1);
		g_array_index(array, GQuark, array->len - 1) = q;
	}
}

GArray *bt_enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
				GQuark q)
{
	return g_hash_table_lookup(enum_declaration->table.quark_to_range_set,
				   (gconstpointer) (unsigned long) q);
}

static
void bt_enum_signed_insert_range_to_quark(struct declaration_enum *enum_declaration,
                        int64_t start, int64_t end, GQuark q)
{
	struct enum_range_to_quark *rtoq;

	rtoq = g_new(struct enum_range_to_quark, 1);
	bt_list_add(&rtoq->node, &enum_declaration->table.range_to_quark);
	rtoq->range.start._signed = start;
	rtoq->range.end._signed = end;
	rtoq->quark = q;
}

static
void bt_enum_unsigned_insert_range_to_quark(struct declaration_enum *enum_declaration,
                        uint64_t start, uint64_t end, GQuark q)
{
	struct enum_range_to_quark *rtoq;

	rtoq = g_new(struct enum_range_to_quark, 1);
	bt_list_add(&rtoq->node, &enum_declaration->table.range_to_quark);
	rtoq->range.start._unsigned = start;
	rtoq->range.end._unsigned = end;
	rtoq->quark = q;
}

void bt_enum_signed_insert(struct declaration_enum *enum_declaration,
                        int64_t start, int64_t end, GQuark q)
{
	GArray *array;
	struct enum_range *range;

	if (start == end) {
		bt_enum_signed_insert_value_to_quark_set(enum_declaration, start, q);
	} else {
		if (start > end) {
			uint64_t tmp;

			tmp = start;
			start = end;
			end = tmp;
		}
		bt_enum_signed_insert_range_to_quark(enum_declaration, start, end, q);
	}

	array = g_hash_table_lookup(enum_declaration->table.quark_to_range_set,
				    (gconstpointer) (unsigned long) q);
	if (!array) {
		array = g_array_sized_new(FALSE, TRUE,
					  sizeof(struct enum_range), 1);
		g_hash_table_insert(enum_declaration->table.quark_to_range_set,
				    (gpointer) (unsigned long) q,
				    array);
	}
	g_array_set_size(array, array->len + 1);
	range = &g_array_index(array, struct enum_range, array->len - 1);
	range->start._signed = start;
	range->end._signed = end;
}

void bt_enum_unsigned_insert(struct declaration_enum *enum_declaration,
			  uint64_t start, uint64_t end, GQuark q)
{
	GArray *array;
	struct enum_range *range;


	if (start == end) {
		bt_enum_unsigned_insert_value_to_quark_set(enum_declaration, start, q);
	} else {
		if (start > end) {
			uint64_t tmp;

			tmp = start;
			start = end;
			end = tmp;
		}
		bt_enum_unsigned_insert_range_to_quark(enum_declaration, start, end, q);
	}

	array = g_hash_table_lookup(enum_declaration->table.quark_to_range_set,
				    (gconstpointer) (unsigned long) q);
	if (!array) {
		array = g_array_sized_new(FALSE, TRUE,
					  sizeof(struct enum_range), 1);
		g_hash_table_insert(enum_declaration->table.quark_to_range_set,
				    (gpointer) (unsigned long) q,
				    array);
	}
	g_array_set_size(array, array->len + 1);
	range = &g_array_index(array, struct enum_range, array->len - 1);
	range->start._unsigned = start;
	range->end._unsigned = end;
}

size_t bt_enum_get_nr_enumerators(struct declaration_enum *enum_declaration)
{
	return g_hash_table_size(enum_declaration->table.quark_to_range_set);
}

static
void _enum_declaration_free(struct bt_declaration *declaration)
{
	struct declaration_enum *enum_declaration =
		container_of(declaration, struct declaration_enum, p);
	struct enum_range_to_quark *iter, *tmp;

	g_hash_table_destroy(enum_declaration->table.value_to_quark_set);
	bt_list_for_each_entry_safe(iter, tmp, &enum_declaration->table.range_to_quark, node) {
		bt_list_del(&iter->node);
		g_free(iter);
	}
	g_hash_table_destroy(enum_declaration->table.quark_to_range_set);
	bt_declaration_unref(&enum_declaration->integer_declaration->p);
	g_free(enum_declaration);
}

struct declaration_enum *
	bt_enum_declaration_new(struct declaration_integer *integer_declaration)
{
	struct declaration_enum *enum_declaration;

	enum_declaration = g_new(struct declaration_enum, 1);

	enum_declaration->table.value_to_quark_set = g_hash_table_new_full(enum_val_hash,
							    enum_val_equal,
							    enum_val_free,
							    enum_range_set_free);
	BT_INIT_LIST_HEAD(&enum_declaration->table.range_to_quark);
	enum_declaration->table.quark_to_range_set = g_hash_table_new_full(g_direct_hash,
							g_direct_equal,
							NULL, enum_range_set_free);
	bt_declaration_ref(&integer_declaration->p);
	enum_declaration->integer_declaration = integer_declaration;
	enum_declaration->p.id = CTF_TYPE_ENUM;
	enum_declaration->p.alignment = 1;
	enum_declaration->p.declaration_free = _enum_declaration_free;
	enum_declaration->p.definition_new = _enum_definition_new;
	enum_declaration->p.definition_free = _enum_definition_free;
	enum_declaration->p.ref = 1;
	return enum_declaration;
}

static
struct bt_definition *
	_enum_definition_new(struct bt_declaration *declaration,
			     struct definition_scope *parent_scope,
			     GQuark field_name, int index,
			     const char *root_name)
{
	struct declaration_enum *enum_declaration =
		container_of(declaration, struct declaration_enum, p);
	struct definition_enum *_enum;
	struct bt_definition *definition_integer_parent;
	int ret;

	_enum = g_new(struct definition_enum, 1);
	bt_declaration_ref(&enum_declaration->p);
	_enum->p.declaration = declaration;
	_enum->declaration = enum_declaration;
	_enum->p.ref = 1;
	/*
	 * Use INT_MAX order to ensure that all fields of the parent
	 * scope are seen as being prior to this scope.
	 */
	_enum->p.index = root_name ? INT_MAX : index;
	_enum->p.name = field_name;
	_enum->p.path = bt_new_definition_path(parent_scope, field_name, root_name);
	_enum->p.scope = bt_new_definition_scope(parent_scope, field_name, root_name);
	_enum->value = NULL;
	ret = bt_register_field_definition(field_name, &_enum->p,
					parent_scope);
	assert(!ret);
	definition_integer_parent =
		enum_declaration->integer_declaration->p.definition_new(&enum_declaration->integer_declaration->p,
				_enum->p.scope,
				g_quark_from_static_string("container"), 0, NULL);
	_enum->integer = container_of(definition_integer_parent,
				      struct definition_integer, p);
	return &_enum->p;
}

static
void _enum_definition_free(struct bt_definition *definition)
{
	struct definition_enum *_enum =
		container_of(definition, struct definition_enum, p);

	bt_definition_unref(&_enum->integer->p);
	bt_free_definition_scope(_enum->p.scope);
	bt_declaration_unref(_enum->p.declaration);
	if (_enum->value)
		g_array_unref(_enum->value);
	g_free(_enum);
}