linker: privatize prio_heap and babeltrace_types
[babeltrace.git] / types / types.c
1 /*
2 * types.c
3 *
4 * BabelTrace - Converter
5 *
6 * Types registry.
7 *
8 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
9 *
10 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 */
22
23 #include <babeltrace/format.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/types.h>
26 #include <limits.h>
27 #include <glib.h>
28 #include <errno.h>
29
30 static
31 GQuark prefix_quark(const char *prefix, GQuark quark)
32 {
33 GQuark nq;
34 GString *str;
35
36 str = g_string_new(prefix);
37 g_string_append(str, g_quark_to_string(quark));
38 nq = g_quark_from_string(g_string_free(str, FALSE));
39 return nq;
40 }
41
42 static
43 struct declaration *
44 lookup_declaration_scope(GQuark declaration_name,
45 struct declaration_scope *scope)
46 {
47 return g_hash_table_lookup(scope->typedef_declarations,
48 (gconstpointer) (unsigned long) declaration_name);
49 }
50
51 struct declaration *lookup_declaration(GQuark declaration_name,
52 struct declaration_scope *scope)
53 {
54 struct declaration *declaration;
55
56 while (scope) {
57 declaration = lookup_declaration_scope(declaration_name,
58 scope);
59 if (declaration)
60 return declaration;
61 scope = scope->parent_scope;
62 }
63 return NULL;
64 }
65
66 int register_declaration(GQuark name, struct declaration *declaration,
67 struct declaration_scope *scope)
68 {
69 if (!name)
70 return -EPERM;
71
72 /* Only lookup in local scope */
73 if (lookup_declaration_scope(name, scope))
74 return -EEXIST;
75
76 g_hash_table_insert(scope->typedef_declarations,
77 (gpointer) (unsigned long) name,
78 declaration);
79 declaration_ref(declaration);
80 return 0;
81 }
82
83 static
84 struct definition *
85 lookup_field_definition_scope(GQuark field_name,
86 struct definition_scope *scope)
87 {
88 return g_hash_table_lookup(scope->definitions,
89 (gconstpointer) (unsigned long) field_name);
90 }
91
92 /*
93 * Returns the index at which the paths differ.
94 * If the value returned equals len, it means the paths are identical
95 * from index 0 to len-1.
96 */
97 static int compare_paths(GArray *a, GArray *b, int len)
98 {
99 int i;
100
101 assert(len <= a->len);
102 assert(len <= b->len);
103
104 for (i = 0; i < len; i++) {
105 GQuark qa, qb;
106
107 qa = g_array_index(a, GQuark, i);
108 qb = g_array_index(b, GQuark, i);
109 if (qa != qb)
110 return i;
111 }
112 return i;
113 }
114
115 static int is_path_child_of(GArray *path, GArray *maybe_parent)
116 {
117 int i, ret;
118
119 if (babeltrace_debug) {
120 int need_dot = 0;
121
122 printf_debug("Is path \"");
123 for (i = 0; i < path->len; need_dot = 1, i++)
124 printf("%s%s", need_dot ? "." : "",
125 g_quark_to_string(g_array_index(path, GQuark, i)));
126 need_dot = 0;
127 printf("\" child of \"");
128 for (i = 0; i < maybe_parent->len; need_dot = 1, i++)
129 printf("%s%s", need_dot ? "." : "",
130 g_quark_to_string(g_array_index(maybe_parent, GQuark, i)));
131 printf("\" ? ");
132 }
133
134 if (path->len <= maybe_parent->len) {
135 ret = 0;
136 goto end;
137 }
138 if (compare_paths(path, maybe_parent, maybe_parent->len)
139 == maybe_parent->len)
140 ret = 1;
141 else
142 ret = 0;
143 end:
144 if (babeltrace_debug)
145 printf("%s\n", ret ? "Yes" : "No");
146 return ret;
147 }
148
149 static struct definition_scope *
150 get_definition_scope(struct definition *definition)
151 {
152 return definition->scope;
153 }
154
155 /*
156 * OK, here is the fun. We want to lookup a field that is:
157 * - either in the same dynamic scope:
158 * - either in the current scope, but prior to the current field.
159 * - or in a parent scope (or parent of parent ...) still in a field
160 * prior to the current field position within the parents.
161 * - or in a different dynamic scope:
162 * - either in a upper dynamic scope (walk down a targeted scope from
163 * the dynamic scope root)
164 * - or in a lower dynamic scope (failure)
165 * The dynamic scope roots are linked together, so we can access the
166 * parent dynamic scope from the child dynamic scope by walking up to
167 * the parent.
168 * If we cannot find such a field that is prior to our current path, we
169 * return NULL.
170 *
171 * cur_path: the path leading to the variant definition.
172 * lookup_path: the path leading to the enum we want to look for.
173 * scope: the definition scope containing the variant definition.
174 */
175 struct definition *
176 lookup_path_definition(GArray *cur_path,
177 GArray *lookup_path,
178 struct definition_scope *scope)
179 {
180 struct definition *definition, *lookup_definition;
181 GQuark last;
182 int index;
183
184 /* Going up in the hierarchy. Check where we come from. */
185 assert(is_path_child_of(cur_path, scope->scope_path));
186 assert(cur_path->len - scope->scope_path->len == 1);
187
188 /*
189 * First, check if the target name is size one, present in
190 * our parent path, located prior to us.
191 */
192 if (lookup_path->len == 1) {
193 last = g_array_index(lookup_path, GQuark, 0);
194 lookup_definition = lookup_field_definition_scope(last, scope);
195 last = g_array_index(cur_path, GQuark, cur_path->len - 1);
196 definition = lookup_field_definition_scope(last, scope);
197 assert(definition);
198 if (lookup_definition && lookup_definition->index < definition->index)
199 return lookup_definition;
200 else
201 return NULL;
202 }
203
204 while (scope) {
205 if (is_path_child_of(cur_path, scope->scope_path) &&
206 cur_path->len - scope->scope_path->len == 1) {
207 last = g_array_index(cur_path, GQuark, cur_path->len - 1);
208 definition = lookup_field_definition_scope(last, scope);
209 assert(definition);
210 index = definition->index;
211 } else {
212 /*
213 * Getting to a dynamic scope parent. We are
214 * guaranteed that the parent is entirely
215 * located before the child.
216 */
217 index = -1;
218 }
219 lookup:
220 if (is_path_child_of(lookup_path, scope->scope_path)) {
221 /* Means we can lookup the field in this scope */
222 last = g_array_index(lookup_path, GQuark,
223 scope->scope_path->len);
224 lookup_definition = lookup_field_definition_scope(last, scope);
225 if (!lookup_definition || ((index != -1) && lookup_definition->index >= index))
226 return NULL;
227 /* Found it! And it is prior to the current field. */
228 if (lookup_path->len - scope->scope_path->len == 1) {
229 /* Direct child */
230 return lookup_definition;
231 } else {
232 scope = get_definition_scope(lookup_definition);
233 /* Check if the definition has a sub-scope */
234 if (!scope)
235 return NULL;
236 /*
237 * Don't compare index anymore, because we are
238 * going within a scope that has been validated
239 * to be entirely prior to the current scope.
240 */
241 cur_path = NULL;
242 index = -1;
243 goto lookup;
244 }
245 } else {
246 /* lookup_path is within an upper scope */
247 cur_path = scope->scope_path;
248 scope = scope->parent_scope;
249 }
250 }
251 return NULL;
252 }
253
254 int register_field_definition(GQuark field_name, struct definition *definition,
255 struct definition_scope *scope)
256 {
257 if (!scope || !field_name)
258 return -EPERM;
259
260 /* Only lookup in local scope */
261 if (lookup_field_definition_scope(field_name, scope))
262 return -EEXIST;
263
264 g_hash_table_insert(scope->definitions,
265 (gpointer) (unsigned long) field_name,
266 definition);
267 /* Don't keep reference on definition */
268 return 0;
269 }
270
271 void declaration_ref(struct declaration *declaration)
272 {
273 declaration->ref++;
274 }
275
276 void declaration_unref(struct declaration *declaration)
277 {
278 if (!declaration)
279 return;
280 if (!--declaration->ref)
281 declaration->declaration_free(declaration);
282 }
283
284 void definition_ref(struct definition *definition)
285 {
286 definition->ref++;
287 }
288
289 void definition_unref(struct definition *definition)
290 {
291 if (!definition)
292 return;
293 if (!--definition->ref)
294 definition->declaration->definition_free(definition);
295 }
296
297 struct declaration_scope *
298 new_declaration_scope(struct declaration_scope *parent_scope)
299 {
300 struct declaration_scope *scope = g_new(struct declaration_scope, 1);
301
302 scope->typedef_declarations = g_hash_table_new_full(g_direct_hash,
303 g_direct_equal, NULL,
304 (GDestroyNotify) declaration_unref);
305 scope->struct_declarations = g_hash_table_new_full(g_direct_hash,
306 g_direct_equal, NULL,
307 (GDestroyNotify) declaration_unref);
308 scope->variant_declarations = g_hash_table_new_full(g_direct_hash,
309 g_direct_equal, NULL,
310 (GDestroyNotify) declaration_unref);
311 scope->enum_declarations = g_hash_table_new_full(g_direct_hash,
312 g_direct_equal, NULL,
313 (GDestroyNotify) declaration_unref);
314 scope->parent_scope = parent_scope;
315 return scope;
316 }
317
318 void free_declaration_scope(struct declaration_scope *scope)
319 {
320 g_hash_table_destroy(scope->enum_declarations);
321 g_hash_table_destroy(scope->variant_declarations);
322 g_hash_table_destroy(scope->struct_declarations);
323 g_hash_table_destroy(scope->typedef_declarations);
324 g_free(scope);
325 }
326
327 static
328 struct declaration_struct *lookup_struct_declaration_scope(GQuark struct_name,
329 struct declaration_scope *scope)
330 {
331 return g_hash_table_lookup(scope->struct_declarations,
332 (gconstpointer) (unsigned long) struct_name);
333 }
334
335 struct declaration_struct *lookup_struct_declaration(GQuark struct_name,
336 struct declaration_scope *scope)
337 {
338 struct declaration_struct *declaration;
339
340 while (scope) {
341 declaration = lookup_struct_declaration_scope(struct_name, scope);
342 if (declaration)
343 return declaration;
344 scope = scope->parent_scope;
345 }
346 return NULL;
347 }
348
349 int register_struct_declaration(GQuark struct_name,
350 struct declaration_struct *struct_declaration,
351 struct declaration_scope *scope)
352 {
353 GQuark prefix_name;
354 int ret;
355
356 if (!struct_name)
357 return -EPERM;
358
359 /* Only lookup in local scope */
360 if (lookup_struct_declaration_scope(struct_name, scope))
361 return -EEXIST;
362
363 g_hash_table_insert(scope->struct_declarations,
364 (gpointer) (unsigned long) struct_name,
365 struct_declaration);
366 declaration_ref(&struct_declaration->p);
367
368 /* Also add in typedef/typealias scopes */
369 prefix_name = prefix_quark("struct ", struct_name);
370 ret = register_declaration(prefix_name, &struct_declaration->p, scope);
371 assert(!ret);
372 return 0;
373 }
374
375 static
376 struct declaration_untagged_variant *
377 lookup_variant_declaration_scope(GQuark variant_name,
378 struct declaration_scope *scope)
379 {
380 return g_hash_table_lookup(scope->variant_declarations,
381 (gconstpointer) (unsigned long) variant_name);
382 }
383
384 struct declaration_untagged_variant *
385 lookup_variant_declaration(GQuark variant_name,
386 struct declaration_scope *scope)
387 {
388 struct declaration_untagged_variant *declaration;
389
390 while (scope) {
391 declaration = lookup_variant_declaration_scope(variant_name, scope);
392 if (declaration)
393 return declaration;
394 scope = scope->parent_scope;
395 }
396 return NULL;
397 }
398
399 int register_variant_declaration(GQuark variant_name,
400 struct declaration_untagged_variant *untagged_variant_declaration,
401 struct declaration_scope *scope)
402 {
403 GQuark prefix_name;
404 int ret;
405
406 if (!variant_name)
407 return -EPERM;
408
409 /* Only lookup in local scope */
410 if (lookup_variant_declaration_scope(variant_name, scope))
411 return -EEXIST;
412
413 g_hash_table_insert(scope->variant_declarations,
414 (gpointer) (unsigned long) variant_name,
415 untagged_variant_declaration);
416 declaration_ref(&untagged_variant_declaration->p);
417
418 /* Also add in typedef/typealias scopes */
419 prefix_name = prefix_quark("variant ", variant_name);
420 ret = register_declaration(prefix_name,
421 &untagged_variant_declaration->p, scope);
422 assert(!ret);
423 return 0;
424 }
425
426 static
427 struct declaration_enum *
428 lookup_enum_declaration_scope(GQuark enum_name,
429 struct declaration_scope *scope)
430 {
431 return g_hash_table_lookup(scope->enum_declarations,
432 (gconstpointer) (unsigned long) enum_name);
433 }
434
435 struct declaration_enum *
436 lookup_enum_declaration(GQuark enum_name,
437 struct declaration_scope *scope)
438 {
439 struct declaration_enum *declaration;
440
441 while (scope) {
442 declaration = lookup_enum_declaration_scope(enum_name, scope);
443 if (declaration)
444 return declaration;
445 scope = scope->parent_scope;
446 }
447 return NULL;
448 }
449
450 int register_enum_declaration(GQuark enum_name,
451 struct declaration_enum *enum_declaration,
452 struct declaration_scope *scope)
453 {
454 GQuark prefix_name;
455 int ret;
456
457 if (!enum_name)
458 return -EPERM;
459
460 /* Only lookup in local scope */
461 if (lookup_enum_declaration_scope(enum_name, scope))
462 return -EEXIST;
463
464 g_hash_table_insert(scope->enum_declarations,
465 (gpointer) (unsigned long) enum_name,
466 enum_declaration);
467 declaration_ref(&enum_declaration->p);
468
469 /* Also add in typedef/typealias scopes */
470 prefix_name = prefix_quark("enum ", enum_name);
471 ret = register_declaration(prefix_name, &enum_declaration->p, scope);
472 assert(!ret);
473 return 0;
474 }
475
476 static struct definition_scope *
477 _new_definition_scope(struct definition_scope *parent_scope,
478 int scope_path_len)
479 {
480 struct definition_scope *scope = g_new(struct definition_scope, 1);
481
482 scope->definitions = g_hash_table_new(g_direct_hash,
483 g_direct_equal);
484 scope->parent_scope = parent_scope;
485 scope->scope_path = g_array_sized_new(FALSE, TRUE, sizeof(GQuark),
486 scope_path_len);
487 g_array_set_size(scope->scope_path, scope_path_len);
488 return scope;
489 }
490
491 GQuark new_definition_path(struct definition_scope *parent_scope,
492 GQuark field_name, const char *root_name)
493 {
494 GQuark path;
495 GString *str;
496 gchar *c_str;
497 int i;
498 int need_dot = 0;
499
500 str = g_string_new("");
501 if (root_name) {
502 g_string_append(str, root_name);
503 need_dot = 1;
504 } else if (parent_scope) {
505 for (i = 0; i < parent_scope->scope_path->len; i++) {
506 GQuark q = g_array_index(parent_scope->scope_path,
507 GQuark, i);
508 if (!q)
509 continue;
510 if (need_dot)
511 g_string_append(str, ".");
512 g_string_append(str, g_quark_to_string(q));
513 need_dot = 1;
514 }
515 }
516 if (field_name) {
517 if (need_dot)
518 g_string_append(str, ".");
519 g_string_append(str, g_quark_to_string(field_name));
520 }
521 c_str = g_string_free(str, FALSE);
522 if (c_str[0] == '\0')
523 return 0;
524 path = g_quark_from_string(c_str);
525 printf_debug("new definition path: %s\n", c_str);
526 g_free(c_str);
527 return path;
528 }
529
530 struct definition_scope *
531 new_definition_scope(struct definition_scope *parent_scope,
532 GQuark field_name, const char *root_name)
533 {
534 struct definition_scope *scope;
535
536 if (root_name) {
537 scope = _new_definition_scope(parent_scope, 0);
538 append_scope_path(root_name, scope->scope_path);
539 } else {
540 int scope_path_len = 1;
541
542 assert(parent_scope);
543 scope_path_len += parent_scope->scope_path->len;
544 scope = _new_definition_scope(parent_scope, scope_path_len);
545 memcpy(scope->scope_path->data, parent_scope->scope_path->data,
546 sizeof(GQuark) * (scope_path_len - 1));
547 g_array_index(scope->scope_path, GQuark, scope_path_len - 1) =
548 field_name;
549 }
550 if (babeltrace_debug) {
551 int i, need_dot = 0;
552
553 printf_debug("new definition scope: ");
554 for (i = 0; i < scope->scope_path->len; need_dot = 1, i++)
555 printf("%s%s", need_dot ? "." : "",
556 g_quark_to_string(g_array_index(scope->scope_path, GQuark, i)));
557 printf("\n");
558 }
559 return scope;
560 }
561
562 /*
563 * in: path (dot separated), out: q (GArray of GQuark)
564 */
565 void append_scope_path(const char *path, GArray *q)
566 {
567 const char *ptrbegin, *ptrend = path;
568 GQuark quark;
569
570 for (;;) {
571 char *str;
572 size_t len;
573
574 ptrbegin = ptrend;
575 ptrend = strchr(ptrbegin, '.');
576 if (!ptrend)
577 break;
578 len = ptrend - ptrbegin;
579 /* Don't accept two consecutive dots */
580 assert(len != 0);
581 str = g_new(char, len + 1); /* include \0 */
582 memcpy(str, ptrbegin, len);
583 str[len] = '\0';
584 quark = g_quark_from_string(str);
585 g_array_append_val(q, quark);
586 g_free(str);
587 ptrend++; /* skip current dot */
588 }
589 /* last. Check for trailing dot (and discard). */
590 if (ptrbegin[0] != '\0') {
591 quark = g_quark_from_string(ptrbegin);
592 g_array_append_val(q, quark);
593 }
594 }
595
596 void free_definition_scope(struct definition_scope *scope)
597 {
598 g_array_free(scope->scope_path, TRUE);
599 g_hash_table_destroy(scope->definitions);
600 g_free(scope);
601 }
602
603 struct definition *lookup_definition(struct definition *definition,
604 const char *field_name)
605 {
606 struct definition_scope *scope = get_definition_scope(definition);
607
608 if (!scope)
609 return NULL;
610
611 return lookup_field_definition_scope(g_quark_from_string(field_name),
612 scope);
613 }
614
615 struct definition_integer *lookup_integer(struct definition *definition,
616 const char *field_name,
617 int signedness)
618 {
619 struct definition *lookup;
620 struct definition_integer *lookup_integer;
621
622 lookup = lookup_definition(definition, field_name);
623 if (!lookup)
624 return NULL;
625 if (lookup->declaration->id != CTF_TYPE_INTEGER)
626 return NULL;
627 lookup_integer = container_of(lookup, struct definition_integer, p);
628 if (lookup_integer->declaration->signedness != signedness)
629 return NULL;
630 return lookup_integer;
631 }
632
633 struct definition_enum *lookup_enum(struct definition *definition,
634 const char *field_name,
635 int signedness)
636 {
637 struct definition *lookup;
638 struct definition_enum *lookup_enum;
639
640 lookup = lookup_definition(definition, field_name);
641 if (!lookup)
642 return NULL;
643 if (lookup->declaration->id != CTF_TYPE_ENUM)
644 return NULL;
645 lookup_enum = container_of(lookup, struct definition_enum, p);
646 if (lookup_enum->integer->declaration->signedness != signedness)
647 return NULL;
648 return lookup_enum;
649 }
650
651 struct definition *lookup_variant(struct definition *definition,
652 const char *field_name)
653 {
654 struct definition *lookup;
655 struct definition_variant *lookup_variant;
656
657 lookup = lookup_definition(definition, field_name);
658 if (!lookup)
659 return NULL;
660 if (lookup->declaration->id != CTF_TYPE_VARIANT)
661 return NULL;
662 lookup_variant = container_of(lookup, struct definition_variant, p);
663 lookup = variant_get_current_field(lookup_variant);
664 assert(lookup);
665 return lookup;
666 }
This page took 0.04173 seconds and 4 git commands to generate.