Add ctf type loading, add integer and bitfield "copy"
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 30 Sep 2010 01:46:03 +0000 (21:46 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 30 Sep 2010 01:46:03 +0000 (21:46 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
formats/ctf/ctf.c
include/babeltrace/types.h
types/bitfield.c [new file with mode: 0644]
types/integer.c
types/types.c

index d128b9ac654c4acf4c409610c2eae97bf53996f4..de760a83c1a48944d839fc1337d3e48b7ddf2a42 100644 (file)
@@ -50,3 +50,5 @@ void ctf_init(void)
        ret = bt_register_format(&ctf_format);
        assert(!ret);
 }
        ret = bt_register_format(&ctf_format);
        assert(!ret);
 }
+
+/* TODO: finalize */
index f8c3c8aa1de80d2a362392c9aa964387432b9ecf..ed3e7aca60fa8caba4085a12336f378d4e4afc76 100644 (file)
@@ -44,11 +44,17 @@ struct type_class_integer {
        int signedness;
 };
 
        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 */
 };
 
 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;
 struct type_class_float {
        struct type_class p;
        size_t mantissa_len;
@@ -66,4 +72,7 @@ struct type_class_struct {
        /* TODO */
 };
 
        /* TODO */
 };
 
+struct type_class *ctf_lookup_type(GQuark qname);
+int ctf_register_type(struct type_class *type_class);
+
 #endif /* _BABELTRACE_TYPES_H */
 #endif /* _BABELTRACE_TYPES_H */
diff --git a/types/bitfield.c b/types/bitfield.c
new file mode 100644 (file)
index 0000000..dc0e474
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * BabelTrace - Bitfield 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>
+#include <stdint.h>
+
+/*
+ * Shortcut to integer copy if we copy bit-aligned data with 0 start_offset.
+ * This skips the bitfield overhead when dealing with enumerations (which use
+ * the bitfield copy functions).
+ */
+extern
+size_t integer_copy(unsigned char *dest, const struct format *fdest, 
+                   const unsigned char *src, const struct format *fsrc,
+                   const struct type_class *type_class);
+
+size_t bitfield_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_bitfield *bitfield_class =
+               container_of(type_class, struct type_class_bitfield, p);
+       struct type_class_integer *int_class = &bitfield_class->p;
+
+       if (!(int_class->p.alignment % CHAR_BIT)
+           && !(int_class->len % CHAR_BIT)
+           && !(bitfield_class->start_offset))
+               return integer_copy(dest, fdest, src, fsrc, type_class);
+
+       if (!int_class->signedness) {
+               uint64_t v;
+
+               v = fsrc->bitfield_unsigned_read(src,
+                                       bitfield_class->start_offset,
+                                       int_class->len,
+                                       int_class->byte_order);
+               return fdest->bitfield_unsigned_write(dest,
+                                       bitfield_class->start_offset,
+                                       int_class->len, int_class->byte_order,
+                                       v);
+       } else {
+               int64_t v;
+
+               v = fsrc->bitfield_signed_read(src,
+                                       bitfield_class->start_offset,
+                                       int_class->len,
+                                       int_class->byte_order);
+               return fdest->bitfield_signed_write(dest,
+                                       bitfield_class->start_offset,
+                                       int_class->len, int_class->byte_order,
+                                       v);
+       }
+}
+
+int bitfield_type_new(const char *name, size_t start_offset,
+                     size_t len, int byte_order, int signedness)
+{
+       struct type_class_bitfield bitfield_class;
+       struct type_class_integer *int_class;
+       int ret;
+
+       /*
+        * Freed when type is unregistered.
+        */
+       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->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;
+}
+
+/* TODO: bitfield_type_free */
index 9b92ce35cd41024b143d5e04bf82f16d128ffe3d..8a540af159013ed23d98bcf8241c0e1c99bb5bc4 100644 (file)
  */
 
 #include <babeltrace/compiler.h>
  */
 
 #include <babeltrace/compiler.h>
