diff options
author | Guillaume Nault <g.nault@alphalink.fr> | 2013-07-10 12:54:57 +0200 |
---|---|---|
committer | Dmitry Kozlov <xeb@mail.ru> | 2013-07-18 22:57:36 +0400 |
commit | 36070c83907db169f2e6cd443b216dbbea2e7637 (patch) | |
tree | f0baf97c5349cc7dd3ea244cf9b9d3ba40b70fc4 | |
parent | 09de0ff06ef5ebb3b594a42ca8996f92a854078a (diff) | |
download | accel-ppp-xebd-36070c83907db169f2e6cd443b216dbbea2e7637.tar.gz accel-ppp-xebd-36070c83907db169f2e6cd443b216dbbea2e7637.zip |
memdbg: Implement asprintf()
Define md_asprintf() as an equivalent of the GNU libc's asprintf()
(with buffer allocated by md_malloc).
Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
-rw-r--r-- | accel-pppd/memdebug.c | 36 | ||||
-rw-r--r-- | accel-pppd/memdebug.h | 4 |
2 files changed, 39 insertions, 1 deletions
diff --git a/accel-pppd/memdebug.c b/accel-pppd/memdebug.c index 5629146..1f4af36 100644 --- a/accel-pppd/memdebug.c +++ b/accel-pppd/memdebug.c @@ -1,5 +1,6 @@ #undef MEMDEBUG +#include <stdarg.h> #include <stdlib.h> #include <stdint.h> #include <string.h> @@ -164,6 +165,41 @@ char __export *md_strndup(const char *ptr, size_t n, const char *fname, int line return str; } +int __export md_asprintf(const char *fname, int line, + char **strp, const char *fmt, ...) +{ + va_list ap; + va_list aq; + int len; + + va_start(ap, fmt); + va_copy(aq, ap); + + len = vsnprintf(NULL, 0, fmt, ap); + if (len < 0) + goto err; + + *strp = md_malloc(len + 1, fname, line); + if (*strp == NULL) + goto err; + + len = vsnprintf(*strp, len + 1, fmt, aq); + if (len < 0) + goto err_strp; + + va_end(aq); + va_end(ap); + + return len; + +err_strp: + md_free(*strp, fname, line); +err: + va_end(aq); + va_end(ap); + return -1; +} + static void siginfo(int num) { struct mem_t *mem; diff --git a/accel-pppd/memdebug.h b/accel-pppd/memdebug.h index 98e87d0..09978f4 100644 --- a/accel-pppd/memdebug.h +++ b/accel-pppd/memdebug.h @@ -10,12 +10,14 @@ #define _free(ptr) md_free(ptr, __FILE__, __LINE__) #define _strdup(str) md_strdup(str, __FILE__, __LINE__) #define _strndup(str, size) md_strndup(str, size, __FILE__, __LINE__) +#define _asprintf(strp, fmt, ...) md_asprintf(__FILE__, __LINE__, strp, fmt, ##__VA_ARGS__) void *md_malloc(size_t size, const char *fname, int line); void *md_realloc(void *ptr, size_t size, const char *fname, int line); void md_free(void *ptr, const char *fname, int line); char* md_strdup(const char *ptr, const char *fname, int line); char* md_strndup(const char *ptr, size_t size, const char *fname, int line); +int md_asprintf(const char *fname, int line, char **strp, const char *fmt, ...) __attribute__((format(gnu_printf, 4, 5))); void md_check(void *ptr); #else @@ -24,7 +26,7 @@ void md_check(void *ptr); #define _free(ptr) free(ptr) #define _strdup(str) strdup(str) #define _strndup(str, size) strndup(str, size) +#define _asprintf(strp, fmt, ...) asprintf(strp, fmt, ##__VA_ARGS__) #endif #endif - |