Use inheritance for trace descriptor and positions
[babeltrace.git] / include / babeltrace / ctf / types.h
1 #ifndef _BABELTRACE_CTF_TYPES_H
2 #define _BABELTRACE_CTF_TYPES_H
3
4 /*
5 * Common Trace Format
6 *
7 * Type header
8 *
9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
22 #include <babeltrace/types.h>
23 #include <sys/mman.h>
24 #include <errno.h>
25 #include <stdint.h>
26 #include <unistd.h>
27 #include <glib.h>
28
29 struct packet_index {
30 off_t offset; /* offset of the packet in the file, in bytes */
31 size_t packet_size; /* packet size, in bits */
32 size_t content_size; /* content size, in bits */
33 };
34
35 /*
36 * Always update ctf_stream_pos with ctf_move_pos and ctf_init_pos.
37 */
38 struct ctf_stream_pos {
39 struct stream_pos parent;
40 int fd; /* backing file fd. -1 if unset. */
41 GArray *packet_index; /* contains struct packet_index */
42 int prot; /* mmap protection */
43 int flags; /* mmap flags */
44
45 /* Current position */
46 off_t mmap_offset; /* mmap offset in the file, in bytes */
47 size_t packet_size; /* current packet size, in bits */
48 size_t content_size; /* current content size, in bits */
49 uint32_t *content_size_loc; /* pointer to current content size */
50 char *base; /* mmap base address */
51 size_t offset; /* offset from base, in bits */
52 size_t cur_index; /* current index in packet index */
53
54 int dummy; /* dummy position, for length calculation */
55 };
56
57 static inline
58 struct ctf_stream_pos *ctf_pos(struct stream_pos *pos)
59 {
60 return container_of(pos, struct ctf_stream_pos, parent);
61 }
62
63 /*
64 * IMPORTANT: All lengths (len) and offsets (start, end) are expressed in bits,
65 * *not* in bytes.
66 *
67 * All write primitives, as well as read for dynamically sized entities, can
68 * receive a NULL ptr/dest parameter. In this case, no write is performed, but
69 * the size is returned.
70 */
71
72 uint64_t ctf_uint_read(struct stream_pos *pos,
73 const struct declaration_integer *integer_declaration);
74 int64_t ctf_int_read(struct stream_pos *pos,
75 const struct declaration_integer *integer_declaration);
76 void ctf_uint_write(struct stream_pos *pos,
77 const struct declaration_integer *integer_declaration,
78 uint64_t v);
79 void ctf_int_write(struct stream_pos *pos,
80 const struct declaration_integer *integer_declaration,
81 int64_t v);
82
83 double ctf_double_read(struct stream_pos *pos,
84 const struct declaration_float *src);
85 void ctf_double_write(struct stream_pos *pos,
86 const struct declaration_float *dest,
87 double v);
88 long double ctf_ldouble_read(struct stream_pos *pos,
89 const struct declaration_float *src);
90 void ctf_ldouble_write(struct stream_pos *pos,
91 const struct declaration_float *dest,
92 long double v);
93 void ctf_float_copy(struct stream_pos *destp,
94 struct stream_pos *srcp,
95 const struct declaration_float *float_declaration);
96
97 void ctf_string_copy(struct stream_pos *dest, struct stream_pos *src,
98 const struct declaration_string *string_declaration);
99 void ctf_string_read(char **dest, struct stream_pos *src,
100 const struct declaration_string *string_declaration);
101 void ctf_string_write(struct stream_pos *dest, const char *src,
102 const struct declaration_string *string_declaration);
103 void ctf_string_free_temp(char *string);
104
105 GArray *ctf_enum_read(struct stream_pos *pos,
106 const struct declaration_enum *src);
107 void ctf_enum_write(struct stream_pos *pos,
108 const struct declaration_enum *dest,
109 GQuark q);
110 void ctf_struct_begin(struct stream_pos *pos,
111 const struct declaration_struct *struct_declaration);
112 void ctf_struct_end(struct stream_pos *pos,
113 const struct declaration_struct *struct_declaration);
114 void ctf_variant_begin(struct stream_pos *pos,
115 const struct declaration_variant *variant_declaration);
116 void ctf_variant_end(struct stream_pos *pos,
117 const struct declaration_variant *variant_declaration);
118 void ctf_array_begin(struct stream_pos *pos,
119 const struct declaration_array *array_declaration);
120 void ctf_array_end(struct stream_pos *pos,
121 const struct declaration_array *array_declaration);
122 void ctf_sequence_begin(struct stream_pos *pos,
123 const struct declaration_sequence *sequence_declaration);
124 void ctf_sequence_end(struct stream_pos *pos,
125 const struct declaration_sequence *sequence_declaration);
126
127 void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset);
128
129 void ctf_init_pos(struct ctf_stream_pos *pos, int fd);
130 void ctf_fini_pos(struct ctf_stream_pos *pos);
131
132 /*
133 * move_pos - move position of a relative bit offset
134 *
135 * TODO: allow larger files by updating base too.
136 */
137 static inline
138 void ctf_move_pos(struct ctf_stream_pos *pos, size_t bit_offset)
139 {
140 if (pos->fd >= 0) {
141 if (((pos->prot == PROT_READ)
142 && (pos->offset + bit_offset >= pos->content_size))
143 || ((pos->prot == PROT_WRITE)
144 && (pos->offset + bit_offset >= pos->packet_size))) {
145 ctf_move_pos_slow(pos, bit_offset);
146 return;
147 }
148 }
149 pos->offset += bit_offset;
150 }
151
152 /*
153 * align_pos - align position on a bit offset (> 0)
154 *
155 * TODO: allow larger files by updating base too.
156 */
157 static inline
158 void ctf_align_pos(struct ctf_stream_pos *pos, size_t bit_offset)
159 {
160 ctf_move_pos(pos, offset_align(pos->offset, bit_offset));
161 }
162
163 static inline
164 char *ctf_get_pos_addr(struct ctf_stream_pos *pos)
165 {
166 /* Only makes sense to get the address after aligning on CHAR_BIT */
167 assert(!(pos->offset % CHAR_BIT));
168 return pos->base + (pos->offset / CHAR_BIT);
169 }
170
171 static inline
172 void ctf_dummy_pos(struct ctf_stream_pos *pos, struct ctf_stream_pos *dummy)
173 {
174 memcpy(dummy, pos, sizeof(struct ctf_stream_pos));
175 dummy->dummy = 1;
176 dummy->fd = -1;
177 }
178
179 /*
180 * Check if current packet can hold data.
181 * Returns 0 for success, negative error otherwise.
182 */
183 static inline
184 int ctf_pos_packet(struct ctf_stream_pos *dummy)
185 {
186 if (dummy->offset > dummy->packet_size)
187 return -ENOSPC;
188 return 0;
189 }
190
191 static inline
192 void ctf_pos_pad_packet(struct ctf_stream_pos *pos)
193 {
194 ctf_move_pos(pos, pos->packet_size - pos->offset);
195 }
196
197 #endif /* _BABELTRACE_CTF_TYPES_H */
This page took 0.034011 seconds and 5 git commands to generate.