From edef02286ee855045120854cdff646ce034a7474 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Wed, 28 Sep 2022 23:11:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=94=99=E8=AF=AF=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E7=9A=84=E5=AE=8F=EF=BC=8C=E4=BB=A5=E5=8F=8A=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E4=BD=BF=E7=94=A8=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/common/compiler.h | 7 ++++++- kernel/common/err.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 kernel/common/err.h diff --git a/kernel/common/compiler.h b/kernel/common/compiler.h index 9265fa14..60da65d8 100644 --- a/kernel/common/compiler.h +++ b/kernel/common/compiler.h @@ -9,4 +9,9 @@ // 内存屏障 #define barrier() __asm__ __volatile__("" :: \ : "memory"); -#endif \ No newline at end of file +#endif + +// 编译器属性 + +// 当函数的返回值未被使用时,编译器抛出警告信息 +#define __must_check __attribute__((__warn_unused_result__)) \ No newline at end of file diff --git a/kernel/common/err.h b/kernel/common/err.h new file mode 100644 index 00000000..cd3c33c3 --- /dev/null +++ b/kernel/common/err.h @@ -0,0 +1,30 @@ +#pragma once +#include +#include +#define MAX_ERRNO 4095 + +#define IS_ERR_VALUE(x) unlikely((x) >= (uint64_t)-MAX_ERRNO) + +/** + * @brief 判断返回的指针是否为errno + * + * @param ptr 待校验的指针 + * @return long 1 => 是错误码 + * 0 => 不是错误码 + */ +static inline long __must_check IS_ERR(const void* ptr) +{ + return IS_ERR_VALUE((uint64_t)ptr); +} + +/** + * @brief 判断返回的指针是否为errno或者为空 + * + * @param ptr 待校验的指针 + * @return long 1 => 是错误码或NULL + * 0 => 不是错误码或NULL + */ +static inline long __must_check IS_ERR_OR_NULL(const void* ptr) +{ + return !ptr || IS_ERR_VALUE((uint64_t)ptr); +}