Am Dienstag, dem 10.09.2024 um 13:51 +0000 schrieb Qing Zhao: > #define alloc(P, FAM, COUNT) ({ \ > typeof(P) __p; \ > size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \ > __p = kmalloc(__size, GFP); \ > typeof(_Generic(__builtin_counted_by_ref(__p->FAM), \ > void *: (size_t *)NULL, \ > default: __builtin_counted_by_ref(__p->FAM))) \ > ret = __builtin_counted_by_ref(__p->FAM); \ > if (ret) \ > *ret = COUNT; \ > P = __p; \ > })
Maybe not too relevant for your patch, but I would use something like this #define alloc(P, FAM, COUNT) ({ \ __auto_type __p = &(P); \ __auto_type __c = (COUNT); \ size_t __size = sizeof(*(*__p)) + sizeof(*(*__p)->FAM) * __c; \ if ((*__p = malloc(__size))) { \ __auto_type ret = __builtin_counted_by_ref((*__p)->FAM); \ *_Generic(ret, void *: &(size_t){0}, default: ret) = __c; \ } \ }) to have brackets around the macro arguments to avoid accidents, to reduce compile time due to multiple evaluation of the macro arguments, and to avoid warnings for the null pointer dereference on clang. (Actually, I would also cast the sizes to a signed type immediately to catch overflows with UBSan, but I kept size_t here.) Martin