From: Mathieu Desnoyers Date: Wed, 19 Jun 2019 18:23:09 +0000 (-0400) Subject: common: implement bt_common_g_string_append and bt_common_g_string_append_c X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=15b4e74c9d44271d73a58a1783dd13ecd19c6ef6 common: implement bt_common_g_string_append and bt_common_g_string_append_c Implement inline versions of g_string_append and g_string_append_c. Since those calls are performed very often within sink.text.pretty, it is relevant for pretty-printing speed. Signed-off-by: Mathieu Desnoyers Change-Id: Ib1069c7d21c6e707f75d839f333f034a50e2a43e Reviewed-on: https://review.lttng.org/c/babeltrace/+/1515 CI-Build: Philippe Proulx Tested-by: jenkins Reviewed-by: Francis Deslauriers Reviewed-by: Philippe Proulx --- diff --git a/src/common/common.h b/src/common/common.h index 220ec61b..e4c775e5 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -728,4 +728,43 @@ end: BT_HIDDEN int bt_common_g_string_append_printf(GString *str, const char *fmt, ...); +static inline +void bt_common_g_string_append(GString *str, const char *s) +{ + gsize len, allocated_len, s_len; + + /* str->len excludes \0. */ + len = str->len; + /* Exclude \0. */ + allocated_len = str->allocated_len - 1; + s_len = strlen(s); + if (G_UNLIKELY(allocated_len < len + s_len)) { + /* Resize. */ + g_string_set_size(str, len + s_len); + } else { + str->len = len + s_len; + } + memcpy(str->str + len, s, s_len + 1); +} + +static inline +void bt_common_g_string_append_c(GString *str, char c) +{ + gsize len, allocated_len, s_len; + + /* str->len excludes \0. */ + len = str->len; + /* Exclude \0. */ + allocated_len = str->allocated_len - 1; + s_len = 1; + if (G_UNLIKELY(allocated_len < len + s_len)) { + /* Resize. */ + g_string_set_size(str, len + s_len); + } else { + str->len = len + s_len; + } + str->str[len] = c; + str->str[len + 1] = '\0'; +} + #endif /* BABELTRACE_COMMON_INTERNAL_H */