From 3a661dbe0e9425e7d9a1aee6aead485f76f093cd Mon Sep 17 00:00:00 2001 From: He Xian Date: Tue, 18 Feb 2025 16:15:15 +0800 Subject: [PATCH] contrib: sync csnippets Signed-off-by: He Xian --- contrib/csnippets/algo/cityhash.c | 34 ++++++---- contrib/csnippets/net/addr.c | 2 +- contrib/csnippets/net/http.c | 8 +-- contrib/csnippets/net/url.c | 20 +++--- contrib/csnippets/utils/buffer.c | 53 ++++++++------- contrib/csnippets/utils/buffer.h | 11 ++- contrib/csnippets/utils/debug.c | 12 ++-- contrib/csnippets/utils/formats.c | 109 ++++++++++++++++-------------- contrib/csnippets/utils/formats.h | 12 ++-- contrib/csnippets/utils/slog.c | 23 ++++--- 10 files changed, 153 insertions(+), 131 deletions(-) diff --git a/contrib/csnippets/algo/cityhash.c b/contrib/csnippets/algo/cityhash.c index 137c30d..932351f 100644 --- a/contrib/csnippets/algo/cityhash.c +++ b/contrib/csnippets/algo/cityhash.c @@ -85,7 +85,7 @@ static inline uint64_t shift_mix(const uint64_t v) // Hash 128 input bits down to 64 bits of output. // This is intended to be a reasonably good hash function. -static inline uint64_t Hash128to64(const uint64_t x[2]) +static inline uint64_t Hash128to64(const uint64_t x[restrict 2]) { // Murmur-inspired hashing. const uint64_t kMul = 0x9ddfea08eb382d69ULL; @@ -113,7 +113,7 @@ static inline uint64_t HashLen16mul(uint64_t u, uint64_t v, uint64_t mul) return b; } -static uint64_t HashLen0to16(const unsigned char *s, size_t len) +static uint64_t HashLen0to16(const unsigned char *restrict s, size_t len) { if (len >= 8) { uint64_t mul = k2 + len * 2; @@ -142,7 +142,8 @@ static uint64_t HashLen0to16(const unsigned char *s, size_t len) // This probably works well for 16-byte strings as well, but it may be overkill // in that case. -static inline uint64_t HashLen17to32(const unsigned char *s, size_t len) +static inline uint64_t +HashLen17to32(const unsigned char *restrict s, size_t len) { uint64_t mul = k2 + len * 2; uint64_t a = read_uint64(s) * k1; @@ -181,7 +182,8 @@ static inline void WeakHashLen32WithSeedsStr( } // Return an 8-byte hash for 33 to 64 bytes. -static inline uint64_t HashLen33to64(const unsigned char *s, size_t len) +static inline uint64_t +HashLen33to64(const unsigned char *restrict s, size_t len) { uint64_t mul = k2 + len * 2; uint64_t a = read_uint64(s) * k2; @@ -203,7 +205,7 @@ static inline uint64_t HashLen33to64(const unsigned char *s, size_t len) return b + x; } -static uint64_t CityHash64(const void *ptr, size_t len) +static uint64_t CityHash64(const void *restrict ptr, size_t len) { const unsigned char *s = ptr; if (len <= 32) { @@ -248,13 +250,14 @@ static uint64_t CityHash64(const void *ptr, size_t len) HashLen16(v[1], w[1]) + x); } -static uint64_t -CityHash64WithSeeds(const void *ptr, size_t len, uint64_t seed0, uint64_t seed1) +static uint64_t CityHash64WithSeeds( + const void *restrict ptr, size_t len, uint64_t seed0, uint64_t seed1) { return HashLen16(CityHash64(ptr, len) - seed0, seed1); } -static uint64_t CityHash64WithSeed(const void *ptr, size_t len, uint64_t seed) +static uint64_t +CityHash64WithSeed(const void *restrict ptr, size_t len, uint64_t seed) { return CityHash64WithSeeds(ptr, len, k2, seed); } @@ -297,8 +300,8 @@ static void CityMurmur( } static void CityHash128WithSeed( - unsigned char hash[16], const void *ptr, size_t len, - const unsigned char seed[16]) + unsigned char hash[restrict 16], const void *restrict ptr, size_t len, + const unsigned char seed[restrict 16]) { const unsigned char *s = ptr; if (len < 128) { @@ -385,20 +388,21 @@ static void CityHash128(unsigned char hash[16], const void *ptr, size_t len) } */ -uint64_t cityhash64_64(const void *ptr, const size_t len, const uint64_t seed) +uint64_t +cityhash64_64(const void *restrict ptr, const size_t len, const uint64_t seed) { return CityHash64WithSeed(ptr, len, seed); } -uint32_t -cityhash64low_32(const void *ptr, const size_t len, const uint32_t seed) +uint32_t cityhash64low_32( + const void *restrict ptr, const size_t len, const uint32_t seed) { return (uint32_t)CityHash64WithSeed(ptr, len, seed); } void cityhash128_128( - unsigned char hash[16], const void *ptr, const size_t len, - const unsigned char seed[16]) + unsigned char hash[restrict 16], const void *restrict ptr, + const size_t len, const unsigned char seed[restrict 16]) { CityHash128WithSeed(hash, ptr, len, seed); } diff --git a/contrib/csnippets/net/addr.c b/contrib/csnippets/net/addr.c index 03a9287..ee09a7b 100644 --- a/contrib/csnippets/net/addr.c +++ b/contrib/csnippets/net/addr.c @@ -7,7 +7,7 @@ #include #include -bool splithostport(char *str, char **host, char **port) +bool splithostport(char *str, char **restrict host, char **restrict port) { char *service = strrchr(str, ':'); if (service == NULL) { diff --git a/contrib/csnippets/net/http.c b/contrib/csnippets/net/http.c index 9153db8..e28d0ad 100644 --- a/contrib/csnippets/net/http.c +++ b/contrib/csnippets/net/http.c @@ -60,7 +60,7 @@ static const struct { "The gateway did not receive a timely response from the upstream server or application." }, }; -static char *skip_whitespace(char *s) +static char *skip_whitespace(char *restrict s) { while (*s == ' ' || *s == '\t') { ++s; @@ -124,12 +124,12 @@ char *http_parsehdr(char *buf, char **key, char **value) return next; } -size_t http_date(char *buf, const size_t buf_size) +size_t http_date(char *restrict buf, const size_t buf_size) { /* RFC 7231: Section 7.1.1.1 */ static const char fmt[] = "%a, %d %b %Y %H:%M:%S GMT"; const time_t now = time(NULL); - const struct tm *gmt = gmtime(&now); + const struct tm *restrict gmt = gmtime(&now); return strftime(buf, buf_size, fmt, gmt); } @@ -143,7 +143,7 @@ const char *http_status(const uint_least16_t code) return NULL; } -int http_error(char *buf, size_t buf_size, const uint_least16_t code) +int http_error(char *restrict buf, size_t buf_size, const uint_least16_t code) { const char *name = NULL, *info = NULL; for (size_t i = 0; i < ARRAY_SIZE(http_resp); i++) { diff --git a/contrib/csnippets/net/url.c b/contrib/csnippets/net/url.c index 319a669..528e3c5 100644 --- a/contrib/csnippets/net/url.c +++ b/contrib/csnippets/net/url.c @@ -9,7 +9,7 @@ #include #include -static void hex(char *p, uint_fast8_t c) +static void hex(char *restrict p, const uint_fast8_t c) { static const char hex[] = "0123456789ABCDEF"; p[1] = hex[c & UINT8_C(0xF)]; @@ -150,15 +150,15 @@ size_t url_escape_path(char *buf, size_t buf_size, const char *path) return escape(buf, buf_size, path, SIZE_MAX, "-_.~$&+,/:;=@", false); } -size_t url_escape_query(char *buf, size_t buf_size, const char *restrict query) +size_t url_escape_query(char *buf, size_t buf_size, const char *query) { const size_t cap = buf_size; for (;;) { - const char *restrict next = strchr(query, '&'); + const char *next = strchr(query, '&'); if (next == NULL) { next = query + strlen(query); } - const char *restrict eq = memchr(query, '=', next - query); + const char *eq = memchr(query, '=', next - query); if (eq == NULL) { return 0; } @@ -270,9 +270,9 @@ static bool unescape(char *str, const bool space) return true; } -static inline char *strlower(char *s) +static inline char *strlower(char *restrict s) { - for (unsigned char *restrict p = (unsigned char *)s; *p != '\0'; ++p) { + for (unsigned char *p = (unsigned char *)s; *p != '\0'; ++p) { *p = tolower(*p); } return s; @@ -372,7 +372,7 @@ bool url_parse(char *raw, struct url *restrict url) return true; } -bool url_path_segment(char **path, char **segment) +bool url_path_segment(char **restrict path, char **restrict segment) { char *s = *path; while (*s == '/') { @@ -391,7 +391,8 @@ bool url_path_segment(char **path, char **segment) return true; } -bool url_query_component(char **query, struct url_query_component *comp) +bool url_query_component( + char **restrict query, struct url_query_component *restrict comp) { char *s = *query; char *next = strchr(s, '&'); @@ -420,7 +421,8 @@ bool url_query_component(char **query, struct url_query_component *comp) return true; } -bool url_unescape_userinfo(char *raw, char **username, char **password) +bool url_unescape_userinfo( + char *raw, char **restrict username, char **restrict password) { const char valid_chars[] = "-._:~!$&\'()*+,;=%@'"; char *colon = NULL; diff --git a/contrib/csnippets/utils/buffer.c b/contrib/csnippets/utils/buffer.c index f7939a9..3c5fa13 100644 --- a/contrib/csnippets/utils/buffer.c +++ b/contrib/csnippets/utils/buffer.c @@ -10,21 +10,22 @@ #include #include -int buf_vappendf(struct buffer *restrict buf, const char *format, va_list args) +int buf_vappendf( + struct buffer *restrict buf, const char *restrict format, va_list args) { - char *b = (char *)(buf->data + buf->len); const size_t maxlen = buf->cap - buf->len; if (maxlen == 0) { return 0; } - const int ret = vsnprintf(b, maxlen, format, args); + char *restrict s = (char *)(buf->data + buf->len); + const int ret = vsnprintf(s, maxlen, format, args); if (ret > 0) { buf->len += MIN((size_t)ret, maxlen - 1); } return ret; } -int buf_appendf(struct buffer *restrict buf, const char *format, ...) +int buf_appendf(struct buffer *restrict buf, const char *restrict format, ...) { va_list args; va_start(args, format); @@ -34,7 +35,7 @@ int buf_appendf(struct buffer *restrict buf, const char *format, ...) } struct vbuffer * -vbuf_grow(struct vbuffer *restrict vbuf, const size_t want, const size_t maxcap) +vbuf_grow(struct vbuffer *vbuf, const size_t want, const size_t maxcap) { size_t cap = 0, len = 0; if (vbuf != NULL) { @@ -63,8 +64,7 @@ vbuf_grow(struct vbuffer *restrict vbuf, const size_t want, const size_t maxcap) cap += grow; } while (cap < want); - struct vbuffer *restrict newbuf = - realloc(vbuf, sizeof(struct vbuffer) + cap); + struct vbuffer *newbuf = realloc(vbuf, sizeof(struct vbuffer) + cap); if (newbuf == NULL) { /* retry with minimal required capacity */ cap = want; @@ -79,7 +79,7 @@ vbuf_grow(struct vbuffer *restrict vbuf, const size_t want, const size_t maxcap) } struct vbuffer * -vbuf_append(struct vbuffer *restrict vbuf, const void *data, size_t n) +vbuf_append(struct vbuffer *restrict vbuf, const void *restrict data, size_t n) { if (n == 0) { return vbuf; @@ -95,28 +95,33 @@ vbuf_append(struct vbuffer *restrict vbuf, const void *data, size_t n) if (n > vbuf->cap - vbuf->len) { n = vbuf->cap - vbuf->len; } - (void)memcpy(vbuf->data + vbuf->len, data, n); + unsigned char *restrict b = vbuf->data + vbuf->len; + (void)memcpy(b, data, n); vbuf->len += n; /* null-byte is reserved by vbuf_alloc() */ - vbuf->data[vbuf->len] = '\0'; + b[n] = '\0'; return vbuf; } -struct vbuffer * -vbuf_vappendf(struct vbuffer *restrict vbuf, const char *format, va_list args) +struct vbuffer *vbuf_vappendf( + struct vbuffer *restrict vbuf, const char *restrict format, + va_list args) { if (vbuf->cap == vbuf->len) { /* allocation failure occurred, skip */ return vbuf; } - char *b = (char *)(vbuf->data + vbuf->len); - size_t maxlen = vbuf->cap - vbuf->len; - va_list args0; - va_copy(args0, args); /* null-byte is reserved by vbuf_alloc() */ - int ret = vsnprintf(b, maxlen + 1, format, args0); - va_end(args0); + int ret; + { + char *restrict s = (char *)(vbuf->data + vbuf->len); + const size_t maxlen = vbuf->cap - vbuf->len; + va_list args0; + va_copy(args0, args); + ret = vsnprintf(s, maxlen + 1, format, args0); + va_end(args0); + } if (ret <= 0) { return vbuf; } @@ -129,9 +134,9 @@ vbuf_vappendf(struct vbuffer *restrict vbuf, const char *format, va_list args) } vbuf = vbuf_grow(vbuf, want, SIZE_MAX); /* when failed, append as much as possible */ - maxlen = vbuf->cap - vbuf->len; - b = (char *)(vbuf->data + vbuf->len); - ret = vsnprintf(b, maxlen + 1, format, args); + char *restrict s = (char *)(vbuf->data + vbuf->len); + const size_t maxlen = vbuf->cap - vbuf->len; + ret = vsnprintf(s, maxlen + 1, format, args); if (ret > 0) { if ((size_t)ret < maxlen) { vbuf->len += (size_t)ret; @@ -143,11 +148,11 @@ vbuf_vappendf(struct vbuffer *restrict vbuf, const char *format, va_list args) } struct vbuffer * -vbuf_appendf(struct vbuffer *restrict vbuf, const char *format, ...) +vbuf_appendf(struct vbuffer *restrict vbuf, const char *restrict format, ...) { va_list args; va_start(args, format); - struct vbuffer *ret = vbuf_vappendf(vbuf, format, args); + vbuf = vbuf_vappendf(vbuf, format, args); va_end(args); - return ret; + return vbuf; } diff --git a/contrib/csnippets/utils/buffer.h b/contrib/csnippets/utils/buffer.h index 968ca45..5037cb2 100644 --- a/contrib/csnippets/utils/buffer.h +++ b/contrib/csnippets/utils/buffer.h @@ -35,14 +35,14 @@ struct buffer { /** @internal */ static inline size_t -buf_append(struct buffer *restrict buf, const void *data, size_t n) +buf_append(struct buffer *restrict buf, const void *restrict data, size_t n) { n = MIN(n, buf->cap - buf->len); if (n == 0) { return 0; } - unsigned char *b = buf->data + buf->len; - (void)memcpy(b, data, n); + void *restrict dest = buf->data + buf->len; + (void)memcpy(dest, data, n); buf->len += n; return n; } @@ -60,8 +60,7 @@ struct vbuffer { }; /** @internal */ -static inline struct vbuffer * -vbuf_alloc(struct vbuffer *restrict vbuf, const size_t cap) +static inline struct vbuffer *vbuf_alloc(struct vbuffer *vbuf, const size_t cap) { if (cap == 0) { free(vbuf); @@ -69,7 +68,7 @@ vbuf_alloc(struct vbuffer *restrict vbuf, const size_t cap) } const size_t len = (vbuf != NULL) ? vbuf->len : 0; /* reserve 1 byte for null terminator */ - struct vbuffer *restrict newbuf = + struct vbuffer *newbuf = realloc(vbuf, sizeof(struct vbuffer) + cap + 1); if (newbuf == NULL) { return vbuf; diff --git a/contrib/csnippets/utils/debug.c b/contrib/csnippets/utils/debug.c index 3620aec..9b61b22 100644 --- a/contrib/csnippets/utils/debug.c +++ b/contrib/csnippets/utils/debug.c @@ -29,7 +29,7 @@ #define INDENT " " -void slog_extra_txt(FILE *f, void *data) +void slog_extra_txt(FILE *restrict f, void *data) { const struct slog_extra_txt *restrict extra = data; size_t n = extra->len; @@ -118,7 +118,7 @@ void slog_extra_txt(FILE *f, void *data) } } -void slog_extra_bin(FILE *f, void *data) +void slog_extra_bin(FILE *restrict f, void *data) { const struct slog_extra_bin *restrict extra = data; size_t n = extra->len; @@ -237,7 +237,7 @@ static struct backtrace_state *bt_state(void) } #endif -int debug_backtrace(void **frames, int skip, const int len) +int debug_backtrace(void **restrict frames, int skip, const int len) { assert(frames != NULL && len > 0); skip++; @@ -275,8 +275,8 @@ int debug_backtrace(void **frames, int skip, const int len) #endif } -static void -slog_extra_stack_default(FILE *f, struct slog_extra_stack *restrict extra) +static void slog_extra_stack_default( + FILE *restrict f, struct slog_extra_stack *restrict extra) { int index = 1; for (size_t i = 0; i < extra->len; i++) { @@ -285,7 +285,7 @@ slog_extra_stack_default(FILE *f, struct slog_extra_stack *restrict extra) } } -void slog_extra_stack(FILE *f, void *data) +void slog_extra_stack(FILE *restrict f, void *data) { struct slog_extra_stack *restrict extra = data; #if WITH_LIBBACKTRACE diff --git a/contrib/csnippets/utils/formats.c b/contrib/csnippets/utils/formats.c index 738c1b6..dfa6b1c 100644 --- a/contrib/csnippets/utils/formats.c +++ b/contrib/csnippets/utils/formats.c @@ -10,27 +10,28 @@ #include #include -static int format_abnormal(char *buf, const size_t bufsize, const double value) +static int +format_abnormal(char *restrict s, const size_t maxlen, const double value) { if (isnan(value)) { if (signbit(value)) { - return snprintf(buf, bufsize, "%s", "-nan"); + return snprintf(s, maxlen, "%s", "-nan"); } - return snprintf(buf, bufsize, "%s", "nan"); + return snprintf(s, maxlen, "%s", "nan"); } if (!isfinite(value)) { if (signbit(value)) { - return snprintf(buf, bufsize, "%s", "-inf"); + return snprintf(s, maxlen, "%s", "-inf"); } - return snprintf(buf, bufsize, "%s", "inf"); + return snprintf(s, maxlen, "%s", "inf"); } if (value == 0.0) { if (signbit(value)) { - return snprintf(buf, bufsize, "%s", "-0"); + return snprintf(s, maxlen, "%s", "-0"); } - return snprintf(buf, bufsize, "%s", "0"); + return snprintf(s, maxlen, "%s", "0"); } - return snprintf(buf, bufsize, "%e", value); + return snprintf(s, maxlen, "%e", value); } static char *si_prefix_pos[] = { @@ -41,54 +42,52 @@ static char *si_prefix_neg[] = { "m", u8"μ", "n", "p", "f", "a", "z", "y", "r", "q", }; -int format_si_prefix(char *buf, const size_t bufsize, const double value) +int format_si_prefix(char *restrict s, const size_t maxlen, const double value) { if (!isnormal(value)) { - return format_abnormal(buf, bufsize, value); + return format_abnormal(s, maxlen, value); } const double absvalue = fabs(value); if (!(1e-30 < absvalue && absvalue < 1e+31)) { - return snprintf(buf, bufsize, "%.2e", value); + return snprintf(s, maxlen, "%.2e", value); } const int e = (int)floor(log10(absvalue) / 3.0); if (e == 0) { - return snprintf(buf, bufsize, "%.3g", value); + return snprintf(s, maxlen, "%.3g", value); } if (e < 0) { const size_t i = MIN((size_t)-e, ARRAY_SIZE(si_prefix_neg)); const double v = value / pow(10, -3.0 * (double)i); const char *prefix = si_prefix_neg[i - 1]; - return snprintf(buf, bufsize, "%.3g%s", v, prefix); + return snprintf(s, maxlen, "%.3g%s", v, prefix); } const size_t i = MIN((size_t)e, ARRAY_SIZE(si_prefix_pos)); const double v = value / pow(10, 3.0 * (double)i); const char *prefix = si_prefix_pos[i - 1]; - return snprintf(buf, bufsize, "%.3g%s", v, prefix); + return snprintf(s, maxlen, "%.3g%s", v, prefix); } static const char *iec_units[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", }; -int format_iec_bytes(char *buf, const size_t bufsize, const double value) +int format_iec_bytes(char *restrict s, const size_t maxlen, const double value) { if (!isnormal(value)) { - return format_abnormal(buf, bufsize, value); + return format_abnormal(s, maxlen, value); } const int e = ((int)log2(fabs(value)) - 1) / 10; const int i = CLAMP(e, 0, (int)ARRAY_SIZE(iec_units) - 1); const double v = ldexp(value, i * -10); if (i > 0) { if (-10.0 < v && v < 10.0) { - return snprintf( - buf, bufsize, "%.2f%s", v, iec_units[i]); + return snprintf(s, maxlen, "%.2f%s", v, iec_units[i]); } if (-100.0 < v && v < 100.0) { - return snprintf( - buf, bufsize, "%.1f%s", v, iec_units[i]); + return snprintf(s, maxlen, "%.1f%s", v, iec_units[i]); } } - return snprintf(buf, bufsize, "%.0f%s", v, iec_units[i]); + return snprintf(s, maxlen, "%.0f%s", v, iec_units[i]); } struct duration make_duration(double value) @@ -140,73 +139,76 @@ struct duration make_duration_nanos(int_least64_t value) #define SIGNED_STR(sign, literal) ((sign) < 0 ? "-" literal : (literal)) -int format_duration_seconds(char *b, const size_t size, const struct duration d) +int format_duration_seconds( + char *restrict s, const size_t maxlen, const struct duration d) { if (d.day) { return snprintf( - b, size, SIGNED_STR(d.sign, "%ud%02u:%02u:%02u"), d.day, - d.hour, d.minute, d.second); + s, maxlen, SIGNED_STR(d.sign, "%ud%02u:%02u:%02u"), + d.day, d.hour, d.minute, d.second); } if (d.hour) { return snprintf( - b, size, SIGNED_STR(d.sign, "%u:%02u:%02u"), d.hour, + s, maxlen, SIGNED_STR(d.sign, "%u:%02u:%02u"), d.hour, d.minute, d.second); } return snprintf( - b, size, SIGNED_STR(d.sign, "%u:%02u"), d.minute, d.second); + s, maxlen, SIGNED_STR(d.sign, "%u:%02u"), d.minute, d.second); } -int format_duration_millis(char *b, const size_t size, const struct duration d) +int format_duration_millis( + char *restrict s, const size_t maxlen, const struct duration d) { if (d.day) { return snprintf( - b, size, SIGNED_STR(d.sign, "%ud%02u:%02u:%02u.%03u"), + s, maxlen, SIGNED_STR(d.sign, "%ud%02u:%02u:%02u.%03u"), d.day, d.hour, d.minute, d.second, d.milli); } if (d.hour) { return snprintf( - b, size, SIGNED_STR(d.sign, "%u:%02u:%02u.%03u"), + s, maxlen, SIGNED_STR(d.sign, "%u:%02u:%02u.%03u"), d.hour, d.minute, d.second, d.milli); } return snprintf( - b, size, SIGNED_STR(d.sign, "%u:%02u.%03u"), d.minute, d.second, - d.milli); + s, maxlen, SIGNED_STR(d.sign, "%u:%02u.%03u"), d.minute, + d.second, d.milli); } -int format_duration_nanos(char *b, const size_t size, const struct duration d) +int format_duration_nanos( + char *restrict s, const size_t maxlen, const struct duration d) { if (d.day) { return snprintf( - b, size, + s, maxlen, SIGNED_STR(d.sign, "%ud%02u:%02u:%02u.%03u%03u%03u"), d.day, d.hour, d.minute, d.second, d.milli, d.micro, d.nano); } if (d.hour) { return snprintf( - b, size, + s, maxlen, SIGNED_STR(d.sign, "%u:%02u:%02u.%03u%03u%03u"), d.hour, d.minute, d.second, d.milli, d.micro, d.nano); } return snprintf( - b, size, SIGNED_STR(d.sign, "%u:%02u.%03u%03u%03u"), d.minute, + s, maxlen, SIGNED_STR(d.sign, "%u:%02u.%03u%03u%03u"), d.minute, d.second, d.milli, d.micro, d.nano); } -int format_duration(char *b, size_t size, struct duration d) +int format_duration(char *restrict s, size_t maxlen, const struct duration d) { if (d.day) { const double seconds = d.second + d.milli * 1e-3 + d.micro * 1e-6 + d.nano * 1e-9; return snprintf( - b, size, SIGNED_STR(d.sign, "%ud%02u:%02u:%02.0f"), + s, maxlen, SIGNED_STR(d.sign, "%ud%02u:%02u:%02.0f"), d.day, d.hour, d.minute, seconds); } if (d.hour) { const double seconds = d.second + d.milli * 1e-3 + d.micro * 1e-6 + d.nano * 1e-9; return snprintf( - b, size, SIGNED_STR(d.sign, "%u:%02u:%02.0f"), d.hour, + s, maxlen, SIGNED_STR(d.sign, "%u:%02u:%02.0f"), d.hour, d.minute, seconds); } if (d.minute) { @@ -214,11 +216,11 @@ int format_duration(char *b, size_t size, struct duration d) d.micro * 1e-6 + d.nano * 1e-9; if (d.minute >= 10) { return snprintf( - b, size, SIGNED_STR(d.sign, "%u:%02.0f"), + s, maxlen, SIGNED_STR(d.sign, "%u:%02.0f"), d.minute, seconds); } return snprintf( - b, size, SIGNED_STR(d.sign, "%u:%04.1f"), d.minute, + s, maxlen, SIGNED_STR(d.sign, "%u:%04.1f"), d.minute, seconds); } if (d.second) { @@ -226,40 +228,47 @@ int format_duration(char *b, size_t size, struct duration d) const double seconds = d.second + d.milli * 1e-3 + d.micro * 1e-6 + d.nano * 1e-9; return snprintf( - b, size, SIGNED_STR(d.sign, "%.2fs"), seconds); + s, maxlen, SIGNED_STR(d.sign, "%.2fs"), + seconds); } const double millis = d.second * 1e+3 + d.milli + d.micro * 1e-3 + d.nano * 1e-6; - return snprintf(b, size, SIGNED_STR(d.sign, "%.0fms"), millis); + return snprintf( + s, maxlen, SIGNED_STR(d.sign, "%.0fms"), millis); } if (d.milli) { const double millis = d.milli + d.micro * 1e-3 + d.nano * 1e-6; if (d.milli >= 100) { return snprintf( - b, size, SIGNED_STR(d.sign, "%.1fms"), millis); + s, maxlen, SIGNED_STR(d.sign, "%.1fms"), + millis); } if (d.milli >= 10) { return snprintf( - b, size, SIGNED_STR(d.sign, "%.2fms"), millis); + s, maxlen, SIGNED_STR(d.sign, "%.2fms"), + millis); } - return snprintf(b, size, SIGNED_STR(d.sign, "%.3fms"), millis); + return snprintf( + s, maxlen, SIGNED_STR(d.sign, "%.3fms"), millis); } if (d.micro) { if (d.micro >= 100) { const double micros = d.micro + d.nano * 1e-3; return snprintf( - b, size, SIGNED_STR(d.sign, "%.1fµs"), micros); + s, maxlen, SIGNED_STR(d.sign, "%.1fµs"), + micros); } if (d.micro >= 10) { const double micros = d.micro + d.nano * 1e-3; return snprintf( - b, size, SIGNED_STR(d.sign, "%.2fµs"), micros); + s, maxlen, SIGNED_STR(d.sign, "%.2fµs"), + micros); } const unsigned int nanos = d.micro * 1000u + d.nano; - return snprintf(b, size, SIGNED_STR(d.sign, "%uns"), nanos); + return snprintf(s, maxlen, SIGNED_STR(d.sign, "%uns"), nanos); } if (d.nano) { - return snprintf(b, size, SIGNED_STR(d.sign, "%uns"), d.nano); + return snprintf(s, maxlen, SIGNED_STR(d.sign, "%uns"), d.nano); } - return snprintf(b, size, SIGNED_STR(d.sign, "0")); + return snprintf(s, maxlen, SIGNED_STR(d.sign, "0")); } diff --git a/contrib/csnippets/utils/formats.h b/contrib/csnippets/utils/formats.h index d0cd914..16b3ce9 100644 --- a/contrib/csnippets/utils/formats.h +++ b/contrib/csnippets/utils/formats.h @@ -11,13 +11,13 @@ * @brief Format the value with human-readable SI metric prefix. * @return Same as snprintf. */ -int format_si_prefix(char *buf, size_t bufsize, double value); +int format_si_prefix(char *s, size_t maxlen, double value); /** * @brief Format byte count as a human-readable string in IEC unit. * @return Same as snprintf. */ -int format_iec_bytes(char *buf, size_t bufsize, double value); +int format_iec_bytes(char *s, size_t maxlen, double value); struct duration { signed int sign; /* +1 or -1, 0 is null, otherwise undefined */ @@ -48,27 +48,27 @@ struct duration make_duration_nanos(int_least64_t nanos); * @details The duration value is truncated. * @return Same as snprintf. */ -int format_duration_seconds(char *b, size_t size, struct duration d); +int format_duration_seconds(char *s, size_t maxlen, struct duration d); /** * @brief Format duration in milliseconds. * @details The duration value is truncated. * @return Same as snprintf. */ -int format_duration_millis(char *b, size_t size, struct duration d); +int format_duration_millis(char *s, size_t maxlen, struct duration d); /** * @brief Format duration in nanoseconds. * @details The duration value remains accurate. * @return Same as snprintf. */ -int format_duration_nanos(char *b, size_t size, struct duration d); +int format_duration_nanos(char *s, size_t maxlen, struct duration d); /** * @brief Format duration into a human-readable format. * @details The duration value is rounded. * @return Same as snprintf. */ -int format_duration(char *b, size_t size, struct duration d); +int format_duration(char *s, size_t maxlen, struct duration d); #endif /* UTILS_FORMATS_H */ diff --git a/contrib/csnippets/utils/slog.c b/contrib/csnippets/utils/slog.c index 213df7d..fc3c3db 100644 --- a/contrib/csnippets/utils/slog.c +++ b/contrib/csnippets/utils/slog.c @@ -113,9 +113,9 @@ static size_t slog_timestamp( return len; } -static const char *slog_filename(const char *file) +static const char *slog_filename(const char *restrict file) { - const char *prefix = ATOMIC_LOAD(&slog_fileprefix); + const char *restrict prefix = ATOMIC_LOAD(&slog_fileprefix); if (prefix == NULL) { return file; } @@ -130,8 +130,9 @@ static const char *slog_filename(const char *file) } static void slog_write_file( - const int level, const char *file, const int line, - struct slog_extra *extra, const char *format, va_list args) + const int level, const char *restrict file, const int line, + struct slog_extra *restrict extra, const char *restrict format, + va_list args) { BUF_INIT(slog_buffer, 2); slog_buffer.data[0] = slog_level_char[level]; @@ -162,8 +163,9 @@ static void slog_write_file( #if HAVE_SYSLOG static void slog_write_syslog( - const int level, const char *file, const int line, - struct slog_extra *extra, const char *format, va_list args) + const int level, const char *restrict file, const int line, + struct slog_extra *restrict extra, const char *restrict format, + va_list args) { static const int slog_level_map[] = { LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, @@ -228,8 +230,9 @@ void slog_setfileprefix(const char *prefix) } void slog_vwrite( - int level, const char *file, int line, struct slog_extra *extra, - const char *format, va_list args) + int level, const char *restrict file, int line, + struct slog_extra *restrict extra, const char *restrict format, + va_list args) { const slog_writer_fn write = ATOMIC_LOAD(&slog_writer); if (write == NULL) { @@ -239,8 +242,8 @@ void slog_vwrite( } void slog_write( - const int level, const char *file, const int line, - struct slog_extra *extra, const char *format, ...) + const int level, const char *restrict file, const int line, + struct slog_extra *restrict extra, const char *restrict format, ...) { const slog_writer_fn write = ATOMIC_LOAD(&slog_writer); if (write == NULL) {