[Concept,06/11] ext4l: Extract alloc declarations into their own file

Message ID 20251216211817.4131167-7-sjg@u-boot.org
State New
Headers
Series ext4l: Add Linux compatibility headers |

Commit Message

Simon Glass Dec. 16, 2025, 9:18 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Create include/linux/slab.h and include/linux/vmalloc.h with memory
allocation functions:

slab.h:
- GFP flags (GFP_KERNEL, GFP_ATOMIC, etc.)
- kmalloc(), kzalloc(), kcalloc(), kmalloc_array()
- kfree(), krealloc(), kmemdup()
- kmem_cache_* functions

vmalloc.h:
- vmalloc(), vzalloc(), vfree()
- __vmalloc()

Update compat.h to include these headers and remove duplicate
definitions.

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

 include/linux/compat.h  | 58 ++----------------------------
 include/linux/slab.h    | 79 +++++++++++++++++++++++++++++++++++++++++
 include/linux/vmalloc.h | 24 +++++++++++++
 3 files changed, 105 insertions(+), 56 deletions(-)
 create mode 100644 include/linux/slab.h
 create mode 100644 include/linux/vmalloc.h
  

Patch

diff --git a/include/linux/compat.h b/include/linux/compat.h
index 190c3820a14..28207fb96b8 100644
--- a/include/linux/compat.h
+++ b/include/linux/compat.h
@@ -14,7 +14,9 @@ 
 #include <linux/export.h>
 #include <linux/freezer.h>
 #include <linux/kernel.h>
+#include <linux/slab.h>
 #include <linux/uaccess.h>
+#include <linux/vmalloc.h>
 
 #ifdef CONFIG_XEN
 #include <xen/events.h>
@@ -29,61 +31,6 @@  struct p_current{
 
 extern struct p_current *current;
 
-#define GFP_ATOMIC ((gfp_t) 0)
-#define GFP_KERNEL ((gfp_t) 0)
-#define GFP_NOFS ((gfp_t) 0)
-#define GFP_USER ((gfp_t) 0)
-#define __GFP_NOWARN ((gfp_t) 0)
-#define __GFP_ZERO	((__force gfp_t)0x8000u)	/* Return zeroed page on success */
-
-void *kmalloc(size_t size, int flags);
-
-static inline void *kzalloc(size_t size, gfp_t flags)
-{
-	return kmalloc(size, flags | __GFP_ZERO);
-}
-
-static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
-{
-	if (size != 0 && n > SIZE_MAX / size)
-		return NULL;
-	return kmalloc(n * size, flags | __GFP_ZERO);
-}
-
-static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
-{
-	return kmalloc_array(n, size, flags | __GFP_ZERO);
-}
-
-#define vmalloc(size)	kmalloc(size, 0)
-#define __vmalloc(size, flags, pgsz)	kmalloc(size, flags)
-static inline void *vzalloc(unsigned long size)
-{
-	return kzalloc(size, 0);
-}
-static inline void kfree(const void *block)
-{
-	free((void *)block);
-}
-static inline void vfree(const void *addr)
-{
-	free((void *)addr);
-}
-
-struct kmem_cache { int sz; };
-
-struct kmem_cache *get_mem(int element_sz);
-#define kmem_cache_create(a, sz, c, d, e)	get_mem(sz)
-void *kmem_cache_alloc(struct kmem_cache *obj, int flag);
-static inline void kmem_cache_free(struct kmem_cache *cachep, void *obj)
-{
-	free(obj);
-}
-static inline void kmem_cache_destroy(struct kmem_cache *cachep)
-{
-	free(cachep);
-}
-
 #define DECLARE_WAITQUEUE(...)	do { } while (0)
 #define add_wait_queue(...)	do { } while (0)
 #define remove_wait_queue(...)	do { } while (0)
@@ -357,7 +304,6 @@  struct writeback_control {
 	unsigned for_sync:1;		/* sync(2) WB_SYNC_ALL writeback */
 };
 
-void *kmemdup(const void *src, size_t len, gfp_t gfp);
 
 typedef int irqreturn_t;
 
diff --git a/include/linux/slab.h b/include/linux/slab.h
new file mode 100644
index 00000000000..2ea124a420d
--- /dev/null
+++ b/include/linux/slab.h
@@ -0,0 +1,79 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Written by Mark Hemment, 1996 (markhe@nextd.demon.co.uk).
+ *
+ * (C) SGI 2006, Christoph Lameter
+ *	Cleaned up and restructured to ease the addition of alternative
+ *	implementations of SLAB allocators.
+ * (C) Linux Foundation 2008-2013
+ *      Unified interface for all slab allocators
+ *
+ * Memory allocation functions for Linux kernel compatibility.
+ * These map to U-Boot's malloc/free infrastructure.
+ */
+#ifndef _LINUX_SLAB_H
+#define _LINUX_SLAB_H
+
+#include <malloc.h>
+#include <linux/types.h>
+
+#define GFP_ATOMIC	((gfp_t)0)
+#define GFP_KERNEL	((gfp_t)0)
+#define GFP_NOFS	((gfp_t)0)
+#define GFP_USER	((gfp_t)0)
+#define GFP_NOWAIT	((gfp_t)0)
+#define __GFP_NOWARN	((gfp_t)0)
+#define __GFP_ZERO	((__force gfp_t)0x8000u)
+#define __GFP_NOFAIL	((gfp_t)0)
+
+void *kmalloc(size_t size, gfp_t flags);
+
+static inline void *kzalloc(size_t size, gfp_t flags)
+{
+	return kmalloc(size, flags | __GFP_ZERO);
+}
+
+static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
+{
+	if (size != 0 && n > SIZE_MAX / size)
+		return NULL;
+	return kmalloc(n * size, flags | __GFP_ZERO);
+}
+
+static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
+{
+	return kmalloc_array(n, size, flags | __GFP_ZERO);
+}
+
+static inline void kfree(const void *block)
+{
+	free((void *)block);
+}
+
+static inline void *krealloc(const void *p, size_t new_size, gfp_t flags)
+{
+	return realloc((void *)p, new_size);
+}
+
+void *kmemdup(const void *src, size_t len, gfp_t gfp);
+
+/* kmem_cache stubs */
+struct kmem_cache {
+	int sz;
+};
+
+struct kmem_cache *get_mem(int element_sz);
+#define kmem_cache_create(a, sz, c, d, e)	get_mem(sz)
+void *kmem_cache_alloc(struct kmem_cache *obj, gfp_t flag);
+
+static inline void kmem_cache_free(struct kmem_cache *cachep, void *obj)
+{
+	free(obj);
+}
+
+static inline void kmem_cache_destroy(struct kmem_cache *cachep)
+{
+	free(cachep);
+}
+
+#endif /* _LINUX_SLAB_H */
diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
new file mode 100644
index 00000000000..b8b5e5e63a2
--- /dev/null
+++ b/include/linux/vmalloc.h
@@ -0,0 +1,24 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * vmalloc functions for Linux kernel compatibility.
+ * In U-Boot, these just map to regular malloc.
+ */
+#ifndef _LINUX_VMALLOC_H
+#define _LINUX_VMALLOC_H
+
+#include <linux/slab.h>
+
+#define vmalloc(size)			kmalloc(size, 0)
+#define __vmalloc(size, flags, pgsz)	kmalloc(size, flags)
+
+static inline void *vzalloc(unsigned long size)
+{
+	return kzalloc(size, 0);
+}
+
+static inline void vfree(const void *addr)
+{
+	free((void *)addr);
+}
+
+#endif /* _LINUX_VMALLOC_H */