[asterisk-commits] eliel: trunk r165502 - in /trunk: include/asterisk/strings.h main/strings.c
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Thu Dec 18 09:25:16 CST 2008
Author: eliel
Date: Thu Dec 18 09:25:15 2008
New Revision: 165502
URL: http://svn.digium.com/view/asterisk?view=rev&rev=165502
Log:
Remove duplicate code from the ast_str API. We now use __AST_STR_* to
access 'struct ast_str' members, but this must only be used inside the API implementation.
(closes issue #14098)
Reported by: eliel
Patches:
ast_str.patch uploaded by eliel (license 64)
Modified:
trunk/include/asterisk/strings.h
trunk/main/strings.c
Modified: trunk/include/asterisk/strings.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/strings.h?view=diff&rev=165502&r1=165501&r2=165502
==============================================================================
--- trunk/include/asterisk/strings.h (original)
+++ trunk/include/asterisk/strings.h Thu Dec 18 09:25:15 2008
@@ -29,6 +29,18 @@
#include "asterisk/utils.h"
#include "asterisk/threadstorage.h"
+
+#if defined(DEBUG_OPAQUE)
+#define __AST_STR_USED used2
+#define __AST_STR_LEN len2
+#define __AST_STR_STR str2
+#define __AST_STR_TS ts2
+#else
+#define __AST_STR_USED used
+#define __AST_STR_LEN len
+#define __AST_STR_STR str
+#define __AST_STR_TS ts
+#endif
/* You may see casts in this header that may seem useless but they ensure this file is C++ clean */
@@ -340,23 +352,13 @@
* struct ast_threadstorage pointer.
*/
struct ast_str {
-#ifdef DEBUG_OPAQUE
- size_t len2;
- size_t used2;
- struct ast_threadstorage *ts2;
-#else
- size_t len; /*!< The current maximum length of the string */
- size_t used; /*!< Amount of space used */
- struct ast_threadstorage *ts; /*!< What kind of storage is this ? */
-#endif
+ size_t __AST_STR_LEN; /*!< The current maximum length of the string */
+ size_t __AST_STR_USED; /*!< Amount of space used */
+ struct ast_threadstorage *__AST_STR_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 */
-#ifdef DEBUG_OPAQUE
- char str2[0];
-#else
- char str[0]; /*!< The string buffer */
-#endif
+ char __AST_STR_STR[0]; /*!< The string buffer */
};
/*!
@@ -379,15 +381,9 @@
if (buf == NULL)
return NULL;
-#ifdef DEBUG_OPAQUE
- buf->len2 = init_len;
- buf->used2 = 0;
- buf->ts2 = DS_MALLOC;
-#else
- buf->len = init_len;
- buf->used = 0;
- buf->ts = DS_MALLOC;
-#endif
+ buf->__AST_STR_LEN = init_len;
+ buf->__AST_STR_USED = 0;
+ buf->__AST_STR_TS = DS_MALLOC;
return buf;
}
@@ -400,15 +396,9 @@
void ast_str_reset(struct ast_str *buf),
{
if (buf) {
-#ifdef DEBUG_OPAQUE
- buf->used2 = 0;
- if (buf->len2)
- buf->str2[0] = '\0';
-#else
- buf->used = 0;
- if (buf->len)
- buf->str[0] = '\0';
-#endif
+ buf->__AST_STR_USED = 0;
+ if (buf->__AST_STR_LEN)
+ buf->__AST_STR_STR[0] = '\0';
}
}
)
@@ -422,15 +412,9 @@
if (!buf) {
return;
}
-#ifdef DEBUG_OPAQUE
- while (buf->used2 && buf->str2[buf->used2 - 1] < 33) {
- buf->str2[--(buf->used2)] = '\0';
- }
-#else
- while (buf->used && buf->str[buf->used - 1] < 33) {
- buf->str[--(buf->used)] = '\0';
- }
-#endif
+ while (buf->__AST_STR_USED && buf->__AST_STR_STR[buf->__AST_STR_USED - 1] < 33) {
+ buf->__AST_STR_STR[--(buf->__AST_STR_USED)] = '\0';
+ }
}
)
@@ -440,11 +424,7 @@
AST_INLINE_API(
size_t ast_str_strlen(struct ast_str *buf),
{
-#ifdef DEBUG_OPAQUE
- return buf->used2;
-#else
- return buf->used;
-#endif
+ return buf->__AST_STR_USED;
}
)
@@ -454,11 +434,7 @@
AST_INLINE_API(
size_t ast_str_size(struct ast_str *buf),
{
-#ifdef DEBUG_OPAQUE
- return buf->len2;
-#else
- return buf->len;
-#endif
+ return buf->__AST_STR_LEN;
}
)
@@ -468,34 +444,20 @@
AST_INLINE_API(
attribute_pure char *ast_str_buffer(struct ast_str *buf),
{
-#ifdef DEBUG_OPAQUE
- return buf->str2;
-#else
- return buf->str;
-#endif
+ return buf->__AST_STR_STR;
}
)
AST_INLINE_API(
char *ast_str_truncate(struct ast_str *buf, ssize_t len),
{
-#ifdef DEBUG_OPAQUE
if (len < 0) {
- buf->used2 += ((ssize_t) abs(len)) > (ssize_t) buf->used2 ? -buf->used2 : len;
+ buf->__AST_STR_USED += ((ssize_t) abs(len)) > (ssize_t) buf->__AST_STR_USED ? -buf->__AST_STR_USED : len;
} else {
- buf->used2 = len;
- }
- buf->str2[buf->used2] = '\0';
- return buf->str2;
-#else
- if (len < 0) {
- buf->used += ((ssize_t) abs(len)) > (ssize_t) buf->used ? -buf->used : len;
- } else {
- buf->used = len;
- }
- buf->str[buf->used] = '\0';
- return buf->str;
-#endif
+ buf->__AST_STR_USED = len;
+ }
+ buf->__AST_STR_STR[buf->__AST_STR_USED] = '\0';
+ return buf->__AST_STR_STR;
}
)
@@ -522,39 +484,21 @@
{
struct ast_str *old_buf = *buf;
-#ifdef DEBUG_OPAQUE
- if (new_len <= (*buf)->len2)
+ if (new_len <= (*buf)->__AST_STR_LEN)
return 0; /* success */
- if ((*buf)->ts2 == DS_ALLOCA || (*buf)->ts2 == DS_STATIC)
+ if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
return -1; /* cannot extend */
*buf = (struct ast_str *)__ast_realloc(*buf, new_len + sizeof(struct ast_str), file, lineno, function);
if (*buf == NULL) {
*buf = old_buf;
return -1;
}
- if ((*buf)->ts2 != DS_MALLOC) {
- pthread_setspecific((*buf)->ts2->key, *buf);
+ if ((*buf)->__AST_STR_TS != DS_MALLOC) {
+ pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
_DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
}
- (*buf)->len2 = new_len;
-#else
- if (new_len <= (*buf)->len)
- return 0; /* success */
- if ((*buf)->ts == DS_ALLOCA || (*buf)->ts == DS_STATIC)
- return -1; /* cannot extend */
- *buf = (struct ast_str *)__ast_realloc(*buf, new_len + sizeof(struct ast_str), file, lineno, function);
- if (*buf == NULL) {
- *buf = old_buf;
- return -1;
- }
- if ((*buf)->ts != DS_MALLOC) {
- pthread_setspecific((*buf)->ts->key, *buf);
- _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
- }
-
- (*buf)->len = new_len;
-#endif
+ (*buf)->__AST_STR_LEN = new_len;
return 0;
}
)
@@ -565,67 +509,36 @@
{
struct ast_str *old_buf = *buf;
-#ifdef DEBUG_OPAQUE
- if (new_len <= (*buf)->len2)
+ if (new_len <= (*buf)->__AST_STR_LEN)
return 0; /* success */
- if ((*buf)->ts2 == DS_ALLOCA || (*buf)->ts2 == DS_STATIC)
+ if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
return -1; /* cannot extend */
*buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
if (*buf == NULL) {
*buf = old_buf;
return -1;
}
- if ((*buf)->ts2 != DS_MALLOC) {
- pthread_setspecific((*buf)->ts2->key, *buf);
+ if ((*buf)->__AST_STR_TS != DS_MALLOC) {
+ pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
_DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
}
- (*buf)->len2 = new_len;
-#else
- if (new_len <= (*buf)->len)
- return 0; /* success */
- if ((*buf)->ts == DS_ALLOCA || (*buf)->ts == DS_STATIC)
- return -1; /* cannot extend */
- *buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
- if (*buf == NULL) {
- *buf = old_buf;
- return -1;
- }
- if ((*buf)->ts != DS_MALLOC) {
- pthread_setspecific((*buf)->ts->key, *buf);
- _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
- }
-
- (*buf)->len = new_len;
+ (*buf)->__AST_STR_LEN = new_len;
+ return 0;
+}
+)
#endif
- return 0;
-}
-)
-#endif
-
-#ifdef DEBUG_OPAQUE
+
#define ast_str_alloca(init_len) \
({ \
struct ast_str *__ast_str_buf; \
__ast_str_buf = alloca(sizeof(*__ast_str_buf) + init_len); \
- __ast_str_buf->len2 = init_len; \
- __ast_str_buf->used2 = 0; \
- __ast_str_buf->ts2 = DS_ALLOCA; \
- __ast_str_buf->str2[0] = '\0'; \
+ __ast_str_buf->__AST_STR_LEN = init_len; \
+ __ast_str_buf->__AST_STR_USED = 0; \
+ __ast_str_buf->__AST_STR_TS = DS_ALLOCA; \
+ __ast_str_buf->__AST_STR_STR[0] = '\0'; \
(__ast_str_buf); \
})
-#else
-#define ast_str_alloca(init_len) \
- ({ \
- struct ast_str *__ast_str_buf; \
- __ast_str_buf = alloca(sizeof(*__ast_str_buf) + init_len); \
- __ast_str_buf->len = init_len; \
- __ast_str_buf->used = 0; \
- __ast_str_buf->ts = DS_ALLOCA; \
- __ast_str_buf->str[0] = '\0'; \
- (__ast_str_buf); \
- })
-#endif
/*!
* \brief Retrieve a thread locally stored dynamic string
@@ -669,19 +582,11 @@
if (buf == NULL)
return NULL;
-#ifdef DEBUG_OPAQUE
- if (!buf->len2) {
- buf->len2 = init_len;
- buf->used2 = 0;
- buf->ts2 = ts;
- }
-#else
- if (!buf->len) {
- buf->len = init_len;
- buf->used = 0;
- buf->ts = ts;
- }
-#endif
+ if (!buf->__AST_STR_LEN) {
+ buf->__AST_STR_LEN = init_len;
+ buf->__AST_STR_USED = 0;
+ buf->__AST_STR_TS = ts;
+ }
return buf;
}
@@ -697,19 +602,11 @@
if (buf == NULL)
return NULL;
-#ifdef DEBUG_OPAQUE
- if (!buf->len2) {
- buf->len2 = init_len;
- buf->used2 = 0;
- buf->ts2 = ts;
- }
-#else
- if (!buf->len) {
- buf->len = init_len;
- buf->used = 0;
- buf->ts = ts;
- }
-#endif
+ if (!buf->__AST_STR_LEN) {
+ buf->__AST_STR_LEN = init_len;
+ buf->__AST_STR_USED = 0;
+ buf->__AST_STR_TS = ts;
+ }
return buf;
}
@@ -862,27 +759,15 @@
{
SQLRETURN res;
if (maxlen == 0) {
-#ifdef DEBUG_OPAQUE
- if (SQLGetData(StatementHandle, ColumnNumber, TargetType, (*buf)->str2, 0, StrLen_or_Ind) == SQL_SUCCESS_WITH_INFO) {
+ if (SQLGetData(StatementHandle, ColumnNumber, TargetType, (*buf)->__AST_STR_STR, 0, StrLen_or_Ind) == SQL_SUCCESS_WITH_INFO) {
ast_str_make_space(buf, *StrLen_or_Ind + 1);
}
- maxlen = (*buf)->len2;
+ maxlen = (*buf)->__AST_STR_LEN;
} else if (maxlen > 0) {
ast_str_make_space(buf, maxlen);
}
- res = SQLGetData(StatementHandle, ColumnNumber, TargetType, (*buf)->str2, maxlen, StrLen_or_Ind);
- (*buf)->used2 = *StrLen_or_Ind;
-#else
- if (SQLGetData(StatementHandle, ColumnNumber, TargetType, (*buf)->str, 0, StrLen_or_Ind) == SQL_SUCCESS_WITH_INFO) {
- ast_str_make_space(buf, *StrLen_or_Ind + 1);
- }
- maxlen = (*buf)->len;
- } else if (maxlen > 0) {
- ast_str_make_space(buf, maxlen);
- }
- res = SQLGetData(StatementHandle, ColumnNumber, TargetType, (*buf)->str, maxlen, StrLen_or_Ind);
- (*buf)->used = *StrLen_or_Ind;
-#endif
+ res = SQLGetData(StatementHandle, ColumnNumber, TargetType, (*buf)->__AST_STR_STR, maxlen, StrLen_or_Ind);
+ (*buf)->__AST_STR_USED = *StrLen_or_Ind;
return res;
}
)
@@ -977,4 +862,5 @@
return abs(hash);
}
+
#endif /* _ASTERISK_STRINGS_H */
Modified: trunk/main/strings.c
URL: http://svn.digium.com/view/asterisk/trunk/main/strings.c?view=diff&rev=165502&r1=165501&r2=165502
==============================================================================
--- trunk/main/strings.c (original)
+++ trunk/main/strings.c Thu Dec 18 09:25:15 2008
@@ -52,67 +52,39 @@
int append, const char *fmt, va_list ap)
{
int res, need;
-#ifdef DEBUG_OPAQUE
- int offset = (append && (*buf)->len2) ? (*buf)->used2 : 0;
-#else
- int offset = (append && (*buf)->len) ? (*buf)->used : 0;
-#endif
+ int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0;
va_list aq;
do {
if (max_len < 0) {
-#ifdef DEBUG_OPAQUE
- max_len = (*buf)->len2; /* don't exceed the allocated space */
-#else
- max_len = (*buf)->len; /* don't exceed the allocated space */
-#endif
+ max_len = (*buf)->__AST_STR_LEN; /* don't exceed the allocated space */
}
/*
* Ask vsnprintf how much space we need. Remember that vsnprintf
* does not count the final '\0' so we must add 1.
*/
va_copy(aq, ap);
-#ifdef DEBUG_OPAQUE
- res = vsnprintf((*buf)->str2 + offset, (*buf)->len2 - offset, fmt, aq);
-#else
- res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, aq);
-#endif
+ res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq);
need = res + offset + 1;
/*
* If there is not enough space and we are below the max length,
* reallocate the buffer and return a message telling to retry.
*/
-#ifdef DEBUG_OPAQUE
- if (need > (*buf)->len2 && (max_len == 0 || (*buf)->len2 < max_len) ) {
-#else
- if (need > (*buf)->len && (max_len == 0 || (*buf)->len < max_len) ) {
-#endif
+ if (need > (*buf)->__AST_STR_LEN && (max_len == 0 || (*buf)->__AST_STR_LEN < max_len) ) {
if (max_len && max_len < need) { /* truncate as needed */
need = max_len;
} else if (max_len == 0) { /* if unbounded, give more room for next time */
need += 16 + need / 4;
}
if (0) { /* debugging */
-#ifdef DEBUG_OPAQUE
- ast_verbose("extend from %d to %d\n", (int)(*buf)->len2, need);
-#else
- ast_verbose("extend from %d to %d\n", (int)(*buf)->len, need);
-#endif
+ ast_verbose("extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need);
}
if (ast_str_make_space(buf, need)) {
-#ifdef DEBUG_OPAQUE
- ast_verbose("failed to extend from %d to %d\n", (int)(*buf)->len2, need);
-#else
- ast_verbose("failed to extend from %d to %d\n", (int)(*buf)->len, need);
-#endif
+ ast_verbose("failed to extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need);
return AST_DYNSTR_BUILD_FAILED;
}
-#ifdef DEBUG_OPAQUE
- (*buf)->str2[offset] = '\0'; /* Truncate the partial write. */
-#else
- (*buf)->str[offset] = '\0'; /* Truncate the partial write. */
-#endif
+ (*buf)->__AST_STR_STR[offset] = '\0'; /* Truncate the partial write. */
/* Restart va_copy before calling vsnprintf() again. */
va_end(aq);
@@ -121,11 +93,7 @@
break;
} while (1);
/* update space used, keep in mind the truncation */
-#ifdef DEBUG_OPAQUE
- (*buf)->used2 = (res + offset > (*buf)->len2) ? (*buf)->len2 : res + offset;
-#else
- (*buf)->used = (res + offset > (*buf)->len) ? (*buf)->len : res + offset;
-#endif
+ (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN : res + offset;
return res;
}
@@ -133,73 +101,41 @@
void ast_str_substitute_variables(struct ast_str **buf, size_t maxlen, struct ast_channel *chan, const char *template)
{
int first = 1;
-#ifdef DEBUG_OPAQUE
do {
ast_str_make_space(buf, maxlen ? maxlen :
- (first ? strlen(template) * 2 : (*buf)->len2 * 2));
- pbx_substitute_variables_helper_full(chan, NULL, template, (*buf)->str2, (*buf)->len2 - 1, &((*buf)->used2));
+ (first ? strlen(template) * 2 : (*buf)->__AST_STR_LEN * 2));
+ pbx_substitute_variables_helper_full(chan, NULL, template, (*buf)->__AST_STR_STR, (*buf)->__AST_STR_LEN - 1, &((*buf)->__AST_STR_USED));
first = 0;
- } while (maxlen == 0 && (*buf)->len2 - 5 < (*buf)->used2);
-#else
- do {
- ast_str_make_space(buf, maxlen ? maxlen :
- (first ? strlen(template) * 2 : (*buf)->len * 2));
- pbx_substitute_variables_helper_full(chan, NULL, template, (*buf)->str, (*buf)->len - 1, &((*buf)->used));
- first = 0;
- } while (maxlen == 0 && (*buf)->len - 5 < (*buf)->used);
-#endif
+ } while (maxlen == 0 && (*buf)->__AST_STR_LEN - 5 < (*buf)->__AST_STR_USED);
}
char *__ast_str_helper2(struct ast_str **buf, size_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
{
int dynamic = 0;
-#ifdef DEBUG_OPAQUE
- char *ptr = append ? &((*buf)->str2[(*buf)->used2]) : (*buf)->str2;
-#else
- char *ptr = append ? &((*buf)->str[(*buf)->used]) : (*buf)->str;
-#endif
+ char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR;
if (!maxlen) {
dynamic = 1;
-#ifdef DEBUG_OPAQUE
- maxlen = (*buf)->len2;
-#else
- maxlen = (*buf)->len;
-#endif
+ maxlen = (*buf)->__AST_STR_LEN;
}
while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) {
if (escapecommas && (*src == '\\' || *src == ',')) {
*ptr++ = '\\';
maxlen--;
-#ifdef DEBUG_OPAQUE
- (*buf)->used2++;
-#else
- (*buf)->used++;
-#endif
+ (*buf)->__AST_STR_USED++;
}
*ptr++ = *src++;
maxsrc--;
maxlen--;
-#ifdef DEBUG_OPAQUE
- (*buf)->used2++;
-#else
- (*buf)->used++;
-#endif
+ (*buf)->__AST_STR_USED++;
+
if (dynamic && (!maxlen || (escapecommas && !(maxlen - 1)))) {
-#ifdef DEBUG_OPAQUE
- size_t old = (*buf)->len2;
- if (ast_str_make_space(buf, (*buf)->len2 * 2)) {
+ size_t old = (*buf)->__AST_STR_LEN;
+ if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) {
/* If the buffer can't be extended, end it. */
break;
}
-#else
- size_t old = (*buf)->len;
- if (ast_str_make_space(buf, (*buf)->len * 2)) {
- /* If the buffer can't be extended, end it. */
- break;
- }
-#endif
/* What we extended the buffer by */
maxlen = old;
}
@@ -208,12 +144,7 @@
ptr--;
}
*ptr = '\0';
-#ifdef DEBUG_OPAQUE
- (*buf)->used2--;
- return (*buf)->str2;
-#else
- (*buf)->used--;
- return (*buf)->str;
-#endif
+ (*buf)->__AST_STR_USED--;
+ return (*buf)->__AST_STR_STR;
}
More information about the asterisk-commits
mailing list