linux/err.h


<linux/err.h>文件的代码

#ifndef _LINUX_ERR_H
#define _LINUX_ERR_H

#include <linux/compiler.h>

#include <asm/errno.h>

/*
 * Kernel pointers have redundant information, so we can use a
 * scheme where we can return either an error code or a dentry
 * pointer with the same return value.
 *
 * This should be a per-architecture thing, to allow different
 * error and pointer decisions.
 */

#define MAX_ERRNO    4095

#ifndef __ASSEMBLY__

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

static inline void *ERR_PTR(long error)
{
    return (void *) error;
}

static inline long PTR_ERR(const void *ptr)
{
    return (long) ptr;
}

static inline long IS_ERR(const void *ptr)
{
    return IS_ERR_VALUE((unsigned long)ptr);
}

/**
 * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
 * @ptr: The pointer to cast.
 *
 * Explicitly cast an error-valued pointer to another pointer type in such a
 * way as to make it clear that's what's going on.
 */

static inline void *ERR_CAST(const void *ptr)
{
    /* cast away the const */
    return (void *) ptr;
}

#endif

#endif /* _LINUX_ERR_H */


内核函数返回一个指针给调用者,而这些函数中很多可能会失败。在大部分情况下,失败是通过一个NULL指针值来表示的,所以只要将返回指针和NULL进行比较,就可以知道函数调用是否成功,如kmalloc。这种方法虽然用起来方便,但它有个缺点:无法知道函数调用失败的原因。因此,许多内核接口通过错误值编码(怎么编码呢?后面讲)到一个指针值来返回错误信息。我们可以得用err.h中的函数,来获取导致函数调用失败的原因。

我们首先来看下错误值是怎么编码的。
http://apps.hi.baidu.com/share/detail/19833514
http://linux.chinaunix.net/bbs/thread-1019406-1-1.html
我们知道,一般的内核出错代码是一个小负数,在-1000和0之间(最大的也在-MAX_ERRNO,0,其中MAX_ERRNO在err.h中定义,刚好在2的12次方之内)。
我们还知道,负数是以补码的方式存储在计算机中的,故-4095在内存中的十六进制为:0xfffff000。
内核返回的指针一般是指向页面的边界(4K边界),即ptr && 0xfff == 0
也就是说,ptr的值不可能落在(0xfffff000, 0xffffffff)之间。所以,如果返回的指针值在上述区间之间,则应该是返回的错误值的补码。
因此,可以根据一个指针指针值是否大于-MAX_ERRNO,即0xfffff000,来判断这个指针值是否是错误的,自然也就知道这个函数调用是否成功。

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

再来分别看下几个内联函数

static inline void *ERR_PTR(long error)
{
    return (void *) error;
}

它是将 有符号 的错误码转换成指针。

static inline long PTR_ERR(const void *ptr)
{
    return (long) ptr;
}

它是将错误码指针转换成错误码,因为一个错误指针的值在(0xfffff000, 0xffffffff)之间,错误码的补码也在这个区间之间,故只需要强制转换一下即可。

static inline long IS_ERR(const void *ptr)
{
    return IS_ERR_VALUE((unsigned long)ptr);
}

这个函数的意思是显而易见的。

注意下使用问题:应该先使用IS_ERR对一个指针进行判断,在其返回真,也是就确实是一个错误指针时,才用PTR_ERR函数进行把错误编码提取出来。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <linux/module.h> #include <linux/fs.h> #include <linux/gpio.h> // 各种gpio的数据结构及函数 #include <linux/cdev.h> #include <linux/init.h> //__init __exit 宏定义声明 #include <linux/device.h> //class devise声明 #include <linux/uaccess.h> //copy_from_user 的头文件 #include <linux/types.h> //设备号 dev_t 类型声明 #include <linux/ioctl.h> MODULE_LICENSE("Dual BSD/GPL"); #define IOCTL_GPIO_OFF 0 /*灭*/ #define IOCTL_GPIO_ON 1 /*亮*/ #define DEVICE_NAME "beepctrl_caiyuxin" static struct class *ioctrl_class; #define BEEP_MAJOR 0 /*预设的主设备号*/ static int BEEP_major = BEEP_MAJOR; /*BEEP设备结构体*/ struct BEEP_dev { struct cdev cdev; /*cdev结构体*/ }; struct BEEP_dev *BEEP_devp; /*设备结构体指针*/ // 定义三色BEEP的GPIO引脚 static const struct gpio beeps[] = { // { 2, GPIOF_OUT_INIT_HIGH, "BEEP_RED" }, // { 3, GPIOF_OUT_INIT_HIGH, "BEEP_GREEN" }, { 25, GPIOF_OUT_INIT_HIGH, "BEEP" }, }; int BEEP_open(struct inode *inode, struct file *filp)//打开设备节点 { // int i; // printk(KERN_INFO " beeps opened\n"); // for(i=0;i<3;i++) // { // gpio_set_value(beeps[i].gpio, 0); // } return 0; } static long int BEEP_ioctl(struct file *filp,unsigned int cmd, unsigned long arg) { //ioctl函数接口 if (arg > sizeof(beeps)/sizeof(unsigned long)) { return -EINVAL; } printk("arg,cmd: %ld %d\n", arg, cmd); switch(cmd) { case IOCTL_GPIO_OFF:// 设置指定引脚的输出电平为0,由电路图可知,输出0时为灭 gpio_set_value(beeps[arg].gpio, 0); break; case IOCTL_GPIO_ON: gpio_set_value(beeps[arg].gpio, 1); break; default: return -EINVAL; } return 0; } int BEEP_release(struct inode *inode, struct file *filp)//释放设备节点 { int i; printk(KERN_INFO "BEEPs driver successfully close\n"); for(i=0;i<3;i++) { gpio_set_value(beeps[i].gpio, 0); } return 0; } static const struct file_operations BEEP_fops = { .owner = THIS_MODULE, .open = BEEP_open, .release = BEEP_release, .unlocked_ioctl = BEEP_ioctl, /* 实现主要控制功能*/ }; /*初始化并注册cdev*/ static void BEEP_setup
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值