- */
-
-#include <babeltrace/align.h>
-#include <babeltrace/list.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <limits.h>
-#include <string.h>
-#include <glib.h>
-#include <assert.h>
-
-/* Preallocate this many fields for structures */
-#define DEFAULT_NR_STRUCT_FIELDS 8
-
-/*
- * Always update stream_pos with move_pos and init_pos.
- */
-struct stream_pos {
- char *base; /* Base address */
- size_t offset; /* Offset from base, in bits */
- int dummy; /* Dummy position, for length calculation */
-};
-
-static inline
-void init_pos(struct stream_pos *pos, char *base)
-{
- pos->base = base; /* initial base, page-aligned */
- pos->offset = 0;
- pos->dummy = false;
-}
-
-/*
- * move_pos - move position of a relative bit offset
- *
- * TODO: allow larger files by updating base too.
- */
-static inline
-void move_pos(struct stream_pos *pos, size_t offset)
-{
- pos->offset = pos->offset + offset;
-}
-
-/*
- * align_pos - align position on a bit offset (> 0)
- *
- * TODO: allow larger files by updating base too.
- */
-static inline
-void align_pos(struct stream_pos *pos, size_t offset)
-{
- pos->offset += offset_align(pos->offset, offset);
-}
-
-static inline
-void copy_pos(struct stream_pos *dest, struct stream_pos *src)
-{
- memcpy(dest, src, sizeof(struct stream_pos));
-}
-
-static inline
-char *get_pos_addr(struct stream_pos *pos)
-{
- /* Only makes sense to get the address after aligning on CHAR_BIT */
- assert(!(pos->offset % CHAR_BIT));
- return pos->base + (pos->offset / CHAR_BIT);
-}
-
-struct format;
-struct definition;
-
-/* type scope */
-struct declaration_scope {
- /* Hash table mapping type name GQuark to "struct declaration" */
- GHashTable *typedef_declarations;
- /* Hash table mapping struct name GQuark to "struct declaration_struct" */
- GHashTable *struct_declarations;
- /* Hash table mapping variant name GQuark to "struct declaration_variant" */
- GHashTable *variant_declarations;
- /* Hash table mapping enum name GQuark to "struct type_enum" */
- GHashTable *enum_declarations;
- struct declaration_scope *parent_scope;
-};
-
-/* definition scope */
-struct definition_scope {
- /* Hash table mapping field name GQuark to "struct definition" */
- GHashTable *definitions;
- struct definition_scope *parent_scope;
-};
-
-enum ctf_type_id {
- CTF_TYPE_UNKNOWN = 0,
- CTF_TYPE_INTEGER,
- CTF_TYPE_FLOAT,
- CTF_TYPE_ENUM,
- CTF_TYPE_STRING,
- CTF_TYPE_STRUCT,
- CTF_TYPE_VARIANT,
- CTF_TYPE_ARRAY,
- CTF_TYPE_SEQUENCE,
- NR_CTF_TYPES,
-};
-
-struct declaration {
- enum ctf_type_id id;
- GQuark name; /* type name */
- size_t alignment; /* type alignment, in bits */
- int ref; /* number of references to the type */
- /*
- * declaration_free called with declaration ref is decremented to 0.
- */
- void (*declaration_free)(struct declaration *declaration);
- struct definition *
- (*definition_new)(struct declaration *declaration,
- struct definition_scope *parent_scope);
- /*
- * definition_free called with definition ref is decremented to 0.
- */
- void (*definition_free)(struct definition *definition);
- /*
- * Definition copy function. Knows how to find the child
- * definition from the parent definition.
- */
- void (*copy)(struct stream_pos *dest, const struct format *fdest,
- struct stream_pos *src, const struct format *fsrc,
- struct definition *definition);
-};
-
-struct definition {
- struct declaration *declaration;
- int ref; /* number of references to the definition */
-};
-
-/*
- * Because we address in bits, bitfields end up being exactly the same as
- * integers, except that their read/write functions must be able to deal with
- * read/write non aligned on CHAR_BIT.
- */
-struct declaration_integer {
- struct declaration p;
- size_t len; /* length, in bits. */
- int byte_order; /* byte order */
- int signedness;
-};
-
-struct definition_integer {
- struct definition p;
- struct declaration_integer *declaration;
- /* Last values read */
- union {
- uint64_t _unsigned;
- int64_t _signed;
- } value;
-};
-
-struct declaration_float {
- struct declaration p;
- struct declaration_integer *sign;
- struct declaration_integer *mantissa;
- struct declaration_integer *exp;
- int byte_order;
- /* TODO: we might want to express more info about NaN, +inf and -inf */
-};
-
-struct definition_float {
- struct definition p;
- struct declaration_float *declaration;
- /* Last values read */
- long double value;
-};
-
-/*
- * enum_val_equal assumes that signed and unsigned memory layout overlap.
- */
-struct enum_range {
- union {
- int64_t _signed;
- uint64_t _unsigned;
- } start; /* lowest range value */
- union {
- int64_t _signed;
- uint64_t _unsigned;
- } end; /* highest range value */
-};
-
-struct enum_range_to_quark {
- struct cds_list_head node;
- struct enum_range range;
- GQuark quark;
-};
-
-/*
- * We optimize the common case (range of size 1: single value) by creating a
- * hash table mapping values to quark sets. We then lookup the ranges to
- * complete the quark set.