[17/28] use 'find' tree function

Message ID 1308256197-29155-17-git-send-email-daniel.lezcano@linaro.org
State Accepted
Headers show

Commit Message

Daniel Lezcano June 16, 2011, 8:29 p.m.
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
---
 clocks.c |  192 +++++++++++++++++++-------------------------------------------
 tree.c   |   11 ++++
 tree.h   |    2 +
 3 files changed, 71 insertions(+), 134 deletions(-)

Patch

diff --git a/clocks.c b/clocks.c
index 5f494cd..d70d480 100644
--- a/clocks.c
+++ b/clocks.c
@@ -149,93 +149,81 @@  static inline int file_read_hex(const char *file, int *value)
 	return file_read_from_format(file, value, "%x");
 }
 
-static void dump_parent(struct clock_info *clk, int line, bool dump)
+static inline const char *clock_rate(int *rate)
 {
-	char *unit = "Hz";
-	double drate;
-	static char spaces[64];
-	char str[256];
-	static int maxline;
+        int r;
 
-	if (maxline < line)
-		maxline = line;
+        /* GHZ */
+        r = *rate >> 30;
+        if (r) {
+                *rate = r;
+                return "GHZ";
+        }
 
-	if (clk && clk->parent)
-		dump_parent(clk->parent, ++line, dump);
+        /* MHZ */
+        r = *rate >> 20;
+        if (r) {
+                *rate = r;
+                return "MHZ";
+        }
 
-	drate = (double)clk->rate;
-	if (drate > 1000 && drate < 1000000) {
-		unit = "KHz";
-		drate /= 1000;
-	}
-	if (drate > 1000000) {
-		unit = "MHz";
-		drate /= 1000000;
-	}
-	if (clk == clocks_info) {
-		line++;
-		strcpy(spaces, "");
-		sprintf(str, "%s%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
-			spaces, clk->name, clk->flags, clk->usecount, drate,
-			unit);
-	} else {
-		if (!(clk->parent == clocks_info))
-			strcat(spaces, "  ");
-		sprintf(str, "%s`- %s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n",
-			spaces, clk->name, clk->flags, clk->usecount, drate,
-			unit);
-	}
-	if (dump)
-		//printf("line=%d:m%d:l%d %s", maxline - line + 2, maxline, line, str);
-		printf("%s", str);
-	else
-		print_one_clock(maxline - line + 2, str, 1, 0);
+        /* KHZ */
+        r = *rate >> 10;
+        if (r) {
+                *rate = r;
+                return "KHZ";
+        }
+
+        return "";
 }
 
