[Concept,11/16] ext4l: Add linux/overflow.h and move struct_size

Message ID 20260119214846.3087611-12-sjg@u-boot.org
State New
Headers
Series ext4l: Move definitions to standard Linux headers |

Commit Message

Simon Glass Jan. 19, 2026, 9:48 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Create include/linux/overflow.h with the struct_size() macro that
calculates the size of a structure with a trailing flexible array
member.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 fs/ext4l/ext4_uboot.h    |  4 ++--
 include/linux/overflow.h | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/overflow.h
  

Patch

diff --git a/fs/ext4l/ext4_uboot.h b/fs/ext4l/ext4_uboot.h
index 69c50d406f7..310d79227b7 100644
--- a/fs/ext4l/ext4_uboot.h
+++ b/fs/ext4l/ext4_uboot.h
@@ -923,8 +923,8 @@  extern struct inode *iget_locked(struct super_block *sb, unsigned long ino);
 ssize_t generic_read_dir(struct file *f, char __user *buf, size_t count,
 			 loff_t *ppos);
 
-/* struct_size helper */
-#define struct_size(p, member, count)		(sizeof(*(p)) + sizeof((p)->member[0]) * (count))
+/* struct_size - use linux/overflow.h */
+#include <linux/overflow.h>
 
 /* file_operations - extended for dir.c */
 struct file_operations {
diff --git a/include/linux/overflow.h b/include/linux/overflow.h
new file mode 100644
index 00000000000..672377ede42
--- /dev/null
+++ b/include/linux/overflow.h
@@ -0,0 +1,23 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Overflow checking utilities
+ *
+ * Based on Linux overflow.h
+ */
+#ifndef _LINUX_OVERFLOW_H
+#define _LINUX_OVERFLOW_H
+
+#include <linux/types.h>
+
+/**
+ * struct_size() - Calculate size of structure with trailing array member
+ * @p: Pointer to the structure
+ * @member: Name of the array member
+ * @count: Number of elements in the array
+ *
+ * Return: Total size of the structure including @count array elements
+ */
+#define struct_size(p, member, count)		\
+	(sizeof(*(p)) + sizeof((p)->member[0]) * (count))
+
+#endif /* _LINUX_OVERFLOW_H */