diff options
author | João Valverde <j@v6e.pt> | 2021-12-12 23:07:23 +0000 |
---|---|---|
committer | Wireshark GitLab Utility <gerald+gitlab-utility@wireshark.org> | 2021-12-14 11:23:05 +0000 |
commit | 77b6bca3870d5ef63303e637cc39e2cf83f49ddc (patch) | |
tree | b4fbc3d3d1121b7799cec21b6b1a496440b15111 | |
parent | 8b15d0e641bde8db440db186f0988fe3cadf761f (diff) |
Convert wmem I/O to use stdio.h
-rw-r--r-- | wsutil/wmem/wmem_strutl.c | 65 | ||||
-rw-r--r-- | wsutil/wmem/wmem_test.c | 11 |
2 files changed, 21 insertions, 55 deletions
diff --git a/wsutil/wmem/wmem_strutl.c b/wsutil/wmem/wmem_strutl.c index aaafe2bce6..0aa3f641e2 100644 --- a/wsutil/wmem/wmem_strutl.c +++ b/wsutil/wmem/wmem_strutl.c @@ -71,71 +71,26 @@ wmem_strdup_printf(wmem_allocator_t *allocator, const gchar *fmt, ...) return dst; } -/* - * Using g_printf_string_upper_bound() to find the needed length almost doubles - * the execution time of this function. Instead we use a pre allocated buffer - * which may waste a bit of memory but are faster. As this is mostly called with - * packet scoped memory(?) that shouldn't matter that much. - * In my test file all strings was less than 72 characters long and quite a few - * over 68 characters long. Chose 80 as the default. - */ -#ifndef _WIN32 #define WMEM_STRDUP_VPRINTF_DEFAULT_BUFFER 80 -gchar * -wmem_strdup_vprintf(wmem_allocator_t *allocator, const gchar *fmt, va_list ap) +char * +wmem_strdup_vprintf(wmem_allocator_t *allocator, const char *fmt, va_list ap) { va_list ap2; - gchar *dst; + char buf[WMEM_STRDUP_VPRINTF_DEFAULT_BUFFER + 1]; int needed_len; va_copy(ap2, ap); - - /* needed_len = g_printf_string_upper_bound(fmt, ap2); */ - - dst = (gchar *)wmem_alloc(allocator, WMEM_STRDUP_VPRINTF_DEFAULT_BUFFER); - - /* Returns: the number of characters which would be produced if the buffer was large enough - * (not including the null, for which we add +1 ourselves). */ - needed_len = g_vsnprintf(dst, (gulong) WMEM_STRDUP_VPRINTF_DEFAULT_BUFFER, fmt, ap2) + 1; + needed_len = vsnprintf(buf, sizeof(buf), fmt, ap2); va_end(ap2); - if (needed_len > WMEM_STRDUP_VPRINTF_DEFAULT_BUFFER) { - wmem_free(allocator, dst); - dst = (gchar *)wmem_alloc(allocator, needed_len); - va_copy(ap2, ap); - g_vsnprintf(dst, (gulong) needed_len, fmt, ap2); - va_end(ap2); - } - - return dst; -} -#else /* _WIN32 */ -/* - * GLib's v*printf routines are surprisingly slow on Windows, at least with - * GLib 2.40.0. This appears to be due to GLib using the gnulib version of - * vasnprintf when compiled under MinGW. If GLib ever ends up using the - * native Windows v*printf routines this can be removed. - */ -gchar * -wmem_strdup_vprintf(wmem_allocator_t *allocator, const gchar *fmt, va_list ap) -{ - va_list ap2; - gchar *dst; - int needed_len; - - va_copy(ap2, ap); + if (needed_len <= WMEM_STRDUP_VPRINTF_DEFAULT_BUFFER) + return wmem_strdup(allocator, buf); - needed_len = _vscprintf(fmt, ap2) + 1; - - dst = (gchar *)wmem_alloc(allocator, needed_len); - - vsprintf_s(dst, needed_len, fmt, ap2); - - va_end(ap2); - - return dst; + size_t new_buf_size = needed_len + 1; + char *new_buf = wmem_alloc(allocator, new_buf_size); + vsnprintf(new_buf, new_buf_size, fmt, ap); + return new_buf; } -#endif /* _WIN32 */ gchar * wmem_strconcat(wmem_allocator_t *allocator, const gchar *first, ...) diff --git a/wsutil/wmem/wmem_test.c b/wsutil/wmem/wmem_test.c index 4b33bf2413..b5066d493e 100644 --- a/wsutil/wmem/wmem_test.c +++ b/wsutil/wmem/wmem_test.c @@ -472,6 +472,17 @@ wmem_test_strutls(void) new_str = wmem_ascii_strdown(allocator, orig_str, -1); g_assert_cmpstr(new_str, ==, "testasciistrdown"); + orig_str = "ShortString"; + new_str = wmem_strdup_printf(allocator, "TEST %s", orig_str); + g_assert_cmpstr(new_str, ==, "TEST ShortString"); + + orig_str = "Very Long .............................." + "................................. String"; + new_str = wmem_strdup_printf(allocator, "TEST %s", orig_str); + g_assert_cmpstr(new_str, ==, + "TEST Very Long .............................." + "................................. String"); + wmem_destroy_allocator(allocator); } |