歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux內核

Linux內核中的IS_ERR()、PTR_ERR()和ERR_PTR()

在看Linux內核源碼的時候,經常會遇到IS_ERR,比如在 linux/arch/arm/kernel/sys_arm.c中

asmlinkage int sys_execve(char __user *filenamei, char __user * __user *argv,
    char __user * __user *envp, struct pt_regs *regs)
{
 int error;
 char * filename;


 filename = getname(filenamei);
 error = PTR_ERR(filename);
 if (IS_ERR(filename))
  goto out;
 error = do_execve(filename, argv, envp, regs);
 putname(filename);
out:
 return error;
}

IS_ERR宏定義在include/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 IS_ERR_VALUE(x) unlikely((x) > (unsigned long)-1000L)

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);
}

#endif /* _LINUX_ERR_H */

Copyright © Linux教程網 All Rights Reserved