zoukankan      html  css  js  c++  java
  • 《Linux Device Drivers》第十一章 核心数据类型——note

    • 基本介绍
      • 因为Linux多平台特性,不管是哪一个重要驱动力应该是便携
      • 与内核代码相关的核心问题应该是访问的同时是数据项的已知长度。能力和利用不同的处理器
      • 内核使用的数据类型主要分为三类
        • 类似int这种标准C语言类型
        • 类似u32这种有确定大小的类型
        • 像pid_t这种用于特定内核对象的类型
      • 本章将讨论在什么情况下使用这三种类型以及怎样使用
    • 使用标准C语言类型
      • 当我们须要“两个字节的填充符”或者“用四个字节字符串表示的某个东西”时。我们不能使用标准类型,由于在不同的体系架构上。普通C语言的数据类型所占空间在大小并不同样
      • 内核中的普通内存地址一般是unsigned long。在当前Linux支持的全部平台上,指针和long整形的大小总是同样的
      • C99标准定义了intptr_t和uintptr_t类型,它们是可以保存指针值的整形变量
    • 为数据项分配确定的空间大小
      • <asm/types.h>
      • <linux/types.h>
      • u8, s8
      • u16, s16
      • u32, s32
      • u64, s64
      • 假设一个用户空间程序须要使用这些类型,它能够在名字前加上两个下划线作为前缀
      • __u8
      • __u32
      • 使用新编译器的系统将支持C99标准类型,如uint8_t和uint32_t

    • 接口特定的类型
      • 内核中最经常使用的数据类型由它们自己的typedef声明。这样能够防止出现不论什么移植性问题
      • “接口特定(interface-specific)”是指由某个库定义的一种数据类型,以便为某个特定的数据结构提供接口
      • 完整的_t类型在<linux/types.h>中定义
      • _t数据项的主要问题是在我们须要打印它们的时候,不太easy选择正确的printk或者printf的输出格式
    • 其它有关移植的问题
      • 一个通用的原则是要避免使用显式的常量值
      • 时间间隔
        • 使用jiffies计算时间间隔的时候,应该用HZ来衡量
      • 页大小
        • 内存页的大小是PAGE_SIZE字节
        • PAGE_SHIFT
        • <asm/page.h>
        • getpagesize库函数
        • get_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
      • 数据对齐
        • <asm/unaligned.h>
          • get_unaligned(ptr);
          • put_unaligned(val, ptr);
      • 指针和错误值
        • 很多内核接口通过把错误值编码到一个指针值中来返回错误信息
        • <linux/err.h>
          • void *ERR_PTR(long error);
          • long IS_ERR(const void *ptr);
          • long PTR_ERR(const void *ptr);
    • 链表
      • <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)

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    [Xcode 实际操作]六、媒体与动画-(2)使用图形上下文转换图片为灰度图
    [Xcode 实际操作]六、媒体与动画-(1)使用图形上下文按一定比例缩放图片
    [Xcode 实际操作]五、使用表格-(11)调整UITableView的单元格顺序
    [Xcode 实际操作]五、使用表格-(10)插入UITableView单元格
    [Xcode 实际操作]五、使用表格-(9)删除UITableView单元格(手势左滑调出删除按钮)
    [Xcode 实际操作]五、使用表格-(8)自定义UITableView单元格Accessory样式(附件图标)
    [Xcode 实际操作]五、使用表格-(7)UITableView单元格间隔背景色
    [Xcode 实际操作]五、使用表格-(6)UITableView滑动到指定单元格
    [Xcode 实际操作]五、使用表格-(5)设置UITableView的单元格背景颜色
    [Xcode 实际操作]五、使用表格-(4)设置UITableView单元格数据库源
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4737895.html
Copyright © 2011-2022 走看看