Make API CTF-agnostic
[babeltrace.git] / include / babeltrace / ctf-writer / serialize-internal.h
1 #ifndef BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H
3
4 /*
5 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * The Common Trace Format (CTF) Specification is available at
26 * http://www.efficios.com/ctf
27 */
28
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <limits.h>
32 #include <babeltrace/compat/mman-internal.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <babeltrace/align-internal.h>
37 #include <babeltrace/endian-internal.h>
38 #include <babeltrace/common-internal.h>
39 #include <babeltrace/mmap-align-internal.h>
40 #include <babeltrace/types.h>
41 #include <babeltrace/ctf-writer/field-types.h>
42 #include <babeltrace/ctf-writer/fields-internal.h>
43 #include <babeltrace/ctf-writer/fields.h>
44 #include <babeltrace/assert-internal.h>
45
46 #define PACKET_LEN_INCREMENT (bt_common_get_page_size() * 8 * CHAR_BIT)
47
48 #if (BYTE_ORDER == BIG_ENDIAN)
49 # define BT_CTF_MY_BYTE_ORDER BT_CTF_BYTE_ORDER_BIG_ENDIAN
50 #else
51 # define BT_CTF_MY_BYTE_ORDER BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
52 #endif
53
54 struct bt_ctf_stream_pos {
55 int fd;
56 int prot; /* mmap protection */
57 int flags; /* mmap flags */
58
59 /* Current position */
60 off_t mmap_offset; /* mmap offset in the file, in bytes */
61 off_t mmap_base_offset; /* offset of start of packet in mmap, in bytes */
62 uint64_t packet_size; /* current packet size, in bits */
63 int64_t offset; /* offset from base, in bits. EOF for end of file. */
64 struct mmap_align *base_mma;/* mmap base address */
65 };
66
67 BT_HIDDEN
68 int bt_ctf_field_integer_write(struct bt_ctf_field_common *field,
69 struct bt_ctf_stream_pos *pos,
70 enum bt_ctf_byte_order native_byte_order);
71
72 BT_HIDDEN
73 int bt_ctf_field_floating_point_write(struct bt_ctf_field_common *field,
74 struct bt_ctf_stream_pos *pos,
75 enum bt_ctf_byte_order native_byte_order);
76
77 static inline
78 int bt_ctf_stream_pos_access_ok(struct bt_ctf_stream_pos *pos, uint64_t bit_len)
79 {
80 uint64_t max_len;
81
82 if (unlikely(pos->offset == EOF))
83 return 0;
84
85 if (pos->prot == PROT_READ) {
86 /*
87 * Reads may only reach up to the "content_size",
88 * regardless of the packet_size.
89 */
90 max_len = pos->offset;
91 } else {
92 /* Writes may take place up to the end of the packet. */
93 max_len = pos->packet_size;
94 }
95 if (unlikely(pos->offset < 0 || bit_len > INT64_MAX - pos->offset)) {
96 return 0;
97 }
98 if (unlikely(pos->offset + bit_len > max_len))
99 return 0;
100 return 1;
101 }
102
103 static inline
104 int bt_ctf_stream_pos_move(struct bt_ctf_stream_pos *pos, uint64_t bit_offset)
105 {
106 int ret = 0;
107
108 ret = bt_ctf_stream_pos_access_ok(pos, bit_offset);
109 if (!ret) {
110 goto end;
111 }
112 pos->offset += bit_offset;
113 end:
114 return ret;
115 }
116
117 static inline
118 int bt_ctf_stream_pos_align(struct bt_ctf_stream_pos *pos, uint64_t bit_offset)
119 {
120 return bt_ctf_stream_pos_move(pos,
121 offset_align(pos->offset, bit_offset));
122 }
123
124 static inline
125 char *bt_ctf_stream_pos_get_addr(struct bt_ctf_stream_pos *pos)
126 {
127 /* Only makes sense to get the address after aligning on CHAR_BIT */
128 BT_ASSERT(!(pos->offset % CHAR_BIT));
129 return ((char *) mmap_align_addr(pos->base_mma)) +
130 pos->mmap_base_offset + (pos->offset / CHAR_BIT);
131 }
132
133 static inline
134 int bt_ctf_stream_pos_init(struct bt_ctf_stream_pos *pos,
135 int fd, int open_flags)
136 {
137 pos->fd = fd;
138
139 switch (open_flags & O_ACCMODE) {
140 case O_RDONLY:
141 pos->prot = PROT_READ;
142 pos->flags = MAP_PRIVATE;
143 break;
144 case O_RDWR:
145 pos->prot = PROT_READ | PROT_WRITE;
146 pos->flags = MAP_SHARED;
147 break;
148 default:
149 abort();
150 }
151
152 return 0;
153 }
154
155 static inline
156 int bt_ctf_stream_pos_fini(struct bt_ctf_stream_pos *pos)
157 {
158 if (pos->base_mma) {
159 int ret;
160
161 /* unmap old base */
162 ret = munmap_align(pos->base_mma);
163 if (ret) {
164 return -1;
165 }
166 }
167
168 return 0;
169 }
170
171 BT_HIDDEN
172 void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos *pos, size_t index,
173 int whence);
174
175 #endif /* BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H */
This page took 0.035593 seconds and 4 git commands to generate.