[asterisk-commits] trunk r17898 - /trunk/config.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Apr 6 09:06:59 MST 2006


Author: rizzo
Date: Thu Apr  6 11:06:57 2006
New Revision: 17898

URL: http://svn.digium.com/view/asterisk?rev=17898&view=rev
Log:
optimize move_variables() so that two lists can be created
in constant time instead of scanning the entire list.
 
On passing, minor formatting fixes.    


Modified:
    trunk/config.c

Modified: trunk/config.c
URL: http://svn.digium.com/view/asterisk/trunk/config.c?rev=17898&r1=17897&r2=17898&view=diff
==============================================================================
--- trunk/config.c (original)
+++ trunk/config.c Thu Apr  6 11:06:57 2006
@@ -115,6 +115,8 @@
 
 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
 {
+	if (!variable)
+		return;
 	if (category->last)
 		category->last->next = variable;
 	else
@@ -142,10 +144,7 @@
 	else
 		cat = ast_category_get(config, category);
 
-	if (cat)
-		return cat->root;
-	else
-		return NULL;
+	return (cat) ? cat->root : NULL;
 }
 
 char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
@@ -185,26 +184,27 @@
  
 static void move_variables(struct ast_category *old, struct ast_category *new)
 {
-	struct ast_variable *var;
-	struct ast_variable *next;
-
-	next = old->root;
+	struct ast_variable *var = old->root;
 	old->root = NULL;
-	for (var = next; var; var = next) {
-		next = var->next;
+#if 1
+	/* we can just move the entire list in a single op */
+	ast_variable_append(new, var);
+#else
+	while (var) {
+		struct ast_variable *next = var->next;
 		var->next = NULL;
 		ast_variable_append(new, var);
-	}
+		var = next;
+	}
+#endif
 }
 
 struct ast_category *ast_category_new(const char *name) 
 {
 	struct ast_category *category;
 
-	if ((category = ast_calloc(1, sizeof(*category)))) {		
+	if ((category = ast_calloc(1, sizeof(*category))))
 		ast_copy_string(category->name, name, sizeof(category->name));
-	}
-
 	return category;
 }
 
@@ -212,6 +212,7 @@
 {
 	struct ast_category *cat;
 
+	/* try exact match first, then case-insensitive match */
 	for (cat = config->root; cat; cat = cat->next) {
 		if (cat->name == category_name && (ignored || !cat->ignored))
 			return cat;
@@ -265,7 +266,7 @@
 	if (prev && config->last_browse && (config->last_browse->name == prev))
 		cat = config->last_browse->next;
 	else if (!prev && config->root)
-			cat = config->root;
+		cat = config->root;
 	else if (prev) {
 		for (cat = config->root; cat; cat = cat->next) {
 			if (cat->name == prev) {
@@ -287,10 +288,7 @@
 		cat = next_available_category(cat);
 
 	config->last_browse = cat;
-	if (cat)
-		return cat->name;
-	else
-		return NULL;
+	return (cat) ? cat->name : NULL;
 }
 
 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
@@ -312,23 +310,16 @@
 {
 	struct ast_variable *var;
 
-	for (var = base->root; var; var = var->next) {
-		struct ast_variable *v;
-		
-		v = variable_clone(var);
-		if (v)
-			ast_variable_append(new, v);
-	}
+	for (var = base->root; var; var = var->next)
+		ast_variable_append(new, variable_clone(var));
 }
 
 struct ast_config *ast_config_new(void) 
 {
 	struct ast_config *config;
 
-	if ((config = ast_calloc(1, sizeof(*config)))) {
+	if ((config = ast_calloc(1, sizeof(*config))))
 		config->max_include_level = MAX_INCLUDE_LEVEL;
-	}
-
 	return config;
 }
 



More information about the asterisk-commits mailing list