mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-25 06:00:36 +00:00
simplify the ast_dynamic_str_*.... routines by
renaming them to ast_str ... and putting the struct ast_threadstorage pointer into the struct ast_str. This makes the code a lot more readable. At this point we can use these routines also to replace ast_build_string(). git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@48510 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -267,50 +267,45 @@ struct ast_realloca {
|
|||||||
* One should never declare a variable with this type, but only a pointer
|
* One should never declare a variable with this type, but only a pointer
|
||||||
* to it, e.g.
|
* to it, e.g.
|
||||||
*
|
*
|
||||||
* struct ast_dynamic_str *ds;
|
* struct ast_str *ds;
|
||||||
*
|
*
|
||||||
* The pointer can be initialized with the following:
|
* The pointer can be initialized with the following:
|
||||||
*
|
*
|
||||||
* ds = ast_dynamic_str_create(init_len);
|
* ds = ast_str_create(init_len);
|
||||||
* creates a malloc()'ed dynamic string;
|
* creates a malloc()'ed dynamic string;
|
||||||
*
|
*
|
||||||
* ds = ast_dynamic_str_alloca(init_len);
|
* ds = ast_str_alloca(init_len);
|
||||||
* creates a string on the stack (not very dynamic!).
|
* creates a string on the stack (not very dynamic!).
|
||||||
*
|
*
|
||||||
* ds = ast_dynamic_str_thread_get(ts, init_len)
|
* ds = ast_str_thread_get(ts, init_len)
|
||||||
* creates a malloc()'ed dynamic string associated to
|
* creates a malloc()'ed dynamic string associated to
|
||||||
* the thread-local storage key ts
|
* the thread-local storage key ts
|
||||||
*
|
*
|
||||||
* Finally, the string can be manipulated with the following:
|
* Finally, the string can be manipulated with the following:
|
||||||
*
|
*
|
||||||
* ast_dynamic_str_set(&buf, max_len, ts, fmt, ...)
|
* ast_str_set(&buf, max_len, ts, fmt, ...)
|
||||||
* ast_dynamic_str_append(&buf, max_len, ts, fmt, ...)
|
* ast_str_append(&buf, max_len, ts, fmt, ...)
|
||||||
* ast_dynamic_str_thread_set(&buf, max_len, ts, fmt, ...)
|
|
||||||
* ast_dynamic_str_thread_append(&buf, max_len, ts, fmt, ...)
|
|
||||||
*
|
*
|
||||||
* and their varargs format.
|
* and their varargs format.
|
||||||
*
|
*
|
||||||
* \arg max_len The maximum allowed length, reallocating if needed.
|
* \arg max_len The maximum allowed length, reallocating if needed.
|
||||||
* 0 means unlimited, -1 means "at most the available space"
|
* 0 means unlimited, -1 means "at most the available space"
|
||||||
*
|
|
||||||
* XXX the [_thread] variants can be removed if we save the ts in the
|
|
||||||
* string descriptor.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*! \brief type of storage used for dynamic string */
|
|
||||||
enum dynstr_type {
|
|
||||||
DS_MALLOC = 1,
|
|
||||||
DS_ALLOCA = 2,
|
|
||||||
DS_STATIC = 3, /* XXX not supported yet */
|
|
||||||
};
|
|
||||||
|
|
||||||
/*! \brief The descriptor of a dynamic string
|
/*! \brief The descriptor of a dynamic string
|
||||||
* XXX storage will be optimized later if needed
|
* XXX storage will be optimized later if needed
|
||||||
|
* We use the ts field to indicate the type of storage.
|
||||||
|
* Three special constants indicate malloc, alloca() or static
|
||||||
|
* variables, all other values indicate a
|
||||||
|
* struct ast_threadstorage pointer.
|
||||||
*/
|
*/
|
||||||
struct ast_dynamic_str {
|
struct ast_str {
|
||||||
size_t len; /*!< The current maximum length of the string */
|
size_t len; /*!< The current maximum length of the string */
|
||||||
size_t used; /*!< Amount of space used */
|
size_t used; /*!< Amount of space used */
|
||||||
enum dynstr_type type; /*!< What kind of storage is this ? */
|
struct ast_threadstorage *ts; /*!< What kind of storage is this ? */
|
||||||
|
#define DS_MALLOC ((struct ast_threadstorage *)1)
|
||||||
|
#define DS_ALLOCA ((struct ast_threadstorage *)2)
|
||||||
|
#define DS_STATIC ((struct ast_threadstorage *)3) /* not supported yet */
|
||||||
char str[0]; /*!< The string buffer */
|
char str[0]; /*!< The string buffer */
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -326,28 +321,28 @@ struct ast_dynamic_str {
|
|||||||
* be free()'d after it is no longer needed.
|
* be free()'d after it is no longer needed.
|
||||||
*/
|
*/
|
||||||
AST_INLINE_API(
|
AST_INLINE_API(
|
||||||
struct ast_dynamic_str * attribute_malloc ast_dynamic_str_create(size_t init_len),
|
struct ast_str * attribute_malloc ast_str_create(size_t init_len),
|
||||||
{
|
{
|
||||||
struct ast_dynamic_str *buf;
|
struct ast_str *buf;
|
||||||
|
|
||||||
if (!(buf = ast_calloc(1, sizeof(*buf) + init_len)))
|
if (!(buf = ast_calloc(1, sizeof(*buf) + init_len)))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
buf->len = init_len;
|
buf->len = init_len;
|
||||||
buf->used = 0;
|
buf->used = 0;
|
||||||
buf->type = DS_MALLOC;
|
buf->ts = DS_MALLOC;
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
#define ast_dynamic_str_alloca(init_len) \
|
#define ast_str_alloca(init_len) \
|
||||||
({ \
|
({ \
|
||||||
struct ast_dynamic_str *buf; \
|
struct ast_str *buf; \
|
||||||
buf = alloca(sizeof(*buf) + init_len); \
|
buf = alloca(sizeof(*buf) + init_len); \
|
||||||
buf->len = init_len; \
|
buf->len = init_len; \
|
||||||
buf->used = 0; \
|
buf->used = 0; \
|
||||||
buf->type = DS_ALLOCA; \
|
buf->ts = DS_ALLOCA; \
|
||||||
buf->str[0] = '\0'; \
|
buf->str[0] = '\0'; \
|
||||||
(buf); \
|
(buf); \
|
||||||
})
|
})
|
||||||
@@ -376,19 +371,19 @@ struct ast_dynamic_str * attribute_malloc ast_dynamic_str_create(size_t init_len
|
|||||||
* ...
|
* ...
|
||||||
* void my_func(const char *fmt, ...)
|
* void my_func(const char *fmt, ...)
|
||||||
* {
|
* {
|
||||||
* struct ast_dynamic_str *buf;
|
* struct ast_str *buf;
|
||||||
*
|
*
|
||||||
* if (!(buf = ast_dynamic_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
|
* if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
|
||||||
* return;
|
* return;
|
||||||
* ...
|
* ...
|
||||||
* }
|
* }
|
||||||
* \endcode
|
* \endcode
|
||||||
*/
|
*/
|
||||||
AST_INLINE_API(
|
AST_INLINE_API(
|
||||||
struct ast_dynamic_str *ast_dynamic_str_thread_get(struct ast_threadstorage *ts,
|
struct ast_str *ast_str_thread_get(struct ast_threadstorage *ts,
|
||||||
size_t init_len),
|
size_t init_len),
|
||||||
{
|
{
|
||||||
struct ast_dynamic_str *buf;
|
struct ast_str *buf;
|
||||||
|
|
||||||
if (!(buf = ast_threadstorage_get(ts, sizeof(*buf) + init_len)))
|
if (!(buf = ast_threadstorage_get(ts, sizeof(*buf) + init_len)))
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -396,7 +391,7 @@ struct ast_dynamic_str *ast_dynamic_str_thread_get(struct ast_threadstorage *ts,
|
|||||||
if (!buf->len) {
|
if (!buf->len) {
|
||||||
buf->len = init_len;
|
buf->len = init_len;
|
||||||
buf->used = 0;
|
buf->used = 0;
|
||||||
buf->type = DS_MALLOC;
|
buf->ts = ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
@@ -404,9 +399,9 @@ struct ast_dynamic_str *ast_dynamic_str_thread_get(struct ast_threadstorage *ts,
|
|||||||
)
|
)
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Error codes from __ast_dyn_str_helper()
|
* \brief Error codes from __ast_str_helper()
|
||||||
* The undelying processing to manipulate dynamic string is done
|
* The undelying processing to manipulate dynamic string is done
|
||||||
* by __ast_dyn_str_helper(), which can return a success, a
|
* by __ast_str_helper(), which can return a success, a
|
||||||
* permanent failure (e.g. no memory), or a temporary one (when
|
* permanent failure (e.g. no memory), or a temporary one (when
|
||||||
* the string needs to be reallocated, and we must run va_start()
|
* the string needs to be reallocated, and we must run va_start()
|
||||||
* again; XXX this convoluted interface is only here because
|
* again; XXX this convoluted interface is only here because
|
||||||
@@ -418,46 +413,43 @@ enum {
|
|||||||
* are undefined */
|
* are undefined */
|
||||||
AST_DYNSTR_BUILD_FAILED = -1,
|
AST_DYNSTR_BUILD_FAILED = -1,
|
||||||
/*! The buffer size for the dynamic string had to be increased, and
|
/*! The buffer size for the dynamic string had to be increased, and
|
||||||
* ast_dynamic_str_thread_build_va() needs to be called again after
|
* __ast_str_helper() needs to be called again after
|
||||||
* a va_end() and va_start().
|
* a va_end() and va_start().
|
||||||
*/
|
*/
|
||||||
AST_DYNSTR_BUILD_RETRY = -2
|
AST_DYNSTR_BUILD_RETRY = -2
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Set a thread locally stored dynamic string from a va_list
|
* \brief Set a dynamic string from a va_list
|
||||||
*
|
*
|
||||||
* \arg buf This is the address of a pointer to an ast_dynamic_str which should
|
* \arg buf This is the address of a pointer to a struct ast_str.
|
||||||
* have been retrieved using ast_dynamic_str_thread_get. It will need to
|
* If it is retrieved using ast_str_thread_get, the
|
||||||
|
struct ast_threadstorage pointer will need to
|
||||||
* be updated in the case that the buffer has to be reallocated to
|
* be updated in the case that the buffer has to be reallocated to
|
||||||
* accommodate a longer string than what it currently has space for.
|
* accommodate a longer string than what it currently has space for.
|
||||||
* \arg max_len This is the maximum length to allow the string buffer to grow
|
* \arg max_len This is the maximum length to allow the string buffer to grow
|
||||||
* to. If this is set to 0, then there is no maximum length.
|
* to. If this is set to 0, then there is no maximum length.
|
||||||
* \arg ts This is a pointer to the thread storage structure declared by using
|
|
||||||
* the AST_THREADSTORAGE macro. If declared with
|
|
||||||
* AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be
|
|
||||||
* (&my_buf).
|
|
||||||
* \arg fmt This is the format string (printf style)
|
* \arg fmt This is the format string (printf style)
|
||||||
* \arg ap This is the va_list
|
* \arg ap This is the va_list
|
||||||
*
|
*
|
||||||
* \return The return value of this function is the same as that of the printf
|
* \return The return value of this function is the same as that of the printf
|
||||||
* family of functions.
|
* family of functions.
|
||||||
*
|
*
|
||||||
* Example usage:
|
* Example usage (the first part is only for thread-local storage)
|
||||||
* \code
|
* \code
|
||||||
* AST_THREADSTORAGE(my_str, my_str_init);
|
* AST_THREADSTORAGE(my_str, my_str_init);
|
||||||
* #define MY_STR_INIT_SIZE 128
|
* #define MY_STR_INIT_SIZE 128
|
||||||
* ...
|
* ...
|
||||||
* void my_func(const char *fmt, ...)
|
* void my_func(const char *fmt, ...)
|
||||||
* {
|
* {
|
||||||
* struct ast_dynamic_str *buf;
|
* struct ast_str *buf;
|
||||||
* va_list ap;
|
* va_list ap;
|
||||||
*
|
*
|
||||||
* if (!(buf = ast_dynamic_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
|
* if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
|
||||||
* return;
|
* return;
|
||||||
* ...
|
* ...
|
||||||
* va_start(fmt, ap);
|
* va_start(fmt, ap);
|
||||||
* ast_dynamic_str_thread_set_va(&buf, 0, &my_str, fmt, ap);
|
* ast_str_set_va(&buf, 0, fmt, ap);
|
||||||
* va_end(ap);
|
* va_end(ap);
|
||||||
*
|
*
|
||||||
* printf("This is the string we just built: %s\n", buf->str);
|
* printf("This is the string we just built: %s\n", buf->str);
|
||||||
@@ -468,11 +460,11 @@ enum {
|
|||||||
* \note: the following two functions must be implemented as macros
|
* \note: the following two functions must be implemented as macros
|
||||||
* because we must do va_end()/va_start() on the original arguments.
|
* because we must do va_end()/va_start() on the original arguments.
|
||||||
*/
|
*/
|
||||||
#define ast_dynamic_str_thread_set_va(buf, max_len, ts, fmt, ap) \
|
#define ast_str_set_va(buf, max_len, fmt, ap) \
|
||||||
({ \
|
({ \
|
||||||
int __res; \
|
int __res; \
|
||||||
while ((__res = __ast_dyn_str_helper(buf, max_len, \
|
while ((__res = __ast_str_helper(buf, max_len, \
|
||||||
ts, 0, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) { \
|
0, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) { \
|
||||||
va_end(ap); \
|
va_end(ap); \
|
||||||
va_start(ap, fmt); \
|
va_start(ap, fmt); \
|
||||||
} \
|
} \
|
||||||
@@ -480,17 +472,15 @@ enum {
|
|||||||
})
|
})
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Append to a thread local dynamic string using a va_list
|
* \brief Append to a dynamic string using a va_list
|
||||||
*
|
*
|
||||||
* The arguments, return values, and usage of this are the same as those for
|
* Same as ast_str_set_va(), but append to the current content.
|
||||||
* ast_dynamic_str_thread_set_va(). However, instead of setting a new value
|
|
||||||
* for the string, this will append to the current value.
|
|
||||||
*/
|
*/
|
||||||
#define ast_dynamic_str_thread_append_va(buf, max_len, ts, fmt, ap) \
|
#define ast_str_append_va(buf, max_len, fmt, ap) \
|
||||||
({ \
|
({ \
|
||||||
int __res; \
|
int __res; \
|
||||||
while ((__res = __ast_dyn_str_helper(buf, max_len, \
|
while ((__res = __ast_str_helper(buf, max_len, \
|
||||||
ts, 1, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) { \
|
1, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) { \
|
||||||
va_end(ap); \
|
va_end(ap); \
|
||||||
va_start(ap, fmt); \
|
va_start(ap, fmt); \
|
||||||
} \
|
} \
|
||||||
@@ -498,10 +488,10 @@ enum {
|
|||||||
})
|
})
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Core functionality of ast_dynamic_str_[thread_](set|append)_va
|
* \brief Core functionality of ast_str_(set|append)_va
|
||||||
*
|
*
|
||||||
* The arguments to this function are the same as those described for
|
* The arguments to this function are the same as those described for
|
||||||
* ast_dynamic_str_thread_set_va except for an addition argument, append.
|
* ast_str_set_va except for an addition argument, append.
|
||||||
* If append is non-zero, this will append to the current string instead of
|
* If append is non-zero, this will append to the current string instead of
|
||||||
* writing over it.
|
* writing over it.
|
||||||
*
|
*
|
||||||
@@ -520,58 +510,35 @@ enum {
|
|||||||
* through calling one of the other functions or macros defined in this
|
* through calling one of the other functions or macros defined in this
|
||||||
* file.
|
* file.
|
||||||
*/
|
*/
|
||||||
int __ast_dyn_str_helper(struct ast_dynamic_str **buf, size_t max_len,
|
int __ast_str_helper(struct ast_str **buf, size_t max_len,
|
||||||
struct ast_threadstorage *ts, int append, const char *fmt, va_list ap);
|
int append, const char *fmt, va_list ap);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Set a thread locally stored dynamic string using variable arguments
|
* \brief Set a dynamic string using variable arguments
|
||||||
*
|
*
|
||||||
* \arg buf This is the address of a pointer to an ast_dynamic_str which should
|
* \arg buf This is the address of a pointer to a struct ast_str which should
|
||||||
* have been retrieved using ast_dynamic_str_thread_get. It will need to
|
* have been retrieved using ast_str_thread_get. It will need to
|
||||||
* be updated in the case that the buffer has to be reallocated to
|
* be updated in the case that the buffer has to be reallocated to
|
||||||
* accomodate a longer string than what it currently has space for.
|
* accomodate a longer string than what it currently has space for.
|
||||||
* \arg max_len This is the maximum length to allow the string buffer to grow
|
* \arg max_len This is the maximum length to allow the string buffer to grow
|
||||||
* to. If this is set to 0, then there is no maximum length.
|
* to. If this is set to 0, then there is no maximum length.
|
||||||
* \arg ts This is a pointer to the thread storage structure declared by using
|
* If set to -1, we are bound to the current maximum length.
|
||||||
* the AST_THREADSTORAGE macro. If declared with
|
|
||||||
* AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be
|
|
||||||
* (&my_buf).
|
|
||||||
* \arg fmt This is the format string (printf style)
|
* \arg fmt This is the format string (printf style)
|
||||||
*
|
*
|
||||||
* \return The return value of this function is the same as that of the printf
|
* \return The return value of this function is the same as that of the printf
|
||||||
* family of functions.
|
* family of functions.
|
||||||
*
|
*
|
||||||
* Example usage:
|
* All the rest is the same as ast_str_set_va()
|
||||||
* \code
|
|
||||||
* AST_THREADSTORAGE(my_str, my_str_init);
|
|
||||||
* #define MY_STR_INIT_SIZE 128
|
|
||||||
* ...
|
|
||||||
* void my_func(int arg1, int arg2)
|
|
||||||
* {
|
|
||||||
* struct ast_dynamic_str *buf;
|
|
||||||
* va_list ap;
|
|
||||||
*
|
|
||||||
* if (!(buf = ast_dynamic_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
|
|
||||||
* return;
|
|
||||||
* ...
|
|
||||||
* ast_dynamic_str_thread_set(&buf, 0, &my_str, "arg1: %d arg2: %d\n",
|
|
||||||
* arg1, arg2);
|
|
||||||
*
|
|
||||||
* printf("This is the string we just built: %s\n", buf->str);
|
|
||||||
* ...
|
|
||||||
* }
|
|
||||||
* \endcode
|
|
||||||
*/
|
*/
|
||||||
AST_INLINE_API(
|
AST_INLINE_API(
|
||||||
int __attribute__ ((format (printf, 4, 5))) ast_dynamic_str_thread_set(
|
int __attribute__ ((format (printf, 3, 4))) ast_str_set(
|
||||||
struct ast_dynamic_str **buf, size_t max_len,
|
struct ast_str **buf, size_t max_len, const char *fmt, ...),
|
||||||
struct ast_threadstorage *ts, const char *fmt, ...),
|
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
res = ast_dynamic_str_thread_set_va(buf, max_len, ts, fmt, ap);
|
res = ast_str_set_va(buf, max_len, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
@@ -582,70 +549,17 @@ int __attribute__ ((format (printf, 4, 5))) ast_dynamic_str_thread_set(
|
|||||||
* \brief Append to a thread local dynamic string
|
* \brief Append to a thread local dynamic string
|
||||||
*
|
*
|
||||||
* The arguments, return values, and usage of this function are the same as
|
* The arguments, return values, and usage of this function are the same as
|
||||||
* ast_dynamic_str_thread_set(). However, instead of setting a new value for
|
* ast_str_set(), but the new data is appended to the current value.
|
||||||
* the string, this function appends to the current value.
|
|
||||||
*/
|
*/
|
||||||
AST_INLINE_API(
|
AST_INLINE_API(
|
||||||
int __attribute__ ((format (printf, 4, 5))) ast_dynamic_str_thread_append(
|
int __attribute__ ((format (printf, 3, 4))) ast_str_append(
|
||||||
struct ast_dynamic_str **buf, size_t max_len,
|
struct ast_str **buf, size_t max_len, const char *fmt, ...),
|
||||||
struct ast_threadstorage *ts, const char *fmt, ...),
|
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
res = ast_dynamic_str_thread_append_va(buf, max_len, ts, fmt, ap);
|
res = ast_str_append_va(buf, max_len, fmt, ap);
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Set a dynamic string
|
|
||||||
*
|
|
||||||
* \arg buf This is the address of a pointer to an ast_dynamic_str. It will
|
|
||||||
* need to be updated in the case that the buffer has to be reallocated to
|
|
||||||
* accommodate a longer string than what it currently has space for.
|
|
||||||
* \arg max_len This is the maximum length to allow the string buffer to grow
|
|
||||||
* to. If this is set to 0, then there is no maximum length.
|
|
||||||
*
|
|
||||||
* \return The return value of this function is the same as that of the printf
|
|
||||||
* family of functions.
|
|
||||||
*/
|
|
||||||
AST_INLINE_API(
|
|
||||||
int __attribute__ ((format (printf, 3, 4))) ast_dynamic_str_set(
|
|
||||||
struct ast_dynamic_str **buf, size_t max_len,
|
|
||||||
const char *fmt, ...),
|
|
||||||
{
|
|
||||||
int res;
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
res = ast_dynamic_str_thread_set_va(buf, max_len, NULL, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Append to a dynamic string
|
|
||||||
*
|
|
||||||
* The arguments, return values, and usage of this function are the same as
|
|
||||||
* ast_dynamic_str_set(). However, this function appends to the string instead
|
|
||||||
* of setting a new value.
|
|
||||||
*/
|
|
||||||
AST_INLINE_API(
|
|
||||||
int __attribute__ ((format (printf, 3, 4))) ast_dynamic_str_append(
|
|
||||||
struct ast_dynamic_str **buf, size_t max_len,
|
|
||||||
const char *fmt, ...),
|
|
||||||
{
|
|
||||||
int res;
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
res = ast_dynamic_str_thread_append_va(buf, max_len, NULL, fmt, ap);
|
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@@ -59,14 +59,14 @@ AST_THREADSTORAGE(ast_cli_buf);
|
|||||||
void ast_cli(int fd, char *fmt, ...)
|
void ast_cli(int fd, char *fmt, ...)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
struct ast_dynamic_str *buf;
|
struct ast_str *buf;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
if (!(buf = ast_dynamic_str_thread_get(&ast_cli_buf, AST_CLI_INITLEN)))
|
if (!(buf = ast_str_thread_get(&ast_cli_buf, AST_CLI_INITLEN)))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
res = ast_dynamic_str_thread_set_va(&buf, 0, &ast_cli_buf, fmt, ap);
|
res = ast_str_set_va(&buf, 0, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if (res != AST_DYNSTR_BUILD_FAILED)
|
if (res != AST_DYNSTR_BUILD_FAILED)
|
||||||
|
|||||||
@@ -695,14 +695,14 @@ static void ast_log_vsyslog(int level, const char *file, int line, const char *f
|
|||||||
void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
|
void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
struct logchannel *chan;
|
struct logchannel *chan;
|
||||||
struct ast_dynamic_str *buf;
|
struct ast_str *buf;
|
||||||
time_t t;
|
time_t t;
|
||||||
struct tm tm;
|
struct tm tm;
|
||||||
char date[256];
|
char date[256];
|
||||||
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
if (!(buf = ast_dynamic_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
|
if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (AST_LIST_EMPTY(&logchannels))
|
if (AST_LIST_EMPTY(&logchannels))
|
||||||
@@ -714,7 +714,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
|
|||||||
if (level != __LOG_VERBOSE) {
|
if (level != __LOG_VERBOSE) {
|
||||||
int res;
|
int res;
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
|
res = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
if (res != AST_DYNSTR_BUILD_FAILED) {
|
if (res != AST_DYNSTR_BUILD_FAILED) {
|
||||||
term_filter_escapes(buf->str);
|
term_filter_escapes(buf->str);
|
||||||
@@ -775,7 +775,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
|
|||||||
if (level != __LOG_VERBOSE) {
|
if (level != __LOG_VERBOSE) {
|
||||||
int res;
|
int res;
|
||||||
sprintf(linestr, "%d", line);
|
sprintf(linestr, "%d", line);
|
||||||
ast_dynamic_str_thread_set(&buf, BUFSIZ, &log_buf,
|
ast_str_set(&buf, BUFSIZ,
|
||||||
"[%s] %s[%ld]: %s:%s %s: ",
|
"[%s] %s[%ld]: %s:%s %s: ",
|
||||||
date,
|
date,
|
||||||
term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
|
term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
|
||||||
@@ -788,7 +788,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
|
|||||||
ast_console_puts_mutable(buf->str);
|
ast_console_puts_mutable(buf->str);
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
|
res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
if (res != AST_DYNSTR_BUILD_FAILED)
|
if (res != AST_DYNSTR_BUILD_FAILED)
|
||||||
ast_console_puts_mutable(buf->str);
|
ast_console_puts_mutable(buf->str);
|
||||||
@@ -796,7 +796,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
|
|||||||
/* File channels */
|
/* File channels */
|
||||||
} else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
|
} else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
|
||||||
int res;
|
int res;
|
||||||
ast_dynamic_str_thread_set(&buf, BUFSIZ, &log_buf,
|
ast_str_set(&buf, BUFSIZ,
|
||||||
"[%s] %s[%ld] %s: ",
|
"[%s] %s[%ld] %s: ",
|
||||||
date, levels[level], (long)GETTID(), file);
|
date, levels[level], (long)GETTID(), file);
|
||||||
res = fprintf(chan->fileptr, "%s", buf->str);
|
res = fprintf(chan->fileptr, "%s", buf->str);
|
||||||
@@ -812,7 +812,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
|
|||||||
int res;
|
int res;
|
||||||
/* No error message, continue printing */
|
/* No error message, continue printing */
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
|
res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
if (res != AST_DYNSTR_BUILD_FAILED) {
|
if (res != AST_DYNSTR_BUILD_FAILED) {
|
||||||
term_strip(buf->str, buf->str, buf->len);
|
term_strip(buf->str, buf->str, buf->len);
|
||||||
@@ -868,7 +868,7 @@ void ast_backtrace(void)
|
|||||||
void ast_verbose(const char *fmt, ...)
|
void ast_verbose(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
struct verb *v;
|
struct verb *v;
|
||||||
struct ast_dynamic_str *buf;
|
struct ast_str *buf;
|
||||||
int res;
|
int res;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
@@ -886,11 +886,11 @@ void ast_verbose(const char *fmt, ...)
|
|||||||
fmt = datefmt;
|
fmt = datefmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(buf = ast_dynamic_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
|
if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
res = ast_dynamic_str_thread_set_va(&buf, 0, &verbose_buf, fmt, ap);
|
res = ast_str_set_va(&buf, 0, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if (res == AST_DYNSTR_BUILD_FAILED)
|
if (res == AST_DYNSTR_BUILD_FAILED)
|
||||||
|
|||||||
@@ -769,13 +769,13 @@ static int send_string(struct mansession *s, char *string)
|
|||||||
void astman_append(struct mansession *s, const char *fmt, ...)
|
void astman_append(struct mansession *s, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
struct ast_dynamic_str *buf;
|
struct ast_str *buf;
|
||||||
|
|
||||||
if (!(buf = ast_dynamic_str_thread_get(&astman_append_buf, ASTMAN_APPEND_BUF_INITSIZE)))
|
if (!(buf = ast_str_thread_get(&astman_append_buf, ASTMAN_APPEND_BUF_INITSIZE)))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
ast_dynamic_str_thread_set_va(&buf, 0, &astman_append_buf, fmt, ap);
|
ast_str_set_va(&buf, 0, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if (s->f != NULL)
|
if (s->f != NULL)
|
||||||
@@ -2277,39 +2277,39 @@ int __manager_event(int category, const char *event,
|
|||||||
char tmp[4096] = "";
|
char tmp[4096] = "";
|
||||||
va_list ap;
|
va_list ap;
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
struct ast_dynamic_str *buf;
|
struct ast_str *buf;
|
||||||
|
|
||||||
/* Abort if there aren't any manager sessions */
|
/* Abort if there aren't any manager sessions */
|
||||||
if (!num_sessions)
|
if (!num_sessions)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!(buf = ast_dynamic_str_thread_get(&manager_event_buf, MANAGER_EVENT_BUF_INITSIZE)))
|
if (!(buf = ast_str_thread_get(&manager_event_buf, MANAGER_EVENT_BUF_INITSIZE)))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
ast_dynamic_str_thread_set(&buf, 0, &manager_event_buf,
|
ast_str_set(&buf, 0,
|
||||||
"Event: %s\r\nPrivilege: %s\r\n",
|
"Event: %s\r\nPrivilege: %s\r\n",
|
||||||
event, authority_to_str(category, auth, sizeof(auth)));
|
event, authority_to_str(category, auth, sizeof(auth)));
|
||||||
|
|
||||||
if (timestampevents) {
|
if (timestampevents) {
|
||||||
now = ast_tvnow();
|
now = ast_tvnow();
|
||||||
ast_dynamic_str_thread_append(&buf, 0, &manager_event_buf,
|
ast_str_append(&buf, 0,
|
||||||
"Timestamp: %ld.%06lu\r\n",
|
"Timestamp: %ld.%06lu\r\n",
|
||||||
now.tv_sec, (unsigned long) now.tv_usec);
|
now.tv_sec, (unsigned long) now.tv_usec);
|
||||||
}
|
}
|
||||||
if (manager_debug) {
|
if (manager_debug) {
|
||||||
static int seq;
|
static int seq;
|
||||||
ast_dynamic_str_thread_append(&buf, 0, &manager_event_buf,
|
ast_str_append(&buf, 0,
|
||||||
"SequenceNumber: %d\r\n",
|
"SequenceNumber: %d\r\n",
|
||||||
ast_atomic_fetchadd_int(&seq, 1));
|
ast_atomic_fetchadd_int(&seq, 1));
|
||||||
ast_dynamic_str_thread_append(&buf, 0, &manager_event_buf,
|
ast_str_append(&buf, 0,
|
||||||
"File: %s\r\nLine: %d\r\nFunc: %s\r\n", file, line, func);
|
"File: %s\r\nLine: %d\r\nFunc: %s\r\n", file, line, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
ast_dynamic_str_thread_append_va(&buf, 0, &manager_event_buf, fmt, ap);
|
ast_str_append_va(&buf, 0, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
ast_dynamic_str_thread_append(&buf, 0, &manager_event_buf, "\r\n");
|
ast_str_append(&buf, 0, "\r\n");
|
||||||
|
|
||||||
append_event(buf->str, category);
|
append_event(buf->str, category);
|
||||||
|
|
||||||
|
|||||||
20
main/utils.c
20
main/utils.c
@@ -976,13 +976,13 @@ int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
|
|||||||
* core handler for dynamic strings.
|
* core handler for dynamic strings.
|
||||||
* This is not meant to be called directly, but rather through the
|
* This is not meant to be called directly, but rather through the
|
||||||
* various wrapper macros
|
* various wrapper macros
|
||||||
* ast_dynamic_str_set(...)
|
* ast_str_set(...)
|
||||||
* ast_dynamic_str_append(...)
|
* ast_str_append(...)
|
||||||
* ast_dynamic_str_thread_set(...)
|
* ast_str_set_va(...)
|
||||||
* ast_dynamic_str_thread_append(...)
|
* ast_str_append_va(...)
|
||||||
*/
|
*/
|
||||||
int __ast_dyn_str_helper(struct ast_dynamic_str **buf, size_t max_len,
|
int __ast_str_helper(struct ast_str **buf, size_t max_len,
|
||||||
struct ast_threadstorage *ts, int append, const char *fmt, va_list ap)
|
int append, const char *fmt, va_list ap)
|
||||||
{
|
{
|
||||||
int res, need;
|
int res, need;
|
||||||
int offset = (append && (*buf)->len) ? (*buf)->used : 0;
|
int offset = (append && (*buf)->len) ? (*buf)->used : 0;
|
||||||
@@ -1005,17 +1005,17 @@ int __ast_dyn_str_helper(struct ast_dynamic_str **buf, size_t max_len,
|
|||||||
need = max_len;
|
need = max_len;
|
||||||
|
|
||||||
/* We can only realloc malloc'ed space. */
|
/* We can only realloc malloc'ed space. */
|
||||||
if ((*buf)->type != DS_MALLOC)
|
if ((*buf)->ts == DS_ALLOCA || (*buf)->ts == DS_STATIC)
|
||||||
return AST_DYNSTR_BUILD_FAILED;
|
return AST_DYNSTR_BUILD_FAILED;
|
||||||
*buf = ast_realloc(*buf, need + sizeof(struct ast_dynamic_str));
|
*buf = ast_realloc(*buf, need + sizeof(struct ast_str));
|
||||||
if (*buf == NULL) /* XXX watch out, we leak memory here */
|
if (*buf == NULL) /* XXX watch out, we leak memory here */
|
||||||
return AST_DYNSTR_BUILD_FAILED;
|
return AST_DYNSTR_BUILD_FAILED;
|
||||||
(*buf)->len = need;
|
(*buf)->len = need;
|
||||||
|
|
||||||
(*buf)->str[offset] = '\0'; /* Truncate the partial write. */
|
(*buf)->str[offset] = '\0'; /* Truncate the partial write. */
|
||||||
|
|
||||||
if (ts)
|
if ((*buf)->ts != DS_ALLOCA)
|
||||||
pthread_setspecific(ts->key, *buf);
|
pthread_setspecific((*buf)->ts->key, *buf);
|
||||||
|
|
||||||
/* va_end() and va_start() must be done before calling
|
/* va_end() and va_start() must be done before calling
|
||||||
* vsnprintf() again. */
|
* vsnprintf() again. */
|
||||||
|
|||||||
Reference in New Issue
Block a user