开发者

What alternatives to __attribute__ exist on 64-bit kernels?

开发者 https://www.devze.com 2022-12-25 05:18 出处:网络
Is there any alternative to non-ISO gcc specific extension __attribute__ on 64-bit kernels ? Three types that i\'ve noticed are: function attributes, type attributes and variable attribu开发者_JAVA技

Is there any alternative to non-ISO gcc specific extension __attribute__ on 64-bit kernels ?

Three types that i've noticed are: function attributes, type attributes and variable attribu开发者_JAVA技巧tes.

eg. i'd like to avoid using __attribute__((__packed__)) for structures passed over the network, even though some gcc based code do use it.

Any suggestions or pointers on how to entirely avoid __attribute__ usage in C systems/kernel code ?

thanks Saifi.


Any suggestions or pointers on how to entirely avoid attribute usage in C systems/kernel code?

You can build your network packets piece by piece, copying each data element into the correct place in a char* buffer.

Pros: you don't have any alignment issues and it's generally portable, especially if you use the exact-width integer types from <stdint.h>

Cons: it's tedious and potentially error-prone


I'm assuming based on your comments that its your code you want to change, not the whole Linux kernel (etc).

Not sure about function attributes etc but specifically for attribute packed, I have done the following with no issues so far.

Basically instead of relying on the compiler to pack, you could use manual pad fields coupled with compile-time asserts.

struct foo {
   u32 field1;
   u16 field2;
   u16 pad; // manual padding
   // continue for other fields that the compiler would automatically pad for you with attribute packed
   u32 field3;
};

To check your structure you can use a compile time assert, something like this:

#define CASSERT(cond, name) typedef cassert__##name[cond ? 1 : -1]

CASSERT(offsetof(foo, field1) == 0, field1_wrong);
CASSERT(offsetof(foo, field2) == 4, field2_wrong);

When your assertions are wrong, the build will fail with a helpful error and line number

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号