Monday, May 27, 2013

How to use IS_ERR and PTR_ERR? What do they mean?

From the kernel definition there are three macros: 
IS_ERR - used to check,
- Returns non-0 value if the ptr is an error. Otherwise 0 if it's not an error
PTR_ERR - used to print.
- Current value of the pointer.
Used as follows- if ptr is the pointer you want to check then use it as follows:
if (IS_ERR(ptr))
     printk("Error here: %ld", PTR_ERR(ptr));
Code definitions:

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

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

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

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

static inline long __must_check IS_ERR_OR_NULL(const void *ptr)
{
    return !ptr || IS_ERR_VALUE((unsigned long)ptr);
}

0 comments:

Post a Comment