Linux Device Drivers, chapter 11th, the kernel data type--note

Source: Internet
Author: User

    • Brief introduction
      • Because of the multi-platform nature of Linux, any important driver should be portable
      • The core issue associated with kernel code is the ability to access data items of known lengths at the same time and take full advantage of the capabilities of different processors
      • The data types used by the kernel are mainly divided into three categories
        • Standard C language types like int
        • A type of deterministic size like u32
        • Types such as pid_t for a specific kernel object
      • This chapter will discuss under what circumstances these three types are used and how to use
    • Use standard C language types
      • When we need a "two-byte shim" or "something in four-byte string", we cannot use the standard type because the size of the data type of the ordinary C language is not the same in different architectures
      • The normal memory address in the kernel is usually unsigned long, and the size of the pointer and long shaping is always the same on all platforms supported by the current Linux
      • The C99 standard defines intptr_t and uintptr_t types, which are shaping variables that can hold pointer values
    • Allocate a determined amount of space for a data item
      • <asm/types.h>
      • <linux/types.h>
      • U8, S8
      • U16, S16
      • U32, S32
      • U64, S64
      • If a user-space program needs to use these types, it can prefix the first name with a two underscore
      • __u8
      • __u32
      • Systems that use the new compiler will support C99 standard types, such as uint8_t and uint32_t

  • interface-specific types
    • The most commonly used data types in the kernel are declared by their own typedef, which can prevent any portability problems
    • "Interface-specific (interface-specific)" refers to a data type defined by a library to provide an interface for a particular data structure
    • The complete _t type is defined in <linux/types.h>
    • The main problem with _t data items is that it's not easy to choose the right PRINTK or printf output format when we need to print them.
  • Other issues related to porting
      • A common principle is to avoid the use of explicit constant values
      • Time interval
        • When using jiffies to calculate the time interval, you should use Hz to measure
      • Page size
        • The size of the memory page is page_size bytes
        • Page_shift
        • <asm/page.h>
        • GetPageSize Library functions
        • Get_order function
      • byte order
        • <asm/byteorder.h>
          • __big_endian
          • __little_endian
        • U32 cpu_to_le32 (U32);
        • U32 le32_to_cpu (U32);
        • Be64_to_cpu
        • Le16_to_cpus
        • cpu_to_le32p
      • Data alignment
        • <asm/unaligned.h>
          • Get_unaligned (PTR);
          • Put_unaligned (Val, PTR);
      • Pointers and error values
        • Many kernel interfaces return an error message by encoding the error value into a pointer value
        • <linux/err.h>
          • void *err_ptr (long error);
          • Long Is_err (const void *ptr);
          • Long Ptr_err (const void *ptr);
  • Linked list
    • <linux/list.h>
      • struct List_head
        • struct List_head *next, *prev;
      • Init_list_head (&list);
      • List_head (list);
      • List_add (struct list_head *new, struct list_head *head);
      • List_add_tail (struct list_head *new, struct list_head *head);
      • List_del (struct list_head *entry);
      • List_del_init (struct list_head *entry);
      • List_move (struct list_head *entry, struct list_head *head);
      • List_move_tail (struct list_head *entry, struct list_head *head);
      • List_empty (struct list_head *head);
      • List_splice (struct list_head *list, struct list_head *head);
      • List_entry (struct List_head *ptr, type_of_struct, field_name);
      • List_for_each (struct list_head *cursor, struct list_head *list)
      • List_for_each_prev (struct list_head *cursor, struct list_head *list)
      • List_for_each_safe (struct list_head *cursor, struct list_head *next, struct list_head *list)
      • List_for_each_entry (type *cursor, struct List_head *list, member)
      • List_for_each_entry_safe (Type *cursor, type *next, struct List_head *list, member)

Linux Device Drivers, chapter 11th, the kernel data type--note

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.