[v6,2/5] eal/windows: hide asprintf() shim

Message ID 20210320130525.16452-3-dmitry.kozliuk@gmail.com (mailing list archive)
State Superseded, archived
Delegated to: David Marchand
Headers
Series eal/windows: do not expose POSIX symbols |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Dmitry Kozlyuk March 20, 2021, 1:05 p.m. UTC
  Make asprintf(3) implementation for Windows private to EAL, so that it's
hidden from external consumers. It is not exposed to internal consumers
either, because they don't need asprintf() and also because callers from
other modules would have no reliable way to free allocated memory.

Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
Acked-by: Khoa To <khot@microsoft.com>
---
 lib/librte_eal/common/eal_private.h | 11 +++++++++++
 lib/librte_eal/windows/eal.c        | 30 +++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
  

Comments

Thomas Monjalon March 26, 2021, 9:04 a.m. UTC | #1
20/03/2021 14:05, Dmitry Kozlyuk:
> Make asprintf(3) implementation for Windows private to EAL, so that it's
> hidden from external consumers. It is not exposed to internal consumers
> either, because they don't need asprintf() and also because callers from
> other modules would have no reliable way to free allocated memory.
> 
> Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
> Acked-by: Khoa To <khot@microsoft.com>
> ---
>  lib/librte_eal/common/eal_private.h | 11 +++++++++++
>  lib/librte_eal/windows/eal.c        | 30 +++++++++++++++++++++++++++++
>  2 files changed, 41 insertions(+)

It would be logic to remove the asprintf implementation from rte_os.h
in this patch.
  

Patch

diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h
index b8a0d20021..31eda4d2da 100644
--- a/lib/librte_eal/common/eal_private.h
+++ b/lib/librte_eal/common/eal_private.h
@@ -745,4 +745,15 @@  void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
  */
 void __rte_thread_uninit(void);
 
+/**
+ * asprintf(3) replacement for Windows.
+ */
+#ifdef RTE_EXEC_ENV_WINDOWS
+__rte_format_printf(2, 3)
+int eal_asprintf(char **buffer, const char *format, ...);
+
+#define asprintf(buffer, format, ...) \
+		eal_asprintf(buffer, format, ##__VA_ARGS__)
+#endif
+
 #endif /* _EAL_PRIVATE_H_ */
diff --git a/lib/librte_eal/windows/eal.c b/lib/librte_eal/windows/eal.c
index 2fc3d6141c..162671f9ce 100644
--- a/lib/librte_eal/windows/eal.c
+++ b/lib/librte_eal/windows/eal.c
@@ -2,6 +2,8 @@ 
  * Copyright(c) 2019 Intel Corporation
  */
 
+#include <stdarg.h>
+
 #include <fcntl.h>
 #include <io.h>
 #include <share.h>
@@ -411,6 +413,34 @@  rte_eal_init(int argc, char **argv)
 	return fctret;
 }
 
+/* Don't use MinGW asprintf() to have identical code with all toolchains. */
+int
+eal_asprintf(char **buffer, const char *format, ...)
+{
+	int size, ret;
+	va_list arg;
+
+	va_start(arg, format);
+	size = vsnprintf(NULL, 0, format, arg);
+	va_end(arg);
+	if (size < 0)
+		return -1;
+	size++;
+
+	*buffer = malloc(size);
+	if (*buffer == NULL)
+		return -1;
+
+	va_start(arg, format);
+	ret = vsnprintf(*buffer, size, format, arg);
+	va_end(arg);
+	if (ret != size - 1) {
+		free(*buffer);
+		return -1;
+	}
+	return ret;
+}
+
 int
 rte_vfio_container_dma_map(__rte_unused int container_fd,
 			__rte_unused uint64_t vaddr,