From ef5f8c18b543d3b46115ae9fd6b2c47b4e481371 Mon Sep 17 00:00:00 2001 From: Hans Petter Selasky Date: Tue, 2 Jun 2020 10:19:45 +0000 Subject: [PATCH] Implement struct_size() function macro in the LinuxKPI. MFC after: 1 week Sponsored by: Mellanox Technologies --- sys/compat/linuxkpi/common/include/linux/kernel.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sys/compat/linuxkpi/common/include/linux/kernel.h b/sys/compat/linuxkpi/common/include/linux/kernel.h index 290265d4d41..e65cf9ad607 100644 --- a/sys/compat/linuxkpi/common/include/linux/kernel.h +++ b/sys/compat/linuxkpi/common/include/linux/kernel.h @@ -555,4 +555,10 @@ linux_ratelimited(linux_ratelimit_t *rl) return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); } +#define struct_size(ptr, field, num) ({ \ + const size_t __size = offsetof(__typeof(*(ptr)), field); \ + const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \ + ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \ +}) + #endif /* _LINUX_KERNEL_H_ */