[asterisk-commits] russell: trunk r38223 - /trunk/cli.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Jul 26 00:48:49 MST 2006


Author: russell
Date: Wed Jul 26 02:48:48 2006
New Revision: 38223

URL: http://svn.digium.com/view/asterisk?rev=38223&view=rev
Log:
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.

Modified:
    trunk/cli.c

Modified: trunk/cli.c
URL: http://svn.digium.com/view/asterisk/trunk/cli.c?rev=38223&r1=38222&r2=38223&view=diff
==============================================================================
--- trunk/cli.c (original)
+++ trunk/cli.c Wed Jul 26 02:48:48 2006
@@ -54,8 +54,8 @@
 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 @@
 
 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);



More information about the asterisk-commits mailing list