一、检测指针出错的函数
//将错误号转化为指针地址
static inline void * __must_check ERR_PTR(long error)
{
return (void *) error;
}
//将错误指针转化为错误号
static inline long __must_check PTR_ERR(__force const void *ptr)
{
return (long) ptr;
}
//若是错误指针返回错误号,否则返回0
static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
{
if (IS_ERR(ptr))
return PTR_ERR(ptr);
else
return 0;
}
#define MAX_ERRNO 4095
#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
//(unsigned long)-MAX_ERRNO=-4095,即是0xFFFFF001,内核空间是从虚拟地址0xC0000000-0xFFFFFFFF.
//而0xFFFFF000就位于linux内核空间,并且从0xFFFFF000到4G的空间大小(0xFFFFFFFF)只有4KB,即只有一个PAGE_SIZE页面的大小。这时如果一个指针落入这4KB的区域,这个指针就不能是一个页面的首地址了,因为这已经不足以分配一个页面了。
static inline long __must_check IS_ERR(__force const void *ptr)
{
return IS_ERR_VALUE((unsigned long)ptr);
}
//用于检查指针是否为空,或者是错误指针
static inline long __must_check IS_ERR_OR_NULL(__force const void *ptr)
{
return !ptr || IS_ERR_VALUE((unsigned long)ptr);
}
static inline void * __must_check ERR_CAST(__force const void *ptr)
{
/* cast away the const */
return (void *) ptr;
}
二、linux错误号:
而一般内核的出错代码也是一个小负数,在-1000到0之间,转变成unsigned long,正好在(0xfffff000,0xffffffff)之间,(0xfffff000,0xffffffff)这个区间可以表示(-4096~-1)之间的unsigned long值。因此可以用:(unsigned long)ptr > (unsigned long)-1000L,来判断内核函数的返回值是一个有效的指针,还是一个出错代码。
//Linux/include/uapi/asm-generic/errno-base.h
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */
三、实例
struct dentry *bm_dentry;
bm_dentry = debugfs_create_dir(dev_name, bm_root_dentry);
//检测指针是否落在top 4KB中
if (IS_ERR(bm_dentry)) {
//将错误指针转化为错误码返回
return PTR_ERR(bm_dentry);
}
阅读(462) | 评论(0) | 转发(0) |