+#include <babeltrace/align.h>
 #include <stdint.h>
 
 #include <stdint.h>
 
-size_t copy_integer(unsigned char *dest, const struct format *fdest, 
+size_t integer_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_integer *int_class =
                container_of(type_class, struct type_class_integer, p);
 
                    const unsigned char *src, const struct format *fsrc,
                    const struct type_class *type_class)
 {
        struct type_class_integer *int_class =
                container_of(type_class, struct type_class_integer, p);
 
+       if (fsrc->p.alignment)
+               src = PTR_ALIGN(src, fsrc->p.alignment / CHAR_BIT);
+       if (fdest->p.alignment)
+               dest = PTR_ALIGN(dest, fdest->p.alignment / CHAR_BIT);
+
        if (!int_class->signedness) {
                uint64_t v;
 
        if (!int_class->signedness) {
                uint64_t v;
 
-               v = fsrc->uint_read(src, int_class->byte_order, int_class->len);
-               return fdest->uint_write(dest, int_class->byte_order,
-                                        int_class->len, v);
+               v = fsrc->uint_read(src, int_class->len, int_class->byte_order);
+               return fdest->uint_write(dest, int_class->len, int_class->byte_order, v);
        } else {
                int64_t v;
 
        } else {
                int64_t v;
 
-               v = fsrc->int_read(src, int_class->byte_order, int_class->len);
-               return fdest->int_write(dest, int_class->byte_order,
-                                       int_class->len, v);
+               v = fsrc->int_read(src, int_class->len, int_class->byte_order);
+               return fdest->int_write(dest, int_class->len, int_class->byte_order, v);
        }
 }
        }
 }
+
+int integer_type_new(const char *name, size_t alignment, size_t len,
+                    int byte_order, int signedness)
+{
+       struct type_class_integer int_class;
+       int ret;
+
+       /*
+        * Freed when type is unregistered.
+        */
+       int_class = g_new(struct type_class_integer, 1);
+       int_class->p.name = g_quark_from_string(name);
+       int_class->p.alignment = alignment;
+       int_class->p.copy = integer_copy;
+       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;
+}
+
+/* TODO: integer_type_free */
index 7aeb74aa002956e03f82203fc07e4f4edb6557d3..5c8905517aadee42f95afec1d92df7530fa7ec79 100644 (file)
 #include <errno.h>
 
 /*
 #include <errno.h>
 
 /*
- * Type class hash table contains the registered type classes. Type class
- * registration is typically performed by a plugin.
- * TODO: support plugin unload (unregistration of type classes).
+ * Type hash table contains the registered types. Type registration is typically
+ * performed by a type plugin.
+ * TODO: support plugin unload (unregistration of types).
  */
  */
-GHashTable *type_classes;
+GHashTable *types;
 
 
-struct type_class *ctf_lookup_type_class(GQuark qname)
+struct type_class *ctf_lookup_type(GQuark qname)
 {
        return g_hash_table_lookup(type_classes,
                                   (gconstpointer) (unsigned long) qname)
 }
 
 {
        return g_hash_table_lookup(type_classes,
                                   (gconstpointer) (unsigned long) qname)
 }
 
-int ctf_register_type_class(const char *name,
-                           void (*read)(),
-                           void (*write)())
+int ctf_register_type(struct type_class *type_class)
 {
 {
-       struct type_class tc = g_new(struct type_class, 1);
-       GQuark qname = g_quark_from_string(name);
-
-       if (ctf_lookup_type_class(qname))
+       if (ctf_lookup_type_class(type_class->name))
                return -EEXIST;
 
        g_hash_table_insert(type_classes,
                return -EEXIST;
 
        g_hash_table_insert(type_classes,
-                           (gconstpointer) (unsigned long) qname,
-                           tc);
+                           (gconstpointer) (unsigned long) type_class->name,
+                           type_class);
        return 0;
 }
 
        return 0;
 }
 
This page took 0.02812 seconds and 4 git commands to generate.