From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 7A1E4A0547; Sat, 6 Mar 2021 01:05:27 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 026EC22A32A; Sat, 6 Mar 2021 01:05:14 +0100 (CET) Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) by mails.dpdk.org (Postfix) with ESMTP id 0EB4C22A32C for ; Sat, 6 Mar 2021 01:05:13 +0100 (CET) Received: by mail-lj1-f179.google.com with SMTP id k12so5168918ljg.9 for ; Fri, 05 Mar 2021 16:05:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hcF6941jVAuCLclcTM4kYTpSSBaCiWZoxfpG7ZlurO0=; b=lU5qfSeT99ifl19JT+UjUFjjHaq3SxqMVtO9oXmvnk8u4PeTPz8XRqC2XRwv8ba0uc 9+81SAMHue8AV0STBm5RJlVXX79tlYcJXb99KLGd+/pzL5AweOp+6eYHC0evPkrMH4Ka IAjwepY72pl+pywJaUCiteEJEFxtqHUAZ4cYM0DmwaOUR1cLnjbTL/fqBEJz5k5hiH7v C7ZNrFJpmwOX6x3/sB2uALqJuN5WZFit+9fwjk+UybV9vvJe2hxsEA8zFDNMkWoNqzXY f9uG+esN8ixk2b1EFURzpD0AV6Nrn1qDfNEjiA7Op1c+7BZFjMPSMofIa+pP47Qu2BEK qj5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hcF6941jVAuCLclcTM4kYTpSSBaCiWZoxfpG7ZlurO0=; b=fWoe/qJYqbN6Rgdxeq0ND/JsWn7ropxQ+irK2corYk7hiHNXZZ+xY1zDLQ13UrWK11 +DXUDG3RNAbaMS6VgFZE0+md89hd1EFk+B5OSKImL8qtSlpyTqEF9tPxj6FbgPY7ycPP o/qggy0VwOF3h1f9RN28CjKXjE+recCTIsQj+f0dCPabs3qdR0N6JEAXP0Vavqui3UY0 XwkOXLPyE3d2mfZRuxgawrwdRiV5zM4bDGeIEZwInmcQFyxEqvSggQf/Z0rIouvajEhz 0QVj95VfrUdRbwqDvm6oE3IaUGVIflsdemzct0DAoeYpp8NJIF5i3yOOgCKiLXQ/FjHa Dp2w== X-Gm-Message-State: AOAM531RQAjhCTgOgPAL3HH00VAvTadxFxNNNJNlBi6NmeDqqM+MkdwL JAUYvfKIcTsSkhISqAUoR3XLUeloouKPSw== X-Google-Smtp-Source: ABdhPJxEFyGb2APRPKJ5H5K+Zf8PqEuJ7N9If/xrEGUatB/1EOgTdzK1VegCdjPeK+OapE3WiSHGRw== X-Received: by 2002:a2e:9084:: with SMTP id l4mr6543737ljg.498.1614989112351; Fri, 05 Mar 2021 16:05:12 -0800 (PST) Received: from localhost.localdomain (broadband-37-110-65-23.ip.moscow.rt.ru. [37.110.65.23]) by smtp.gmail.com with ESMTPSA id s22sm494645ljd.28.2021.03.05.16.05.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Mar 2021 16:05:11 -0800 (PST) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: Nick Connolly , Khoa To , Dmitry Kozlyuk , Jerin Jacob , Sunil Kumar Kori , Narcisa Ana Maria Vasile , Dmitry Malloy , Pallavi Kadam Date: Sat, 6 Mar 2021 03:04:58 +0300 Message-Id: <20210306000500.8698-3-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210306000500.8698-1-dmitry.kozliuk@gmail.com> References: <20210221142819.6769-1-dmitry.kozliuk@gmail.com> <20210306000500.8698-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v4 2/4] eal: add asprintf() internal wrapper X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" POSIX asprintf() is unavailable on Windows. Add eal_asprintf() wrapper for EAL internal use. On Windows it's a function, on Unix it's a macro for asprintf(). Signed-off-by: Dmitry Kozlyuk Acked-by: Khoa To --- lib/librte_eal/common/eal_common_lcore.c | 2 +- lib/librte_eal/common/eal_common_options.c | 8 ++--- lib/librte_eal/common/eal_common_trace.c | 2 +- lib/librte_eal/common/eal_common_trace_ctf.c | 2 +- .../common/eal_common_trace_utils.c | 2 +- lib/librte_eal/common/eal_private.h | 18 +++++++++++ lib/librte_eal/windows/eal.c | 30 +++++++++++++++++++ 7 files changed, 56 insertions(+), 8 deletions(-) diff --git a/lib/librte_eal/common/eal_common_lcore.c b/lib/librte_eal/common/eal_common_lcore.c index 66d6bad1a..db16a34cc 100644 --- a/lib/librte_eal/common/eal_common_lcore.c +++ b/lib/librte_eal/common/eal_common_lcore.c @@ -282,7 +282,7 @@ rte_lcore_callback_register(const char *name, rte_lcore_init_cb init, callback = calloc(1, sizeof(*callback)); if (callback == NULL) return NULL; - if (asprintf(&callback->name, "%s-%p", name, arg) == -1) { + if (eal_asprintf(&callback->name, "%s-%p", name, arg) == -1) { free(callback); return NULL; } diff --git a/lib/librte_eal/common/eal_common_options.c b/lib/librte_eal/common/eal_common_options.c index 622c7bc42..230bac9f3 100644 --- a/lib/librte_eal/common/eal_common_options.c +++ b/lib/librte_eal/common/eal_common_options.c @@ -1435,7 +1435,7 @@ available_cores(void) return NULL; /* first sequence */ - if (asprintf(&str, "%d", idx) < 0) + if (eal_asprintf(&str, "%d", idx) < 0) return NULL; previous = idx; sequence = 0; @@ -1452,7 +1452,7 @@ available_cores(void) /* finish current sequence */ if (sequence) { - if (asprintf(&tmp, "%s-%d", str, previous) < 0) { + if (eal_asprintf(&tmp, "%s-%d", str, previous) < 0) { free(str); return NULL; } @@ -1461,7 +1461,7 @@ available_cores(void) } /* new sequence */ - if (asprintf(&tmp, "%s,%d", str, idx) < 0) { + if (eal_asprintf(&tmp, "%s,%d", str, idx) < 0) { free(str); return NULL; } @@ -1473,7 +1473,7 @@ available_cores(void) /* finish last sequence */ if (sequence) { - if (asprintf(&tmp, "%s-%d", str, previous) < 0) { + if (eal_asprintf(&tmp, "%s-%d", str, previous) < 0) { free(str); return NULL; } diff --git a/lib/librte_eal/common/eal_common_trace.c b/lib/librte_eal/common/eal_common_trace.c index 24e27387b..d57bb8ecc 100644 --- a/lib/librte_eal/common/eal_common_trace.c +++ b/lib/librte_eal/common/eal_common_trace.c @@ -435,7 +435,7 @@ __rte_trace_point_emit_field(size_t sz, const char *in, const char *datatype) fixup = trace_metadata_fixup_field(in); if (fixup != NULL) in = fixup; - rc = asprintf(&field, "%s %s %s;\n", + rc = eal_asprintf(&field, "%s %s %s;\n", RTE_PER_LCORE(ctf_field) != NULL ? RTE_PER_LCORE(ctf_field) : "", datatype, in); diff --git a/lib/librte_eal/common/eal_common_trace_ctf.c b/lib/librte_eal/common/eal_common_trace_ctf.c index 33e419aac..f64ca9496 100644 --- a/lib/librte_eal/common/eal_common_trace_ctf.c +++ b/lib/librte_eal/common/eal_common_trace_ctf.c @@ -389,7 +389,7 @@ char *trace_metadata_fixup_field(const char *field) for (i = 0; i < RTE_DIM(ctf_reserved_words); i++) { if (strcmp(field, ctf_reserved_words[i]) != 0) continue; - if (asprintf(&out, "_%s", ctf_reserved_words[i]) == -1) + if (eal_asprintf(&out, "_%s", ctf_reserved_words[i]) == -1) out = NULL; return out; } diff --git a/lib/librte_eal/common/eal_common_trace_utils.c b/lib/librte_eal/common/eal_common_trace_utils.c index 64f58fb66..e32237b8e 100644 --- a/lib/librte_eal/common/eal_common_trace_utils.c +++ b/lib/librte_eal/common/eal_common_trace_utils.c @@ -253,7 +253,7 @@ eal_trace_dir_args_save(char const *val) return -ENAMETOOLONG; } - if (asprintf(&dir_path, "%s/", val) == -1) { + if (eal_asprintf(&dir_path, "%s/", val) == -1) { trace_err("failed to copy directory: %s", strerror(errno)); return -ENOMEM; } diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index b8a0d2002..323240dd7 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -745,4 +745,22 @@ void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset); */ void __rte_thread_uninit(void); +/** + * Allocate a buffer large enough to hold the formatted string + * and perform formatting, equivalent to Unix asprintf(3). + * + * @param buffer + * Receives a pointer to allocated memory, call free(buffer) to deallocate. + * @param format + * Format string. + * @return + * Number of bytes allocated on success, (-1) on failure. + */ +#ifdef RTE_EXEC_ENV_WINDOWS +__rte_format_printf(2, 3) +int eal_asprintf(char **buffer, const char *format, ...); +#else +#define eal_asprintf asprintf +#endif + #endif /* _EAL_PRIVATE_H_ */ diff --git a/lib/librte_eal/windows/eal.c b/lib/librte_eal/windows/eal.c index 2fc3d6141..162671f9c 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 + #include #include #include @@ -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, -- 2.29.2