From 913e0f408c48f3baf6933807f9cfe4195fde5c75 Mon Sep 17 00:00:00 2001 From: Gerald Combs Date: Fri, 15 Jul 2016 13:59:23 -0700 Subject: Use Windows CRT string functions in some places. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copy wsutil/wsprintf.h from change 16537. Update it to use functions appropriate to Visual C++ >= 2015, < 2015, and everything else. Add notes about specific API issues. Update epan/expert.c to use ws_snprintf, since the VS profiler shows it as a minor hot spot. This reduces load time for a large-ish capture from ~14s to ~12s here. Migrate a previous column-utils change. Change-Id: Id4064b7c06c35fd447b63c73f731afee181df4f9 Reviewed-on: https://code.wireshark.org/review/16483 Reviewed-by: Gerald Combs Petri-Dish: Gerald Combs Tested-by: Petri Dish Buildbot Reviewed-by: João Valverde --- wsutil/ws_printf.h | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 wsutil/ws_printf.h (limited to 'wsutil/ws_printf.h') diff --git a/wsutil/ws_printf.h b/wsutil/ws_printf.h new file mode 100644 index 0000000000..1ee3bf69e6 --- /dev/null +++ b/wsutil/ws_printf.h @@ -0,0 +1,100 @@ +/* + * Wrappers for printf like functions. + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 2007 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __WS_PRINTF_H__ +#define __WS_PRINTF_H__ + +/* + * GLib's string utility routines are slow on windows, likely due to calling + * g_printf_string_upper_bound. Using ws_snprintf and ws_vsnprintf in hot + * code paths can speed up program execution. Otherwise you're probably safe + * sticking with g_snprintf and g_vsnprintf. + */ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef _WIN32 +#include + +#if _MSC_VER < 1900 +#include + +/* + * vsnprintf_s's return value isn't compatible with C99 vsnprintf. We don't + * return anything in order to avoid confusion. + */ + +static __inline void +ws_vsnprintf(char *buffer, size_t size_of_buffer, const char *format, va_list argptr) { + /* We could alternatively use StringCchVPrintfA */ + vsnprintf_s(buffer, size_of_buffer, _TRUNCATE, format, argptr); +} + +#else /* _MSC_VER uses UCRT */ + +/* The UCRT versions of snprintf and vsnprintf conform to C99 */ + +static __inline void +ws_vsnprintf(char *buffer, size_t size_of_buffer, const char *format, va_list argptr) +{ + vsnprintf(buffer, size_of_buffer, format, argptr); +} + +#endif /* _MSC_VER */ + +#else /* _WIN32 */ + +#include + +/* + * Use g_vsnprintf. On Linux and macOS these should be a thin wrapper around + * vsprintf. + */ + +static inline void +ws_vsnprintf(char *buffer, size_t size_of_buffer, const char *format, va_list argptr) +{ + g_vsnprintf(buffer, (gulong) size_of_buffer, format, argptr); +} + +#endif /* _WIN32 */ + +#ifdef _WIN32 +static __inline void +#else +static inline void +#endif +ws_snprintf(char *buffer, size_t size_of_buffer, const char * format, ...) { + va_list argptr; + + va_start(argptr, format); + ws_vsnprintf(buffer, size_of_buffer, format, argptr); + va_end(argptr); +} + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __WS_PRINTF_H__ */ -- cgit v1.2.1