aboutsummaryrefslogtreecommitdiffstats
path: root/cli.c
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-07-23 15:19:16 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-07-23 15:19:16 +0000
commit54da881b9321d3c7e52afd611984da253b18257c (patch)
tree6a611697f13d1f4f1101beb89aba3dbd286e056b /cli.c
parentec5e68f253e0bca9e287957a5ff0d123401bc133 (diff)
Merge team/russell/ast_cli_tls into the trunk.
This improves the performance of ast_cli() by not doing a heap memory allocation and deallocation every single time the function is called. Instead, a thread-specific buffer is allocatted the first time the function is called and automatically free'd when the thread exits. Also note that this buffer will only be allocatted in threads that actually call this function, which is probably only the threads spawned to service connected asterisk consoles. This does introduce a new limitation on the maximum length of the resulting string from the arguments passed to ast_cli. Previously there was no limit since it was just allocating a buffer big enough every time the function was called. The current buffer size is 16kB. If there is ever a case where we want to print more than 16k characters in a single call to ast_cli(), this will have to be increased. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@38127 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'cli.c')
-rw-r--r--cli.c33
1 files changed, 24 insertions, 9 deletions
diff --git a/cli.c b/cli.c
index b12f0684d..565a82572 100644
--- a/cli.c
+++ b/cli.c
@@ -50,22 +50,37 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#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;
+
+/*! \brief Maximum length of resulting strings in ast_cli() */
+#define AST_CLI_MAXSTRLEN 16384
+
+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_malloc(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);