Replace all assert(false) and assert(0) with abort()
[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 <sys/mman.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <babeltrace/ctf-ir/field-types.h>
37 #include <babeltrace/ctf-ir/fields.h>
38 #include <babeltrace/ctf-ir/fields-internal.h>
39 #include <babeltrace/align-internal.h>
40 #include <babeltrace/mmap-align-internal.h>
41 #include <babeltrace/types.h>
42
43 struct bt_ctf_stream_pos {
44 int fd;
45 int prot; /* mmap protection */
46 int flags; /* mmap flags */
47
48 /* Current position */
49 off_t mmap_offset; /* mmap offset in the file, in bytes */
50 off_t mmap_base_offset; /* offset of start of packet in mmap, in bytes */
51 uint64_t packet_size; /* current packet size, in bits */
52 int64_t offset; /* offset from base, in bits. EOF for end of file. */
53 struct mmap_align *base_mma;/* mmap base address */
54 };
55
56 BT_HIDDEN
57 int bt_ctf_field_integer_write(struct bt_ctf_field_integer *field,
58 struct bt_ctf_stream_pos *pos,
59 enum bt_ctf_byte_order native_byte_order);
60
61 BT_HIDDEN
62 int bt_ctf_field_floating_point_write(struct bt_ctf_field_floating_point *field,
63 struct bt_ctf_stream_pos *pos,
64 enum bt_ctf_byte_order native_byte_order);
65
66 static inline
67 int bt_ctf_stream_pos_access_ok(struct bt_ctf_stream_pos *pos, uint64_t bit_len)
68 {
69 uint64_t max_len;
70
71 if (unlikely(pos->offset == EOF))
72 return 0;
73
74 if (pos->prot == PROT_READ) {
75 /*
76 * Reads may only reach up to the "content_size",
77 * regardless of the packet_size.
78 */
79 max_len = pos->offset;
80 } else {
81 /* Writes may take place up to the end of the packet. */
82 max_len = pos->packet_size;
83 }
84 if (unlikely(pos->offset + bit_len > max_len))
85 return 0;
86 return 1;
87 }
88
89 static inline
90 int bt_ctf_stream_pos_move(struct bt_ctf_stream_pos *pos, uint64_t bit_offset)
91 {
92 int ret = 0;
93
94 ret = bt_ctf_stream_pos_access_ok(pos, bit_offset);
95 if (!ret) {
96 goto end;
97 }
98 pos->offset += bit_offset;
99 end:
100 return ret;
101 }
102
103 static inline
104 int bt_ctf_stream_pos_align(struct bt_ctf_stream_pos *pos, uint64_t bit_offset)
105 {
106 return bt_ctf_stream_pos_move(pos,
107 offset_align(pos->offset, bit_offset));
108 }
109
110 static inline
111 char *bt_ctf_stream_pos_get_addr(struct bt_ctf_stream_pos *pos)
112 {
113 /* Only makes sense to get the address after aligning on CHAR_BIT */
114 assert(!(pos->offset % CHAR_BIT));
115 return ((char *) mmap_align_addr(pos->base_mma)) +
116 pos->mmap_base_offset + (pos->offset / CHAR_BIT);
117 }
118
119 static inline
120 int bt_ctf_stream_pos_init(struct bt_ctf_stream_pos *pos,
121 int fd, int open_flags)
122 {
123 pos->fd = fd;
124
125 switch (open_flags & O_ACCMODE) {
126 case O_RDONLY:
127 pos->prot = PROT_READ;
128 pos->flags = MAP_PRIVATE;
129 break;
130 case O_RDWR:
131 pos->prot = PROT_READ | PROT_WRITE;
132 pos->flags = MAP_SHARED;
133 break;
134 default:
135 abort();
136 }
137
138 return 0;
139 }
140
141 static inline
142 int bt_ctf_stream_pos_fini(struct bt_ctf_stream_pos *pos)
143 {
144 if (pos->base_mma) {
145 int ret;
146
147 /* unmap old base */
148 ret = munmap_align(pos->base_mma);
149 if (ret) {
150 return -1;
151 }
152 }
153
154 return 0;
155 }
156
157 BT_HIDDEN
158 void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos *pos, size_t index,
159 int whence);
160
161 #endif /* BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H */
This page took 0.032791 seconds and 4 git commands to generate.