aboutsummaryrefslogtreecommitdiffstats
path: root/utils.c
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-08-08 06:32:04 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-08-08 06:32:04 +0000
commit7eba1e86bd7c5e41d392c68c5d20f4fc3e7706f6 (patch)
tree18bceefb669763e143e8cc5266734662aafa476f /utils.c
parentbb280b04db58728ae0dba1cc3a91faac99890ce6 (diff)
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as ast_free() in utils.h and remove the copies from all the files. - centralize and abstract the code used for doing thread storage. The code lives in threadstorage.h, with one function being implemented in utils.c. This new API includes generic thread storage as well as special functions for handling thread local dynamic length string buffers. - update ast_inet_ntoa() to use the new threadstorage API - update ast_state2str() to use the new threadstorage API - update ast_cli() to use the new threadstorage API - Modify manager_event() to use thread storage. Instead of using a buffer of 4096 characters as the workspace for building the manager event, use a thread local dynamic string. Now there is no length limitation on the length of the body of a manager event. - Significantly simplify the handling of ast_verbose() ... - Instead of using a static char buffer and a lock to make sure only one thread can be using ast_verbose() at a time, use a thread local dynamic string as the workspace for preparing the verbose message. Instead of locking around the entire function, the only locking done now is when the message has been built and is being deliviered to the list of registered verbose message handlers. - This function was doing a strdup() on every message passed to it and keeping a queue of the last 200 messages in memory. This has been completely removed. The only place this was used was that if there were any messages in the verbose queue when a verbose handler was registered, all of the messages in the queue would be fed to it. So, I just made sure that the console verbose handler and the network verbose handler (for remote asterisk consoles) were registered before any verbose messages. pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at startup, but I didn't feel the performance hit of this message queue was worth saving the initial verbose output for these very rarely used modules. - I have removed the last three arguments to the verbose handlers, leaving only the string itself because they aren't needed anymore. For example, ast_verbose had some logic for telling the verbose handler to add a newline if the buffer was completely full. Now that the buffer can grow as needed, this doesn't matter anymore. - remove unused function, ast_verbose_dmesg() which was to dispatch the message queue - Convert the list of verbose handlers to use the linked list macros. - add missing newline characters to a few ast_verbose() calls - convert the list of log channels to use the linked list macros in logger.c - fix close_logger() to close all of the files it opened for logging - update ast_log() to use a thread local dynamic string for its workspace for preparing log messages instead of a buffer of size BUFSIZ (8kB on my system) allocated on the stack. The dynamic string in this case is limited to only growing to a maximum size of BUFSIZ. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@39272 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c62
1 files changed, 40 insertions, 22 deletions
diff --git a/utils.c b/utils.c
index ef251bfa9..0aaa791a5 100644
--- a/utils.c
+++ b/utils.c
@@ -59,20 +59,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */
#include "asterisk/utils.h"
+#define AST_API_MODULE
+#include "asterisk/threadstorage.h"
+
static char base64[64];
static char b2a[256];
-static pthread_key_t inet_ntoa_buf_key;
-static pthread_once_t inet_ntoa_buf_once = PTHREAD_ONCE_INIT;
-
-#ifdef __AST_DEBUG_MALLOC
-static void FREE(void *ptr)
-{
- free(ptr);
-}
-#else
-#define FREE free
-#endif
+AST_THREADSTORAGE(inet_ntoa_buf, inet_ntoa_buf_init);
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) || defined(__CYGWIN__)
@@ -495,22 +488,13 @@ void ast_uri_decode(char *s)
*o = '\0';
}
-static void inet_ntoa_buf_key_create(void)
-{
- pthread_key_create(&inet_ntoa_buf_key, FREE);
-}
-
/*! \brief ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa */
const char *ast_inet_ntoa(struct in_addr ia)
{
char *buf;
- pthread_once(&inet_ntoa_buf_once, inet_ntoa_buf_key_create);
- if (!(buf = pthread_getspecific(inet_ntoa_buf_key))) {
- if (!(buf = ast_calloc(1, INET_ADDRSTRLEN)))
- return NULL;
- pthread_setspecific(inet_ntoa_buf_key, buf);
- }
+ if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN)))
+ return "";
return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
}
@@ -1211,3 +1195,37 @@ int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
return -1;
}
+int ast_dynamic_str_thread_build_va(struct ast_dynamic_str **buf, size_t max_len,
+ struct ast_threadstorage *ts, int append, const char *fmt, va_list ap)
+{
+ int res;
+ int offset = append ? strlen((*buf)->str) : 0;
+
+ res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, ap);
+
+ /* Check to see if there was not enough space in the string buffer to prepare
+ * the string. Also, if a maximum length is present, make sure the current
+ * length is less than the maximum before increasing the size. */
+ if ((res + offset + 1) > (*buf)->len && (max_len ? ((*buf)->len < max_len) : 1)) {
+ /* Set the new size of the string buffer to be the size needed
+ * to hold the resulting string (res) plus one byte for the
+ * terminating '\0'. If this size is greater than the max, set
+ * the new length to be the maximum allowed. */
+ if (max_len)
+ (*buf)->len = ((res + offset + 1) < max_len) ? (res + offset + 1) : max_len;
+ else
+ (*buf)->len = res + offset + 1;
+
+ if (!(*buf = ast_realloc(*buf, (*buf)->len + sizeof(*(*buf)))))
+ return AST_DYNSTR_BUILD_FAILED;
+
+ if (ts)
+ pthread_setspecific(ts->key, *buf);
+
+ /* va_end() and va_start() must be done before calling
+ * vsnprintf() again. */
+ return AST_DYNSTR_BUILD_RETRY;
+ }
+
+ return res;
+}