* http://www.efficios.com/ctf
*/
+#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
-#include <sys/mman.h>
+#include <babeltrace/compat/mman-internal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <babeltrace/ctf-ir/fields.h>
#include <babeltrace/ctf-ir/fields-internal.h>
#include <babeltrace/align-internal.h>
+#include <babeltrace/common-internal.h>
#include <babeltrace/mmap-align-internal.h>
+#include <babeltrace/types.h>
+#include <babeltrace/assert-internal.h>
-struct bt_ctf_stream_pos {
+#define PACKET_LEN_INCREMENT (bt_common_get_page_size() * 8 * CHAR_BIT)
+
+struct bt_stream_pos {
int fd;
int prot; /* mmap protection */
int flags; /* mmap flags */
off_t mmap_offset; /* mmap offset in the file, in bytes */
off_t mmap_base_offset; /* offset of start of packet in mmap, in bytes */
uint64_t packet_size; /* current packet size, in bits */
- uint64_t content_size; /* current content size, in bits */
int64_t offset; /* offset from base, in bits. EOF for end of file. */
struct mmap_align *base_mma;/* mmap base address */
};
BT_HIDDEN
-int bt_ctf_field_integer_write(struct bt_ctf_field_integer *field,
- struct bt_ctf_stream_pos *pos,
- enum bt_ctf_byte_order native_byte_order);
+int bt_field_integer_write(struct bt_field_integer *field,
+ struct bt_stream_pos *pos,
+ enum bt_byte_order native_byte_order);
BT_HIDDEN
-int bt_ctf_field_floating_point_write(struct bt_ctf_field_floating_point *field,
- struct bt_ctf_stream_pos *pos,
- enum bt_ctf_byte_order native_byte_order);
+int bt_field_floating_point_write(struct bt_field_floating_point *field,
+ struct bt_stream_pos *pos,
+ enum bt_byte_order native_byte_order);
static inline
-int bt_ctf_stream_pos_access_ok(struct bt_ctf_stream_pos *pos, uint64_t bit_len)
+int bt_stream_pos_access_ok(struct bt_stream_pos *pos, uint64_t bit_len)
{
uint64_t max_len;
if (unlikely(pos->offset == EOF))
return 0;
+
if (pos->prot == PROT_READ) {
/*
* Reads may only reach up to the "content_size",
* regardless of the packet_size.
*/
- max_len = pos->content_size;
+ max_len = pos->offset;
} else {
/* Writes may take place up to the end of the packet. */
max_len = pos->packet_size;
}
+ if (unlikely(pos->offset < 0 || bit_len > INT64_MAX - pos->offset)) {
+ return 0;
+ }
if (unlikely(pos->offset + bit_len > max_len))
return 0;
return 1;
}
static inline
-int bt_ctf_stream_pos_move(struct bt_ctf_stream_pos *pos, uint64_t bit_offset)
+int bt_stream_pos_move(struct bt_stream_pos *pos, uint64_t bit_offset)
{
int ret = 0;
- ret = bt_ctf_stream_pos_access_ok(pos, bit_offset);
+ ret = bt_stream_pos_access_ok(pos, bit_offset);
if (!ret) {
goto end;
}
}
static inline
-int bt_ctf_stream_pos_align(struct bt_ctf_stream_pos *pos, uint64_t bit_offset)
+int bt_stream_pos_align(struct bt_stream_pos *pos, uint64_t bit_offset)
{
- return bt_ctf_stream_pos_move(pos,
+ return bt_stream_pos_move(pos,
offset_align(pos->offset, bit_offset));
}
static inline
-char *bt_ctf_stream_pos_get_addr(struct bt_ctf_stream_pos *pos)
+char *bt_stream_pos_get_addr(struct bt_stream_pos *pos)
{
/* Only makes sense to get the address after aligning on CHAR_BIT */
- assert(!(pos->offset % CHAR_BIT));
+ BT_ASSERT(!(pos->offset % CHAR_BIT));
return ((char *) mmap_align_addr(pos->base_mma)) +
pos->mmap_base_offset + (pos->offset / CHAR_BIT);
}
static inline
-int bt_ctf_stream_pos_init(struct bt_ctf_stream_pos *pos,
+int bt_stream_pos_init(struct bt_stream_pos *pos,
int fd, int open_flags)
{
pos->fd = fd;
pos->flags = MAP_SHARED;
break;
default:
- assert(false);
+ abort();
}
return 0;
}
static inline
-int bt_ctf_stream_pos_fini(struct bt_ctf_stream_pos *pos)
+int bt_stream_pos_fini(struct bt_stream_pos *pos)
{
if (pos->base_mma) {
int ret;
}
BT_HIDDEN
-void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos *pos, size_t index,
+void bt_stream_pos_packet_seek(struct bt_stream_pos *pos, size_t index,
int whence);
#endif /* BABELTRACE_CTF_WRITER_SERIALIZE_INTERNAL_H */