lib: move `bt_value` structures to `value-internal.h`
[babeltrace.git] / include / babeltrace / bitfield-internal.h
index d3609e3c99ca9d67885cc57a6d337263af9abb8a..c5d5eccdf0d191714d9c53421ad5a97fc6b2599c 100644 (file)
@@ -2,10 +2,6 @@
 #define _BABELTRACE_BITFIELD_H
 
 /*
- * BabelTrace
- *
- * Bitfields read/write functions.
- *
  * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -29,7 +25,6 @@
 
 #include <stdint.h>    /* C99 5.2.4.2 Numerical limits */
 #include <babeltrace/compat/limits-internal.h> /* C99 5.2.4.2 Numerical limits */
-#include <assert.h>
 #include <babeltrace/endian-internal.h>        /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
 
 /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
 
 #define _bt_is_signed_type(type)       ((type) -1 < (type) 0)
 
+/*
+ * NOTE: The cast to (uint64_t) below ensures that we're not casting a
+ * negative value, which is undefined in C. However, this limits the
+ * maximum type size of `type` and `v` to 64-bit. The
+ * _bt_check_max_64bit() is used to check that the users of this header
+ * do not use types with a size greater than 64-bit.
+ */
 #define _bt_unsigned_cast(type, v)                                     \
 ({                                                                     \
        (sizeof(v) < sizeof(type)) ?                                    \
-               ((type) (v)) & (~(~(type) 0 << (sizeof(v) * CHAR_BIT))) : \
+               ((type) (v)) & ((type) (~(~(uint64_t) 0 << (sizeof(v) * CHAR_BIT)))) : \
                (type) (v);                                             \
 })
 
+#define _bt_check_max_64bit(type)                                      \
+       char _max_64bit_assertion[sizeof(type) <= sizeof(uint64_t) ? 1 : -1] __attribute__((unused))
+
 /*
  * bt_bitfield_write - write integer to a bitfield in native endianness
  *
@@ -215,7 +220,7 @@ do {                                                                        \
 
 #define bt_bitfield_write_le(ptr, type, _start, _length, _v)           \
        _bt_bitfield_write_le(ptr, type, _start, _length, _v)
-       
+
 #define bt_bitfield_write_be(ptr, type, _start, _length, _v)           \
        _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
 
@@ -226,7 +231,7 @@ do {                                                                        \
 
 #define bt_bitfield_write_le(ptr, type, _start, _length, _v)           \
        _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
-       
+
 #define bt_bitfield_write_be(ptr, type, _start, _length, _v)           \
        _bt_bitfield_write_be(ptr, type, _start, _length, _v)
 
@@ -247,6 +252,10 @@ do {                                                                       \
        unsigned long start_unit, end_unit, this_unit;                  \
        unsigned long end, cshift; /* cshift is "complement shift" */   \
                                                                        \
+       { _bt_check_max_64bit(type); }                                  \
+       { _bt_check_max_64bit(typeof(*_vptr)); }                        \
+       { _bt_check_max_64bit(typeof(*_ptr)); }                         \
+                                                                       \
        if (!__length) {                                                \
                *__vptr = 0;                                            \
                break;                                                  \
@@ -314,6 +323,10 @@ do {                                                                       \
        unsigned long start_unit, end_unit, this_unit;                  \
        unsigned long end, cshift; /* cshift is "complement shift" */   \
                                                                        \
+       { _bt_check_max_64bit(type); }                                  \
+       { _bt_check_max_64bit(typeof(*_vptr)); }                        \
+       { _bt_check_max_64bit(typeof(*_ptr)); }                         \
+                                                                       \
        if (!__length) {                                                \
                *__vptr = 0;                                            \
                break;                                                  \
@@ -383,7 +396,7 @@ do {                                                                        \
 
 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)                \
        _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
-       
+
 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)                \
        _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
 
@@ -394,7 +407,7 @@ do {                                                                        \
 
 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)                \
        _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
-       
+
 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)                \
        _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
 
This page took 0.025758 seconds and 4 git commands to generate.