-static struct clock_info *find_clock(struct clock_info *clk, char *clkarg)
+static int dump_clock_cb(struct tree *t, void *data)
 {
-	int i;
-	struct clock_info *ret = clk;
-
-	if (!strcmp(clk->name, clkarg))
-		return ret;
+	struct clock_info *clk = t->private;
+	struct clock_info *pclk;
+	const char *unit;
+	int ret = 0;
+	int rate = clk->rate;
 
-	if (clk->children) {
-		for (i = 0; i < clk->num_children; i++) {
-			if (!strcmp(clk->children[i]->name, clkarg))
-				return clk->children[i];
-		}
-		for (i = 0; i < clk->num_children; i++) {
-			ret = find_clock(clk->children[i], clkarg);
-			if (ret)
-				return ret;
-		}
+	if (!t->parent) {
+		printf("/\n");
+		clk->prefix = "";
+		return 0;
 	}
 
-	return NULL;
+	pclk = t->parent->private;
+
+	if (!clk->prefix)
+		ret = asprintf(&clk->prefix, "%s%s%s", pclk->prefix,
+			       t->depth > 1 ? "   ": "", t->next ? "|" : " ");
+	if (ret < 0)
+		return -1;
+
+	unit = clock_rate(&rate);
+
+	printf("%s%s-- %s (flags:0x%x, usecount:%d, rate: %d %s)\n",
+	       clk->prefix,  !t->next ? "`" : "", t->name, clk->flags,
+	       clk->usecount, rate, unit);
+
+	return 0;
 }
 
-static void dump_all_parents(char *clkarg, bool dump)
+int dump_clock_info(void)
 {
-	struct clock_info *clk;
-	char spaces[1024];
-
-	strcpy(spaces, "");
+	return tree_for_each(clock_tree, dump_clock_cb, NULL);
+}
 
-	clk = find_clock(clocks_info, clkarg);
+static int dump_all_parents(char *clkarg, bool dump)
+{
+	struct tree *tree;
 
-	if (!clk)
+	tree = tree_find(clock_tree, clkarg);
+	if (!tree) {
 		printf("Clock NOT found!\n");
-	else {
-		/* while(clk && clk != clocks_info) { */
-		/* 	printf("%s\n", clk->name); */
-		/* 	strcat(spaces, "  "); */
-		/* 	clk = clk->parent; */
-		/* 	printf("%s <-- ", spaces); */
-		/* } */
-		/* printf("  /\n"); */
-		dump_parent(clk, 1, dump);
+		return -1;
 	}
+
+	return tree_for_each_parent(tree, dump_clock_cb, NULL);
 }
 
 void find_parents_for_clock(char *clkname, int complete)
@@ -587,70 +575,6 @@  void read_and_dump_clock_info_one(char *clk, bool dump)
 	printf("\n\n");
 }
 
-static inline const char *clock_rate(int *rate)
-{
-        int r;
-
-        /* GHZ */
-        r = *rate >> 30;
-        if (r) {
-                *rate = r;
-                return "GHZ";
-        }
-
-        /* MHZ */
-        r = *rate >> 20;
-        if (r) {
-                *rate = r;
-                return "MHZ";
-        }
-
-        /* KHZ */
-        r = *rate >> 10;
-        if (r) {
-                *rate = r;
-                return "KHZ";
-        }
-
-        return "";
-}
-
-static int dump_clock_cb(struct tree *t, void *data)
-{
-	struct clock_info *clk = t->private;
-	struct clock_info *pclk;
-	const char *unit;
-	int ret = 0;
-	int rate = clk->rate;
-
-	if (!t->parent) {
-		printf("/\n");
-		clk->prefix = "";
-		return 0;
-	}
-
-	pclk = t->parent->private;
-
-	if (!clk->prefix)
-		ret = asprintf(&clk->prefix, "%s%s%s", pclk->prefix,
-			       t->depth > 1 ? "   ": "", t->next ? "|" : " ");
-	if (ret < 0)
-		return -1;
-
-	unit = clock_rate(&rate);
-
-	printf("%s%s-- %s (flags:0x%x, usecount:%d, rate: %d %s)\n",
-	       clk->prefix,  !t->next ? "`" : "", t->name, clk->flags,
-	       clk->usecount, rate, unit);
-
-	return 0;
-}
-
-int dump_clock_info(void)
-{
-	return tree_for_each(clock_tree, dump_clock_cb, NULL);
-}
-
 void read_and_dump_clock_info(int verbose)
 {
 	(void)verbose;
diff --git a/tree.c b/tree.c
index 7466e31..0a9c119 100644
--- a/tree.c
+++ b/tree.c
@@ -224,6 +224,17 @@  int tree_for_each(struct tree *tree, tree_cb_t cb, void *data)
 	return tree_for_each(tree->next, cb, data);
 }
 
+int tree_for_each_parent(struct tree *tree, tree_cb_t cb, void *data)
+{
+	if (!tree)
+		return 0;
+
+	if (tree_for_each_parent(tree->parent, cb, data))
+		return -1;
+
+	return cb(tree, data);
+}
+
 struct tree *tree_find(struct tree *tree, const char *name)
 {
 	struct tree *t;
diff --git a/tree.h b/tree.h
index 7be3fc4..176dd23 100644
--- a/tree.h
+++ b/tree.h
@@ -45,3 +45,5 @@  extern struct tree *tree_load(const char *path, tree_filter_t filter);
 extern struct tree *tree_find(struct tree *tree, const char *name);
 
 extern int tree_for_each(struct tree *tree, tree_cb_t cb, void *data);
+
+extern int tree_for_each_parent(struct tree *tree, tree_cb_t cb, void *data);