[svn-commits] russell: branch russell/ast_cli_tls r38087 - /team/russell/ast_cli_tls/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Fri Jul 21 21:43:32 MST 2006


Author: russell
Date: Fri Jul 21 23:43:32 2006
New Revision: 38087

URL: http://svn.digium.com/view/asterisk?rev=38087&view=rev
Log:
Change the implementation of ast_cli() to use thread local storage. Previously,
ast_cli() did a heap memory allocation and free every time the function was
called. The one limitation that this change introduces is that the resulting
string's length is limited to AST_CLI_STRLEN, which I currently have defined to
be 256 characters. I can't imagine that there are any cases where we would
really need more than that so I think this is an acceptable limitation, but I
am certainly open to feedback on it.

Modified:
    team/russell/ast_cli_tls/   (props changed)
    team/russell/ast_cli_tls/cli.c

Propchange: team/russell/ast_cli_tls/
------------------------------------------------------------------------------
    automerge = *

Propchange: team/russell/ast_cli_tls/
------------------------------------------------------------------------------
    automerge-email = russell at digium.com

Propchange: team/russell/ast_cli_tls/
------------------------------------------------------------------------------
    svnmerge-integrated = /trunk:1-38085

Modified: team/russell/ast_cli_tls/cli.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_cli_tls/cli.c?rev=38087&r1=38086&r2=38087&view=diff
==============================================================================
--- team/russell/ast_cli_tls/cli.c (original)
+++ team/russell/ast_cli_tls/cli.c Fri Jul 21 23:43:32 2006
@@ -50,22 +50,36 @@
 #include "editline/readline/readline.h"
 
 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;
+
+#define AST_CLI_MAXSTRLEN   256
+
+static void ast_cli_buf_key_create(void)
+{
+	pthread_key_create(&ast_cli_buf_key, free);
+}
+
 void ast_cli(int fd, char *fmt, ...)
 {
-	char *stuff;
+	char *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_calloc(1, AST_CLI_MAXSTRLEN)))
+			return;
+		pthread_setspecific(ast_cli_buf_key, buf);
+	}
+
 	va_start(ap, fmt);
-	res = vasprintf(&stuff, fmt, ap);
+	res = vsnprintf(buf, AST_CLI_MAXSTRLEN, fmt, ap);
 	va_end(ap);
-	if (res == -1) {
-		ast_log(LOG_ERROR, "Memory allocation failure\n");
-	} else {
-		ast_carefulwrite(fd, stuff, strlen(stuff), 100);
-		free(stuff);
-	}
+
+	if (res > 0)
+		ast_carefulwrite(fd, buf, strlen(buf), 100);
 }
 
 static AST_LIST_HEAD_STATIC(helpers, ast_cli_entry);



More information about the svn-commits mailing list