日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

ERR_PTR PTR_ERR IS_ERR ERROR

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ERR_PTR PTR_ERR IS_ERR ERROR 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在linux-x.xx/include/uapi/asm-generic/errno-base.h和errno.h里分別定義了返回錯誤的信息。

errno-base.h:

 1 #ifndef _ASM_GENERIC_ERRNO_BASE_H
 2 #define _ASM_GENERIC_ERRNO_BASE_H
 3 
 4 #define    EPERM         1    /* Operation not permitted */
 5 #define    ENOENT         2    /* No such file or directory */
 6 #define    ESRCH         3    /* No such process */
 7 ......
 8 ......
 9 ......
10 #define    EDOM        33    /* Math argument out of domain of func */
11 #define    ERANGE        34    /* Math result not representable */
12 
13 #endif

errno.h:

 1 #define _ASM_GENERIC_ERRNO_H
 2 
 3 #include <asm-generic/errno-base.h>
 4 
 5 #define EDEADLK         35      /* Resource deadlock would occur */
 6 #define ENAMETOOLONG    36      /* File name too long */
 7 #define ENOLCK          37      /* No record locks available */
 8 ......
 9 ......
10 ......
11 #define ERFKILL         132     /* Operation not possible due to RF-kill */
12 
13 #define EHWPOISON       133     /* Memory page has hardware error */
14 
15 #endif

以上可知錯誤信息在1~133之間,返回錯誤會添加-號,故返回-133~-1。

部分函數內部有記錄錯誤的信息并且返回。

對于返回非指針的函數,一般返回-ERROR。對于返回指針的函數,需要把ERROR轉化為指針。

ERR_PTR、PTR_ERR和IS_ERR定義在linux-x.xx/include/linux/err.h:

 1 #define MAX_ERRNO    4095
 2 
 3 #ifndef __ASSEMBLY__
 4 EPERM        
 5 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
 6 ERROR
 7 static inline void * __must_check ERR_PTR(long error)        //把錯誤轉化指針
 8 {
 9     return (void *) error;
10 }
11 
12 static inline long __must_check PTR_ERR(const void *ptr)            //把指針轉化錯誤
13 {
14     return (long) ptr;
15 }
16 
17 static inline long __must_check IS_ERR(const void *ptr)        //判斷是否錯誤指針
18 {
19     return IS_ERR_VALUE((unsigned long)ptr);
20 }
21 
22 static inline long __must_check IS_ERR_OR_NULL(const void *ptr)        //判斷空指針或者錯誤指針
23 {
24     return !ptr || IS_ERR_VALUE((unsigned long)ptr);
25 }

重點關注IS_ERR的實現。首先把指針轉化成long,然后和(unsigned long)-4095比較。

對于32位系統,-4095=0xFFFF F001,對于64位系統,-4095=0xFFFF FFFF?FFFF?F001

對于32位系統,內核從0xFFFF F001 ~?0xFFFF FFFF是冗余空間,對于64位系統,內核從0xFFFF?FFFF?FFFF?F001 ~?0xFFFF?FFFF?FFFF?FFFF是冗余空間。

分配任何指針不可能到達這個區域,而錯誤信息在-133~-1在這個區域,故可判斷系統的錯誤指針,從而判斷返回的錯誤指針。

?

轉載于:https://www.cnblogs.com/kevinhwang/p/5649954.html

總結

以上是生活随笔為你收集整理的ERR_PTR PTR_ERR IS_ERR ERROR的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。