From a2b8e52e46cfd888ac5a48d8ce632bd70a5caa93 Mon Sep 17 00:00:00 2001
From: Tomas Bzatek <tbzatek@redhat.com>
Date: Tue, 10 Oct 2023 18:16:24 +0200
Subject: [PATCH] util: Introduce alloc helper with alignment support
Similar to nvme-cli an alloc helper is needed for a couple
of ioctls sent out during tree scan.
Signed-off-by: Tomas Bzatek <tbzatek@redhat.com>
---
src/nvme/private.h | 2 ++
src/nvme/util.c | 13 +++++++++++++
2 files changed, 15 insertions(+)
diff --git a/src/nvme/private.h b/src/nvme/private.h
index 6fb9784a..ee9d738b 100644
--- a/src/nvme/private.h
+++ b/src/nvme/private.h
@@ -182,6 +182,8 @@ nvme_ctrl_t __nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport,
const char *host_iface, const char *trsvcid,
const char *subsysnqn, nvme_ctrl_t p);
+void *__nvme_alloc(size_t len);
+
#if (LOG_FUNCNAME == 1)
#define __nvme_log_func __func__
#else
diff --git a/src/nvme/util.c b/src/nvme/util.c
index 8fe094d5..20679685 100644
--- a/src/nvme/util.c
+++ b/src/nvme/util.c
@@ -7,6 +7,7 @@
* Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
*/
+#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
@@ -1058,3 +1059,15 @@ bool nvme_iface_primary_addr_matches(const struct ifaddrs *iface_list, const cha
}
#endif /* HAVE_NETDB */
+
+void *__nvme_alloc(size_t len)
+{
+ size_t _len = round_up(len, 0x1000);
+ void *p;
+
+ if (posix_memalign((void *)&p, getpagesize(), _len))
+ return NULL;
+
+ memset(p, 0, _len);
+ return p;
+}