1 #ifndef BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H
5 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 * The Common Trace Format (CTF) Specification is available at
26 * http://www.efficios.com/ctf
32 #include <babeltrace/compat/mman-internal.h>
33 #include <sys/types.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>
46 #define PACKET_LEN_INCREMENT (bt_common_get_page_size() * 8 * CHAR_BIT)
48 #if (BYTE_ORDER == BIG_ENDIAN)
49 # define BT_CTF_MY_BYTE_ORDER BT_CTF_BYTE_ORDER_BIG_ENDIAN
51 # define BT_CTF_MY_BYTE_ORDER BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
54 struct bt_ctf_stream_pos
{
56 int prot
; /* mmap protection */
57 int flags
; /* mmap flags */
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 */
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
);
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
);
78 int bt_ctf_stream_pos_access_ok(struct bt_ctf_stream_pos
*pos
, uint64_t bit_len
)
82 if (unlikely(pos
->offset
== EOF
))
85 if (pos
->prot
== PROT_READ
) {
87 * Reads may only reach up to the "content_size",
88 * regardless of the packet_size.
90 max_len
= pos
->offset
;
92 /* Writes may take place up to the end of the packet. */
93 max_len
= pos
->packet_size
;
95 if (unlikely(pos
->offset
< 0 || bit_len
> INT64_MAX
- pos
->offset
)) {
98 if (unlikely(pos
->offset
+ bit_len
> max_len
))
104 int bt_ctf_stream_pos_move(struct bt_ctf_stream_pos
*pos
, uint64_t bit_offset
)
108 ret
= bt_ctf_stream_pos_access_ok(pos
, bit_offset
);
112 pos
->offset
+= bit_offset
;
118 int bt_ctf_stream_pos_align(struct bt_ctf_stream_pos
*pos
, uint64_t bit_offset
)
120 return bt_ctf_stream_pos_move(pos
,
121 offset_align(pos
->offset
, bit_offset
));
125 char *bt_ctf_stream_pos_get_addr(struct bt_ctf_stream_pos
*pos
)
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
);
134 int bt_ctf_stream_pos_init(struct bt_ctf_stream_pos
*pos
,
135 int fd
, int open_flags
)
139 switch (open_flags
& O_ACCMODE
) {
141 pos
->prot
= PROT_READ
;
142 pos
->flags
= MAP_PRIVATE
;
145 pos
->prot
= PROT_READ
| PROT_WRITE
;
146 pos
->flags
= MAP_SHARED
;
156 int bt_ctf_stream_pos_fini(struct bt_ctf_stream_pos
*pos
)
162 ret
= munmap_align(pos
->base_mma
);
172 void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos
*pos
, size_t index
,
175 #endif /* BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H */