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