Endian wrapper use fix
[babeltrace.git] / formats / ctf / types / integer.c
index e43e5547217011e5d3680574d18f87f825be4508..5b56227d85d4d0ccfbd323aa52d3804c31d3682b 100644 (file)
@@ -3,7 +3,9 @@
  *
  * Integers read/write functions.
  *
- * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
 #include <babeltrace/bitfield.h>
 #include <stdint.h>
 #include <glib.h>
-#include <endian.h>
+#include <babeltrace/endian.h>
+
+/*
+ * The aligned read/write functions are expected to be faster than the
+ * bitfield variants. They will be enabled eventually as an
+ * optimisation.
+ */
 
 static
-void _aligned_integer_read(struct stream_pos *ppos,
-                          struct definition *definition)
+int _aligned_integer_read(struct stream_pos *ppos,
+                         struct definition *definition)
 {
        struct definition_integer *integer_definition =
                container_of(definition, struct definition_integer, p);
@@ -34,15 +42,18 @@ void _aligned_integer_read(struct stream_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 (!ctf_pos_access_ok(pos, integer_declaration->len))
+               return -EFAULT;
+
+       assert(!(pos->offset % CHAR_BIT));
        if (!integer_declaration->signedness) {
                switch (integer_declaration->len) {
                case 8:
                {
                        uint8_t v;
 
-                       v = *(const uint8_t *)pos->base;
+                       v = *(const uint8_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._unsigned = v;
                        break;
                }
@@ -50,7 +61,7 @@ void _aligned_integer_read(struct stream_pos *ppos,
                {
                        uint16_t v;
 
-                       v = *(const uint16_t *)pos->base;
+                       v = *(const uint16_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._unsigned =
                                rbo ? GUINT16_SWAP_LE_BE(v) : v;
                        break;
@@ -59,7 +70,7 @@ void _aligned_integer_read(struct stream_pos *ppos,
                {
                        uint32_t v;
 
-                       v = *(const uint32_t *)pos->base;
+                       v = *(const uint32_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._unsigned =
                                rbo ? GUINT32_SWAP_LE_BE(v) : v;
                        break;
@@ -68,7 +79,7 @@ void _aligned_integer_read(struct stream_pos *ppos,
                {
                        uint64_t v;
 
-                       v = *(const uint64_t *)pos->base;
+                       v = *(const uint64_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._unsigned =
                                rbo ? GUINT64_SWAP_LE_BE(v) : v;
                        break;
@@ -82,7 +93,7 @@ void _aligned_integer_read(struct stream_pos *ppos,
                {
                        int8_t v;
 
-                       v = *(const int8_t *)pos->base;
+                       v = *(const int8_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._signed = v;
                        break;
                }
@@ -90,27 +101,27 @@ void _aligned_integer_read(struct stream_pos *ppos,
                {
                        int16_t v;
 
-                       v = *(const int16_t *)pos->base;
+                       v = *(const int16_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._signed =
-                               rbo ? GUINT16_SWAP_LE_BE(v) : v;
+                               rbo ? (int16_t) GUINT16_SWAP_LE_BE(v) : v;
                        break;
                }
                case 32:
                {
                        int32_t v;
 
-                       v = *(const int32_t *)pos->base;
+                       v = *(const int32_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._signed =
-                               rbo ? GUINT32_SWAP_LE_BE(v) : v;
+                               rbo ? (int32_t) GUINT32_SWAP_LE_BE(v) : v;
                        break;
                }
                case 64:
                {
                        int64_t v;
 
-                       v = *(const int64_t *)pos->base;
+                       v = *(const int64_t *) ctf_get_pos_addr(pos);
                        integer_definition->value._signed =
-                               rbo ? GUINT64_SWAP_LE_BE(v) : v;
+                               rbo ? (int64_t) GUINT64_SWAP_LE_BE(v) : v;
                        break;
                }
                default:
@@ -118,10 +129,11 @@ void _aligned_integer_read(struct stream_pos *ppos,
                }
        }
        ctf_move_pos(pos, integer_declaration->len);
+       return 0;
 }
 
 static
-void _aligned_integer_write(struct stream_pos *ppos,
+int _aligned_integer_write(struct stream_pos *ppos,
                            struct definition *definition)
 {
        struct definition_integer *integer_definition =
@@ -132,8 +144,11 @@ void _aligned_integer_write(struct stream_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 (!ctf_pos_access_ok(pos, integer_declaration->len))
+               return -EFAULT;
+
+       assert(!(pos->offset % CHAR_BIT));
        if (pos->dummy)
                goto end;
        if (!integer_declaration->signedness) {
@@ -167,12 +182,12 @@ void _aligned_integer_write(struct stream_pos *ppos,
                        break;
                case 16:
                        *(int16_t *) ctf_get_pos_addr(pos) = rbo ?
-                                                GUINT16_SWAP_LE_BE((int16_t) v) :
+                                                (int16_t) 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) GUINT32_SWAP_LE_BE((int32_t) v) :
                                                 (int32_t) v;
                        break;
                case 64:
@@ -185,9 +200,10 @@ void _aligned_integer_write(struct stream_pos *ppos,
        }
 end:
        ctf_move_pos(pos, integer_declaration->len);
+       return 0;
 }
 
-void ctf_integer_read(struct stream_pos *ppos, struct definition *definition)
+int ctf_integer_read(struct stream_pos *ppos, struct definition *definition)
 {
        struct definition_integer *integer_definition =
                container_of(definition, struct definition_integer, p);
@@ -195,7 +211,16 @@ void ctf_integer_read(struct stream_pos *ppos, struct definition *definition)
                integer_definition->declaration;
        struct ctf_stream_pos *pos = ctf_pos(ppos);
 
+       if (!(integer_declaration->p.alignment % CHAR_BIT)
+           && !(integer_declaration->len % CHAR_BIT)) {
+               return _aligned_integer_read(ppos, definition);
+       }
+
        ctf_align_pos(pos, integer_declaration->p.alignment);
+
+       if (!ctf_pos_access_ok(pos, integer_declaration->len))
+               return -EFAULT;
+
        if (!integer_declaration->signedness) {
                if (integer_declaration->byte_order == LITTLE_ENDIAN)
                        bt_bitfield_read_le(pos->base, unsigned long,
@@ -216,9 +241,10 @@ void ctf_integer_read(struct stream_pos *ppos, struct definition *definition)
                                &integer_definition->value._signed);
        }
        ctf_move_pos(pos, integer_declaration->len);
+       return 0;
 }
 
-void ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
+int ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
 {
        struct definition_integer *integer_definition =
                container_of(definition, struct definition_integer, p);
@@ -226,7 +252,16 @@ void ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
                integer_definition->declaration;
        struct ctf_stream_pos *pos = ctf_pos(ppos);
 
+       if (!(integer_declaration->p.alignment % CHAR_BIT)
+           && !(integer_declaration->len % CHAR_BIT)) {
+               return _aligned_integer_write(ppos, definition);
+       }
+
        ctf_align_pos(pos, integer_declaration->p.alignment);
+
+       if (!ctf_pos_access_ok(pos, integer_declaration->len))
+               return -EFAULT;
+
        if (pos->dummy)
                goto end;
        if (!integer_declaration->signedness) {
@@ -250,4 +285,5 @@ void ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
        }
 end:
        ctf_move_pos(pos, integer_declaration->len);
+       return 0;
 }
This page took 0.037699 seconds and 4 git commands to generate.