From f9cb4eb568ac5527a47dc01bf364f0a963b4dc04 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 26 Oct 2018 23:08:50 +0100 Subject: [PATCH] Make a few small helper functions inline. Notably toint(), which ought to compile down to the identity function in any case so you don't really want to put in a pointless call overhead, and make_ptrlen() (and a couple of its wrappers) which is standing in for what ought to be a struct-literal syntax. --- misc.c | 41 ----------------------------------------- misc.h | 46 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 45 deletions(-) diff --git a/misc.c b/misc.c index c11b2621..25ed8636 100644 --- a/misc.c +++ b/misc.c @@ -349,29 +349,6 @@ void burnstr(char *string) /* sfree(str), only clear it first */ } } -int toint(unsigned u) -{ - /* - * Convert an unsigned to an int, without running into the - * undefined behaviour which happens by the strict C standard if - * the value overflows. You'd hope that sensible compilers would - * do the sensible thing in response to a cast, but actually I - * don't trust modern compilers not to do silly things like - * assuming that _obviously_ you wouldn't have caused an overflow - * and so they can elide an 'if (i < 0)' test immediately after - * the cast. - * - * Sensible compilers ought of course to optimise this entire - * function into 'just return the input value'! - */ - if (u <= (unsigned)INT_MAX) - return (int)u; - else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */ - return INT_MIN + (int)(u - (unsigned)INT_MIN); - else - return INT_MIN; /* fallback; should never occur on binary machines */ -} - int string_length_for_printf(size_t s) { /* Truncate absurdly long strings (should one show up) to fit @@ -1235,24 +1212,6 @@ int nullstrcmp(const char *a, const char *b) return strcmp(a, b); } -ptrlen make_ptrlen(const void *ptr, size_t len) -{ - ptrlen pl; - pl.ptr = ptr; - pl.len = len; - return pl; -} - -ptrlen ptrlen_from_asciz(const char *str) -{ - return make_ptrlen(str, strlen(str)); -} - -ptrlen ptrlen_from_strbuf(strbuf *sb) -{ - return make_ptrlen(sb->u, sb->len); -} - bool ptrlen_eq_string(ptrlen pl, const char *str) { size_t len = strlen(str); diff --git a/misc.h b/misc.h index 5d493c48..2302fbbc 100644 --- a/misc.h +++ b/misc.h @@ -12,6 +12,7 @@ #include /* for FILE * */ #include /* for va_list */ #include /* for struct tm */ +#include /* for INT_MAX/MIN */ unsigned long parse_blocksize(const char *bs); char ctrlparse(char *s, char **next); @@ -57,7 +58,29 @@ void strbuf_finalise_agent_query(strbuf *buf); wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, int len); wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string); -int toint(unsigned); +static inline int toint(unsigned u) +{ + /* + * Convert an unsigned to an int, without running into the + * undefined behaviour which happens by the strict C standard if + * the value overflows. You'd hope that sensible compilers would + * do the sensible thing in response to a cast, but actually I + * don't trust modern compilers not to do silly things like + * assuming that _obviously_ you wouldn't have caused an overflow + * and so they can elide an 'if (i < 0)' test immediately after + * the cast. + * + * Sensible compilers ought of course to optimise this entire + * function into 'just return the input value', and since it's + * also declared inline, elide it completely in their output. + */ + if (u <= (unsigned)INT_MAX) + return (int)u; + else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */ + return INT_MIN + (int)(u - (unsigned)INT_MIN); + else + return INT_MIN; /* fallback; should never occur on binary machines */ +} char *fgetline(FILE *fp); char *chomp(char *str); @@ -96,9 +119,24 @@ struct tm ltime(void); */ int nullstrcmp(const char *a, const char *b); -ptrlen make_ptrlen(const void *ptr, size_t len); -ptrlen ptrlen_from_asciz(const char *str); -ptrlen ptrlen_from_strbuf(strbuf *sb); +static inline ptrlen make_ptrlen(const void *ptr, size_t len) +{ + ptrlen pl; + pl.ptr = ptr; + pl.len = len; + return pl; +} + +static inline ptrlen ptrlen_from_asciz(const char *str) +{ + return make_ptrlen(str, strlen(str)); +} + +static inline ptrlen ptrlen_from_strbuf(strbuf *sb) +{ + return make_ptrlen(sb->u, sb->len); +} + bool ptrlen_eq_string(ptrlen pl, const char *str); bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2); bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);