From: Mathieu Desnoyers Date: Mon, 4 Oct 2010 18:57:09 +0000 (-0400) Subject: Remove bitfields, which are just a bit-addressed integer X-Git-Tag: v0.1~224 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=7fe001942cc8ece60d945cbfbd1d135ff548dc7d Remove bitfields, which are just a bit-addressed integer Because we can directly map bitfields to non-byte-aligned integers, we don't need to have a separate class type for bitfields. Merge them with integers. We can eventually use faster read/write functions for integers aligned on byte boundaries and which length is a multiple of 1 byte. Signed-off-by: Mathieu Desnoyers --- diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 9a0e003a..eec36148 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -30,10 +30,6 @@ static const struct format ctf_format = { .int_read = ctf_int_read, .uint_write = ctf_uint_write, .int_write = ctf_int_write, - .bitfield_unsigned_read = ctf_bitfield_unsigned_read, - .bitfield_signed_read = ctf_bitfield_signed_read, - .bitfield_unsigned_write = ctf_bitfield_unsigned_write, - .bitfield_signed_write = ctf_bitfield_signed_write, .double_read = ctf_double_read, .double_write = ctf_double_write, .float_copy = ctf_float_copy, diff --git a/formats/ctf/types/bitfield.c b/formats/ctf/types/bitfield.c deleted file mode 100644 index 2d7a2ae0..00000000 --- a/formats/ctf/types/bitfield.c +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Common Trace Format - * - * Bitfields read/write functions. - * - * Copyright (c) 2010 Mathieu Desnoyers - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include - -uint64_t ctf_bitfield_unsigned_read(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class) -{ - uint64_t v; - - align_pos(pos, bitfield_class->p.p.alignment); - if (bitfield_class->p.byte_order == LITTLE_ENDIAN) - ctf_bitfield_read_le(pos->base, pos->offset, - bitfield_class->p.len, &v); - else - ctf_bitfield_read_be(pos->base, pos->offset, - bitfield_class->p.len, &v); - move_pos(pos, bitfield_class->p.len); - return v; -} - -int64_t ctf_bitfield_signed_read(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class) -{ - int64_t v; - - align_pos(pos, bitfield_class->p.p.alignment); - - if (bitfield_class->p.byte_order == LITTLE_ENDIAN) - ctf_bitfield_read_le(pos->base, pos->offset, - bitfield_class->p.len, &v); - else - ctf_bitfield_read_be(pos->base, pos->offset, - bitfield_class->p.len, &v); - move_pos(pos, bitfield_class->p.len); - return v; -} - -void ctf_bitfield_unsigned_write(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class, - uint64_t v) -{ - align_pos(pos, bitfield_class->p.p.alignment); - if (pos->dummy) - goto end; - if (bitfield_class->p.byte_order == LITTLE_ENDIAN) - ctf_bitfield_write_le(pos->base, pos->offset, - bitfield_class->p.len, v); - else - ctf_bitfield_write_be(pos->base, pos->offset, - bitfield_class->p.len,, v); -end: - move_pos(pos, bitfield_class->p.len); -} - -void ctf_bitfield_signed_write(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class, - int64_t v) -{ - align_pos(pos, bitfield_class->p.p.alignment); - if (pos->dummy) - goto end; - if (bitfield_class->p.byte_order == LITTLE_ENDIAN) - ctf_bitfield_write_le(pos->base, pos->offset, - bitfield_class->p.len, v); - else - ctf_bitfield_write_be(pos->base, pos->offset, - bitfield_class->p.len, v); -end: - move_pos(pos, bitfield_class->p.len); -} diff --git a/formats/ctf/types/enum.c b/formats/ctf/types/enum.c index a209b47d..37859f03 100644 --- a/formats/ctf/types/enum.c +++ b/formats/ctf/types/enum.c @@ -27,18 +27,17 @@ GQuark ctf_enum_read(struct stream_pos *pos, const struct type_class_enum *src) { - struct type_class_bitfield *bitfield_class = &src->p; - struct type_class_integer *int_class = &bitfield_class->p; + struct type_class_integer *int_class = &src->p; if (!int_class->signedness) { uint64_t v; - v = ctf_bitfield_unsigned_read(pos, bitfield_class); + v = ctf_uint_read(pos, int_class); return enum_uint_to_quark(src, v); } else { int64_t v; - v = fsrc->bitfield_signed_read(pos, bitfield_class); + v = fsrc->ctf_int_read(pos, int_class); return enum_int_to_quark(src, v); } } @@ -47,18 +46,17 @@ size_t ctf_enum_write(struct stream_pos *pos, const struct type_class_enum *dest, GQuark q) { - struct type_class_bitfield *bitfield_class = &dest->p; - struct type_class_integer *int_class = &bitfield_class->p; + struct type_class_integer *int_class = &dest->p; if (!int_class->signedness) { uint64_t v; v = enum_quark_to_uint(dest, q); - return ctf_bitfield_unsigned_write(pos, bitfield_class, v); + return ctf_uint_write(pos, int_class, v); } else { int64_t v; v = enum_quark_to_int(dest, q); - return ctf_bitfield_signed_write(pos, bitfield_class, v); + return ctf_int_write(pos, int_class, v); } } diff --git a/formats/ctf/types/float.c b/formats/ctf/types/float.c index 20cf7fa5..6fd0c72e 100644 --- a/formats/ctf/types/float.c +++ b/formats/ctf/types/float.c @@ -83,27 +83,23 @@ void _ctf_float_copy(struct stream_pos *destp, /* Read */ if (src->byte_order == LITTLE_ENDIAN) { - mantissa = ctf_bitfield_unsigned_read(srcp, - src_class->mantissa); - exp = ctf_bitfield_signed_read(srcp, src_class->exp); - sign = ctf_bitfield_unsigned_read(srcp, src_class->sign); + mantissa = ctf_uint_read(srcp, src_class->mantissa); + exp = ctf_int_read(srcp, src_class->exp); + sign = ctf_uint_read(srcp, src_class->sign); } else { - sign = ctf_bitfield_unsigned_read(srcp, src_class->sign); - exp = ctf_bitfield_signed_read(srcp, src_class->exp); - mantissa = ctf_bitfield_unsigned_read(srcp, - src_class->mantissa); + sign = ctf_uint_read(srcp, src_class->sign); + exp = ctf_int_read(srcp, src_class->exp); + mantissa = ctf_uint_read(srcp, src_class->mantissa); } /* Write */ if (dest->byte_order == LITTLE_ENDIAN) { - ctf_bitfield_unsigned_write(destp, dest_class->mantissa, - mantissa); - ctf_bitfield_signed_write(destp, dest_class->exp, exp); - ctf_bitfield_unsigned_write(destp, dest_class->sign, sign); + ctf_uint_write(destp, dest_class->mantissa, mantissa); + ctf_int_write(destp, dest_class->exp, exp); + ctf_uint_write(destp, dest_class->sign, sign); } else { - ctf_bitfield_unsigned_write(destp, dest_class->sign, sign); - ctf_bitfield_signed_write(destp, dest_class->exp, exp); - ctf_bitfield_unsigned_write(destp, dest_class->mantissa, - mantissa); + ctf_uint_write(destp, dest_class->sign, sign); + ctf_int_write(destp, dest_class->exp, exp); + ctf_uint_write(destp, dest_class->mantissa, mantissa); } } diff --git a/formats/ctf/types/integer.c b/formats/ctf/types/integer.c index b2c429f5..f7f5f23d 100644 --- a/formats/ctf/types/integer.c +++ b/formats/ctf/types/integer.c @@ -21,11 +21,13 @@ */ #include +#include #include #include #include -uint64_t ctf_uint_read(struct stream_pos *pos, +static +uint64_t _aligned_uint_read(struct stream_pos *pos, const struct type_class_integer *int_class) { int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */ @@ -70,7 +72,8 @@ uint64_t ctf_uint_read(struct stream_pos *pos, } } -int64_t ctf_int_read(struct stream_pos *pos, +static +int64_t _aligned_int_read(struct stream_pos *pos, const struct type_class_integer *int_class) { int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */ @@ -115,7 +118,8 @@ int64_t ctf_int_read(struct stream_pos *pos, } } -void ctf_uint_write(struct stream_pos *pos, +static +void _aligned_uint_write(struct stream_pos *pos, const struct type_class_integer *int_class, uint64_t v) { @@ -150,7 +154,8 @@ end: move_pos(pos, int_class->len); } -void ctf_int_write(struct stream_pos *pos, +static +void _aligned_int_write(struct stream_pos *pos, const struct type_class_integer *int_class, int64_t v) { @@ -185,3 +190,69 @@ end: move_pos(pos, int_class->len); return; } + +uint64_t ctf_uint_read(struct stream_pos *pos, + const struct type_class_bitfield *int_class) +{ + uint64_t v; + + align_pos(pos, int_class->p.alignment); + if (int_class->byte_order == LITTLE_ENDIAN) + ctf_bitfield_read_le(pos->base, pos->offset, + int_class->len, &v); + else + ctf_bitfield_read_be(pos->base, pos->offset, + int_class->len, &v); + move_pos(pos, int_class->len); + return v; +} + +int64_t ctf_int_read(struct stream_pos *pos, + const struct type_class_bitfield *int_class) +{ + int64_t v; + + align_pos(pos, int_class->p.alignment); + if (int_class->byte_order == LITTLE_ENDIAN) + ctf_bitfield_read_le(pos->base, pos->offset, + int_class->len, &v); + else + ctf_bitfield_read_be(pos->base, pos->offset, + int_class->len, &v); + move_pos(pos, int_class->len); + return v; +} + +void ctf_uint_write(struct stream_pos *pos, + const struct type_class_bitfield *int_class, + uint64_t v) +{ + align_pos(pos, int_class->p.alignment); + if (pos->dummy) + goto end; + if (int_class->byte_order == LITTLE_ENDIAN) + ctf_bitfield_write_le(pos->base, pos->offset, + int_class->len, v); + else + ctf_bitfield_write_be(pos->base, pos->offset, + int_class->len,, v); +end: + move_pos(pos, int_class->len); +} + +void ctf_int_write(struct stream_pos *pos, + const struct type_class_bitfield *int_class, + int64_t v) +{ + align_pos(pos, int_class->p.alignment); + if (pos->dummy) + goto end; + if (int_class->byte_order == LITTLE_ENDIAN) + ctf_bitfield_write_le(pos->base, pos->offset, + int_class->len, v); + else + ctf_bitfield_write_be(pos->base, pos->offset, + int_class->len, v); +end: + move_pos(pos, int_class->len); +} diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index 40b89847..49427a5b 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -46,17 +46,6 @@ void ctf_int_write(struct stream_pos *pos, const struct type_class_integer *int_class, int64_t v); -uint64_t ctf_bitfield_unsigned_read(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class); -int64_t ctf_bitfield_signed_read(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class); -void ctf_bitfield_unsigned_write(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class, - uint64_t v); -void ctf_bitfield_signed_write(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class, - int64_t v); - double ctf_double_read(struct stream_pos *pos, const struct type_class_float *src); void ctf_double_write(struct stream_pos *pos, diff --git a/include/babeltrace/format.h b/include/babeltrace/format.h index 53919729..5909ce39 100644 --- a/include/babeltrace/format.h +++ b/include/babeltrace/format.h @@ -40,17 +40,6 @@ struct format { const struct type_class_integer *int_class, int64_t v); - uint64_t (*bitfield_unsigned_read)(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class); - int64_t (*bitfield_signed_read)(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class); - void (*bitfield_unsigned_write)(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class, - uint64_t v); - void (*bitfield_signed_write)(struct stream_pos *pos, - const struct type_class_bitfield *bitfield_class, - int64_t v); - void (*float_copy)(struct stream_pos *dest, struct stream_pos *src, const struct type_class_float *src); diff --git a/include/babeltrace/types.h b/include/babeltrace/types.h index 3c66563f..305d553d 100644 --- a/include/babeltrace/types.h +++ b/include/babeltrace/types.h @@ -93,27 +93,23 @@ struct type_class { void (*free)(struct type_class *type_class); }; -struct type_class_integer { - struct type_class p; - size_t len; /* length, in bits. */ - int byte_order; /* byte order */ - int signedness; -}; - /* * 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 type_class_bitfield { - struct type_class_integer p; +struct type_class_integer { + struct type_class p; + size_t len; /* length, in bits. */ + int byte_order; /* byte order */ + int signedness; }; struct type_class_float { struct type_class p; - struct bitfield_class *sign; - struct bitfield_class *mantissa; - struct bitfield_class *exp; + struct int_class *sign; + struct int_class *mantissa; + struct int_class *exp; int byte_order; /* TODO: we might want to express more info about NaN, +inf and -inf */ }; @@ -124,7 +120,7 @@ struct enum_table { }; struct type_class_enum { - struct type_class_bitfield p; /* inherit from bitfield */ + struct type_class_int p; /* inherit from integer */ struct enum_table table; }; @@ -148,12 +144,6 @@ struct type_class_integer *integer_type_new(const char *name, size_t alignment); void integer_type_free(struct type_class_integer *int_class); -struct type_class_bitfield *bitfield_type_new(const char *name, - size_t len, int byte_order, - int signedness, - size_t alignment); -void bitfield_type_free(struct type_class_bitfield *bitfield_class); - /* * mantissa_len is the length of the number of bytes represented by the mantissa * (e.g. result of DBL_MANT_DIG). It includes the leading 1. diff --git a/types/bitfield.c b/types/bitfield.c deleted file mode 100644 index 69774877..00000000 --- a/types/bitfield.c +++ /dev/null @@ -1,84 +0,0 @@ -/* - * BabelTrace - Bitfield Type Converter - * - * Copyright (c) 2010 Mathieu Desnoyers - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include -#include - -size_t bitfield_copy(struct stream_pos *dest, const struct format *fdest, - struct stream_pos *src, const struct format *fsrc, - const struct type_class *type_class) -{ - struct type_class_bitfield *bitfield_class = - container_of(type_class, struct type_class_bitfield, p); - struct type_class_integer *int_class = &bitfield_class->p; - - if (!int_class->signedness) { - uint64_t v; - - v = fsrc->bitfield_unsigned_read(src, bitfield_class); - return fdest->bitfield_unsigned_write(dest, bitfield_class, v); - } else { - int64_t v; - - v = fsrc->bitfield_signed_read(src, bitfield_class); - return fdest->bitfield_signed_write(dest, bitfield_class, v); - } -} - -void bitfield_type_free(struct type_class_bitfield *bitfield_class) -{ - g_free(bitfield_class); -} - -static void _bitfield_type_free(struct type_class *type_class) -{ - struct type_class_bitfield *bitfield_class = - container_of(type_class, struct type_class_bitfield, p); - bitfield_type_free(bitfield_class); -} - -struct type_class_bitfield *bitfield_type_new(const char *name, - size_t len, int byte_order, - int signedness, - size_t alignment) -{ - struct type_class_bitfield *bitfield_class; - struct type_class_integer *int_class; - int ret; - - bitfield_class = g_new(struct type_class_bitfield, 1); - int_class = &bitfield_class->p; - int_class->p.name = g_quark_from_string(name); - int_class->p.alignment = alignment; - int_class->p.copy = bitfield_copy; - int_class->p.free = _bitfield_type_free; - int_class->len = len; - int_class->byte_order = byte_order; - int_class->signedness = signedness; - if (int_class->p.name) { - ret = ctf_register_type(&int_class->p); - if (ret) { - g_free(bitfield_class); - return NULL; - } - } - return bitfield_class; -} diff --git a/types/enum.c b/types/enum.c index 354603c4..8e8ef694 100644 --- a/types/enum.c +++ b/types/enum.c @@ -172,8 +172,7 @@ size_t enum_copy(unsigned char *dest, const struct format *fdest, { struct type_class_enum *enum_class = container_of(type_class, struct type_class_enum, p); - struct type_class_bitfield *bitfield_class = &enum_class->p; - struct type_class_integer *int_class = &bitfield_class->p; + struct type_class_integer *int_class = &enum_class->p; GQuark v; v = fsrc->enum_read(src, enum_class) @@ -200,18 +199,16 @@ struct type_class_enum *enum_type_new(const char *name, int signedness, size_t alignment) { - struct type_class_bitfield *bitfield_class; struct type_class_integer *int_class; int ret; - enum_class = g_new(struct type_class_bitfield, 1); + enum_class = g_new(struct type_class_enum, 1); enum_class->table.value_to_quark = g_hash_table(enum_val_hash, enum_val_equal); enum_class->table.quark_to_value = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, enum_val_free); - bitfield_class = &enum_class->p; - int_class = &bitfield_class->p; + int_class = &enum_class->p; int_class->p.name = g_quark_from_string(name); int_class->p.alignment = alignment; int_class->p.copy = enum_copy; diff --git a/types/float.c b/types/float.c index 5c2e7f8d..266f7579 100644 --- a/types/float.c +++ b/types/float.c @@ -56,7 +56,6 @@ struct type_class_float *float_type_new(const char *name, size_t alignment) { struct type_class_float *float_class; - struct type_class_bitfield *bitfield_class; struct type_class_integer *int_class; struct type_class *type_class; int ret; @@ -70,16 +69,16 @@ struct type_class_float *float_type_new(const char *name, type_class->free = _float_type_free; float_class->byte_order = byte_order; - float_class->sign = bitfield_type_new(NULL, 1, - byte_order, false, 1); + float_class->sign = integer_type_new(NULL, 1, + byte_order, false, 1); if (!float_class->mantissa) goto error_sign; - float_class->mantissa = bitfield_type_new(NULL, mantissa_len - 1, - byte_order, false, 1); + float_class->mantissa = integer_type_new(NULL, mantissa_len - 1, + byte_order, false, 1); if (!float_class->mantissa) goto error_mantissa; - float_class->exp = bitfield_type_new(NULL, exp_len, - byte_order, true, 1); + float_class->exp = integer_type_new(NULL, exp_len, + byte_order, true, 1); if (!float_class->exp) goto error_exp; @@ -91,11 +90,11 @@ struct type_class_float *float_type_new(const char *name, return float_class; error_register: - bitfield_type_free(float_class->exp); + integer_type_free(float_class->exp); error_exp: - bitfield_type_free(float_class->mantissa); + integer_type_free(float_class->mantissa); error_mantissa: - bitfield_type_free(float_class->sign); + integer_type_free(float_class->sign); error_sign: g_free(float_class); return NULL;