[asterisk-commits] branch kpfleming/stringfields - r7756 in
/team/kpfleming/stringfields: ./ inc...
asterisk-commits at lists.digium.com
asterisk-commits at lists.digium.com
Tue Jan 3 18:19:20 CST 2006
Author: kpfleming
Date: Tue Jan 3 18:19:19 2006
New Revision: 7756
URL: http://svn.digium.com/view/asterisk?rev=7756&view=rev
Log:
add asprintf() wrapper for astmm
improve string field macros
Modified:
team/kpfleming/stringfields/astmm.c
team/kpfleming/stringfields/include/asterisk/astmm.h
team/kpfleming/stringfields/include/asterisk/stringfields.h
Modified: team/kpfleming/stringfields/astmm.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/stringfields/astmm.c?rev=7756&r1=7755&r2=7756&view=diff
==============================================================================
--- team/kpfleming/stringfields/astmm.c (original)
+++ team/kpfleming/stringfields/astmm.c Tue Jan 3 18:19:19 2006
@@ -47,6 +47,7 @@
#define FUNC_STRDUP 4
#define FUNC_STRNDUP 5
#define FUNC_VASPRINTF 6
+#define FUNC_ASPRINTF 7
/* Undefine all our macros */
#undef malloc
@@ -56,6 +57,7 @@
#undef strndup
#undef free
#undef vasprintf
+#undef asprintf
#define FENCE_MAGIC 0xdeadbeef
@@ -253,6 +255,28 @@
return ptr;
}
+int __ast_asprintf(char **strp, const char *file, int lineno, const char *func, const char *fmt, ...)
+{
+ int size;
+ va_list ap, ap2;
+ char s;
+
+ *strp = NULL;
+ va_start(ap, fmt);
+ va_copy(ap2, ap);
+ size = vsnprintf(&s, 1, fmt, ap2);
+ va_end(ap2);
+ *strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func);
+ if (!*strp) {
+ va_end(ap);
+ return -1;
+ }
+ vsnprintf(*strp, size + 1, fmt, ap);
+ va_end(ap);
+
+ return size;
+}
+
int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func)
{
int size;
@@ -264,8 +288,10 @@
size = vsnprintf(&s, 1, fmt, ap2);
va_end(ap2);
*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func);
- if (!*strp)
+ if (!*strp) {
+ va_end(ap);
return -1;
+ }
vsnprintf(*strp, size + 1, fmt, ap);
return size;
Modified: team/kpfleming/stringfields/include/asterisk/astmm.h
URL: http://svn.digium.com/view/asterisk/team/kpfleming/stringfields/include/asterisk/astmm.h?rev=7756&r1=7755&r2=7756&view=diff
==============================================================================
--- team/kpfleming/stringfields/include/asterisk/astmm.h (original)
+++ team/kpfleming/stringfields/include/asterisk/astmm.h Tue Jan 3 18:19:19 2006
@@ -38,6 +38,7 @@
#undef realloc
#undef strdup
#undef strndup
+#undef asprintf
#undef vasprintf
void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func);
@@ -46,6 +47,7 @@
void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func);
char *__ast_strdup(const char *s, const char *file, int lineno, const char *func);
char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func);
+int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format, ...);
int __ast_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func);
void __ast_mm_init(void);
@@ -70,6 +72,9 @@
#define strndup(a,b) \
__ast_strndup(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+#define asprintf(a, b, c...) \
+ __ast_asprintf(a, __FILE__, __LINE__, __PRETTY_FUNCTION__, b, c)
+
#define vasprintf(a,b,c) \
__ast_vasprintf(a,b,c,__FILE__, __LINE__, __PRETTY_FUNCTION__)
Modified: team/kpfleming/stringfields/include/asterisk/stringfields.h
URL: http://svn.digium.com/view/asterisk/team/kpfleming/stringfields/include/asterisk/stringfields.h?rev=7756&r1=7755&r2=7756&view=diff
==============================================================================
--- team/kpfleming/stringfields/include/asterisk/stringfields.h (original)
+++ team/kpfleming/stringfields/include/asterisk/stringfields.h Tue Jan 3 18:19:19 2006
@@ -43,21 +43,28 @@
#define ast_string_field_count(x) \
(offsetof(typeof(*x), __end_field) - offsetof(typeof(*x), __begin_field)) / sizeof(ast_string_field)
-#define ast_string_field_set(x, field, data) \
- x->field = strdup(data)
+#define ast_string_field_set(x, field, data) do { \
+ if (x->field) \
+ free((char *) x->field); \
+ x->field = strdup(data); \
+ } while (0)
-#define ast_string_field_build(x, field, fmt, args...) \
- asprintf((char **) &x->field, fmt, args)
+#define ast_string_field_build(x, field, fmt, args...) do { \
+ if (x->field) \
+ free((char *) x->field); \
+ asprintf((char **) &x->field, fmt, args); \
+ } while (0)
-#define ast_string_field_free(x, field) \
+#define ast_string_field_free(x, field) do { \
if (x->field) \
- free((char *) x->field)
+ free((char *) x->field) \
+ } while(0)
-#define ast_string_field_free_all(x) { \
+#define ast_string_field_free_all(x) do { \
int index; \
for (index = 0; index < ast_string_field_count(x); index ++) \
if (x->__begin_field[index]) \
free(x->__begin_field[index]); \
- }
+ } while(0)
#endif /* _ASTERISK_STRINGFIELDS_H */
More information about the asterisk-commits
mailing list