From 2c802534923fbb2a12a75b0cbff0630afffc358d Mon Sep 17 00:00:00 2001 From: russell Date: Wed, 26 Jul 2006 07:48:48 +0000 Subject: merge the changes from my ast_cli_tls branch ... Instead of having a static buffer size of 16k, start out with 256 bytes and increase the size of the buffer as needed. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@38223 f38db490-d61c-443f-a65b-d21fe96a405b --- cli.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'cli.c') diff --git a/cli.c b/cli.c index 565a82572..f411229cd 100644 --- a/cli.c +++ b/cli.c @@ -54,8 +54,8 @@ extern unsigned long global_fin, global_fout; static pthread_key_t ast_cli_buf_key; static pthread_once_t ast_cli_buf_once = PTHREAD_ONCE_INIT; -/*! \brief Maximum length of resulting strings in ast_cli() */ -#define AST_CLI_MAXSTRLEN 16384 +/*! \brief Initial buffer size for resulting strings in ast_cli() */ +#define AST_CLI_MAXSTRLEN 256 static void ast_cli_buf_key_create(void) { @@ -64,23 +64,36 @@ static void ast_cli_buf_key_create(void) void ast_cli(int fd, char *fmt, ...) { - char *buf; + struct { + size_t len; + char str[0]; + } *buf; int res; va_list ap; pthread_once(&ast_cli_buf_once, ast_cli_buf_key_create); if (!(buf = pthread_getspecific(ast_cli_buf_key))) { - if (!(buf = ast_malloc(AST_CLI_MAXSTRLEN))) + if (!(buf = ast_malloc(AST_CLI_MAXSTRLEN + sizeof(*buf)))) return; + buf->len = AST_CLI_MAXSTRLEN; pthread_setspecific(ast_cli_buf_key, buf); } va_start(ap, fmt); - res = vsnprintf(buf, AST_CLI_MAXSTRLEN, fmt, ap); + res = vsnprintf(buf->str, buf->len, fmt, ap); + while (res >= buf->len) { + if (!(buf = ast_realloc(buf, (buf->len * 2) + sizeof(*buf)))) { + va_end(ap); + return; + } + buf->len *= 2; + pthread_setspecific(ast_cli_buf_key, buf); + res = vsnprintf(buf->str, buf->len, fmt, ap); + } va_end(ap); if (res > 0) - ast_carefulwrite(fd, buf, strlen(buf), 100); + ast_carefulwrite(fd, buf->str, strlen(buf->str), 100); } static AST_LIST_HEAD_STATIC(helpers, ast_cli_entry); -- cgit v1.2.3