Re: [PATCH] bluetooth.h: fix compile issue when using in C++

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2012/3/8 Markus Rathgeb <maggu2810@xxxxxxxxxxxxxx>:
> 2012/3/8 Marcel Holtmann <marcel@xxxxxxxxxxxx>:
>> Hi Markus,
>>
>> please do not top post on this mailing list.
>>
>>> Why can't we get rid off the "typeof" keyword completely and using a
>>> template similar approach:
>>>
>>> ===
>>>
>>> #define bt_get_unaligned_tmpl(type, ptr)         \
>>> ({                                               \
>>>        struct __p_s  {                           \
>>>                type __v;                         \
>>>        } __attribute__((packed));                \
>>>        struct __p_s *__p = (struct __p_s*)(ptr); \
>>>        __p->__v;                                 \
>>> })
>>>
>>> static inline uint64_t bt_get_le64(void *ptr)
>>> {
>>>       return bt_get_unaligned_tmpl(uint64_t, ptr);
>>> }
>>>
>>> static inline uint64_t bt_get_be64(void *ptr)
>>> {
>>>       return bswap_64(bt_get_unaligned_tmpl(uint64_t, ptr));
>>> }
>>>
>>> ===
>>>
>>> Okay, the macro parameter type is not encapsulated by parenthesis, but
>>> it seems okay and working.
>>> So it seems to be a very more portable solution (IMHO).
>>
>> what does seems okay and working actually mean? Have you actually
>> verified on platform that require correct unaligned access to data?
>>
>> Regards
>>
>> Marcel
>>
>>
>
> Hi!
>
> Sorry for the top posting. I have forgotten, to not do that.
>
> I could check that on an ARM architecture, that needs correct
> unaligned access handling.
>
> What did I mean with "seems okay"?
> You could write code and test it if it is correct, if it fails, it is
> not correct, if it not fails, it could be correct.
> Or you can write code, think about what the code is doing, and say,
> this should be correct.
> Sorry, this should not be arrogant, just explain, that I see no
> different in the code (if I compare both).
>
> I will write a test suite after the weekend.
> But perhaps another one could have a look at.

#include <stdint.h>
#include <stdio.h>

#define DO_UNALIGNED 1

uint64_t bt_get_le64_old(void *ptr)
{
 return (
            {
                struct __attribute__((packed))
                {
                    typeof(*((uint64_t *) ptr)) __v;
                } *__p = (void *) ((uint64_t *) ptr);
                __p->__v;
            }
        );
}

uint64_t bt_get_le64_new(void *ptr)
{
 return (
            {
                struct __p_s
                {
                    uint64_t __v;
                } __attribute__((packed));
                struct __p_s *__p = (struct __p_s*)(ptr);
                __p->__v;
            }
        );
}


int main(int argc, char* argv[])
{
    int i, j;
    uint8_t data[2 * sizeof(uint64_t)];
    void* ptr;
    uint64_t result_old, result_new;

    for (i = 0; i < sizeof(data); ++i)
        data[i] = i;

    for (i = 0; i < sizeof(data) - sizeof(uint64_t); ++i)
    {
        ptr = data + i;

        printf("as is: ");

#if __BYTE_ORDER == __LITTLE_ENDIAN
        for (j = sizeof(uint64_t) - 1; j >= 0; --j)
#else
        for (j = 0; j < sizeof(uint64_t); ++j)
#endif
        {
            printf("%02X", *(data + i + j));
        }
        printf("\n");

#if DO_UNALIGNED
        printf("unali: %016llX\n", *((uint64_t*)ptr));
#endif

        result_old = bt_get_le64_old(ptr);
        result_new = bt_get_le64_new(ptr);
        printf("  old: %016llX\n  new: %016llX\n---\n", result_old, result_new);
    }

    return 0;
}

===

Running on
Processor       : ARM926EJ-S rev 5 (v5l)

Let's ignore alignment errors:
echo 0 > /proc/cpu/alignment

===

# ./unaligned_arm
as is: 0706050403020100
unali: 0706050403020100
  old: 0706050403020100
  new: 0706050403020100
---
as is: 0807060504030201
unali: 0706050403020100
  old: 0807060504030201
  new: 0807060504030201
---
as is: 0908070605040302
unali: 0706050403020100
  old: 0908070605040302
  new: 0908070605040302
---
as is: 0A09080706050403
unali: 0706050403020100
  old: 0A09080706050403
  new: 0A09080706050403
---
as is: 0B0A090807060504
unali: 0B0A090807060504
  old: 0B0A090807060504
  new: 0B0A090807060504
---
as is: 0C0B0A0908070605
unali: 0B0A090807060504
  old: 0C0B0A0908070605
  new: 0C0B0A0908070605
---
as is: 0D0C0B0A09080706
unali: 0B0A090807060504
  old: 0D0C0B0A09080706
  new: 0D0C0B0A09080706
---
as is: 0E0D0C0B0A090807
unali: 0B0A090807060504
  old: 0E0D0C0B0A090807
  new: 0E0D0C0B0A090807
---

===

I could also run a loop and break if result_new != result_old...
--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux