From d11e9c4975d88591e2324b6b11f426a22995833f Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Fri, 6 May 2011 18:36:52 -0400 Subject: [PATCH] Restructure objects around generic_rw() accessor Signed-off-by: Mathieu Desnoyers --- formats/ctf/ctf.c | 55 +++-- formats/ctf/types/array.c | 10 +- formats/ctf/types/enum.c | 65 +++--- formats/ctf/types/float.c | 146 ++++++------- formats/ctf/types/integer.c | 383 ++++++++++++++++----------------- formats/ctf/types/sequence.c | 12 +- formats/ctf/types/string.c | 52 +++-- formats/ctf/types/struct.c | 12 +- formats/ctf/types/variant.c | 11 +- include/babeltrace/ctf/types.h | 66 ++---- include/babeltrace/format.h | 58 ----- include/babeltrace/types.h | 39 +++- types/array.c | 22 +- types/enum.c | 27 --- types/float.c | 43 ++-- types/integer.c | 26 --- types/sequence.c | 33 +-- types/string.c | 16 +- types/struct.c | 24 +-- types/variant.c | 23 +- 20 files changed, 445 insertions(+), 678 deletions(-) diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 614adc02..8556055e 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -49,30 +49,29 @@ extern int yydebug; struct trace_descriptor *ctf_open_trace(const char *path, int flags); void ctf_close_trace(struct trace_descriptor *descriptor); -static struct format ctf_format = { - .uint_read = ctf_uint_read, - .int_read = ctf_int_read, - .uint_write = ctf_uint_write, - .int_write = ctf_int_write, - .double_read = ctf_double_read, - .double_write = ctf_double_write, - .ldouble_read = ctf_ldouble_read, - .ldouble_write = ctf_ldouble_write, - .float_copy = ctf_float_copy, - .string_copy = ctf_string_copy, - .string_read = ctf_string_read, - .string_write = ctf_string_write, - .string_free_temp = ctf_string_free_temp, - .enum_read = ctf_enum_read, - .enum_write = ctf_enum_write, - .struct_begin = ctf_struct_begin, - .struct_end = ctf_struct_end, - .variant_begin = ctf_variant_begin, - .variant_end = ctf_variant_end, - .array_begin = ctf_array_begin, - .array_end = ctf_array_end, - .sequence_begin = ctf_sequence_begin, - .sequence_end = ctf_sequence_end, +static rw_dispatch read_dispatch_table[] = { + [ CTF_TYPE_INTEGER ] = ctf_integer_read, + [ CTF_TYPE_FLOAT ] = ctf_float_read, + [ CTF_TYPE_ENUM ] = ctf_enum_read, + [ CTF_TYPE_STRING ] = ctf_string_read, + [ CTF_TYPE_STRUCT ] = ctf_struct_rw, + [ CTF_TYPE_VARIANT ] = ctf_variant_rw, + [ CTF_TYPE_ARRAY ] = ctf_array_rw, + [ CTF_TYPE_SEQUENCE ] = ctf_sequence_rw, +}; + +static rw_dispatch write_dispatch_table[] = { + [ CTF_TYPE_INTEGER ] = ctf_integer_write, + [ CTF_TYPE_FLOAT ] = ctf_float_write, + [ CTF_TYPE_ENUM ] = ctf_enum_write, + [ CTF_TYPE_STRING ] = ctf_string_write, + [ CTF_TYPE_STRUCT ] = ctf_struct_rw, + [ CTF_TYPE_VARIANT ] = ctf_variant_rw, + [ CTF_TYPE_ARRAY ] = ctf_array_rw, + [ CTF_TYPE_SEQUENCE ] = ctf_sequence_rw, +}; + +struct format ctf_format = { .open_trace = ctf_open_trace, .close_trace = ctf_close_trace, }; @@ -97,11 +96,13 @@ void ctf_init_pos(struct ctf_stream_pos *pos, int fd) case O_RDONLY: pos->prot = PROT_READ; pos->flags = MAP_PRIVATE; + pos->parent.rw_table = read_dispatch_table; break; case O_WRONLY: case O_RDWR: pos->prot = PROT_WRITE; /* Write has priority */ pos->flags = MAP_SHARED; + pos->parent.rw_table = write_dispatch_table; ctf_move_pos_slow(pos, 0); /* position for write */ break; default: @@ -304,8 +305,7 @@ int create_stream_packet_index(struct ctf_trace *td, /* read and check header, set stream id (and check) */ if (td->packet_header) { /* Read packet header */ - td->packet_header->p.declaration->copy(NULL, NULL, - &pos->parent, &ctf_format, &td->packet_header->p); + generic_rw(&pos->parent, &td->packet_header->p); len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic")); if (len_index >= 0) { @@ -390,8 +390,7 @@ int create_stream_packet_index(struct ctf_trace *td, if (stream->packet_context) { /* Read packet context */ - stream->packet_context->p.declaration->copy(NULL, NULL, - &pos->parent, &ctf_format, &stream->packet_context->p); + generic_rw(&pos->parent, &stream->packet_context->p); /* read content size from header */ len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size")); diff --git a/formats/ctf/types/array.c b/formats/ctf/types/array.c index 8cfd0ae5..aa3ffcf0 100644 --- a/formats/ctf/types/array.c +++ b/formats/ctf/types/array.c @@ -18,13 +18,7 @@ #include -void ctf_array_begin(struct stream_pos *pos, - const struct declaration_array *array_declaration) -{ - /* No need to align, because the first field will align itself. */ -} - -void ctf_array_end(struct stream_pos *pos, - const struct declaration_array *array_declaration) +void ctf_array_rw(struct stream_pos *pos, struct definition *definition) { + array_rw(pos, definition); } diff --git a/formats/ctf/types/enum.c b/formats/ctf/types/enum.c index 15e2590a..153c82bd 100644 --- a/formats/ctf/types/enum.c +++ b/formats/ctf/types/enum.c @@ -20,45 +20,38 @@ #include #include -/* - * The caller should unref the GArray. - */ -GArray *ctf_enum_read(struct stream_pos *pos, - const struct declaration_enum *src) +void ctf_enum_read(struct stream_pos *ppos, struct definition *definition) { - const struct declaration_integer *integer_declaration = src->integer_declaration; - - if (!integer_declaration->signedness) { - uint64_t v; - - v = ctf_uint_read(pos, integer_declaration); - return enum_uint_to_quark_set(src, v); - } else { - int64_t v; - - v = ctf_int_read(pos, integer_declaration); - return enum_int_to_quark_set(src, v); - } + struct definition_enum *enum_definition = + container_of(definition, struct definition_enum, p); + const struct declaration_enum *enum_declaration = + enum_definition->declaration; + struct definition_integer *integer_definition = + enum_definition->integer; + const struct declaration_integer *integer_declaration = + integer_definition->declaration; + GArray *qs; + + ctf_integer_read(ppos, &integer_definition->p); + if (!integer_declaration->signedness) + qs = enum_uint_to_quark_set(enum_declaration, + integer_definition->value._unsigned); + else + qs = enum_int_to_quark_set(enum_declaration, + integer_definition->value._signed); + assert(qs); + /* unref previous quark set */ + if (enum_definition->value) + g_array_unref(enum_definition->value); + enum_definition->value = qs; } -/* - * Arbitrarily choose the start of the first matching range. - */ -void ctf_enum_write(struct stream_pos *pos, - const struct declaration_enum *dest, - GQuark q) +void ctf_enum_write(struct stream_pos *pos, struct definition *definition) { - const struct declaration_integer *integer_declaration = dest->integer_declaration; - GArray *array; - - array = enum_quark_to_range_set(dest, q); - assert(array); + struct definition_enum *enum_definition = + container_of(definition, struct definition_enum, p); + struct definition_integer *integer_definition = + enum_definition->integer; - if (!integer_declaration->signedness) { - uint64_t v = g_array_index(array, struct enum_range, 0).start._unsigned; - ctf_uint_write(pos, integer_declaration, v); - } else { - int64_t v = g_array_index(array, struct enum_range, 0).start._unsigned; - ctf_int_write(pos, integer_declaration, v); - } + ctf_integer_write(pos, &integer_definition->p); } diff --git a/formats/ctf/types/float.c b/formats/ctf/types/float.c index d33e6ad9..a30c3ea4 100644 --- a/formats/ctf/types/float.c +++ b/formats/ctf/types/float.c @@ -64,123 +64,105 @@ union ldoubleIEEE754 { #endif }; +static struct declaration_float *static_ldouble_declaration; + struct pos_len { size_t sign_start, exp_start, mantissa_start, len; }; void _ctf_float_copy(struct stream_pos *destp, - const struct declaration_float *dest_declaration, + struct definition_float *dest_definition, struct stream_pos *srcp, - const struct declaration_float *src_declaration) + const struct definition_float *src_definition) { - uint8_t sign; - int64_t exp; - uint64_t mantissa; - /* Read */ - if (src_declaration->byte_order == LITTLE_ENDIAN) { - mantissa = ctf_uint_read(srcp, src_declaration->mantissa); - exp = ctf_int_read(srcp, src_declaration->exp); - sign = ctf_uint_read(srcp, src_declaration->sign); + if (src_definition->declaration->byte_order == LITTLE_ENDIAN) { + ctf_integer_read(srcp, &src_definition->mantissa->p); + ctf_integer_read(srcp, &src_definition->exp->p); + ctf_integer_read(srcp, &src_definition->sign->p); } else { - sign = ctf_uint_read(srcp, src_declaration->sign); - exp = ctf_int_read(srcp, src_declaration->exp); - mantissa = ctf_uint_read(srcp, src_declaration->mantissa); + ctf_integer_read(srcp, &src_definition->sign->p); + ctf_integer_read(srcp, &src_definition->exp->p); + ctf_integer_read(srcp, &src_definition->mantissa->p); } + + dest_definition->mantissa->value._unsigned = + src_definition->mantissa->value._unsigned; + dest_definition->exp->value._signed = + src_definition->exp->value._signed; + dest_definition->sign->value._unsigned = + src_definition->sign->value._unsigned; + /* Write */ - if (dest_declaration->byte_order == LITTLE_ENDIAN) { - ctf_uint_write(destp, dest_declaration->mantissa, mantissa); - ctf_int_write(destp, dest_declaration->exp, exp); - ctf_uint_write(destp, dest_declaration->sign, sign); + if (dest_definition->declaration->byte_order == LITTLE_ENDIAN) { + ctf_integer_write(destp, &dest_definition->mantissa->p); + ctf_integer_write(destp, &dest_definition->exp->p); + ctf_integer_write(destp, &dest_definition->sign->p); } else { - ctf_uint_write(destp, dest_declaration->sign, sign); - ctf_int_write(destp, dest_declaration->exp, exp); - ctf_uint_write(destp, dest_declaration->mantissa, mantissa); + ctf_integer_write(destp, &dest_definition->sign->p); + ctf_integer_write(destp, &dest_definition->exp->p); + ctf_integer_write(destp, &dest_definition->mantissa->p); } } -void ctf_float_copy(struct stream_pos *dest, struct stream_pos *src, - const struct declaration_float *float_declaration) -{ - ctf_align_pos(ctf_pos(src), float_declaration->p.alignment); - ctf_align_pos(ctf_pos(dest), float_declaration->p.alignment); - _ctf_float_copy(dest, float_declaration, src, float_declaration); -} - -double ctf_double_read(struct stream_pos *srcp, - const struct declaration_float *float_declaration) +void ctf_float_read(struct stream_pos *ppos, struct definition *definition) { - union doubleIEEE754 u; - struct declaration_float *dest_declaration = - float_declaration_new(DBL_MANT_DIG, - sizeof(double) * CHAR_BIT - DBL_MANT_DIG, - BYTE_ORDER, - __alignof__(double)); + struct definition_float *float_definition = + container_of(definition, struct definition_float, p); + const struct declaration_float *float_declaration = + float_definition->declaration; + struct ctf_stream_pos *pos = ctf_pos(ppos); + union ldoubleIEEE754 u; + struct definition *tmpdef = + static_ldouble_declaration->p.definition_new(&static_ldouble_declaration->p, + NULL, 0, 0); + struct definition_float *tmpfloat = + container_of(tmpdef, struct definition_float, p); struct ctf_stream_pos destp; - ctf_align_pos(ctf_pos(srcp), float_declaration->p.alignment); ctf_init_pos(&destp, -1); destp.base = (char *) u.bits; - _ctf_float_copy(&destp.parent, dest_declaration, srcp, float_declaration); - declaration_unref(&dest_declaration->p); - return u.v; + + ctf_align_pos(pos, float_declaration->p.alignment); + _ctf_float_copy(&destp.parent, tmpfloat, ppos, float_definition); + float_definition->value = u.v; + definition_unref(tmpdef); } -void ctf_double_write(struct stream_pos *destp, - const struct declaration_float *float_declaration, - double v) +void ctf_float_write(struct stream_pos *ppos, struct definition *definition) { - union doubleIEEE754 u; - struct declaration_float *src_declaration = - float_declaration_new(DBL_MANT_DIG, - sizeof(double) * CHAR_BIT - DBL_MANT_DIG, - BYTE_ORDER, - __alignof__(double)); + struct definition_float *float_definition = + container_of(definition, struct definition_float, p); + const struct declaration_float *float_declaration = + float_definition->declaration; + struct ctf_stream_pos *pos = ctf_pos(ppos); + union ldoubleIEEE754 u; + struct definition *tmpdef = + static_ldouble_declaration->p.definition_new(&static_ldouble_declaration->p, + NULL, 0, 0); + struct definition_float *tmpfloat = + container_of(tmpdef, struct definition_float, p); struct ctf_stream_pos srcp; - u.v = v; - ctf_align_pos(ctf_pos(destp), float_declaration->p.alignment); ctf_init_pos(&srcp, -1); srcp.base = (char *) u.bits; - _ctf_float_copy(destp, float_declaration, &srcp.parent, src_declaration); - declaration_unref(&src_declaration->p); + + u.v = float_definition->value; + ctf_align_pos(pos, float_declaration->p.alignment); + _ctf_float_copy(ppos, float_definition, &srcp.parent, tmpfloat); + definition_unref(tmpdef); } -long double ctf_ldouble_read(struct stream_pos *srcp, - const struct declaration_float *float_declaration) +void __attribute__((constructor)) ctf_float_init(void) { - union ldoubleIEEE754 u; - struct declaration_float *dest_declaration = + static_ldouble_declaration = float_declaration_new(LDBL_MANT_DIG, sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG, BYTE_ORDER, __alignof__(long double)); - struct ctf_stream_pos destp; - - ctf_align_pos(ctf_pos(srcp), float_declaration->p.alignment); - ctf_init_pos(&destp, -1); - destp.base = (char *) u.bits; - _ctf_float_copy(&destp.parent, dest_declaration, srcp, float_declaration); - declaration_unref(&dest_declaration->p); - return u.v; } -void ctf_ldouble_write(struct stream_pos *destp, - const struct declaration_float *float_declaration, - long double v) +void __attribute__((destructor)) ctf_float_fini(void) { - union ldoubleIEEE754 u; - struct declaration_float *src_declaration = - float_declaration_new(LDBL_MANT_DIG, - sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG, - BYTE_ORDER, - __alignof__(long double)); - struct ctf_stream_pos srcp; - - u.v = v; - ctf_align_pos(ctf_pos(destp), float_declaration->p.alignment); - ctf_init_pos(&srcp, -1); - srcp.base = (char *) u.bits; - _ctf_float_copy(destp, float_declaration, &srcp.parent, src_declaration); - declaration_unref(&src_declaration->p); + declaration_unref(&static_ldouble_declaration->p); } diff --git a/formats/ctf/types/integer.c b/formats/ctf/types/integer.c index c0245300..e43e5547 100644 --- a/formats/ctf/types/integer.c +++ b/formats/ctf/types/integer.c @@ -23,242 +23,231 @@ #include static -uint64_t _aligned_uint_read(struct stream_pos *ppos, - const struct declaration_integer *integer_declaration) +void _aligned_integer_read(struct stream_pos *ppos, + struct definition *definition) { + struct definition_integer *integer_definition = + container_of(definition, struct definition_integer, p); + const struct declaration_integer *integer_declaration = + integer_definition->declaration; struct ctf_stream_pos *pos = ctf_pos(ppos); int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ ctf_align_pos(pos, integer_declaration->p.alignment); assert(!(pos->offset % CHAR_BIT)); - switch (integer_declaration->len) { - case 8: - { - uint8_t v; - v = *(const uint8_t *)pos->base; - ctf_move_pos(pos, integer_declaration->len); - return v; - } - case 16: - { - uint16_t v; - - v = *(const uint16_t *)pos->base; - ctf_move_pos(pos, integer_declaration->len); - return rbo ? GUINT16_SWAP_LE_BE(v) : v; - } - case 32: - { - uint32_t v; - - v = *(const uint32_t *)pos->base; - ctf_move_pos(pos, integer_declaration->len); - return rbo ? GUINT32_SWAP_LE_BE(v) : v; - } - case 64: - { - uint64_t v; - - v = *(const uint64_t *)pos->base; - ctf_move_pos(pos, integer_declaration->len); - return rbo ? GUINT64_SWAP_LE_BE(v) : v; - } - default: - assert(0); + if (!integer_declaration->signedness) { + switch (integer_declaration->len) { + case 8: + { + uint8_t v; + + v = *(const uint8_t *)pos->base; + integer_definition->value._unsigned = v; + break; + } + case 16: + { + uint16_t v; + + v = *(const uint16_t *)pos->base; + integer_definition->value._unsigned = + rbo ? GUINT16_SWAP_LE_BE(v) : v; + break; + } + case 32: + { + uint32_t v; + + v = *(const uint32_t *)pos->base; + integer_definition->value._unsigned = + rbo ? GUINT32_SWAP_LE_BE(v) : v; + break; + } + case 64: + { + uint64_t v; + + v = *(const uint64_t *)pos->base; + integer_definition->value._unsigned = + rbo ? GUINT64_SWAP_LE_BE(v) : v; + break; + } + default: + assert(0); + } + } else { + switch (integer_declaration->len) { + case 8: + { + int8_t v; + + v = *(const int8_t *)pos->base; + integer_definition->value._signed = v; + break; + } + case 16: + { + int16_t v; + + v = *(const int16_t *)pos->base; + integer_definition->value._signed = + rbo ? GUINT16_SWAP_LE_BE(v) : v; + break; + } + case 32: + { + int32_t v; + + v = *(const int32_t *)pos->base; + integer_definition->value._signed = + rbo ? GUINT32_SWAP_LE_BE(v) : v; + break; + } + case 64: + { + int64_t v; + + v = *(const int64_t *)pos->base; + integer_definition->value._signed = + rbo ? GUINT64_SWAP_LE_BE(v) : v; + break; + } + default: + assert(0); + } } + ctf_move_pos(pos, integer_declaration->len); } static -int64_t _aligned_int_read(struct stream_pos *ppos, - const struct declaration_integer *integer_declaration) +void _aligned_integer_write(struct stream_pos *ppos, + struct definition *definition) { + struct definition_integer *integer_definition = + container_of(definition, struct definition_integer, p); + const struct declaration_integer *integer_declaration = + integer_definition->declaration; struct ctf_stream_pos *pos = ctf_pos(ppos); int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ ctf_align_pos(pos, integer_declaration->p.alignment); assert(!(pos->offset % CHAR_BIT)); - switch (integer_declaration->len) { - case 8: - { - int8_t v; - v = *(const int8_t *)pos->base; - ctf_move_pos(pos, integer_declaration->len); - return v; - } - case 16: - { - int16_t v; - - v = *(const int16_t *)pos->base; - ctf_move_pos(pos, integer_declaration->len); - return rbo ? GUINT16_SWAP_LE_BE(v) : v; - } - case 32: - { - int32_t v; - - v = *(const int32_t *)pos->base; - ctf_move_pos(pos, integer_declaration->len); - return rbo ? GUINT32_SWAP_LE_BE(v) : v; - } - case 64: - { - int64_t v; - - v = *(const int64_t *)pos->base; - ctf_move_pos(pos, integer_declaration->len); - return rbo ? GUINT64_SWAP_LE_BE(v) : v; - } - default: - assert(0); - } -} - -static -void _aligned_uint_write(struct stream_pos *ppos, - const struct declaration_integer *integer_declaration, - uint64_t v) -{ - struct ctf_stream_pos *pos = ctf_pos(ppos); - int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ - - ctf_align_pos(pos, integer_declaration->p.alignment); - assert(!(pos->offset % CHAR_BIT)); if (pos->dummy) goto end; - - switch (integer_declaration->len) { - case 8: *(uint8_t *) ctf_get_pos_addr(pos) = (uint8_t) v; - break; - case 16: - *(uint16_t *) ctf_get_pos_addr(pos) = rbo ? - GUINT16_SWAP_LE_BE((uint16_t) v) : - (uint16_t) v; - break; - case 32: - *(uint32_t *) ctf_get_pos_addr(pos) = rbo ? - GUINT32_SWAP_LE_BE((uint32_t) v) : - (uint32_t) v; - break; - case 64: - *(uint64_t *) ctf_get_pos_addr(pos) = rbo ? - GUINT64_SWAP_LE_BE(v) : v; - break; - default: - assert(0); + if (!integer_declaration->signedness) { + uint64_t v = integer_definition->value._unsigned; + + switch (integer_declaration->len) { + case 8: *(uint8_t *) ctf_get_pos_addr(pos) = (uint8_t) v; + break; + case 16: + *(uint16_t *) ctf_get_pos_addr(pos) = rbo ? + GUINT16_SWAP_LE_BE((uint16_t) v) : + (uint16_t) v; + break; + case 32: + *(uint32_t *) ctf_get_pos_addr(pos) = rbo ? + GUINT32_SWAP_LE_BE((uint32_t) v) : + (uint32_t) v; + break; + case 64: + *(uint64_t *) ctf_get_pos_addr(pos) = rbo ? + GUINT64_SWAP_LE_BE(v) : v; + break; + default: + assert(0); + } + } else { + int64_t v = integer_definition->value._signed; + + switch (integer_declaration->len) { + case 8: *(int8_t *) ctf_get_pos_addr(pos) = (int8_t) v; + break; + case 16: + *(int16_t *) ctf_get_pos_addr(pos) = rbo ? + GUINT16_SWAP_LE_BE((int16_t) v) : + (int16_t) v; + break; + case 32: + *(int32_t *) ctf_get_pos_addr(pos) = rbo ? + GUINT32_SWAP_LE_BE((int32_t) v) : + (int32_t) v; + break; + case 64: + *(int64_t *) ctf_get_pos_addr(pos) = rbo ? + GUINT64_SWAP_LE_BE(v) : v; + break; + default: + assert(0); + } } end: ctf_move_pos(pos, integer_declaration->len); } -static -void _aligned_int_write(struct stream_pos *ppos, - const struct declaration_integer *integer_declaration, - int64_t v) +void ctf_integer_read(struct stream_pos *ppos, struct definition *definition) { + struct definition_integer *integer_definition = + container_of(definition, struct definition_integer, p); + const struct declaration_integer *integer_declaration = + integer_definition->declaration; struct ctf_stream_pos *pos = ctf_pos(ppos); - int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ ctf_align_pos(pos, integer_declaration->p.alignment); - assert(!(pos->offset % CHAR_BIT)); - if (pos->dummy) - goto end; - - switch (integer_declaration->len) { - case 8: *(int8_t *) ctf_get_pos_addr(pos) = (int8_t) v; - break; - case 16: - *(int16_t *) ctf_get_pos_addr(pos) = rbo ? - GUINT16_SWAP_LE_BE((int16_t) v) : - (int16_t) v; - break; - case 32: - *(int32_t *) ctf_get_pos_addr(pos) = rbo ? - GUINT32_SWAP_LE_BE((int32_t) v) : - (int32_t) v; - break; - case 64: - *(int64_t *) ctf_get_pos_addr(pos) = rbo ? - GUINT64_SWAP_LE_BE(v) : v; - break; - default: - assert(0); + if (!integer_declaration->signedness) { + if (integer_declaration->byte_order == LITTLE_ENDIAN) + bt_bitfield_read_le(pos->base, unsigned long, + pos->offset, integer_declaration->len, + &integer_definition->value._unsigned); + else + bt_bitfield_read_be(pos->base, unsigned long, + pos->offset, integer_declaration->len, + &integer_definition->value._unsigned); + } else { + if (integer_declaration->byte_order == LITTLE_ENDIAN) + bt_bitfield_read_le(pos->base, unsigned long, + pos->offset, integer_declaration->len, + &integer_definition->value._signed); + else + bt_bitfield_read_be(pos->base, unsigned long, + pos->offset, integer_declaration->len, + &integer_definition->value._signed); } -end: ctf_move_pos(pos, integer_declaration->len); - return; } -uint64_t ctf_uint_read(struct stream_pos *ppos, - const struct declaration_integer *integer_declaration) -{ - struct ctf_stream_pos *pos = ctf_pos(ppos); - uint64_t v = 0; - - ctf_align_pos(pos, integer_declaration->p.alignment); - if (integer_declaration->byte_order == LITTLE_ENDIAN) - bt_bitfield_read_le(pos->base, unsigned long, pos->offset, - integer_declaration->len, &v); - else - bt_bitfield_read_be(pos->base, unsigned long, pos->offset, - integer_declaration->len, &v); - ctf_move_pos(pos, integer_declaration->len); - return v; -} - -int64_t ctf_int_read(struct stream_pos *ppos, - const struct declaration_integer *integer_declaration) -{ - struct ctf_stream_pos *pos = ctf_pos(ppos); - int64_t v = 0; - - ctf_align_pos(pos, integer_declaration->p.alignment); - if (integer_declaration->byte_order == LITTLE_ENDIAN) - bt_bitfield_read_le(pos->base, unsigned long, pos->offset, - integer_declaration->len, &v); - else - bt_bitfield_read_be(pos->base, unsigned long, pos->offset, - integer_declaration->len, &v); - ctf_move_pos(pos, integer_declaration->len); - return v; -} - -void ctf_uint_write(struct stream_pos *ppos, - const struct declaration_integer *integer_declaration, - uint64_t v) -{ - struct ctf_stream_pos *pos = ctf_pos(ppos); - - ctf_align_pos(pos, integer_declaration->p.alignment); - if (pos->dummy) - goto end; - if (integer_declaration->byte_order == LITTLE_ENDIAN) - bt_bitfield_write_le(pos->base, unsigned long, pos->offset, - integer_declaration->len, v); - else - bt_bitfield_write_be(pos->base, unsigned long, pos->offset, - integer_declaration->len, v); -end: - ctf_move_pos(pos, integer_declaration->len); -} - -void ctf_int_write(struct stream_pos *ppos, - const struct declaration_integer *integer_declaration, - int64_t v) +void ctf_integer_write(struct stream_pos *ppos, struct definition *definition) { + struct definition_integer *integer_definition = + container_of(definition, struct definition_integer, p); + const struct declaration_integer *integer_declaration = + integer_definition->declaration; struct ctf_stream_pos *pos = ctf_pos(ppos); ctf_align_pos(pos, integer_declaration->p.alignment); if (pos->dummy) goto end; - if (integer_declaration->byte_order == LITTLE_ENDIAN) - bt_bitfield_write_le(pos->base, unsigned long, pos->offset, - integer_declaration->len, v); - else - bt_bitfield_write_be(pos->base, unsigned long, pos->offset, - integer_declaration->len, v); + if (!integer_declaration->signedness) { + if (integer_declaration->byte_order == LITTLE_ENDIAN) + bt_bitfield_write_le(pos->base, unsigned long, + pos->offset, integer_declaration->len, + integer_definition->value._unsigned); + else + bt_bitfield_write_be(pos->base, unsigned long, + pos->offset, integer_declaration->len, + integer_definition->value._unsigned); + } else { + if (integer_declaration->byte_order == LITTLE_ENDIAN) + bt_bitfield_write_le(pos->base, unsigned long, + pos->offset, integer_declaration->len, + integer_definition->value._signed); + else + bt_bitfield_write_be(pos->base, unsigned long, + pos->offset, integer_declaration->len, + integer_definition->value._signed); + } end: ctf_move_pos(pos, integer_declaration->len); } diff --git a/formats/ctf/types/sequence.c b/formats/ctf/types/sequence.c index 640f4517..1f36a49f 100644 --- a/formats/ctf/types/sequence.c +++ b/formats/ctf/types/sequence.c @@ -18,15 +18,11 @@ #include -void ctf_sequence_begin(struct stream_pos *ppos, - const struct declaration_sequence *sequence_declaration) +void ctf_sequence_rw(struct stream_pos *ppos, struct definition *definition) { + struct declaration *declaration = definition->declaration; struct ctf_stream_pos *pos = ctf_pos(ppos); - ctf_align_pos(pos, sequence_declaration->p.alignment); -} - -void ctf_sequence_end(struct stream_pos *pos, - const struct declaration_sequence *sequence_declaration) -{ + ctf_align_pos(pos, declaration->alignment); + sequence_rw(ppos, definition); } diff --git a/formats/ctf/types/string.c b/formats/ctf/types/string.c index ac25e1ad..b19335e2 100644 --- a/formats/ctf/types/string.c +++ b/formats/ctf/types/string.c @@ -41,39 +41,47 @@ end: ctf_move_pos(src, len); } -void ctf_string_read(char **dest, struct stream_pos *psrc, - const struct declaration_string *string_declaration) +void ctf_string_read(struct stream_pos *ppos, struct definition *definition) { - struct ctf_stream_pos *src = ctf_pos(psrc); + struct definition_string *string_definition = + container_of(definition, struct definition_string, p); + const struct declaration_string *string_declaration = + string_definition->declaration; + struct ctf_stream_pos *pos = ctf_pos(ppos); size_t len; char *srcaddr; - ctf_align_pos(src, string_declaration->p.alignment); - srcaddr = ctf_get_pos_addr(src); + ctf_align_pos(pos, string_declaration->p.alignment); + srcaddr = ctf_get_pos_addr(pos); len = strlen(srcaddr) + 1; - *dest = g_realloc(*dest, len); - strcpy(*dest, srcaddr); - ctf_move_pos(src, len); + if (string_definition->alloc_len < len) { + string_definition->value = + g_realloc(string_definition->value, len); + string_definition->alloc_len = len; + } + memcpy(string_definition->value, srcaddr, len); + string_definition->len = len; + ctf_move_pos(pos, len); } -void ctf_string_write(struct stream_pos *pdest, const char *src, - const struct declaration_string *string_declaration) +void ctf_string_write(struct stream_pos *ppos, + struct definition *definition) { - struct ctf_stream_pos *dest = ctf_pos(pdest); + struct definition_string *string_definition = + container_of(definition, struct definition_string, p); + const struct declaration_string *string_declaration = + string_definition->declaration; + struct ctf_stream_pos *pos = ctf_pos(ppos); size_t len; char *destaddr; - ctf_align_pos(dest, string_declaration->p.alignment); - len = strlen(src) + 1; - if (dest->dummy) + ctf_align_pos(pos, string_declaration->p.alignment); + assert(string_definition->value != NULL); + len = string_definition->len; + if (pos->dummy) goto end; - destaddr = ctf_get_pos_addr(dest); - strcpy(destaddr, src); + destaddr = ctf_get_pos_addr(pos); + strcpy(destaddr, string_definition->value); end: - ctf_move_pos(dest, len); -} - -void ctf_string_free_temp(char *string) -{ - g_free(string); + ctf_move_pos(pos, len); } diff --git a/formats/ctf/types/struct.c b/formats/ctf/types/struct.c index 2a3fa4c5..7c0a249b 100644 --- a/formats/ctf/types/struct.c +++ b/formats/ctf/types/struct.c @@ -18,15 +18,11 @@ #include -void ctf_struct_begin(struct stream_pos *ppos, - const struct declaration_struct *struct_declaration) +void ctf_struct_rw(struct stream_pos *ppos, struct definition *definition) { + struct declaration *declaration = definition->declaration; struct ctf_stream_pos *pos = ctf_pos(ppos); - ctf_align_pos(pos, struct_declaration->p.alignment); -} - -void ctf_struct_end(struct stream_pos *ppos, - const struct declaration_struct *struct_declaration) -{ + ctf_align_pos(pos, declaration->alignment); + struct_rw(ppos, definition); } diff --git a/formats/ctf/types/variant.c b/formats/ctf/types/variant.c index 75dba56d..13b97cf7 100644 --- a/formats/ctf/types/variant.c +++ b/formats/ctf/types/variant.c @@ -18,12 +18,11 @@ #include -void ctf_variant_begin(struct stream_pos *pos, - const struct declaration_variant *variant_declaration) +void ctf_variant_rw(struct stream_pos *ppos, struct definition *definition) { -} + struct declaration *declaration = definition->declaration; + struct ctf_stream_pos *pos = ctf_pos(ppos); -void ctf_variant_end(struct stream_pos *pos, - const struct declaration_variant *variant_declaration) -{ + ctf_align_pos(pos, declaration->alignment); + variant_rw(ppos, definition); } diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index d47cab82..121beda5 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -69,60 +69,18 @@ struct ctf_stream_pos *ctf_pos(struct stream_pos *pos) * the size is returned. */ -uint64_t ctf_uint_read(struct stream_pos *pos, - const struct declaration_integer *integer_declaration); -int64_t ctf_int_read(struct stream_pos *pos, - const struct declaration_integer *integer_declaration); -void ctf_uint_write(struct stream_pos *pos, - const struct declaration_integer *integer_declaration, - uint64_t v); -void ctf_int_write(struct stream_pos *pos, - const struct declaration_integer *integer_declaration, - int64_t v); - -double ctf_double_read(struct stream_pos *pos, - const struct declaration_float *src); -void ctf_double_write(struct stream_pos *pos, - const struct declaration_float *dest, - double v); -long double ctf_ldouble_read(struct stream_pos *pos, - const struct declaration_float *src); -void ctf_ldouble_write(struct stream_pos *pos, - const struct declaration_float *dest, - long double v); -void ctf_float_copy(struct stream_pos *destp, - struct stream_pos *srcp, - const struct declaration_float *float_declaration); - -void ctf_string_copy(struct stream_pos *dest, struct stream_pos *src, - const struct declaration_string *string_declaration); -void ctf_string_read(char **dest, struct stream_pos *src, - const struct declaration_string *string_declaration); -void ctf_string_write(struct stream_pos *dest, const char *src, - const struct declaration_string *string_declaration); -void ctf_string_free_temp(char *string); - -GArray *ctf_enum_read(struct stream_pos *pos, - const struct declaration_enum *src); -void ctf_enum_write(struct stream_pos *pos, - const struct declaration_enum *dest, - GQuark q); -void ctf_struct_begin(struct stream_pos *pos, - const struct declaration_struct *struct_declaration); -void ctf_struct_end(struct stream_pos *pos, - const struct declaration_struct *struct_declaration); -void ctf_variant_begin(struct stream_pos *pos, - const struct declaration_variant *variant_declaration); -void ctf_variant_end(struct stream_pos *pos, - const struct declaration_variant *variant_declaration); -void ctf_array_begin(struct stream_pos *pos, - const struct declaration_array *array_declaration); -void ctf_array_end(struct stream_pos *pos, - const struct declaration_array *array_declaration); -void ctf_sequence_begin(struct stream_pos *pos, - const struct declaration_sequence *sequence_declaration); -void ctf_sequence_end(struct stream_pos *pos, - const struct declaration_sequence *sequence_declaration); +void ctf_integer_read(struct stream_pos *pos, struct definition *definition); +void ctf_integer_write(struct stream_pos *pos, struct definition *definition); +void ctf_float_read(struct stream_pos *pos, struct definition *definition); +void ctf_float_write(struct stream_pos *pos, struct definition *definition); +void ctf_string_read(struct stream_pos *pos, struct definition *definition); +void ctf_string_write(struct stream_pos *pos, struct definition *definition); +void ctf_enum_read(struct stream_pos *pos, struct definition *definition); +void ctf_enum_write(struct stream_pos *pos, struct definition *definition); +void ctf_struct_rw(struct stream_pos *pos, struct definition *definition); +void ctf_variant_rw(struct stream_pos *pos, struct definition *definition); +void ctf_array_rw(struct stream_pos *pos, struct definition *definition); +void ctf_sequence_rw(struct stream_pos *pos, struct definition *definition); void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset); diff --git a/include/babeltrace/format.h b/include/babeltrace/format.h index cbba50b0..877e8bb6 100644 --- a/include/babeltrace/format.h +++ b/include/babeltrace/format.h @@ -31,64 +31,6 @@ struct trace_descriptor { struct format { GQuark name; - uint64_t (*uint_read)(struct stream_pos *pos, - const struct declaration_integer *integer_declaration); - int64_t (*int_read)(struct stream_pos *pos, - const struct declaration_integer *integer_declaration); - void (*uint_write)(struct stream_pos *pos, - const struct declaration_integer *integer_declaration, - uint64_t v); - void (*int_write)(struct stream_pos *pos, - const struct declaration_integer *integer_declaration, - int64_t v); - - void (*float_copy)(struct stream_pos *destp, - struct stream_pos *srcp, - const struct declaration_float *float_declaration); - double (*double_read)(struct stream_pos *pos, - const struct declaration_float *float_declaration); - void (*double_write)(struct stream_pos *pos, - const struct declaration_float *float_declaration, - double v); - long double (*ldouble_read)(struct stream_pos *pos, - const struct declaration_float *float_declaration); - void (*ldouble_write)(struct stream_pos *pos, - const struct declaration_float *float_declaration, - long double v); - - void (*string_copy)(struct stream_pos *dest, struct stream_pos *src, - const struct declaration_string *string_declaration); - void (*string_read)(char **dest, struct stream_pos *src, - const struct declaration_string *string_declaration); - void (*string_write)(struct stream_pos *dest, const char *src, - const struct declaration_string *string_declaration); - void (*string_free_temp)(char *string); - - /* - * enum_read returns a GArray of GQuark. Must be released with - * g_array_unref(). - */ - GArray *(*enum_read)(struct stream_pos *pos, - const struct declaration_enum *src); - void (*enum_write)(struct stream_pos *pos, - const struct declaration_enum *dest, - GQuark q); - void (*struct_begin)(struct stream_pos *pos, - const struct declaration_struct *struct_declaration); - void (*struct_end)(struct stream_pos *pos, - const struct declaration_struct *struct_declaration); - void (*variant_begin)(struct stream_pos *pos, - const struct declaration_variant *variant_declaration); - void (*variant_end)(struct stream_pos *pos, - const struct declaration_variant *variant_declaration); - void (*array_begin)(struct stream_pos *pos, - const struct declaration_array *array_declaration); - void (*array_end)(struct stream_pos *pos, - const struct declaration_array *array_declaration); - void (*sequence_begin)(struct stream_pos *pos, - const struct declaration_sequence *sequence_declaration); - void (*sequence_end)(struct stream_pos *pos, - const struct declaration_sequence *sequence_declaration); struct trace_descriptor *(*open_trace)(const char *path, int flags); void (*close_trace)(struct trace_descriptor *descriptor); }; diff --git a/include/babeltrace/types.h b/include/babeltrace/types.h index 080ea6f1..c44ea2dd 100644 --- a/include/babeltrace/types.h +++ b/include/babeltrace/types.h @@ -35,10 +35,6 @@ struct stream_pos; struct format; struct definition; -/* Parent of per-plugin positions */ -struct stream_pos { -}; - /* type scope */ struct declaration_scope { /* Hash table mapping type name GQuark to "struct declaration" */ @@ -99,13 +95,6 @@ struct declaration { * 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 { @@ -114,6 +103,26 @@ struct definition { int ref; /* number of references to the definition */ }; +typedef void (*rw_dispatch)(struct stream_pos *pos, + struct definition *definition); + +/* Parent of per-plugin positions */ +struct stream_pos { + /* read/write dispatch table. Specific to plugin used for stream. */ + rw_dispatch *rw_table; /* rw dispatch table */ +}; + +static inline +void generic_rw(struct stream_pos *pos, struct definition *definition) +{ + enum ctf_type_id dispatch_id = definition->declaration->id; + rw_dispatch call; + + assert(pos->rw_table[dispatch_id] != NULL); + call = pos->rw_table[dispatch_id]; + call(pos, 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 @@ -148,6 +157,9 @@ struct declaration_float { struct definition_float { struct definition p; struct declaration_float *declaration; + struct definition_integer *sign; + struct definition_integer *mantissa; + struct definition_integer *exp; /* Last values read */ long double value; }; @@ -219,6 +231,7 @@ struct definition_string { struct definition p; struct declaration_string *declaration; char *value; /* freed at definition_string teardown */ + size_t len, alloc_len; }; struct declaration_field { @@ -424,6 +437,7 @@ struct_declaration_get_field_from_index(struct declaration_struct *struct_declar struct field * struct_definition_get_field_from_index(struct definition_struct *struct_definition, int index); +void struct_rw(struct stream_pos *pos, struct definition *definition); /* * The tag enumeration is validated to ensure that it contains only mappings @@ -452,6 +466,7 @@ int variant_definition_set_tag(struct definition_variant *variant, * to. */ struct field *variant_get_current_field(struct definition_variant *variant); +void variant_rw(struct stream_pos *pos, struct definition *definition); /* * elem_declaration passed as parameter now belongs to the array. No @@ -463,6 +478,7 @@ struct declaration_array * struct declaration_scope *parent_scope); uint64_t array_len(struct definition_array *array); struct definition *array_index(struct definition_array *array, uint64_t i); +void array_rw(struct stream_pos *pos, struct definition *definition); /* * int_declaration and elem_declaration passed as parameter now belong @@ -474,6 +490,7 @@ struct declaration_sequence * struct declaration_scope *parent_scope); uint64_t sequence_len(struct definition_sequence *sequence); struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i); +void sequence_rw(struct stream_pos *pos, struct definition *definition); /* * in: path (dot separated), out: q (GArray of GQuark) diff --git a/types/array.c b/types/array.c index ae994301..cd7b31bf 100644 --- a/types/array.c +++ b/types/array.c @@ -27,27 +27,20 @@ struct definition *_array_definition_new(struct declaration *declaration, static void _array_definition_free(struct definition *definition); -void array_copy(struct stream_pos *dest, const struct format *fdest, - struct stream_pos *src, const struct format *fsrc, - struct definition *definition) +void array_rw(struct stream_pos *pos, struct definition *definition) { - struct definition_array *array = + struct definition_array *array_definition = container_of(definition, struct definition_array, p); - struct declaration_array *array_declaration = array->declaration; + const struct declaration_array *array_declaration = + array_definition->declaration; uint64_t i; - fsrc->array_begin(src, array_declaration); - if (fdest) - fdest->array_begin(dest, array_declaration); - + /* No need to align, because the first field will align itself. */ for (i = 0; i < array_declaration->len; i++) { struct definition *elem = - g_array_index(array->elems, struct field, i).definition; - elem->declaration->copy(dest, fdest, src, fsrc, elem); + g_array_index(array_definition->elems, struct field, i).definition; + generic_rw(pos, elem); } - fsrc->array_end(src, array_declaration); - if (fdest) - fdest->array_end(dest, array_declaration); } static @@ -78,7 +71,6 @@ struct declaration_array * declaration->id = CTF_TYPE_ARRAY; /* No need to align the array, the first element will align itself */ declaration->alignment = 1; - declaration->copy = array_copy; declaration->declaration_free = _array_declaration_free; declaration->definition_new = _array_definition_new; declaration->definition_free = _array_definition_free; diff --git a/types/enum.c b/types/enum.c index 368c21a6..5e192311 100644 --- a/types/enum.c +++ b/types/enum.c @@ -349,32 +349,6 @@ size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration) return g_hash_table_size(enum_declaration->table.quark_to_range_set); } -void enum_copy(struct stream_pos *dest, const struct format *fdest, - struct stream_pos *src, const struct format *fsrc, - struct definition *definition) -{ - struct definition_enum *_enum = - container_of(definition, struct definition_enum, p); - struct declaration_enum *enum_declaration= _enum->declaration; - GArray *array; - GQuark v; - - array = fsrc->enum_read(src, enum_declaration); - assert(array); - /* unref previous array */ - if (_enum->value) - g_array_unref(_enum->value); - _enum->value = array; - /* - * Arbitrarily choose the first one. - * TODO: use direct underlying declaration read/write intead. Not doing it for - * now to test enum read and write code. - */ - v = g_array_index(array, GQuark, 0); - if (fdest) - fdest->enum_write(dest, enum_declaration, v); -} - static void _enum_declaration_free(struct declaration *declaration) { @@ -411,7 +385,6 @@ struct declaration_enum * enum_declaration->integer_declaration = integer_declaration; enum_declaration->p.id = CTF_TYPE_ENUM; enum_declaration->p.alignment = 1; - enum_declaration->p.copy = enum_copy; enum_declaration->p.declaration_free = _enum_declaration_free; enum_declaration->p.definition_new = _enum_definition_new; enum_declaration->p.definition_free = _enum_definition_free; diff --git a/types/float.c b/types/float.c index 9113bb8c..a5917beb 100644 --- a/types/float.c +++ b/types/float.c @@ -26,23 +26,6 @@ struct definition *_float_definition_new(struct declaration *declaration, static void _float_definition_free(struct definition *definition); -void float_copy(struct stream_pos *destp, - const struct format *fdest, - struct stream_pos *srcp, - const struct format *fsrc, - struct definition *definition) -{ - struct definition_float *_float = - container_of(definition, struct definition_float, p); - struct declaration_float *float_declaration = _float->declaration; - long double v; - - v = fsrc->ldouble_read(srcp, float_declaration); - _float->value = v; - if (fdest) - fdest->ldouble_write(destp, float_declaration, v); -} - static void _float_declaration_free(struct declaration *declaration) { @@ -66,7 +49,6 @@ struct declaration_float * declaration = &float_declaration->p; declaration->id = CTF_TYPE_FLOAT; declaration->alignment = alignment; - declaration->copy = float_copy; declaration->declaration_free = _float_declaration_free; declaration->definition_new = _float_definition_new; declaration->definition_free = _float_definition_free; @@ -91,11 +73,33 @@ struct definition * struct declaration_float *float_declaration = container_of(declaration, struct declaration_float, p); struct definition_float *_float; + struct definition *tmp; _float = g_new(struct definition_float, 1); declaration_ref(&float_declaration->p); _float->p.declaration = declaration; _float->declaration = float_declaration; + if (float_declaration->byte_order == LITTLE_ENDIAN) { + tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p, + parent_scope, g_quark_from_static_string("mantissa"), 0); + _float->mantissa = container_of(tmp, struct definition_integer, p); + tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p, + parent_scope, g_quark_from_static_string("exp"), 1); + _float->exp = container_of(tmp, struct definition_integer, p); + tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p, + parent_scope, g_quark_from_static_string("sign"), 2); + _float->sign = container_of(tmp, struct definition_integer, p); + } else { + tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p, + parent_scope, g_quark_from_static_string("sign"), 0); + _float->sign = container_of(tmp, struct definition_integer, p); + tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p, + parent_scope, g_quark_from_static_string("exp"), 1); + _float->exp = container_of(tmp, struct definition_integer, p); + tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p, + parent_scope, g_quark_from_static_string("mantissa"), 2); + _float->mantissa = container_of(tmp, struct definition_integer, p); + } _float->p.ref = 1; _float->p.index = index; _float->value = 0.0; @@ -108,6 +112,9 @@ void _float_definition_free(struct definition *definition) struct definition_float *_float = container_of(definition, struct definition_float, p); + definition_unref(&_float->sign->p); + definition_unref(&_float->exp->p); + definition_unref(&_float->mantissa->p); declaration_unref(_float->p.declaration); g_free(_float); } diff --git a/types/integer.c b/types/integer.c index 254dcbcb..4f79c4ab 100644 --- a/types/integer.c +++ b/types/integer.c @@ -28,31 +28,6 @@ struct definition *_integer_definition_new(struct declaration *declaration, static void _integer_definition_free(struct definition *definition); -void integer_copy(struct stream_pos *dest, const struct format *fdest, - struct stream_pos *src, const struct format *fsrc, - struct definition *definition) -{ - struct definition_integer *integer = - container_of(definition, struct definition_integer, p); - struct declaration_integer *integer_declaration = integer->declaration; - - if (!integer_declaration->signedness) { - uint64_t v; - - v = fsrc->uint_read(src, integer_declaration); - integer->value._unsigned = v; - if (fdest) - fdest->uint_write(dest, integer_declaration, v); - } else { - int64_t v; - - v = fsrc->int_read(src, integer_declaration); - integer->value._signed = v; - if (fdest) - fdest->int_write(dest, integer_declaration, v); - } -} - static void _integer_declaration_free(struct declaration *declaration) { @@ -70,7 +45,6 @@ struct declaration_integer * integer_declaration = g_new(struct declaration_integer, 1); integer_declaration->p.id = CTF_TYPE_INTEGER; integer_declaration->p.alignment = alignment; - integer_declaration->p.copy = integer_copy; integer_declaration->p.declaration_free = _integer_declaration_free; integer_declaration->p.definition_free = _integer_definition_free; integer_declaration->p.definition_new = _integer_definition_new; diff --git a/types/sequence.c b/types/sequence.c index e3586d38..61a5b80d 100644 --- a/types/sequence.c +++ b/types/sequence.c @@ -31,23 +31,16 @@ struct definition *_sequence_definition_new(struct declaration *declaration, static void _sequence_definition_free(struct definition *definition); -void sequence_copy(struct stream_pos *dest, const struct format *fdest, - struct stream_pos *src, const struct format *fsrc, - struct definition *definition) +void sequence_rw(struct stream_pos *pos, struct definition *definition) { - struct definition_sequence *sequence = + struct definition_sequence *sequence_definition = container_of(definition, struct definition_sequence, p); - struct declaration_sequence *sequence_declaration = sequence->declaration; + const struct declaration_sequence *sequence_declaration = + sequence_definition->declaration; uint64_t len, oldlen, i; - fsrc->sequence_begin(src, sequence_declaration); - if (fdest) - fdest->sequence_begin(dest, sequence_declaration); - - sequence->len->p.declaration->copy(dest, fdest, src, fsrc, - &sequence->len->p); - len = sequence->len->value._unsigned; - g_array_set_size(sequence->elems, len); + generic_rw(pos, &sequence_definition->len->p); + len = sequence_definition->len->value._unsigned; /* * Yes, large sequences could be _painfully slow_ to parse due * to memory allocation for each event read. At least, never @@ -56,9 +49,9 @@ void sequence_copy(struct stream_pos *dest, const struct format *fdest, * One should always look at the sequence->len->value._unsigned * value for that. */ - oldlen = sequence->elems->len; + oldlen = sequence_definition->elems->len; if (oldlen < len) - g_array_set_size(sequence->elems, len); + g_array_set_size(sequence_definition->elems, len); for (i = oldlen; i < len; i++) { struct field *field; @@ -70,16 +63,13 @@ void sequence_copy(struct stream_pos *dest, const struct format *fdest, (void) g_string_free(str, TRUE); name = g_quark_from_string(str->str); - field = &g_array_index(sequence->elems, struct field, i); + field = &g_array_index(sequence_definition->elems, struct field, i); field->name = name; field->definition = sequence_declaration->elem->definition_new(sequence_declaration->elem, - sequence->scope, + sequence_definition->scope, name, i); - field->definition->declaration->copy(dest, fdest, src, fsrc, field->definition); + generic_rw(pos, field->definition); } - fsrc->sequence_end(src, sequence_declaration); - if (fdest) - fdest->sequence_end(dest, sequence_declaration); } static @@ -112,7 +102,6 @@ struct declaration_sequence * sequence_declaration->scope = new_declaration_scope(parent_scope); declaration->id = CTF_TYPE_SEQUENCE; declaration->alignment = max(len_declaration->p.alignment, elem_declaration->alignment); - declaration->copy = sequence_copy; declaration->declaration_free = _sequence_declaration_free; declaration->definition_new = _sequence_definition_new; declaration->definition_free = _sequence_definition_free; diff --git a/types/string.c b/types/string.c index 18d4c919..a83a1541 100644 --- a/types/string.c +++ b/types/string.c @@ -27,19 +27,6 @@ struct definition *_string_definition_new(struct declaration *declaration, static void _string_definition_free(struct definition *definition); -void string_copy(struct stream_pos *dest, const struct format *fdest, - struct stream_pos *src, const struct format *fsrc, - struct definition *definition) -{ - struct definition_string *string = - container_of(definition, struct definition_string, p); - struct declaration_string *string_declaration = string->declaration; - - fsrc->string_read(&string->value, src, string_declaration); - if (fdest) - fdest->string_write(dest, string->value, string_declaration); -} - static void _string_declaration_free(struct declaration *declaration) { @@ -56,7 +43,6 @@ struct declaration_string * string_declaration = g_new(struct declaration_string, 1); string_declaration->p.id = CTF_TYPE_STRING; string_declaration->p.alignment = CHAR_BIT; - string_declaration->p.copy = string_copy; string_declaration->p.declaration_free = _string_declaration_free; string_declaration->p.definition_new = _string_definition_new; string_declaration->p.definition_free = _string_definition_free; @@ -82,6 +68,8 @@ struct definition * string->p.ref = 1; string->p.index = index; string->value = NULL; + string->len = 0; + string->alloc_len = 0; return &string->p; } diff --git a/types/struct.c b/types/struct.c index ea2e28c1..211d1b02 100644 --- a/types/struct.c +++ b/types/struct.c @@ -30,30 +30,17 @@ struct definition *_struct_definition_new(struct declaration *declaration, static void _struct_definition_free(struct definition *definition); -void struct_copy(struct stream_pos *dest, const struct format *fdest, - struct stream_pos *src, const struct format *fsrc, - struct definition *definition) +void struct_rw(struct stream_pos *ppos, struct definition *definition) { - struct definition_struct *_struct = + struct definition_struct *struct_definition = container_of(definition, struct definition_struct, p); - struct declaration_struct *struct_declaration = _struct->declaration; unsigned long i; - fsrc->struct_begin(src, struct_declaration); - if (fdest) - fdest->struct_begin(dest, struct_declaration); - - for (i = 0; i < _struct->fields->len; i++) { - struct field *field = &g_array_index(_struct->fields, + for (i = 0; i < struct_definition->fields->len; i++) { + struct field *field = &g_array_index(struct_definition->fields, struct field, i); - struct declaration *field_declaration = field->definition->declaration; - - field_declaration->copy(dest, fdest, src, fsrc, field->definition); - + generic_rw(ppos, field->definition); } - fsrc->struct_end(src, struct_declaration); - if (fdest) - fdest->struct_end(dest, struct_declaration); } static @@ -92,7 +79,6 @@ struct declaration_struct * struct_declaration->scope = new_declaration_scope(parent_scope); declaration->id = CTF_TYPE_STRUCT; declaration->alignment = 1; - declaration->copy = struct_copy; declaration->declaration_free = _struct_declaration_free; declaration->definition_new = _struct_definition_new; declaration->definition_free = _struct_definition_free; diff --git a/types/variant.c b/types/variant.c index 1af0c790..460b2fb2 100644 --- a/types/variant.c +++ b/types/variant.c @@ -27,27 +27,14 @@ struct definition *_variant_definition_new(struct declaration *declaration, static void _variant_definition_free(struct definition *definition); -void variant_copy(struct stream_pos *dest, const struct format *fdest, - struct stream_pos *src, const struct format *fsrc, - struct definition *definition) +void variant_rw(struct stream_pos *ppos, struct definition *definition) { - struct definition_variant *variant = + struct definition_variant *variant_definition = container_of(definition, struct definition_variant, p); - struct declaration_variant *variant_declaration = variant->declaration; struct field *field; - struct declaration *field_declaration; - - fsrc->variant_begin(src, variant_declaration); - if (fdest) - fdest->variant_begin(dest, variant_declaration); - - field = variant_get_current_field(variant); - field_declaration = field->definition->declaration; - field_declaration->copy(dest, fdest, src, fsrc, field->definition); - fsrc->variant_end(src, variant_declaration); - if (fdest) - fdest->variant_end(dest, variant_declaration); + field = variant_get_current_field(variant_definition); + generic_rw(ppos, field->definition); } static @@ -86,7 +73,6 @@ struct declaration_untagged_variant *untagged_variant_declaration_new( untagged_variant_declaration->scope = new_declaration_scope(parent_scope); declaration->id = CTF_TYPE_UNTAGGED_VARIANT; declaration->alignment = 1; - declaration->copy = NULL; declaration->declaration_free = _untagged_variant_declaration_free; declaration->definition_new = NULL; declaration->definition_free = NULL; @@ -117,7 +103,6 @@ struct declaration_variant * append_scope_path(tag, variant_declaration->tag_name); declaration->id = CTF_TYPE_VARIANT; declaration->alignment = 1; - declaration->copy = variant_copy; declaration->declaration_free = _variant_declaration_free; declaration->definition_new = _variant_definition_new; declaration->definition_free = _variant_definition_free; -- 2.34.1