[PATCH v2] include/linux/kfifo.h: fix some IDEs Intelligence errors and the previous patch was misnamed
From: Xiaofeng Lian
Date: Wed Oct 02 2024 - 13:24:45 EST
In some IDEs, when using the ARRAY_SIZE macro to calculate the
buf size of kfifo, an error occurs (using incomplete types),
which belongs to the compiler INTELLIGENCE reporting error,
and can be compiled normally, specifically due to the gcc
compiler's __builtin_types_compatible_p function. Because the
KFIFO_PTR type uses a zero-length array and the macro definition
is a simple substitution, the ARRAY_SIZE macro will report
an error because of the zero-length array when calculating
__kfifo_esize in the INIT_KFIFO macro. Notice that the difference
between KFIFO and KFIFO_PTR is only whether or not the buf is inlined,
so (sizeof(KFIFO) - sizeof(KFIFO_PTR)) / sizeof(type_of_kfifo_member)
will give you the size of the buf of the kfifo, which bypasses the
compiler! Intelligence and bypasses the possibility that
ARRAY_SIZE may be passed in as a pointer, resulting in a compilation
error.
Signed-off-by: Xiaofeng Lian <1198715581lxf@xxxxxxxxx>
---
include/linux/kfifo.h | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/include/linux/kfifo.h b/include/linux/kfifo.h
index 564868bdce89..e5972c2b10f9 100644
--- a/include/linux/kfifo.h
+++ b/include/linux/kfifo.h
@@ -123,6 +123,21 @@ struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
*/
#define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
+/**
+ * get_kfifo_data_type - macro to get type of kfifo's member
+ * @fifo: pointer of kfifo
+ */
+#define get_kfifo_data_type(fifo) typeof(*(fifo)->type)
+
+/**
+ * __STACK_SIZE - macro to calculate kfifo's buffer size
+ * @fifo: pointer of kfifo
+ */
+#define __KFIFO_SIZE(fifo)\
+ ({\
+ DECLARE_KFIFO_PTR(__tmp_kfifo, get_kfifo_data_type(fifo));\
+ (sizeof(*(fifo)) - sizeof(__tmp_kfifo)) / sizeof(get_kfifo_data_type(fifo));\
+ })
/**
* INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
* @fifo: name of the declared fifo datatype
@@ -133,7 +148,7 @@ struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
struct __kfifo *__kfifo = &__tmp->kfifo; \
__kfifo->in = 0; \
__kfifo->out = 0; \
- __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
+ __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : __KFIFO_SIZE(__tmp->buf) - 1;\
__kfifo->esize = sizeof(*__tmp->buf); \
__kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
})
--
2.45.2