diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2018-09-07 14:51:19 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2018-09-07 14:51:19 +0200 |
commit | 91a64d5ccfd724eda5826b6f02704600a956e12e (patch) | |
tree | 468d2d95b6e2481883a7e8d46c19ce7f31d07017 /src/tc-string.c | |
parent | cb22888f08466e46b115bdef6c4ee39b9a2a983e (diff) | |
parent | 7dfa6d0b6b18d54050671ed5430b9421a165481c (diff) |
Merge branch 'release/debian/3.23-1'debian/3.23-1
Diffstat (limited to 'src/tc-string.c')
-rw-r--r-- | src/tc-string.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/tc-string.c b/src/tc-string.c index 826a9a0..112a845 100644 --- a/src/tc-string.c +++ b/src/tc-string.c @@ -12,12 +12,14 @@ # include <stddef.h> # include <stdio.h> # include <stdlib.h> +# include <time.h> #else # include <cassert> # include <cerrno> # include <cstddef> # include <cstdio> # include <cstdlib> +# include <ctime> #endif #include <libHX/defs.h> #include <libHX/init.h> @@ -194,6 +196,89 @@ static void t_split2(void) HX_zvecfree(a); } +/* avoid these being inlined */ +extern char *f_strlcpy_str(char *, const char *, size_t); +extern char *f_strlcpy_mem(char *, const char *, size_t); + +EXPORT_SYMBOL char *f_strlcpy_str(char *d, const char *s, size_t n) +{ + strncpy(d, s, n); + d[n-1] = '\0'; + return d; +} + +EXPORT_SYMBOL char *f_strlcpy_mem(char *dest, const char *src, size_t dsize) +{ + size_t slen = strlen(src); + if (slen < dsize) + return static_cast(char *, memcpy(dest, src, slen + 1)); + if (dsize > 0) { + memcpy(dest, src, dsize - 1); + dest[dsize-1] = '\0'; + } + return dest; +} + +static const char s_lorem_ipsum[] = /* 1368 chars */ +"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo " +"ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis " +"parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, " +"pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec " +"pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, " +"rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede " +"mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper " +"nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, " +"consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra " +"quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. " +"Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur " +"ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, " +"tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing " +"sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit " +"id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut " +"libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros " +"faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec " +"sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit " +"cursus nunc,"; + +static void t_strlcpy(void) +{ + static const size_t picksizes[] = + {4, 8, 16, 32, 64, 80, 128, 256, 1024, 2048}; + char ibuf[2048], obuf[2048]; + size_t ipick, opick, k, runs = 10000000 + HX_irand(0, 1); + struct timespec start, stop, d1, d2, d3; + + for (ipick = 0; ipick < ARRAY_SIZE(picksizes); ++ipick) { + /* Select string size */ + HX_strlcpy(ibuf, s_lorem_ipsum, picksizes[ipick]); + + for (opick = 0; opick < ARRAY_SIZE(picksizes); ++opick) { + /* Select buffer size */ + clock_gettime(CLOCK_MONOTONIC, &start); + for (k = 0; k < runs; ++k) + f_strlcpy_str(reinterpret_cast(char *, obuf), + ibuf, picksizes[opick]); + clock_gettime(CLOCK_MONOTONIC, &stop); + HX_timespec_sub(&d1, &stop, &start); + + clock_gettime(CLOCK_MONOTONIC, &start); + for (k = 0; k < runs; ++k) + f_strlcpy_mem(reinterpret_cast(char *, obuf), + ibuf, picksizes[opick]); + clock_gettime(CLOCK_MONOTONIC, &stop); + HX_timespec_sub(&d2, &stop, &start); + + HX_timespec_sub(&d3, &d1, &d2); + printf("%4zu->%4zu: %1ld.%09ld (str=%ld.%09ld mem=%ld.%09ld)\n", + strlen(ibuf), picksizes[opick], + static_cast(long, d3.tv_sec), d3.tv_nsec, + static_cast(long, d1.tv_sec), d1.tv_nsec, + static_cast(long, d2.tv_sec), d2.tv_nsec + ); + } + } +} + int main(int argc, const char **argv) { hxmc_t *tx = NULL; @@ -222,6 +307,7 @@ int main(int argc, const char **argv) t_strtrim(); t_split(); t_split2(); + t_strlcpy(); HXmc_free(tx); HX_exit(); return EXIT_SUCCESS; |