Create copy of integer declaration before applying base-16 for pointers
[babeltrace.git] / types / enum.c
... / ...
CommitLineData
1/*
2 * enum.c
3 *
4 * BabelTrace - Enumeration Type
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19#include <babeltrace/compiler.h>
20#include <babeltrace/format.h>
21#include <stdint.h>
22#include <glib.h>
23
24static
25struct definition *_enum_definition_new(struct declaration *declaration,
26 struct definition_scope *parent_scope,
27 GQuark field_name, int index);
28static
29void _enum_definition_free(struct definition *definition);
30
31static
32void enum_range_set_free(void *ptr)
33{
34 g_array_unref(ptr);
35}
36
37/*
38 * Returns a GArray or NULL.
39 * Caller must release the GArray with g_array_unref().
40 */
41GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
42 uint64_t v)
43{
44 struct enum_range_to_quark *iter;
45 GArray *qs, *ranges = NULL;
46
47 /* Single values lookup */
48 qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v);
49
50 /* Range lookup */
51 cds_list_for_each_entry(iter, &enum_declaration->table.range_to_quark, node) {
52 if (iter->range.start._unsigned > v || iter->range.end._unsigned < v)
53 continue;
54 if (!ranges) {
55 size_t qs_len = 0;
56
57 if (qs)
58 qs_len = qs->len;
59 ranges = g_array_sized_new(FALSE, TRUE,
60 sizeof(struct enum_range),
61 qs_len + 1);
62 g_array_set_size(ranges, qs_len + 1);
63 if (qs)
64 memcpy(ranges->data, qs->data,
65 sizeof(struct enum_range) * qs_len);
66 g_array_index(ranges, struct enum_range, qs_len) = iter->range;
67 } else {
68 g_array_set_size(ranges, ranges->len + 1);
69 g_array_index(ranges, struct enum_range, ranges->len) = iter->range;
70 }
71 }
72 if (!ranges) {
73 ranges = qs;
74 g_array_ref(ranges);
75 }
76 return ranges;
77}
78
79/*
80 * Returns a GArray or NULL.
81 * Caller must release the GArray with g_array_unref().
82 */
83GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
84 uint64_t v)
85{
86 struct enum_range_to_quark *iter;
87 GArray *qs, *ranges = NULL;
88
89 /* Single values lookup */
90 qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v);
91
92 /* Range lookup */
93 cds_list_for_each_entry(iter, &enum_declaration->table.range_to_quark, node) {
94 if (iter->range.start._signed > v || iter->range.end._signed < v)
95 continue;
96 if (!ranges) {
97 size_t qs_len = 0;
98
99 if (qs)
100 qs_len = qs->len;
101 ranges = g_array_sized_new(FALSE, TRUE,
102 sizeof(struct enum_range),
103 qs_len + 1);
104 g_array_set_size(ranges, qs_len + 1);
105 if (qs)
106 memcpy(ranges->data, qs->data,
107 sizeof(struct enum_range) * qs_len);
108 g_array_index(ranges, struct enum_range, qs_len) = iter->range;
109 } else {
110 g_array_set_size(ranges, ranges->len + 1);
111 g_array_index(ranges, struct enum_range, ranges->len) = iter->range;
112 }
113 }
114 if (!ranges) {
115 ranges = qs;
116 g_array_ref(ranges);
117 }
118 return ranges;
119}
120
121#if (__WORDSIZE == 32)
122static
123guint enum_val_hash(gconstpointer key)
124{
125 int64_t ukey = *(const int64_t *)key;
126
127 return (guint)ukey ^ (guint)(ukey >> 32);
128}
129
130static
131gboolean enum_val_equal(gconstpointer a, gconstpointer b)
132{
133 int64_t ua = *(const int64_t *)a;
134 int64_t ub = *(const int64_t *)b;
135
136 return ua == ub;
137}
138
139static
140void enum_val_free(void *ptr)
141{
142 g_free(ptr);
143}
144
145static
146void enum_signed_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
147 int64_t v, GQuark q)
148{
149 int64_t *valuep;
150 GArray *array;
151
152 array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v);
153 if (!array) {
154 array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
155 g_array_set_size(array, 1);
156 g_array_index(array, GQuark, array->len - 1) = q;
157 valuep = g_new(int64_t, 1);
158 *valuep = v;
159 g_hash_table_insert(enum_declaration->table.value_to_quark_set, valuep, array);
160 } else {
161 g_array_set_size(array, array->len + 1);
162 g_array_index(array, GQuark, array->len - 1) = q;
163 }
164}
165
166static
167void enum_unsigned_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
168 uint64_t v, GQuark q)
169{
170 uint64_t *valuep;
171 GArray *array;
172
173 array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v);
174 if (!array) {
175 array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
176 g_array_set_size(array, 1);
177 g_array_index(array, GQuark, array->len - 1) = q;
178 valuep = g_new(uint64_t, 1);
179 *valuep = v;
180 g_hash_table_insert(enum_declaration->table.value_to_quark_set, valuep, array);
181 } else {
182 g_array_set_size(array, array->len + 1);
183 g_array_index(array, GQuark, array->len - 1) = q;
184 }
185}
186#else /* __WORDSIZE != 32 */
187static
188guint enum_val_hash(gconstpointer key)
189{
190 return g_direct_hash(key);
191}
192
193static
194gboolean enum_val_equal(gconstpointer a, gconstpointer b)
195{
196 return g_direct_equal(a, b);
197}
198
199static
200void enum_val_free(void *ptr)
201{
202}
203
204static
205void enum_signed_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
206 int64_t v, GQuark q)
207{
208 GArray *array;
209
210 array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set,
211 (gconstpointer) v);
212 if (!array) {
213 array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
214 g_array_set_size(array, 1);
215 g_array_index(array, GQuark, array->len - 1) = q;
216 g_hash_table_insert(enum_declaration->table.value_to_quark_set,
217 (gpointer) v, array);
218 } else {
219 g_array_set_size(array, array->len + 1);
220 g_array_index(array, GQuark, array->len - 1) = q;
221 }
222}
223
224static
225void enum_unsigned_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
226 uint64_t v, GQuark q)
227{
228 GArray *array;
229
230 array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set,
231 (gconstpointer) v);
232 if (!array) {
233 array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
234 g_array_set_size(array, 1);
235 g_array_index(array, GQuark, array->len - 1) = q;
236 g_hash_table_insert(enum_declaration->table.value_to_quark_set,
237 (gpointer) v, array);
238 } else {
239 g_array_set_size(array, array->len + 1);
240 g_array_index(array, GQuark, array->len - 1) = q;
241 }
242}
243#endif /* __WORDSIZE != 32 */
244
245GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
246 GQuark q)
247{
248 return g_hash_table_lookup(enum_declaration->table.quark_to_range_set,
249 (gconstpointer) (unsigned long) q);
250}
251
252static
253void enum_signed_insert_range_to_quark(struct declaration_enum *enum_declaration,
254 int64_t start, int64_t end, GQuark q)
255{
256 struct enum_range_to_quark *rtoq;
257
258 rtoq = g_new(struct enum_range_to_quark, 1);
259 cds_list_add(&rtoq->node, &enum_declaration->table.range_to_quark);
260 rtoq->range.start._signed = start;
261 rtoq->range.end._signed = end;
262 rtoq->quark = q;
263}
264
265static
266void enum_unsigned_insert_range_to_quark(struct declaration_enum *enum_declaration,
267 uint64_t start, uint64_t end, GQuark q)
268{
269 struct enum_range_to_quark *rtoq;
270
271 rtoq = g_new(struct enum_range_to_quark, 1);
272 cds_list_add(&rtoq->node, &enum_declaration->table.range_to_quark);
273 rtoq->range.start._unsigned = start;
274 rtoq->range.end._unsigned = end;
275 rtoq->quark = q;
276}
277
278void enum_signed_insert(struct declaration_enum *enum_declaration,
279 int64_t start, int64_t end, GQuark q)
280{
281 GArray *array;
282 struct enum_range *range;
283
284 if (start == end) {
285 enum_signed_insert_value_to_quark_set(enum_declaration, start, q);
286 } else {
287 if (start > end) {
288 uint64_t tmp;
289
290 tmp = start;
291 start = end;
292 end = tmp;
293 }
294 enum_signed_insert_range_to_quark(enum_declaration, start, end, q);
295 }
296
297 array = g_hash_table_lookup(enum_declaration->table.quark_to_range_set,
298 (gconstpointer) (unsigned long) q);
299 if (!array) {
300 array = g_array_sized_new(FALSE, TRUE,
301 sizeof(struct enum_range), 1);
302 g_hash_table_insert(enum_declaration->table.quark_to_range_set,
303 (gpointer) (unsigned long) q,
304 array);
305 }
306 g_array_set_size(array, array->len + 1);
307 range = &g_array_index(array, struct enum_range, array->len - 1);
308 range->start._signed = start;
309 range->end._signed = end;
310}
311
312void enum_unsigned_insert(struct declaration_enum *enum_declaration,
313 uint64_t start, uint64_t end, GQuark q)
314{
315 GArray *array;
316 struct enum_range *range;
317
318
319 if (start == end) {
320 enum_unsigned_insert_value_to_quark_set(enum_declaration, start, q);
321 } else {
322 if (start > end) {
323 uint64_t tmp;
324
325 tmp = start;
326 start = end;
327 end = tmp;
328 }
329 enum_unsigned_insert_range_to_quark(enum_declaration, start, end, q);
330 }
331
332 array = g_hash_table_lookup(enum_declaration->table.quark_to_range_set,
333 (gconstpointer) (unsigned long) q);
334 if (!array) {
335 array = g_array_sized_new(FALSE, TRUE,
336 sizeof(struct enum_range), 1);
337 g_hash_table_insert(enum_declaration->table.quark_to_range_set,
338 (gpointer) (unsigned long) q,
339 array);
340 }
341 g_array_set_size(array, array->len + 1);
342 range = &g_array_index(array, struct enum_range, array->len - 1);
343 range->start._unsigned = start;
344 range->end._unsigned = end;
345}
346
347size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration)
348{
349 return g_hash_table_size(enum_declaration->table.quark_to_range_set);
350}
351
352static
353void _enum_declaration_free(struct declaration *declaration)
354{
355 struct declaration_enum *enum_declaration =
356 container_of(declaration, struct declaration_enum, p);
357 struct enum_range_to_quark *iter, *tmp;
358
359 g_hash_table_destroy(enum_declaration->table.value_to_quark_set);
360 cds_list_for_each_entry_safe(iter, tmp, &enum_declaration->table.range_to_quark, node) {
361 cds_list_del(&iter->node);
362 g_free(iter);
363 }
364 g_hash_table_destroy(enum_declaration->table.quark_to_range_set);
365 declaration_unref(&enum_declaration->integer_declaration->p);
366 g_free(enum_declaration);
367}
368
369struct declaration_enum *
370 enum_declaration_new(struct declaration_integer *integer_declaration)
371{
372 struct declaration_enum *enum_declaration;
373
374 enum_declaration = g_new(struct declaration_enum, 1);
375
376 enum_declaration->table.value_to_quark_set = g_hash_table_new_full(enum_val_hash,
377 enum_val_equal,
378 enum_val_free,
379 enum_range_set_free);
380 CDS_INIT_LIST_HEAD(&enum_declaration->table.range_to_quark);
381 enum_declaration->table.quark_to_range_set = g_hash_table_new_full(g_direct_hash,
382 g_direct_equal,
383 NULL, enum_range_set_free);
384 declaration_ref(&integer_declaration->p);
385 enum_declaration->integer_declaration = integer_declaration;
386 enum_declaration->p.id = CTF_TYPE_ENUM;
387 enum_declaration->p.alignment = 1;
388 enum_declaration->p.declaration_free = _enum_declaration_free;
389 enum_declaration->p.definition_new = _enum_definition_new;
390 enum_declaration->p.definition_free = _enum_definition_free;
391 enum_declaration->p.ref = 1;
392 return enum_declaration;
393}
394
395static
396struct definition *
397 _enum_definition_new(struct declaration *declaration,
398 struct definition_scope *parent_scope,
399 GQuark field_name, int index)
400{
401 struct declaration_enum *enum_declaration =
402 container_of(declaration, struct declaration_enum, p);
403 struct definition_enum *_enum;
404 struct definition *definition_integer_parent;
405
406 _enum = g_new(struct definition_enum, 1);
407 declaration_ref(&enum_declaration->p);
408 _enum->p.declaration = declaration;
409 _enum->declaration = enum_declaration;
410 _enum->p.ref = 1;
411 _enum->p.index = index;
412 _enum->p.name = field_name;
413 _enum->p.path = new_definition_path(parent_scope, field_name);
414 _enum->value = NULL;
415 definition_integer_parent =
416 enum_declaration->integer_declaration->p.definition_new(&enum_declaration->integer_declaration->p,
417 parent_scope,
418 g_quark_from_static_string("container"), 0);
419 _enum->integer = container_of(definition_integer_parent,
420 struct definition_integer, p);
421 return &_enum->p;
422}
423
424static
425void _enum_definition_free(struct definition *definition)
426{
427 struct definition_enum *_enum =
428 container_of(definition, struct definition_enum, p);
429
430 definition_unref(&_enum->integer->p);
431 declaration_unref(_enum->p.declaration);
432 if (_enum->value)
433 g_array_unref(_enum->value);
434 g_free(_enum);
435}
This page took 0.023739 seconds and 4 git commands to generate.