[asterisk-commits] branch kpfleming/stringfields - r7761 in /team/kpfleming/stringfields: ./ inc...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Jan 3 20:07:02 CST 2006


Author: kpfleming
Date: Tue Jan  3 20:07:02 2006
New Revision: 7761

URL: http://svn.digium.com/view/asterisk?rev=7761&view=rev
Log:
store pool info in a struct, and prepare for ability to increase pool space

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

Modified: team/kpfleming/stringfields/include/asterisk/stringfields.h
URL: http://svn.digium.com/view/asterisk/team/kpfleming/stringfields/include/asterisk/stringfields.h?rev=7761&r1=7760&r2=7761&view=diff
==============================================================================
--- team/kpfleming/stringfields/include/asterisk/stringfields.h (original)
+++ team/kpfleming/stringfields/include/asterisk/stringfields.h Tue Jan  3 20:07:02 2006
@@ -33,7 +33,16 @@
 
 typedef const char const * ast_string_field;
 
-int __ast_string_field_init(char **pool, size_t size, size_t *pool_size);
+struct ast_string_field_pool {
+	char *base;
+	size_t size;
+	size_t space;
+	size_t used;
+};
+
+int __ast_string_field_init(struct ast_string_field_pool *pool, size_t size);
+char *__ast_string_field_alloc_space(struct ast_string_field_pool *pool, size_t needed,
+				     ast_string_field *first_field, int num_fields);
 
 #define AST_STRING_FIELD_DEFAULT_POOL 1024
 
@@ -43,9 +52,7 @@
 	ast_string_field __begin_field[0]; \
 	field_list \
 	ast_string_field __end_field[0]; \
-	char *__field_pool; \
-	size_t __field_pool_size; \
-	size_t __field_pool_used;
+	struct ast_string_field_pool __field_pool;
 
 #define ast_string_field_count(x) \
 	(offsetof(typeof(*x), __end_field) - offsetof(typeof(*x), __begin_field)) / sizeof(ast_string_field)
@@ -54,36 +61,32 @@
 	(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)
+	__ast_string_field_init(&x->__field_pool, AST_STRING_FIELD_DEFAULT_POOL)
 
 #define ast_string_field_set(x, field, data) do { \
-	x->field = x->__field_pool + x->__field_pool_used; \
-	x->__field_pool_used += strlen(data) + 1; \
-	strcpy((char *) x->field, data); \
+	if ((x->field = __ast_string_field_alloc_space(&x->__field_pool, strlen(data) + 1, &x->__begin_field[0], ast_string_field_count(x)))) \
+		strcpy((char *) x->field, data); \
 	} while (0)
 
 #define ast_string_field_index_set(x, index, data) do { \
-	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); \
+	if ((x->__begin_field[index] = __ast_string_field_alloc_space(&x->__field_pool, strlen(data) + 1, &x->__begin_field[0], ast_string_field_count(x)))) \
+		strcpy((char *) x->__begin_field[index], data); \
 	} while (0)
 
 #define ast_string_field_build(x, field, fmt, args...) do { \
 	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); \
+	size_t needed; \
+	needed = snprintf(&s, 1, fmt, args) + 1; \
+	if ((x->field = __ast_string_field_alloc_space(&x->__field_pool, needed, &x->__begin_field[0], ast_string_field_count(x)))) \
+		sprintf((char *) x->field, fmt, args); \
 	} while (0)
 
 #define ast_string_field_index_build(x, index, fmt, args...) do { \
 	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); \
+	size_t needed; \
+	needed = snprintf(&s, 1, fmt, args) + 1; \
+	if ((x->__begin_field[index] = __ast_string_field_alloc_space(&x->__field_pool, needed, &x->__begin_field[0], ast_string_field_count(x)))) \
+		sprintf((char *) x->__begin_field[index], fmt, args); \
 	} while (0)
 
 #define ast_string_field_free(x, field) do { \
@@ -98,7 +101,7 @@
 	int index; \
 	for (index = 0; index < ast_string_field_count(x); index ++) \
 		ast_string_field_index_free(x, index); \
-	free(x->__field_pool); \
+	free(x->__field_pool.base); \
 	} 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=7761&r1=7760&r2=7761&view=diff
==============================================================================
--- team/kpfleming/stringfields/utils.c (original)
+++ team/kpfleming/stringfields/utils.c Tue Jan  3 20:07:02 2006
@@ -926,10 +926,26 @@
 	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;
-}
+int __ast_string_field_init(struct ast_string_field_pool *pool, size_t size)
+{
+	pool->base = calloc(1, size);
+	if (pool->base) {
+		pool->size = size;
+		pool->space = size;
+	}
+	return pool->base ? 0 : -1;
+}
+
+char *__ast_string_field_alloc_space(struct ast_string_field_pool *pool, size_t needed,
+				     ast_string_field *first_field, int num_fields)
+{
+	char *result = NULL;
+
+	if (__builtin_expect(needed > pool->space , 0)) {
+		/* make more space */
+	}
+
+	result = pool->base + pool->used;
+	pool->used += needed;
+	return result;
+}



More information about the asterisk-commits mailing list