linux内核常用宏实例总结-----持续更新

/* Force a compilation error if condition is true */
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])

如果条件为真则引起一个编译时错误。

使用实例:
static int __init spidev_init(void)
{
 int status;

 /* Claim our 256 reserved device numbers.  Then register a class
  * that will key udev/mdev to add/remove /dev nodes.  Last, register
  * the driver which manages those device numbers.
  */
 BUILD_BUG_ON(N_SPI_MINORS > 256);//如果条件为真,则引起一个编译错误
 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);//注册驱动
 if(status < 0)
  return status;

 spidev_class = class_create(THIS_MODULE, "spidev");//创建设备文件
 if(IS_ERR(spidev_class))
 {
  unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  return PTR_ERR(spidev_class);
 }

 status = spi_register_driver(&spidev_spi);
 if(status < 0)
 {
  class_destroy(spidev_class);
  unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
 }
 return status;
}

N_SPI_MINORS为手动定义的设备号,若大于256,则编译错误。

使用module_init函数加载模块

module_init(spidev_init);  

__init宏,在/linux/init.h中有它的定义:
#define __init  __section(.init.text) __cold notrace
/linux/compiler.h中,# define __section(S) __attribute__ ((__section__(#S)))通过__init,会把函数中的代码放到.text.init段,这个段在系统启动后会被释放。
系统启动后,会有如下打印信息:
Freeing init memory: 424K
这就是使用到__init宏后释放的空间。由于这些函数只用执行一次,因此可以添加__init前缀以节省内存空间。

 

linux平台下驱动与应用间互传数据,通过下面四个宏实现:
copy_from_user:从用户空间的缓冲区复制数据到内核空间的缓冲区
copy_to_user:从内核空间的缓冲区复制数据到用户空间的缓冲区
get_user:从用户空间获得指定类型的数据
put_user:向用户空间写入指定类型的数据

container_of 宏

container_of(pointer, container_type, container_field); 

这个宏使用一个指向 container_field 类型的成员的指针, 它在一个 container_type 类型的结构中, 并且返回一个指针指向包含结构.
 struct cdev {
struct kobject kobj;
struct module *owner;
struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
struct cdev *device = container_of(kp, struct cdev, kobj);

程序员常常定义一个简单的宏来"后向转换" kobject 指针到包含类型.
BUG_ON()

<asm-generic/bug.h>

#ifndef HAVE_ARCH_BUG

#define BUG() do { /

    printk("BUG: failure at %s:%d/%s()!/n", __FILE__, __LINE__, __func__); /

    panic("BUG!"); /

} while (0)

#endif

#ifndef HAVE_ARCH_BUG_ON

#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while(0)

#endif

作用:一些内核调用可以用来方便标记bug,提供断言并输出信息。最常用的两个是BUG()和BUG_ON()。当被调用的时候,它们会引发oops,导致栈的回溯和错误信息的打印。为什么这些声明会导致 oops跟硬件的体系结构是相关的。大部分体系结构把BUG()和BUG_ON()定义成某种非法操作,这样自然会产生需要的oops。你可以把这些调用当作断言使用,想要断言某种情况不该发生 :
if (bad_thing)
BUG();
或者使用更好的形式:
BUG_ON(bad_thing);

可以用panic()引发更严重的错误。调用panic()不但会打印错误消息而且还会挂起整个系统。显然,你只应该在极端恶劣的情况下使用它:
if (terrible_thing)
       panic("foo is %ld/n", foo);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值