联博以太坊_Linux驱动内核数据结构
发表时间:2021-01-04 浏览量:81
Linux驱动内核数据结构
我们写的驱动程序,全力能够运行在多个平台上(如:X86、ARM),为此,我们需要在数据类型、字节对齐、内存分页等多方面举行思量,使我们的驱动程序有很强的可移植性。
1.数据类型
只管使用typedef的数据类型,由于可能基础数据类型,如:long类型,在某些平台上可能是4字节,在某些平台上可能是8字节,而<linux/types.h>的typedef数据类型,为我们规避的这一不确定性。
#include <linux/types.h>
typedef u8;
typedef u16;
typedef u32;
typedef u64;
2.内存分页
内存分页是在#include <asm/page.h>中,通过宏PAGE_SIZE界说的。
3.字节序
Big-endian(大端序):数据的高位字节存放在地址的低端,低位字节存放在地址高端,即:高字节存在低地址,好别扭。
Little-endian(小端序):数据的高位字节存放在地址的高端,低位字节存放在地址低端,即:低字节存在低地址,好顺眼。
记着:别扭的是大端序,顺眼的是小端序。
#include <asm/byteorder.h>
__LITTLE_ENDIAN
__BIG_ENDIAN
凭据体系结构,仅界说两个符号中的一个。可以通过#ifdef来编写你的代码。
u32 __cpu_to_le32 (u32);
u32 __le32_to_cpu (u32);
这两个函数可以在已知字节顺序和处置器字节顺序之间转换的函数,这样的函数有许多。
4.字节对齐
不要一向的以为4字节对齐,只管大多数是这样的。
,,www.u-healer.com采用以太坊区块链高度哈希值作为统计数据,联博以太坊统计数据开源、公平、无任何作弊可能性。联博统计免费提供API接口,支持多语言接入。
#include <asm/unaligned.h>
get_unaligned(ptr);
put_unaligned(val, ptr);
有些架构需要使用这些宏来珍爱未对齐的数据接见。宏扩展到允许接见未对齐数据的体系结构的通俗指针作废引用。
5.指针和错误值
许多内核函数返回一个指针给调用者,而这些函数中许多可能导致失败,在大部分情况下,通过NULL返回,但有时也会通过一个err指针返回,可通过下面函数来确定详细错误信息,而这种错误是不能简朴的与NULL对照的,需要通过下面的函数处置才气获得详细的错误信息。
#include <linux/err.h>
void *ERR_PTR(long error);
long PTR_ERR(const void *ptr);
long IS_ERR(const void *ptr);
6.内核链表list
内核链表list,界说在#include <linux/list.h>中,是一组异常壮大的双向链表,不仅可以应用于内核和驱动的开发,在我们通俗应用程序开发中,也可以广泛应用。
#include <linux/list.h>
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_empty(struct list_head *head);
list_entry(entry, type, member);
list_move(struct list_head *entry, struct list_head *head);
list_move_tail(struct list_head *entry, struct list_head *head);
list_splice(struct list_head *list, struct list_head *head);
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)
内核驱动的数据结构远不止这些,这里仅仅是一个基础中的基础,后面还需要更深入的学习。
0
珍藏