Add float type class to babeltrace types
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 30 Sep 2010 17:20:18 +0000 (13:20 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 30 Sep 2010 17:20:18 +0000 (13:20 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
formats/ctf/ctf.c
include/babeltrace/ctf/types.h
include/babeltrace/format.h
include/babeltrace/types.h
types/bitfield.c
types/float.c [new file with mode: 0644]
types/integer.c

index de760a83c1a48944d839fc1337d3e48b7ddf2a42..0f0d69211964175aee368a6ce1f50d00282b3daf 100644 (file)
@@ -34,6 +34,8 @@ static const struct format ctf_format = {
        .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,
        .string_copy = ctf_string_copy,
        .enum_uint_to_quark = ctf_enum_uint_to_quark,
index 894dda24bdd479882d4546777c5b45bfb533675f..2d06d6a58bde0ae7551bbd0618bc87364f2c38ac 100644 (file)
@@ -53,11 +53,11 @@ size_t ctf_bitfield_signed_write(unsigned char *ptr,
                                 unsigned long start, unsigned long len,
                                 int byte_order, int64_t v);
 
-double ctf_double_read(const unsigned char *ptr, const struct type_class_float *src)
+double ctf_double_read(const unsigned char *ptr, const struct type_class_float *src);
 size_t ctf_double_write(unsigned char *ptr, const struct type_class_float *dest,
                        double v);
 long double ctf_ldouble_read(const unsigned char *ptr,
-                            const struct type_class_float *src)
+                            const struct type_class_float *src);
 size_t ctf_ldouble_write(unsigned char *ptr, const struct type_class_float *dest,
                         long double v);
 void ctf_float_copy(unsigned char *destp, const struct type_class_float *dest,
index 4981f122f4f69ba66cabe5703c5f13b375ec8029..d31ee831bf15198cd1a2d21efb19d7bda9cd14f3 100644 (file)
@@ -50,6 +50,10 @@ struct format {
 
        void (*float_copy)(unsigned char *destp, const struct type_class_float *dest,
                    const unsigned char *srcp, const struct type_class_float *src);
+       double (*double_read)(const unsigned char *ptr, const struct type_class_float *src);
+       size_t (*double_write)(unsigned char *ptr, const struct type_class_float *dest,
+                       double v);
+
 
        size_t (*string_copy)(unsigned char *dest, const unsigned char *src);
 
index ed3e7aca60fa8caba4085a12336f378d4e4afc76..b77ac4002017dbb3a7ed0c50fa00b374fbfe23f2 100644 (file)
@@ -44,22 +44,17 @@ struct type_class_integer {
        int signedness;
 };
 
-int integer_type_new(const char *name, size_t len, int byte_order,
-                    int signedness);
-
 struct type_class_bitfield {
        struct type_class_integer p;
        size_t start_offset;    /* offset from base address, in bits */
 };
 
-int bitfield_type_new(const char *name, size_t start_offset,
-                     size_t len, int byte_order, int signedness);
-
 struct type_class_float {
        struct type_class p;
        size_t mantissa_len;
        size_t exp_len;
        int byte_order;
+       /* TODO: we might want to express more info about NaN, +inf and -inf */
 };
 
 struct type_class_enum {
@@ -75,4 +70,24 @@ struct type_class_struct {
 struct type_class *ctf_lookup_type(GQuark qname);
 int ctf_register_type(struct type_class *type_class);
 
+/* Nameless types can be created by passing a NULL name */
+
+struct type_class_integer *integer_type_new(const char *name,
+                                           size_t start_offset,
+                                           size_t len, int byte_order,
+                                           int signedness);
+void integer_type_free(struct type_class_integer *int_class);
+
+struct type_class_bitfield *bitfield_type_new(const char *name,
+                                             size_t start_offset,
+                                             size_t len, int byte_order,
+                                             int signedness);
+void bitfield_type_free(struct type_class_bitfield *bitfield_class);
+
+struct type_class_float *float_type_new(const char *name,
+                                       size_t mantissa_len,
+                                       size_t exp_len, int byte_order,
+                                       size_t alignment);
+void float_type_free(struct type_class_float *float_class);
+
 #endif /* _BABELTRACE_TYPES_H */
index e9a9f69662e31ab53af56d051e0426cb9dbe0fd9..b21fdc917355fc675d05e856cba8906b5d1b6583 100644 (file)
@@ -73,10 +73,13 @@ size_t bitfield_copy(unsigned char *dest, const struct format *fdest,
        }
 }
 
-int bitfield_type_new(const char *name, size_t start_offset,
-                     size_t len, int byte_order, int signedness)
+struct type_class_bitfield *bitfield_type_new(const char *name,
+                                             size_t start_offset,
+                                             size_t len, int byte_order,
+                                             int signedness,
+                                             size_t alignment)
 {
-       struct type_class_bitfield bitfield_class;
+       struct type_class_bitfield *bitfield_class;
        struct type_class_integer *int_class;
        int ret;
 
@@ -86,14 +89,23 @@ int bitfield_type_new(const char *name, size_t start_offset,
        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->len = len;
        int_class->byte_order = byte_order;
        int_class->signedness = signedness;
        bitfield_class->start_offset = start_offset;
-       ret = ctf_register_type(&int_class->p);
-       if (ret)
-               g_free(bitfield_class);
-       return ret;
+       if (int_class->p.name) {
+               ret = ctf_register_type(&int_class->p);
+               if (ret) {
+                       g_free(bitfield_class);
+                       return NULL;
+               }
+       }
+       return bitfield_class;
 }
 
-/* TODO: bitfield_type_free */
+void bitfield_type_free(struct type_class_bitfield *bitfield_class)
+{
+       if (!bitfield_class->name)
+               g_free(bitfield_class);
+}
diff --git a/types/float.c b/types/float.c
new file mode 100644 (file)
index 0000000..ac15255
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * BabelTrace - Float Type Converter
+ *
+ * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * 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 <babeltrace/compiler.h>
+
+size_t float_copy(unsigned char *dest, const struct format *fdest, 
+                 const unsigned char *src, const struct format *fsrc,
+                 const struct type_class *type_class)
+{
+       struct type_class_float *float_class =
+               container_of(type_class, struct type_class_float, p);
+
+       if (fsrc->float_copy == fdest->float_copy) {
+               fsrc->float_copy(dest, float_class, src, float_class);
+               return float_class->mantissa_len + float_class->exp_len;
+       } else {
+               double v;
+
+               v = fsrc->double_read(src, fsrc);
+               return fdest->double_write(dest, fdest, v);
+       }
+}
+
+struct type_class_float *float_type_new(const char *name,
+                                       size_t mantissa_len,
+                                       size_t exp_len, int byte_order,
+                                       size_t alignment)
+{
+       struct type_class_float *float_class;
+       int ret;
+
+       /*
+        * Freed when type is unregistered.
+        */
+       float_class = g_new(struct type_class_float, 1);
+       float_class->p.name = g_quark_from_string(name);
+       float_class->p.alignment = alignment;
+       float_class->mantissa_len = mantissa_len;
+       float_class->exp_len = exp_len;
+       float_class->byte_order = byte_order;
+       if (float_class->p.name) {
+               ret = ctf_register_type(&float_class->p);
+               if (ret) {
+                       g_free(float_class);
+                       return NULL;
+               }
+       }
+       return float_class;
+}
+
+void float_type_free(struct type_class_float *float_class)
+{
+       if (!float_class->name)
+               g_free(float_class);
+}
index 8a540af159013ed23d98bcf8241c0e1c99bb5bc4..b338a5913971f86f7787c6a9d4d3a2db4b6778e9 100644 (file)
@@ -47,10 +47,13 @@ size_t integer_copy(unsigned char *dest, const struct format *fdest,
        }
 }
 
-int integer_type_new(const char *name, size_t alignment, size_t len,
-                    int byte_order, int signedness)
+struct type_class_integer *integer_type_new(const char *name,
+                                           size_t start_offset,
+                                           size_t len, int byte_order,
+                                           int signedness,
+                                           size_t alignment)
 {
-       struct type_class_integer int_class;
+       struct type_class_integer *int_class;
        int ret;
 
        /*
@@ -63,10 +66,18 @@ int integer_type_new(const char *name, size_t alignment, size_t len,
        int_class->len = len;
        int_class->byte_order = byte_order;
        int_class->signedness = signedness;
-       ret = ctf_register_type(&int_class.p);
-       if (ret)
-               g_free(int_class);
-       return ret;
+       if (int_class->p.name) {
+               ret = ctf_register_type(&int_class.p);
+               if (ret) {
+                       g_free(int_class);
+                       return NULL;
+               }
+       }
+       return int_class;
 }
 
-/* TODO: integer_type_free */
+void integer_type_free(struct type_class_integer *int_class)
+{
+       if (!int_class->name)
+               g_free(int_class);
+}
This page took 0.040329 seconds and 4 git commands to generate.