Commit | Line | Data |
---|---|---|
d79865b9 MD |
1 | #ifndef _BABELTRACE_CTF_TYPES_H |
2 | #define _BABELTRACE_CTF_TYPES_H | |
6dc2ca62 MD |
3 | |
4 | /* | |
5 | * Common Trace Format | |
6 | * | |
7 | * Type header | |
8 | * | |
ccd7e1c8 | 9 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
6dc2ca62 | 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: | |
de0ba614 | 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. | |
6dc2ca62 MD |
20 | */ |
21 | ||
4c8bfb7e | 22 | #include <babeltrace/types.h> |
6dc2ca62 | 23 | #include <stdint.h> |
0f980a35 | 24 | #include <unistd.h> |
6dc2ca62 MD |
25 | #include <glib.h> |
26 | ||
0f980a35 MD |
27 | struct packet_index { |
28 | off_t offset; /* offset of the packet in the file, in bytes */ | |
29 | size_t packet_size; /* packet size, in bits */ | |
30 | size_t content_size; /* content size, in bits */ | |
dd2544fd MD |
31 | }; |
32 | ||
dd2544fd | 33 | /* |
0f980a35 | 34 | * Always update stream_pos with move_pos and init_pos. |
dd2544fd | 35 | */ |
0f980a35 MD |
36 | struct stream_pos { |
37 | int fd; /* backing file fd. -1 if unset. */ | |
38 | GArray *packet_index; /* contains struct packet_index */ | |
dd2544fd | 39 | |
0f980a35 MD |
40 | /* Current position */ |
41 | off_t mmap_offset; /* mmap offset in the file, in bytes */ | |
42 | size_t packet_size; /* current packet size, in bits */ | |
43 | size_t content_size; /* current content size, in bits */ | |
44 | char *base; /* mmap base address */ | |
45 | size_t offset; /* offset from base, in bits */ | |
dd2544fd | 46 | |
0f980a35 MD |
47 | int dummy; /* dummy position, for length calculation */ |
48 | }; | |
dd2544fd | 49 | |
6dc2ca62 | 50 | /* |
de0ba614 MD |
51 | * IMPORTANT: All lengths (len) and offsets (start, end) are expressed in bits, |
52 | * *not* in bytes. | |
53 | * | |
54 | * All write primitives, as well as read for dynamically sized entities, can | |
6dc2ca62 MD |
55 | * receive a NULL ptr/dest parameter. In this case, no write is performed, but |
56 | * the size is returned. | |
57 | */ | |
58 | ||
bed864a7 | 59 | uint64_t ctf_uint_read(struct stream_pos *pos, |
f6625916 | 60 | const struct declaration_integer *integer_declaration); |
bed864a7 | 61 | int64_t ctf_int_read(struct stream_pos *pos, |
f6625916 | 62 | const struct declaration_integer *integer_declaration); |
bed864a7 | 63 | void ctf_uint_write(struct stream_pos *pos, |
f6625916 | 64 | const struct declaration_integer *integer_declaration, |
bed864a7 MD |
65 | uint64_t v); |
66 | void ctf_int_write(struct stream_pos *pos, | |
f6625916 | 67 | const struct declaration_integer *integer_declaration, |
bed864a7 | 68 | int64_t v); |
6dc2ca62 | 69 | |
bed864a7 | 70 | double ctf_double_read(struct stream_pos *pos, |
f6625916 | 71 | const struct declaration_float *src); |
bed864a7 | 72 | void ctf_double_write(struct stream_pos *pos, |
f6625916 | 73 | const struct declaration_float *dest, |
bed864a7 MD |
74 | double v); |
75 | long double ctf_ldouble_read(struct stream_pos *pos, | |
f6625916 | 76 | const struct declaration_float *src); |
bed864a7 | 77 | void ctf_ldouble_write(struct stream_pos *pos, |
f6625916 | 78 | const struct declaration_float *dest, |
bed864a7 | 79 | long double v); |
4c8bfb7e MD |
80 | void ctf_float_copy(struct stream_pos *destp, |
81 | struct stream_pos *srcp, | |
f6625916 | 82 | const struct declaration_float *float_declaration); |
6dc2ca62 | 83 | |
bed864a7 | 84 | void ctf_string_copy(struct stream_pos *dest, struct stream_pos *src, |
f6625916 | 85 | const struct declaration_string *string_declaration); |
47e0f2e2 | 86 | void ctf_string_read(char **dest, struct stream_pos *src, |
f6625916 | 87 | const struct declaration_string *string_declaration); |
47e0f2e2 | 88 | void ctf_string_write(struct stream_pos *dest, const char *src, |
f6625916 | 89 | const struct declaration_string *string_declaration); |
47e0f2e2 | 90 | void ctf_string_free_temp(char *string); |
6dc2ca62 | 91 | |
47e0f2e2 | 92 | GArray *ctf_enum_read(struct stream_pos *pos, |
f6625916 | 93 | const struct declaration_enum *src); |
bed864a7 | 94 | void ctf_enum_write(struct stream_pos *pos, |
f6625916 | 95 | const struct declaration_enum *dest, |
bed864a7 | 96 | GQuark q); |
11796b96 | 97 | void ctf_struct_begin(struct stream_pos *pos, |
f6625916 | 98 | const struct declaration_struct *struct_declaration); |
11796b96 | 99 | void ctf_struct_end(struct stream_pos *pos, |
f6625916 | 100 | const struct declaration_struct *struct_declaration); |
6b71274a | 101 | void ctf_variant_begin(struct stream_pos *pos, |
f6625916 | 102 | const struct declaration_variant *variant_declaration); |
6b71274a | 103 | void ctf_variant_end(struct stream_pos *pos, |
f6625916 | 104 | const struct declaration_variant *variant_declaration); |
d06d03db | 105 | void ctf_array_begin(struct stream_pos *pos, |
f6625916 | 106 | const struct declaration_array *array_declaration); |
d06d03db | 107 | void ctf_array_end(struct stream_pos *pos, |
f6625916 | 108 | const struct declaration_array *array_declaration); |
d06d03db | 109 | void ctf_sequence_begin(struct stream_pos *pos, |
f6625916 | 110 | const struct declaration_sequence *sequence_declaration); |
d06d03db | 111 | void ctf_sequence_end(struct stream_pos *pos, |
f6625916 | 112 | const struct declaration_sequence *sequence_declaration); |
6dc2ca62 | 113 | |
0f980a35 MD |
114 | void move_pos_slow(struct stream_pos *pos, size_t offset); |
115 | ||
116 | static inline | |
117 | void init_pos(struct stream_pos *pos, int fd) | |
118 | { | |
119 | pos->fd = fd; | |
120 | pos->mmap_offset = 0; | |
121 | pos->packet_size = 0; | |
122 | pos->content_size = 0; | |
123 | pos->base = NULL; | |
124 | pos->offset = 0; | |
125 | pos->dummy = false; | |
126 | } | |
127 | ||
128 | /* | |
129 | * move_pos - move position of a relative bit offset | |
130 | * | |
131 | * TODO: allow larger files by updating base too. | |
132 | */ | |
133 | static inline | |
134 | void move_pos(struct stream_pos *pos, size_t offset) | |
135 | { | |
136 | if (pos->fd >= 0 && pos->offset + offset >= pos->content_size) | |
137 | move_pos_slow(pos, offset); | |
138 | else | |
139 | pos->offset += offset; | |
140 | } | |
141 | ||
142 | /* | |
143 | * align_pos - align position on a bit offset (> 0) | |
144 | * | |
145 | * TODO: allow larger files by updating base too. | |
146 | */ | |
147 | static inline | |
148 | void align_pos(struct stream_pos *pos, size_t offset) | |
149 | { | |
150 | pos->offset += offset_align(pos->offset, offset); | |
151 | } | |
152 | ||
153 | static inline | |
154 | void copy_pos(struct stream_pos *dest, struct stream_pos *src) | |
155 | { | |
156 | memcpy(dest, src, sizeof(struct stream_pos)); | |
157 | } | |
158 | ||
159 | static inline | |
160 | char *get_pos_addr(struct stream_pos *pos) | |
161 | { | |
162 | /* Only makes sense to get the address after aligning on CHAR_BIT */ | |
163 | assert(!(pos->offset % CHAR_BIT)); | |
164 | return pos->base + (pos->offset / CHAR_BIT); | |
165 | } | |
166 | ||
d79865b9 | 167 | #endif /* _BABELTRACE_CTF_TYPES_H */ |