LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Yunsheng Lin <linyunsheng@huawei.com>
To: <davem@davemloft.net>, <kuba@kernel.org>, <mst@redhat.com>,
<jasowang@redhat.com>
Cc: <nickhu@andestech.com>, <green.hu@gmail.com>,
<deanbo422@gmail.com>, <akpm@linux-foundation.org>,
<yury.norov@gmail.com>, <andriy.shevchenko@linux.intel.com>,
<ojeda@kernel.org>, <ndesaulniers@gooogle.com>, <joe@perches.com>,
<linux-kernel@vger.kernel.org>,
<virtualization@lists.linux-foundation.org>,
<netdev@vger.kernel.org>, <linuxarm@openeuler.org>
Subject: [PATCH v2 2/4] tools headers UAPI: add kmalloc/vmalloc related interface
Date: Tue, 20 Jul 2021 10:21:47 +0800 [thread overview]
Message-ID: <1626747709-34013-3-git-send-email-linyunsheng@huawei.com> (raw)
In-Reply-To: <1626747709-34013-1-git-send-email-linyunsheng@huawei.com>
Implement the kmalloc/vmalloc related interface based on
malloc interface in user space.
Signed-off-by: Yunsheng Lin <linyunsheng@huawei.com>
---
tools/include/linux/gfp.h | 2 ++
tools/include/linux/slab.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+)
create mode 100644 tools/include/linux/slab.h
diff --git a/tools/include/linux/gfp.h b/tools/include/linux/gfp.h
index 2203075..a660ab9 100644
--- a/tools/include/linux/gfp.h
+++ b/tools/include/linux/gfp.h
@@ -1,4 +1,6 @@
#ifndef _TOOLS_INCLUDE_LINUX_GFP_H
#define _TOOLS_INCLUDE_LINUX_GFP_H
+#define __GFP_ZERO 0x100u
+
#endif /* _TOOLS_INCLUDE_LINUX_GFP_H */
diff --git a/tools/include/linux/slab.h b/tools/include/linux/slab.h
new file mode 100644
index 0000000..f0b7da6
--- /dev/null
+++ b/tools/include/linux/slab.h
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __TOOLS_LINUX_SLAB_H
+#define __TOOLS_LINUX_SLAB_H
+
+#include <linux/gfp.h>
+#include <linux/cache.h>
+
+static inline void *kmalloc(size_t size, gfp_t gfp)
+{
+ void *p;
+
+ p = memalign(SMP_CACHE_BYTES, size);
+ if (!p)
+ return p;
+
+ if (gfp & __GFP_ZERO)
+ memset(p, 0, size);
+
+ return p;
+}
+
+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)
+{
+ return kmalloc(n * size, flags);
+}
+
+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(void *p)
+{
+ free(p);
+}
+
+#define kvmalloc_array kmalloc_array
+#define kvfree kfree
+#define KMALLOC_MAX_SIZE SIZE_MAX
+
+#endif
--
2.7.4
next prev parent reply other threads:[~2021-07-20 2:41 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-20 2:21 [PATCH v2 0/4] refactor the ringtest testing for ptr_ring Yunsheng Lin
2021-07-20 2:21 ` [PATCH v2 1/4] tools headers UAPI: add cache aligning related macro Yunsheng Lin
2021-07-20 2:21 ` Yunsheng Lin [this message]
2021-07-20 2:21 ` [PATCH v2 3/4] tools headers UAPI: add cpu_relax() implementation for x86 and arm64 Yunsheng Lin
2021-07-21 20:53 ` David Laight
2021-07-22 8:18 ` Yunsheng Lin
2021-07-22 8:45 ` David Laight
2021-07-20 2:21 ` [PATCH v2 4/4] tools/virtio: use common infrastructure to build ptr_ring.h Yunsheng Lin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1626747709-34013-3-git-send-email-linyunsheng@huawei.com \
--to=linyunsheng@huawei.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=davem@davemloft.net \
--cc=deanbo422@gmail.com \
--cc=green.hu@gmail.com \
--cc=jasowang@redhat.com \
--cc=joe@perches.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxarm@openeuler.org \
--cc=mst@redhat.com \
--cc=ndesaulniers@gooogle.com \
--cc=netdev@vger.kernel.org \
--cc=nickhu@andestech.com \
--cc=ojeda@kernel.org \
--cc=virtualization@lists.linux-foundation.org \
--cc=yury.norov@gmail.com \
--subject='Re: [PATCH v2 2/4] tools headers UAPI: add kmalloc/vmalloc related interface' \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).