When to use stdint.h scanners in driver code

It seemed to me that when using our own scalar types (integer, short, char) or those provided by stdint, there is no consistency and best practice: uint32_t uint16_t uint8_t.

It really beats me up, because drivers are an integral part of the kernel, which should be stable, consistent, stable and good.

Here is an example illustration in gcc (used for the hobby project for raspberries pi):

// using native scalars
struct fbinfo {
        unsigned width, height;
        unsigned vwidth, vheight;
        unsigned pitch, bits;
        int x, y;
        void *ptr;
        unsigned size;
} __attribute__((aligned(16)));

// using stdint scalars
struct fbinfo {
        uint32_t width, height;
        uint32_t vwidth, vheight;
        uint32_t pitch, bits;
        int32_t x, y;
        uint32_t ptr; // convert to void* in order to use it
        uint32_t size;
} __attribute__((aligned(16)));

For me, the first example seems more logical, because this piece of code is intended only for working on raspberry pi. It would be pointless to run this on other equipment.

, , C . 16 - . uint32_t, uint_fast32_t : . X .

stdint, ​​linux : u32, __u32 endian, , __le32.

typedef'd? stdint.h?

+4
1

1.

. . printf() int32_t PRIi32 :

printk("foo=" PRIi32 ", bar=" PRIi32 "\n", foo, bar);

/ ; DMA. writel() readl(), .

, (, __attribute__((__aligned__(16))) , .

(int32_t x,y ) , .

, -

    uint32_t ptr; // convert to void* in order to use it

C uintptr_t ptr,

    unsigned long ptr;

dma_addr_t .

2. uint32_t __u32

10 uint32_t, , C99, , () linux .

uint32_t ( ​​ , C99), , .

, typedef'ed ( ). ​​

3. uint_fastX_t

, . uint32_t ( ) int ( ).

4. __le32 __u32

endian, (, ). (, , endian_variable = native_variable).

, . (, DMA); , endian , .

+3

Source: https://habr.com/ru/post/1654166/


All Articles