Pointer and error value in Linux kernel

Source: Internet
Author: User
& Nbsp; The ERR_PTR, IS_ERR, and PTR_ERR inline functions are often seen in kernel code and drivers. Many kernel functions need to return a pointer, but the function call may fail. in this case, we usually return a NULL pointer, just as malloc or kmalloc does not get the specified  

We often see ERR_PTR, IS_ERR, and PTR_ERR inline functions in kernel code and drivers.

Many kernel functions need to return a pointer, but the function call may fail. in this case, we usually return a NULL pointer, it is like the return value of malloc or kmalloc when the specified space application is not obtained. However, sometimes we want to know the cause of function failure, but returning NULL is not enough information. Therefore, some functions return an actual error code to handle the cause of the error. Many kernel interfaces return error messages by coding error values to a pointer value. When processing such a function, it cannot be compared with NULL to determine whether the call is successful. To facilitate the use of such interfaces, the 2.6 kernel implements three inline functions in linux/err. h:

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

A pointer-type function can return an error value. The error here is the normal negative error code. The caller can use IS_ERR to check whether the returned pointer is an error code.

        
         inline long IS_ERR(const void *ptr){return (unsigned long)ptr > (unsigned long)-1000L;}
        

You can use the PTR_ERR function to extract the actual error code.

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

PTR_ERR can be used only when IS_ERR returns true.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.