Commit | Line | Data |
---|---|---|
dc3fffef PP |
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 | ||
0fbb9a9f | 29 | #include <stdlib.h> |
dc3fffef PP |
30 | #include <stdint.h> |
31 | #include <limits.h> | |
8f76831a | 32 | #include <babeltrace/compat/mman-internal.h> |
dc3fffef PP |
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> | |
3d9990ac | 39 | #include <babeltrace/align-internal.h> |
108e5a1e | 40 | #include <babeltrace/common-internal.h> |
3d9990ac | 41 | #include <babeltrace/mmap-align-internal.h> |
c55a9f58 | 42 | #include <babeltrace/types.h> |
f6ccaed9 | 43 | #include <babeltrace/assert-internal.h> |
dc3fffef | 44 | |
108e5a1e MJ |
45 | #define PACKET_LEN_INCREMENT (bt_common_get_page_size() * 8 * CHAR_BIT) |
46 | ||
50842bdc | 47 | struct bt_stream_pos { |
dc3fffef PP |
48 | int fd; |
49 | int prot; /* mmap protection */ | |
50 | int flags; /* mmap flags */ | |
51 | ||
52 | /* Current position */ | |
53 | off_t mmap_offset; /* mmap offset in the file, in bytes */ | |
54 | off_t mmap_base_offset; /* offset of start of packet in mmap, in bytes */ | |
55 | uint64_t packet_size; /* current packet size, in bits */ | |
dc3fffef PP |
56 | int64_t offset; /* offset from base, in bits. EOF for end of file. */ |
57 | struct mmap_align *base_mma;/* mmap base address */ | |
58 | }; | |
59 | ||
60 | BT_HIDDEN | |
50842bdc PP |
61 | int bt_field_integer_write(struct bt_field_integer *field, |
62 | struct bt_stream_pos *pos, | |
63 | enum bt_byte_order native_byte_order); | |
dc3fffef PP |
64 | |
65 | BT_HIDDEN | |
50842bdc PP |
66 | int bt_field_floating_point_write(struct bt_field_floating_point *field, |
67 | struct bt_stream_pos *pos, | |
68 | enum bt_byte_order native_byte_order); | |
dc3fffef PP |
69 | |
70 | static inline | |
50842bdc | 71 | int bt_stream_pos_access_ok(struct bt_stream_pos *pos, uint64_t bit_len) |
dc3fffef PP |
72 | { |
73 | uint64_t max_len; | |
74 | ||
75 | if (unlikely(pos->offset == EOF)) | |
76 | return 0; | |
b3376dd9 | 77 | |
dc3fffef PP |
78 | if (pos->prot == PROT_READ) { |
79 | /* | |
80 | * Reads may only reach up to the "content_size", | |
81 | * regardless of the packet_size. | |
82 | */ | |
b3376dd9 | 83 | max_len = pos->offset; |
dc3fffef PP |
84 | } else { |
85 | /* Writes may take place up to the end of the packet. */ | |
86 | max_len = pos->packet_size; | |
87 | } | |
6c711b23 MD |
88 | if (unlikely(pos->offset < 0 || bit_len > INT64_MAX - pos->offset)) { |
89 | return 0; | |
90 | } | |
dc3fffef PP |
91 | if (unlikely(pos->offset + bit_len > max_len)) |
92 | return 0; | |
93 | return 1; | |
94 | } | |
95 | ||
96 | static inline | |
50842bdc | 97 | int bt_stream_pos_move(struct bt_stream_pos *pos, uint64_t bit_offset) |
dc3fffef PP |
98 | { |
99 | int ret = 0; | |
100 | ||
50842bdc | 101 | ret = bt_stream_pos_access_ok(pos, bit_offset); |
dc3fffef PP |
102 | if (!ret) { |
103 | goto end; | |
104 | } | |
105 | pos->offset += bit_offset; | |
106 | end: | |
107 | return ret; | |
108 | } | |
109 | ||
110 | static inline | |
50842bdc | 111 | int bt_stream_pos_align(struct bt_stream_pos *pos, uint64_t bit_offset) |
dc3fffef | 112 | { |
50842bdc | 113 | return bt_stream_pos_move(pos, |
dc3fffef PP |
114 | offset_align(pos->offset, bit_offset)); |
115 | } | |
116 | ||
117 | static inline | |
50842bdc | 118 | char *bt_stream_pos_get_addr(struct bt_stream_pos *pos) |
dc3fffef PP |
119 | { |
120 | /* Only makes sense to get the address after aligning on CHAR_BIT */ | |
f6ccaed9 | 121 | BT_ASSERT(!(pos->offset % CHAR_BIT)); |
544d0515 | 122 | return ((char *) mmap_align_addr(pos->base_mma)) + |
dc3fffef PP |
123 | pos->mmap_base_offset + (pos->offset / CHAR_BIT); |
124 | } | |
125 | ||
126 | static inline | |
50842bdc | 127 | int bt_stream_pos_init(struct bt_stream_pos *pos, |
dc3fffef PP |
128 | int fd, int open_flags) |
129 | { | |
130 | pos->fd = fd; | |
131 | ||
132 | switch (open_flags & O_ACCMODE) { | |
133 | case O_RDONLY: | |
134 | pos->prot = PROT_READ; | |
135 | pos->flags = MAP_PRIVATE; | |
136 | break; | |
137 | case O_RDWR: | |
138 | pos->prot = PROT_READ | PROT_WRITE; | |
139 | pos->flags = MAP_SHARED; | |
140 | break; | |
141 | default: | |
0fbb9a9f | 142 | abort(); |
dc3fffef PP |
143 | } |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | static inline | |
50842bdc | 149 | int bt_stream_pos_fini(struct bt_stream_pos *pos) |
dc3fffef PP |
150 | { |
151 | if (pos->base_mma) { | |
152 | int ret; | |
153 | ||
154 | /* unmap old base */ | |
155 | ret = munmap_align(pos->base_mma); | |
156 | if (ret) { | |
157 | return -1; | |
158 | } | |
159 | } | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | BT_HIDDEN | |
50842bdc | 165 | void bt_stream_pos_packet_seek(struct bt_stream_pos *pos, size_t index, |
dc3fffef PP |
166 | int whence); |
167 | ||
168 | #endif /* BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H */ |