Changeset: d270ea19a9e5 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/d270ea19a9e5 Added Files: common/utils/mstring.c Modified Files: clients/Tests/exports.stable.out common/utils/CMakeLists.txt common/utils/mstring.h Branch: default Log Message:
Move str[lt]concat functions to mutils library. Functions with variable number of arguments are not inlined by the compiler, even when declared as such. So in order to not have a copy in every file where these functions are used, we just have a single copy in a shared library. diffs (192 lines): diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out --- a/clients/Tests/exports.stable.out +++ b/clients/Tests/exports.stable.out @@ -1360,6 +1360,8 @@ char *prompt_getlogin(void); struct dirent *readdir(DIR *dir); void rewinddir(DIR *dir); char *simple_prompt(const char *prompt, int maxlen, int echo, const char *def); +size_t strlconcat(char *restrict dst, size_t n, const char *restrict src, ...) __attribute__((__access__(write_only, 1, 2))) __attribute__(()) __attribute__((__sentinel__)); +ssize_t strtconcat(char *restrict dst, size_t n, const char *restrict src, ...) __attribute__((__access__(write_only, 1, 2))) __attribute__(()) __attribute__((__sentinel__)); char *utf16toutf8(const uint16_t *src); const uint8_t utf8d[364]; uint16_t *utf8toutf16(const char *src); diff --git a/common/utils/CMakeLists.txt b/common/utils/CMakeLists.txt --- a/common/utils/CMakeLists.txt +++ b/common/utils/CMakeLists.txt @@ -21,6 +21,7 @@ target_sources(mutils md5.c md5.h mprompt.h msabaoth.c msabaoth.h + mstring.c mutf8.c mutf8.h mutils.c mutils.h muuid.c muuid.h diff --git a/common/utils/mstring.c b/common/utils/mstring.c new file mode 100644 --- /dev/null +++ b/common/utils/mstring.c @@ -0,0 +1,63 @@ +/* + * SPDX-License-Identifier: MPL-2.0 + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * For copyright information, see the file debian/copyright. + */ + +#include "monetdb_config.h" +#include "mstring.h" +#include <stdarg.h> /* va_list etc. */ + +/* copy the NULL terminated list of src strings with a maximum of n + * bytes to dst; return the combined length of the src strings; dst is + * guaranteed to be NULL-terminated (if n > 0) */ +size_t +strlconcat(char *restrict dst, size_t n, const char *restrict src, ...) +{ + va_list ap; + size_t i = 0; + + va_start(ap, src); + while (src) { + size_t l; + if (i < n) + l = strlcpy(dst + i, src, n - i); + else + l = strlen(src); + i += l; + src = va_arg(ap, const char *); + } + va_end(ap); + return i; +} + +/* copy the NULL terminated list of src strings with a maximum of n + * bytes to dst; return -1 if the buffer was too small, else the + * combined length of the src strings; dst is guaranteed to be + * NULL-terminated (if n > 0) */ +ssize_t +strtconcat(char *restrict dst, size_t n, const char *restrict src, ...) +{ + va_list ap; + char *end = dst + n; + + if (n == 0) { + errno = ENOBUFS; + return -1; + } + va_start(ap, src); + while (src && dst) { + dst = stpecpy(dst, end, src); + src = va_arg(ap, const char *); + } + va_end(ap); + if (dst == NULL) { + errno = E2BIG; + return -1; + } + return dst - (end - n); +} diff --git a/common/utils/mstring.h b/common/utils/mstring.h --- a/common/utils/mstring.h +++ b/common/utils/mstring.h @@ -11,8 +11,7 @@ #ifndef _MSTRING_H_ #define _MSTRING_H_ -#include <stdarg.h> /* va_list etc. */ -#include <string.h> /* strlen */ +#include <string.h> #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4)) /* not on CentOS 6 (GCC 4.4.7) */ @@ -33,6 +32,18 @@ #define __nonnull__(...) #endif +#ifndef mutils_export +#if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__) +#ifndef LIBMUTILS +#define mutils_export extern __declspec(dllimport) +#else +#define mutils_export extern __declspec(dllexport) +#endif +#else +#define mutils_export extern +#endif +#endif + /* naming convention (also see Linux man page string_copying(7)): * strl*: copy string, truncating if too long, return length of source; * strt*: copy string, truncating if too long, return -1 if too long. @@ -123,58 +134,21 @@ stpecpy(char *restrict dst, char *end, c /* copy the NULL terminated list of src strings with a maximum of n * bytes to dst; return the combined length of the src strings; dst is * guaranteed to be NULL-terminated (if n > 0) */ -__attribute__((__access__(write_only, 1, 2))) -__attribute__((__nonnull__(1))) -__attribute__((__sentinel__)) -static inline size_t -strlconcat(char *restrict dst, size_t n, const char *restrict src, ...) -{ - va_list ap; - size_t i = 0; - - va_start(ap, src); - while (src) { - size_t l; - if (i < n) - l = strlcpy(dst + i, src, n - i); - else - l = strlen(src); - i += l; - src = va_arg(ap, const char *); - } - va_end(ap); - return i; -} +mutils_export size_t strlconcat(char *restrict dst, size_t n, + const char *restrict src, ...) + __attribute__((__access__(write_only, 1, 2))) + __attribute__((__nonnull__(1))) + __attribute__((__sentinel__)); /* copy the NULL terminated list of src strings with a maximum of n * bytes to dst; return -1 if the buffer was too small, else the * combined length of the src strings; dst is guaranteed to be * NULL-terminated (if n > 0) */ -__attribute__((__access__(write_only, 1, 2))) -__attribute__((__nonnull__(1))) -__attribute__((__sentinel__)) -static inline ssize_t -strtconcat(char *restrict dst, size_t n, const char *restrict src, ...) -{ - va_list ap; - char *end = dst + n; - - if (n == 0) { - errno = ENOBUFS; - return -1; - } - va_start(ap, src); - while (src && dst) { - dst = stpecpy(dst, end, src); - src = va_arg(ap, const char *); - } - va_end(ap); - if (dst == NULL) { - errno = E2BIG; - return -1; - } - return dst - (end - n); -} +mutils_export ssize_t strtconcat(char *restrict dst, size_t n, + const char *restrict src, ...) + __attribute__((__access__(write_only, 1, 2))) + __attribute__((__nonnull__(1))) + __attribute__((__sentinel__)); #ifdef __has_builtin #if __has_builtin(__builtin_expect) _______________________________________________ checkin-list mailing list -- [email protected] To unsubscribe send an email to [email protected]
