Make API CTF-agnostic
[babeltrace.git] / include / babeltrace / ctf-writer / serialize-internal.h
CommitLineData
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>
3d9990ac 36#include <babeltrace/align-internal.h>
44c440bc 37#include <babeltrace/endian-internal.h>
108e5a1e 38#include <babeltrace/common-internal.h>
3d9990ac 39#include <babeltrace/mmap-align-internal.h>
c55a9f58 40#include <babeltrace/types.h>
3dca2276 41#include <babeltrace/ctf-writer/field-types.h>
16ca5ff0
PP
42#include <babeltrace/ctf-writer/fields-internal.h>
43#include <babeltrace/ctf-writer/fields.h>
f6ccaed9 44#include <babeltrace/assert-internal.h>
dc3fffef 45
108e5a1e
MJ
46#define PACKET_LEN_INCREMENT (bt_common_get_page_size() * 8 * CHAR_BIT)
47
44c440bc
PP
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
3dca2276 54struct bt_ctf_stream_pos {
dc3fffef
PP
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 */
dc3fffef
PP
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
67BT_HIDDEN
16ca5ff0 68int bt_ctf_field_integer_write(struct bt_ctf_field_common *field,
3dca2276
PP
69 struct bt_ctf_stream_pos *pos,
70 enum bt_ctf_byte_order native_byte_order);
dc3fffef
PP
71
72BT_HIDDEN
16ca5ff0 73int bt_ctf_field_floating_point_write(struct bt_ctf_field_common *field,
3dca2276
PP
74 struct bt_ctf_stream_pos *pos,
75 enum bt_ctf_byte_order native_byte_order);
dc3fffef
PP
76
77static inline
3dca2276 78int bt_ctf_stream_pos_access_ok(struct bt_ctf_stream_pos *pos, uint64_t bit_len)
dc3fffef
PP
79{
80 uint64_t max_len;
81
82 if (unlikely(pos->offset == EOF))
83 return 0;
b3376dd9 84
dc3fffef
PP
85 if (pos->prot == PROT_READ) {
86 /*
87 * Reads may only reach up to the "content_size",
88 * regardless of the packet_size.
89 */
b3376dd9 90 max_len = pos->offset;
dc3fffef
PP
91 } else {
92 /* Writes may take place up to the end of the packet. */
93 max_len = pos->packet_size;
94 }
6c711b23
MD
95 if (unlikely(pos->offset < 0 || bit_len > INT64_MAX - pos->offset)) {
96 return 0;
97 }
dc3fffef
PP
98 if (unlikely(pos->offset + bit_len > max_len))
99 return 0;
100 return 1;
101}
102
103static inline
3dca2276 104int bt_ctf_stream_pos_move(struct bt_ctf_stream_pos *pos, uint64_t bit_offset)
dc3fffef
PP
105{
106 int ret = 0;
107
3dca2276 108 ret = bt_ctf_stream_pos_access_ok(pos, bit_offset);
dc3fffef
PP
109 if (!ret) {
110 goto end;
111 }
112 pos->offset += bit_offset;
113end:
114 return ret;
115}
116
117static inline
3dca2276 118int bt_ctf_stream_pos_align(struct bt_ctf_stream_pos *pos, uint64_t bit_offset)
dc3fffef 119{
3dca2276 120 return bt_ctf_stream_pos_move(pos,
dc3fffef
PP
121 offset_align(pos->offset, bit_offset));
122}
123
124static inline
3dca2276 125char *bt_ctf_stream_pos_get_addr(struct bt_ctf_stream_pos *pos)
dc3fffef
PP
126{
127 /* Only makes sense to get the address after aligning on CHAR_BIT */
f6ccaed9 128 BT_ASSERT(!(pos->offset % CHAR_BIT));
544d0515 129 return ((char *) mmap_align_addr(pos->base_mma)) +
dc3fffef
PP
130 pos->mmap_base_offset + (pos->offset / CHAR_BIT);
131}
132
133static inline
3dca2276 134int bt_ctf_stream_pos_init(struct bt_ctf_stream_pos *pos,
dc3fffef
PP
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:
0fbb9a9f 149 abort();
dc3fffef
PP
150 }
151
152 return 0;
153}
154
155static inline
3dca2276 156int bt_ctf_stream_pos_fini(struct bt_ctf_stream_pos *pos)
dc3fffef
PP
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
171BT_HIDDEN
3dca2276 172void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos *pos, size_t index,
dc3fffef
PP
173 int whence);
174
175#endif /* BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H */
This page took 0.043889 seconds and 4 git commands to generate.