mirror of
				https://github.com/asterisk/asterisk.git
				synced 2025-11-04 05:15:22 +00:00 
			
		
		
		
	add a vasprintf replacement. Bug #839
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@2030 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
		
							
								
								
									
										20
									
								
								astmm.c
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								astmm.c
									
									
									
									
									
								
							@@ -32,6 +32,7 @@
 | 
				
			|||||||
#define FUNC_REALLOC	3
 | 
					#define FUNC_REALLOC	3
 | 
				
			||||||
#define FUNC_STRDUP		4
 | 
					#define FUNC_STRDUP		4
 | 
				
			||||||
#define FUNC_STRNDUP	5
 | 
					#define FUNC_STRNDUP	5
 | 
				
			||||||
 | 
					#define FUNC_VASPRINTF	6
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Undefine all our macros */
 | 
					/* Undefine all our macros */
 | 
				
			||||||
#undef malloc
 | 
					#undef malloc
 | 
				
			||||||
@@ -40,6 +41,7 @@
 | 
				
			|||||||
#undef strdup
 | 
					#undef strdup
 | 
				
			||||||
#undef strndup
 | 
					#undef strndup
 | 
				
			||||||
#undef free
 | 
					#undef free
 | 
				
			||||||
 | 
					#undef vasprintf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static FILE *mmlog;
 | 
					static FILE *mmlog;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -217,6 +219,24 @@ char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const
 | 
				
			|||||||
	return ptr;
 | 
						return ptr;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func) 
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int n, size = strlen(fmt) + 1;
 | 
				
			||||||
 | 
						if ((*strp = __ast_alloc_region(size, FUNC_VASPRINTF, file, lineno, func)) == NULL)
 | 
				
			||||||
 | 
							return -1; 
 | 
				
			||||||
 | 
						for (;;) {
 | 
				
			||||||
 | 
							n = vsnprintf(*strp, size, fmt, ap);
 | 
				
			||||||
 | 
							if (n > -1 && n < size)
 | 
				
			||||||
 | 
								return n;
 | 
				
			||||||
 | 
							if (n > -1)	/* glibc 2.1 */
 | 
				
			||||||
 | 
								size = n+1;
 | 
				
			||||||
 | 
							else		/* glibc 2.0 */
 | 
				
			||||||
 | 
								size *= 2;
 | 
				
			||||||
 | 
							if ((*strp = __ast_realloc(*strp, size, file, lineno, func)) == NULL)
 | 
				
			||||||
 | 
								return -1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int handle_show_memory(int fd, int argc, char *argv[])
 | 
					static int handle_show_memory(int fd, int argc, char *argv[])
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	char *fn = NULL;
 | 
						char *fn = NULL;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,6 +21,7 @@
 | 
				
			|||||||
#include <sys/types.h>
 | 
					#include <sys/types.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Undefine any macros */
 | 
					/* Undefine any macros */
 | 
				
			||||||
#undef malloc
 | 
					#undef malloc
 | 
				
			||||||
@@ -28,6 +29,7 @@
 | 
				
			|||||||
#undef realloc
 | 
					#undef realloc
 | 
				
			||||||
#undef strdup
 | 
					#undef strdup
 | 
				
			||||||
#undef strndup
 | 
					#undef strndup
 | 
				
			||||||
 | 
					#undef vasprintf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func);
 | 
					void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func);
 | 
				
			||||||
void *__ast_malloc(size_t size, const char *file, int lineno, const char *func);
 | 
					void *__ast_malloc(size_t size, const char *file, int lineno, const char *func);
 | 
				
			||||||
@@ -35,6 +37,7 @@ void __ast_free(void *ptr, const char *file, int lineno, const char *func);
 | 
				
			|||||||
void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func);
 | 
					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_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);
 | 
					char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func);
 | 
				
			||||||
 | 
					int __ast_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void __ast_mm_init(void);
 | 
					void __ast_mm_init(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -58,6 +61,9 @@ void __ast_mm_init(void);
 | 
				
			|||||||
#define strndup(a,b) \
 | 
					#define strndup(a,b) \
 | 
				
			||||||
	__ast_strndup(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
 | 
						__ast_strndup(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define vasprintf(a,b,c) \
 | 
				
			||||||
 | 
						__ast_vasprintf(a,b,c,__FILE__, __LINE__, __PRETTY_FUNCTION__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
#error "NEVER INCLUDE astmm.h DIRECTLY!!"
 | 
					#error "NEVER INCLUDE astmm.h DIRECTLY!!"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user