[asterisk-commits] branch kpfleming/stringfields - r7760 in /team/kpfleming/stringfields: ./ cha...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Jan 3 19:40:51 CST 2006


Author: kpfleming
Date: Tue Jan  3 19:40:51 2006
New Revision: 7760

URL: http://svn.digium.com/view/asterisk?rev=7760&view=rev
Log:
use a basic memory pool for string allocation

Modified:
    team/kpfleming/stringfields/channels/chan_sip.c
    team/kpfleming/stringfields/include/asterisk/stringfields.h
    team/kpfleming/stringfields/utils.c

Modified: team/kpfleming/stringfields/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/stringfields/channels/chan_sip.c?rev=7760&r1=7759&r2=7760&view=diff
==============================================================================
--- team/kpfleming/stringfields/channels/chan_sip.c (original)
+++ team/kpfleming/stringfields/channels/chan_sip.c Tue Jan  3 19:40:51 2006
@@ -3048,6 +3048,11 @@
 	if (!(p = calloc(1, sizeof(*p))))
 		return NULL;
 
+	if (ast_string_field_init(p)) {
+		free(p);
+		return NULL;
+	}
+
 	ast_mutex_init(&p->lock);
 
 	p->method = intended_method;
@@ -3253,12 +3258,17 @@
 		ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
 		return -1;
 	}
-	reg = malloc(sizeof(struct sip_registry));
-	if (!reg) {
+	if (!(reg = calloc(1, sizeof(*reg)))) {
 		ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
 		return -1;
 	}
-	memset(reg, 0, sizeof(struct sip_registry));
+
+	if (ast_string_field_init(reg)) {
+		ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
+		free(reg);
+		return -1;
+	}
+
 	regobjs++;
 	ASTOBJ_INIT(reg);
 	ast_copy_string(reg->contact, contact, sizeof(reg->contact));

Modified: team/kpfleming/stringfields/include/asterisk/stringfields.h
URL: http://svn.digium.com/view/asterisk/team/kpfleming/stringfields/include/asterisk/stringfields.h?rev=7760&r1=7759&r2=7760&view=diff
==============================================================================
--- team/kpfleming/stringfields/include/asterisk/stringfields.h (original)
+++ team/kpfleming/stringfields/include/asterisk/stringfields.h Tue Jan  3 19:40:51 2006
@@ -33,12 +33,19 @@
 
 typedef const char const * ast_string_field;
 
+int __ast_string_field_init(char **pool, size_t size, size_t *pool_size);
+
+#define AST_STRING_FIELD_DEFAULT_POOL 1024
+
 #define AST_STRING_FIELD(name) ast_string_field name;
 
 #define AST_DECLARE_STRING_FIELDS(field_list) \
 	ast_string_field __begin_field[0]; \
 	field_list \
-	ast_string_field __end_field[0];
+	ast_string_field __end_field[0]; \
+	char *__field_pool; \
+	size_t __field_pool_size; \
+	size_t __field_pool_used;
 
 #define ast_string_field_count(x) \
 	(offsetof(typeof(*x), __end_field) - offsetof(typeof(*x), __begin_field)) / sizeof(ast_string_field)
@@ -46,44 +53,52 @@
 #define ast_string_field_index(x, field) \
 	(offsetof(typeof(*x), field) - offsetof(typeof(*x), __begin_field)) / sizeof(ast_string_field)
 
+#define ast_string_field_init(x) \
+	__ast_string_field_init(&x->__field_pool, AST_STRING_FIELD_DEFAULT_POOL, &x->__field_pool_size)
+
 #define ast_string_field_set(x, field, data) do { \
-	if (x->field) \
-		free((char *) x->field); \
-	x->field = (ast_string_field) strdup(data); \
+	x->field = x->__field_pool + x->__field_pool_used; \
+	x->__field_pool_used += strlen(data) + 1; \
+	strcpy((char *) x->field, data); \
 	} while (0)
 
 #define ast_string_field_index_set(x, index, data) do { \
-	if (x->__begin_field[index]) \
-		free((char *) x->__begin_field[index]); \
-	x->__begin_field[index] = (ast_string_field) strdup(data); \
+	x->__begin_field[index] = x->__field_pool + x->__field_pool_used; \
+	x->__field_pool_used += strlen(data) + 1; \
+	strcpy((char *) x->__begin_field[index], data); \
 	} while (0)
 
 #define ast_string_field_build(x, field, fmt, args...) do { \
-	if (x->field) \
-		free((char *) x->field); \
-	asprintf((char **) &x->field, fmt, args); \
+	char s; \
+	size_t size; \
+	size = snprintf(&s, 1, fmt, args); \
+	x->field = x->__field_pool + x->__field_pool_used; \
+	x->__field_pool_used += size + 1; \
+	sprintf((char *) x->field, fmt, args); \
 	} while (0)
 
 #define ast_string_field_index_build(x, index, fmt, args...) do { \
-	if (x->__begin_field[index]) \
-		free((char *) x->__begin_field[index]); \
-	asprintf((char **) &x->__begin_field[index], fmt, args); \
+	char s; \
+	size_t size; \
+	size = snprintf(&s, 1, fmt, args); \
+	x->__begin_field[index] = x->__field_pool + x->__field_pool_used; \
+	x->__field_pool_used += size + 1; \
+	sprintf((char *) x->__begin_field[index], fmt, args); \
 	} while (0)
 
 #define ast_string_field_free(x, field) do { \
-	if (x->field) \
-		free((char *) x->field); \
+	x->field = NULL; \
 	} while(0)
 
 #define ast_string_field_index_free(x, index) do { \
-	if (x->__begin_field[index]) \
-		free((char *) x->__begin_field[index]); \
+	x->__begin_field[index] = NULL; \
 	} while(0)
 
 #define ast_string_field_free_all(x) do { \
 	int index; \
 	for (index = 0; index < ast_string_field_count(x); index ++) \
 		ast_string_field_index_free(x, index); \
+	free(x->__field_pool); \
 	} while(0)
 
 #endif /* _ASTERISK_STRINGFIELDS_H */

Modified: team/kpfleming/stringfields/utils.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/stringfields/utils.c?rev=7760&r1=7759&r2=7760&view=diff
==============================================================================
--- team/kpfleming/stringfields/utils.c (original)
+++ team/kpfleming/stringfields/utils.c Tue Jan  3 19:40:51 2006
@@ -51,6 +51,9 @@
 
 #define AST_API_MODULE		/* ensure that inlinable API functions will be built in this module if required */
 #include "asterisk/time.h"
+
+#define AST_API_MODULE		/* ensure that inlinable API functions will be built in this module if required */
+#include "asterisk/stringfields.h"
 
 #define AST_API_MODULE		/* ensure that inlinable API functions will be built in this module if required */
 #include "asterisk/utils.h"
@@ -922,3 +925,11 @@
 		ofs--;
 	s[ofs] = '\0';
 }
+
+int __ast_string_field_init(char **pool, size_t size, size_t *pool_size)
+{
+	*pool = calloc(1, size);
+	if (*pool)
+		*pool_size = size;
+	return *pool ? 0 : -1;
+}



More information about the asterisk-commits mailing list