[Linux kernel]linux kernel programming specification

Source: Internet
Author: User

The role of the 1,__ATTRUBTE__ keyword

Click to open link

The role of

__attrubte__ (packed) is to tell the compiler to undo the optimization alignment of the structure during compilation and to align it by the actual number of bytes consumed.
struct str_struct{
        __u8    A;
        __u8    B;
        __u8    C;
        __u16   D;
} __attribute__ ((packed));
the size of the struct above is 5
/*  when using a TypeDef, pay special attention to where __attribute__ ((packed)) is placed, equivalent to:
  *  typedef struct STR_ Stuct str;
  *  and the struct str_struct is the structure above.
  */
typedef struct {
        __u8    A;
        __u8    B;
        __u8    C;
        __u16   D;
} __attribute__ ((packed)) Str;
the size of the struct above is 5

/* In the following typedef structure, __ATTRIBUTE__ ((packed)) is placed after the structure name Str_temp, its function is ignored, notice the difference from the structure str. */
typedef struct {
__u8 A;
__u8 b;
__u8 C;
__u16 D;
}str_temp __attribute__ ((packed));
The size of the above structure is 6

There are two underscores before and after attribute, followed by a pair of original brackets, and the corresponding __attribute__ parameters in parentheses.
The syntax format is: __attribute__ ((attribute-list))
Attribute sections are used for functions and variables, and typically the compiler places functions in the. Text section, where variables are placed in the. data or. BSS sections, and the section property allows the compiler to place functions or variables in the specified section.

2, meaning of Is_err () in driver

All drivers are running in kernel space, although the kernel space is large, but always limited, and in this limited space, its last page is reserved specifically.
All in the kernel pointer if the last page is referred to, then this is an error pointer, kernel space is 3g-4g, so oxfffff000-0xffffffff is invalid pointer.

[CPP]View Plaincopy
    1. /*static Inline long is_err (const void *PTR)
    2. {
    3. return (unsigned long) ptr > (unsigned long) -1000l;
    4. }*/
    5. static inline void * __must_check err_ptr (long error)
    6. {
    7. return (void *) error;
    8. }

Ptr_err () Converts the pointer to an error code.

Likely () and unlikely functions of the 3,linux kernel

Click to open link

Likely and unlikely are only used to change the performance of the system and do not change the values that have been previously
Does not often occur with unlikely, often occurring with likly

Differences between the 4,mdelay and Msleep () functions

Click to open link

Mdelay accuracy is higher, but will always occupy the CPU resources, msleep the current process into the waiting queue according to personal experience, the time delay of 10ms or less is better than the time delay of the time series is best or use the mdelay,100ms or more than the Msleep, Delay of 100ms or more
There is a certain impact on the operation of the multi-tasking system of Linux.

[Linux kernel]linux kernel programming specification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.