Remove namespace conflicts from list.h
[babeltrace.git] / include / babeltrace / types.h
CommitLineData
d79865b9
MD
1#ifndef _BABELTRACE_TYPES_H
2#define _BABELTRACE_TYPES_H
3
4/*
5 * BabelTrace
6 *
fc93b2bd 7 * Type Header
d79865b9 8 *
ccd7e1c8 9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d79865b9 10 *
ccd7e1c8
MD
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
d79865b9 17 *
ccd7e1c8
MD
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
d79865b9
MD
20 */
21
bed864a7 22#include <babeltrace/align.h>
4c8bfb7e
MD
23#include <stdbool.h>
24#include <stdint.h>
25#include <limits.h>
bed864a7 26#include <string.h>
4c8bfb7e 27#include <glib.h>
8eab883c 28#include <assert.h>
bed864a7 29
11796b96
MD
30/* Preallocate this many fields for structures */
31#define DEFAULT_NR_STRUCT_FIELDS 8
32
bed864a7
MD
33/*
34 * Always update stream_pos with move_pos and init_pos.
35 */
36struct stream_pos {
37 unsigned char *base; /* Base address */
38 size_t offset; /* Offset from base, in bits */
39 int dummy; /* Dummy position, for length calculation */
40};
41
42static inline
43void init_pos(struct stream_pos *pos, unsigned char *base)
44{
45 pos->base = base; /* initial base, page-aligned */
46 pos->offset = 0;
47 pos->dummy = false;
48}
49
50/*
51 * move_pos - move position of a relative bit offset
52 *
53 * TODO: allow larger files by updating base too.
54 */
55static inline
56void move_pos(struct stream_pos *pos, size_t offset)
57{
58 pos->offset = pos->offset + offset;
59}
60
61/*
62 * align_pos - align position on a bit offset (> 0)
63 *
64 * TODO: allow larger files by updating base too.
65 */
66static inline
67void align_pos(struct stream_pos *pos, size_t offset)
68{
69 pos->offset += offset_align(pos->offset, offset);
70}
71
72static inline
73void copy_pos(struct stream_pos *dest, struct stream_pos *src)
74{
75 memcpy(dest, src, sizeof(struct stream_pos));
76}
77
78static inline
79unsigned char *get_pos_addr(struct stream_pos *pos)
80{
81 /* Only makes sense to get the address after aligning on CHAR_BIT */
4c8bfb7e 82 assert(!(pos->offset % CHAR_BIT));
bed864a7
MD
83 return pos->base + (pos->offset / CHAR_BIT);
84}
fc93b2bd 85
4c8bfb7e
MD
86struct format;
87
fc93b2bd
MD
88struct type_class {
89 GQuark name; /* type name */
90 size_t alignment; /* type alignment, in bits */
4c8bfb7e 91 int ref; /* number of references to the type */
fc93b2bd
MD
92 /*
93 * Type copy function. Knows how to find the child type_class from the
94 * parent type_class.
95 */
4c8bfb7e
MD
96 void (*copy)(struct stream_pos *dest, const struct format *fdest,
97 struct stream_pos *src, const struct format *fsrc,
98 const struct type_class *type_class);
90b676d7 99 void (*free)(struct type_class *type_class);
fc93b2bd
MD
100};
101
bed864a7
MD
102/*
103 * Because we address in bits, bitfields end up being exactly the same as
104 * integers, except that their read/write functions must be able to deal with
105 * read/write non aligned on CHAR_BIT.
106 */
7fe00194
MD
107struct type_class_integer {
108 struct type_class p;
109 size_t len; /* length, in bits. */
110 int byte_order; /* byte order */
111 int signedness;
fc93b2bd
MD
112};
113
114struct type_class_float {
115 struct type_class p;
4c8bfb7e
MD
116 struct type_class_integer *sign;
117 struct type_class_integer *mantissa;
118 struct type_class_integer *exp;
fc93b2bd 119 int byte_order;
0a46062b 120 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
121};
122
448d3cc7
MD
123struct enum_table {
124 GHashTable *value_to_quark; /* Tuples (value, GQuark) */
125 GHashTable *quark_to_value; /* Tuples (GQuark, value) */
126};
127
fc93b2bd 128struct type_class_enum {
4c8bfb7e 129 struct type_class_integer p; /* inherit from integer */
448d3cc7 130 struct enum_table table;
fc93b2bd
MD
131};
132
bed864a7
MD
133struct type_class_string {
134 struct type_class p;
135};
136
11796b96
MD
137struct field {
138 GQuark name;
139 struct type_class *type_class;
140};
141
fc93b2bd
MD
142struct type_class_struct {
143 struct type_class p;
11796b96
MD
144 GHashTable *fields_by_name; /* Tuples (field name, field index) */
145 GArray *fields; /* Array of fields */
146};
147
148struct type_class_array {
149 struct type_class p;
150 size_t len;
151 struct type_class *elem;
152};
153
154struct type_class_sequence {
155 struct type_class p;
2e7d72cf 156 struct type_class_integer *len_class;
11796b96 157 struct type_class *elem;
fc93b2bd
MD
158};
159
4c8bfb7e
MD
160struct type_class *lookup_type(GQuark qname);
161int register_type(struct type_class *type_class);
162
163void type_ref(struct type_class *type_class);
164void type_unref(struct type_class *type_class);
698f0fe4 165
0a46062b
MD
166/* Nameless types can be created by passing a NULL name */
167
168struct type_class_integer *integer_type_new(const char *name,
0a46062b 169 size_t len, int byte_order,
11d43b90
MD
170 int signedness,
171 size_t alignment);
0a46062b
MD
172void integer_type_free(struct type_class_integer *int_class);
173
11d43b90
MD
174/*
175 * mantissa_len is the length of the number of bytes represented by the mantissa
176 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
177 */
0a46062b
MD
178struct type_class_float *float_type_new(const char *name,
179 size_t mantissa_len,
180 size_t exp_len, int byte_order,
181 size_t alignment);
182void float_type_free(struct type_class_float *float_class);
183
448d3cc7
MD
184/*
185 * A GQuark can be translated to/from strings with g_quark_from_string() and
186 * g_quark_to_string().
187 */
188GQuark enum_uint_to_quark(const struct type_class_enum *enum_class, uint64_t v);
189GQuark enum_int_to_quark(const struct type_class_enum *enum_class, uint64_t v);
190uint64_t enum_quark_to_uint(const struct type_class_enum *enum_class,
4c8bfb7e 191 GQuark q);
448d3cc7 192int64_t enum_quark_to_int(const struct type_class_enum *enum_class,
4c8bfb7e 193 GQuark q);
448d3cc7
MD
194void enum_signed_insert(struct type_class_enum *enum_class,
195 int64_t v, GQuark q);
196void enum_unsigned_insert(struct type_class_enum *enum_class,
197 uint64_t v, GQuark q);
198
199struct type_class_enum *enum_type_new(const char *name,
448d3cc7
MD
200 size_t len, int byte_order,
201 int signedness,
202 size_t alignment);
203void enum_type_free(struct type_class_enum *enum_class);
204
11796b96
MD
205struct type_class_struct *struct_type_new(const char *name);
206void struct_type_free(struct type_class_struct *struct_class);
207void struct_type_add_field(struct type_class_struct *struct_class,
4c8bfb7e 208 const char *field_name,
11796b96
MD
209 struct type_class *type_class);
210/*
211 * Returns the index of a field within a structure.
212 */
213unsigned long
214struct_type_lookup_field_index(struct type_class_struct *struct_class,
215 GQuark field_name);
216/*
217 * field returned only valid as long as the field structure is not appended to.
218 */
219struct field *
220struct_type_get_field_from_index(struct type_class_struct *struct_class,
221 unsigned long index);
222
d06d03db
MD
223/*
224 * elem_class passed as parameter now belongs to the array. No need to free it
225 * explicitely.
226 */
11796b96
MD
227struct type_class_array *array_type_new(const char *name,
228 size_t len,
229 struct type_class *elem_class);
230void array_type_free(struct type_class_array *array_class);
231
d06d03db
MD
232/*
233 * int_class and elem_class passed as parameter now belongs to the sequence. No
234 * need to free them explicitely.
235 */
11796b96 236struct type_class_sequence *sequence_type_new(const char *name,
2e7d72cf 237 struct type_class_integer *len_class,
11796b96 238 struct type_class *elem_class);
d06d03db 239void sequence_type_free(struct type_class_sequence *sequence_class);
11796b96 240
d79865b9 241#endif /* _BABELTRACE_TYPES_H */
This page took 0.032549 seconds and 4 git commands to generate.