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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无边框窗体和用户控件以及权限
- 下一篇: Elasticsearch内存分配设置详