ext4l: Extract alloc declarations into their own file

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>
This commit is contained in:
Simon Glass
2025-12-16 13:37:22 -07:00
committed by Simon Glass
parent f592f31464
commit 43f6682069
5 changed files with 124 additions and 59 deletions

View File

@@ -248,6 +248,7 @@
#include <g_dnl.h>
#include <dm/devres.h>
#include <linux/bug.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/usb/ch9.h>
@@ -278,7 +279,6 @@ static const char fsg_string_interface[] = "Mass Storage";
/*-------------------------------------------------------------------------*/
#define GFP_ATOMIC ((gfp_t) 0)
#define PAGE_CACHE_SHIFT 12
#define PAGE_CACHE_SIZE (1 << PAGE_CACHE_SHIFT)
#define kthread_create(...) __builtin_return_address(0)

View File

@@ -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;

95
include/linux/slab.h Normal file
View File

@@ -0,0 +1,95 @@
/* 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>
#ifndef GFP_ATOMIC
#define GFP_ATOMIC ((gfp_t)0)
#endif
#ifndef GFP_KERNEL
#define GFP_KERNEL ((gfp_t)0)
#endif
#ifndef GFP_NOFS
#define GFP_NOFS ((gfp_t)0)
#endif
#ifndef GFP_USER
#define GFP_USER ((gfp_t)0)
#endif
#ifndef GFP_NOWAIT
#define GFP_NOWAIT ((gfp_t)0)
#endif
#ifndef __GFP_NOWARN
#define __GFP_NOWARN ((gfp_t)0)
#endif
#ifndef __GFP_ZERO
#define __GFP_ZERO ((__force gfp_t)0x8000u)
#endif
#ifndef __GFP_NOFAIL
#define __GFP_NOFAIL ((gfp_t)0)
#endif
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 */

24
include/linux/vmalloc.h Normal file
View File

@@ -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 */

View File

@@ -10,7 +10,7 @@ struct p_current cur = {
};
__maybe_unused struct p_current *current = &cur;
void *kmalloc(size_t size, int flags)
void *kmalloc(size_t size, gfp_t flags)
{
void *p;
@@ -31,7 +31,7 @@ struct kmem_cache *get_mem(int element_sz)
return ret;
}
void *kmem_cache_alloc(struct kmem_cache *obj, int flag)
void *kmem_cache_alloc(struct kmem_cache *obj, gfp_t flag)
{
return malloc_cache_aligned(obj->sz);
}