[PATCH mtd-utils v2 013/102] ubifs-utils: Add linux kernel memory allocation implementations

Zhihao Cheng chengzhihao1 at huawei.com
Sun Nov 10 23:02:16 PST 2024


Add linux kernel memory allocation implementations, because there are many
memory allocations(eg. kmalloc, kzalloc) used in UBIFS linux kernel libs.

This is a preparation for replacing implementation of UBIFS utils with
linux kernel libs.

Signed-off-by: Zhihao Cheng <chengzhihao1 at huawei.com>
---
 ubifs-utils/Makemodule.am |  2 ++
 ubifs-utils/common/kmem.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++
 ubifs-utils/common/kmem.h | 56 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 ubifs-utils/common/kmem.c
 create mode 100644 ubifs-utils/common/kmem.h

diff --git a/ubifs-utils/Makemodule.am b/ubifs-utils/Makemodule.am
index 69192ffd..13171b74 100644
--- a/ubifs-utils/Makemodule.am
+++ b/ubifs-utils/Makemodule.am
@@ -2,6 +2,8 @@ common_SOURCES = \
 	ubifs-utils/common/compiler_attributes.h \
 	ubifs-utils/common/linux_types.h \
 	ubifs-utils/common/linux_err.h \
+	ubifs-utils/common/kmem.h \
+	ubifs-utils/common/kmem.c \
 	ubifs-utils/common/defs.h \
 	ubifs-utils/common/crc16.h \
 	ubifs-utils/common/crc16.c \
diff --git a/ubifs-utils/common/kmem.c b/ubifs-utils/common/kmem.c
new file mode 100644
index 00000000..e926a137
--- /dev/null
+++ b/ubifs-utils/common/kmem.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Simple memory interface
+ */
+
+#include "compiler_attributes.h"
+#include "linux_types.h"
+#include "kmem.h"
+#include "defs.h"
+
+static void *kmem_alloc(size_t size)
+{
+	void *ptr = malloc(size);
+
+	if (ptr == NULL)
+		sys_errmsg("malloc failed (%d bytes)", (int)size);
+	return ptr;
+}
+
+static void *kmem_zalloc(size_t size)
+{
+	void *ptr = kmem_alloc(size);
+
+	if (!ptr)
+		return ptr;
+
+	memset(ptr, 0, size);
+	return ptr;
+}
+
+void *kmalloc(size_t size, gfp_t flags)
+{
+	if (flags & __GFP_ZERO)
+		return kmem_zalloc(size);
+	return kmem_alloc(size);
+}
+
+void *krealloc(void *ptr, size_t new_size, __unused gfp_t flags)
+{
+	ptr = realloc(ptr, new_size);
+	if (ptr == NULL)
+		sys_errmsg("realloc failed (%d bytes)", (int)new_size);
+	return ptr;
+}
+
+void *kmalloc_array(size_t n, size_t size, gfp_t flags)
+{
+	size_t bytes;
+
+	if (unlikely(check_mul_overflow(n, size, &bytes)))
+		return NULL;
+	return kmalloc(bytes, flags);
+}
+
+void *kmemdup(const void *src, size_t len, gfp_t gfp)
+{
+	void *p;
+
+	p = kmalloc(len, gfp);
+	if (p)
+		memcpy(p, src, len);
+
+	return p;
+}
diff --git a/ubifs-utils/common/kmem.h b/ubifs-utils/common/kmem.h
new file mode 100644
index 00000000..9fe2a364
--- /dev/null
+++ b/ubifs-utils/common/kmem.h
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2008 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ */
+#ifndef __KMEM_H__
+#define __KMEM_H__
+
+#include <stdlib.h>
+
+typedef unsigned int gfp_t;
+
+#define GFP_KERNEL	0
+#define GFP_NOFS	0
+#define __GFP_NOWARN	0
+#define __GFP_ZERO	1
+
+#define vmalloc(size)	malloc(size)
+#define vfree(ptr)	free(ptr)
+
+extern void	*kmalloc(size_t, gfp_t);
+extern void	*krealloc(void *, size_t, __attribute__((unused)) gfp_t);
+extern void	*kmalloc_array(size_t, size_t, gfp_t);
+extern void	*kmemdup(const void *src, size_t len, gfp_t gfp);
+
+static inline void kfree(const void *ptr)
+{
+	free((void *)ptr);
+}
+
+static inline void kvfree(const void *ptr)
+{
+	kfree(ptr);
+}
+
+static inline void *kvmalloc(size_t size, gfp_t flags)
+{
+	return kmalloc(size, flags);
+}
+
+static inline void *kzalloc(size_t size, gfp_t flags)
+{
+	return kmalloc(size, flags | __GFP_ZERO);
+}
+
+static inline void *__vmalloc(unsigned long size, gfp_t gfp_mask)
+{
+	return kmalloc(size, gfp_mask);
+}
+
+static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
+{
+	return kmalloc_array(n, size, flags | __GFP_ZERO);
+}
+
+#endif
-- 
2.13.6




More information about the linux-mtd mailing list