[08/28] provide a clock allocator

Message ID 1308256197-29155-8-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     |   43 ++++++++++++++++++++++++++++++++-----------
 powerdebug.h |    2 +-
 2 files changed, 33 insertions(+), 12 deletions(-)

Patch

diff --git a/clocks.c b/clocks.c
index 677db01..d08c926 100644
--- a/clocks.c
+++ b/clocks.c
@@ -451,33 +451,48 @@  static struct clock_info *read_clock_info_recur(char *clkpath, int level,
 	return cur;
 }
 
-void read_clock_info(char *clkpath)
+static struct clock_info *clock_alloc(const char *name)
+{
+	struct clock_info *ci;
+
+	ci = malloc(sizeof(*ci));
+	if (ci) {
+		memset(ci, 0, sizeof(*ci));
+		strcpy(ci->name, name);
+	}
+
+	return ci;
+}
+
+int read_clock_info(char *clkpath)
 {
 	DIR *dir;
 	struct dirent *item;
-	char filename[NAME_MAX], clockname[NAME_MAX];
+	char filename[NAME_MAX];
 	struct clock_info *child;
 	struct clock_info *cur;
+	int ret = -1;
 
 	dir = opendir(clkpath);
 	if (!dir)
-		return;
+		return -1;
 
-	clocks_info = (struct clock_info *)malloc(sizeof(struct clock_info));
-	memset(clocks_info, 0, sizeof(clocks_info));
-	strcpy(clocks_info->name, "/");
-	clocks_info->level = 0;
+	clocks_info = clock_alloc("/");
+	if (!clocks_info)
+		return -1;
 
 	while ((item = readdir(dir))) {
+
 		/* skip hidden dirs except ".." */
 		if (item->d_name[0] == '.')
 			continue;
 
-		strcpy(clockname, item->d_name);
 		sprintf(filename, "%s/%s", clkpath, item->d_name);
-		cur = (struct clock_info *)malloc(sizeof(struct clock_info));
-		memset(cur, 0, sizeof(struct clock_info));
-		strcpy(cur->name, clockname);
+
+		cur = clock_alloc(item->d_name);
+		if (!cur)
+			goto out;
+
 		cur->parent = clocks_info;
 		cur->num_children = 0;
 		cur->expanded = 0;
@@ -485,7 +500,13 @@  void read_clock_info(char *clkpath)
 		insert_children(&clocks_info, cur);
 		child = read_clock_info_recur(filename, 2, cur);
 	}
+
+	ret = 0;
+
+out:
 	closedir(dir);
+
+	return ret;
 }
 
 void read_and_dump_clock_info_one(char *clk, bool dump)
diff --git a/powerdebug.h b/powerdebug.h
index 1018998..0d8e8d1 100644
--- a/powerdebug.h
+++ b/powerdebug.h
@@ -30,7 +30,7 @@  enum {CLOCK_SELECTED = 1, REFRESH_WINDOW};
 
 extern void read_and_dump_clock_info(int verbose);
 extern void read_and_dump_clock_info_one(char *clk, bool dump);
-extern void read_clock_info(char *clkpath);
+extern int read_clock_info(char *clkpath);
 extern void find_parents_for_clock(char *clkname, int complete);
 extern int  read_and_print_clock_info(int verbose, int hrow, int selected);
 extern void print_clock_info(int verbose, int hrow, int selected);