orcutils: refactor /proc/cpuinfo reading code from orccpu-arm.c
authorJames Cowgill <jcowgill@debian.org>
Fri, 6 Apr 2018 14:24:47 +0000 (15:24 +0100)
committerTim-Philipp Müller <tim@centricular.com>
Sun, 22 Apr 2018 19:22:40 +0000 (20:22 +0100)
This code will later be used by mips target, so it makes sense
to use a common function instead of duplicating the code.

https://bugzilla.gnome.org/show_bug.cgi?id=760834

orc/orccpu-arm.c
orc/orcutils.c
orc/orcutils.h

index fd01c3a..5431e0d 100644 (file)
@@ -98,55 +98,6 @@ orc_check_neon_proc_auxv (void)
 }
 #endif
 
-static char *
-get_proc_cpuinfo (void)
-{
-  char *cpuinfo;
-  int fd;
-  int n;
-
-  cpuinfo = malloc(4096);
-  if (cpuinfo == NULL) return NULL;
-
-  fd = open("/proc/cpuinfo", O_RDONLY);
-  if (fd < 0) {
-    free (cpuinfo);
-    return NULL;
-  }
-
-  n = read(fd, cpuinfo, 4095);
-  if (n < 0) {
-    free (cpuinfo);
-    close (fd);
-    return NULL;
-  }
-  cpuinfo[n] = 0;
-
-  close (fd);
-
-  return cpuinfo;
-}
-
-static char *
-get_cpuinfo_line (char *cpuinfo, const char *tag)
-{
-  char *flags;
-  char *end;
-  char *colon;
-
-  flags = strstr(cpuinfo,tag);
-  if (flags == NULL) return NULL;
-
-  end = strchr(flags, '\n');
-  if (end == NULL) return NULL;
-  colon = strchr (flags, ':');
-  if (colon == NULL) return NULL;
-  colon++;
-  if(colon >= end) return NULL;
-
-  return _strndup (colon, end-colon);
-}
-
 static unsigned long
 orc_cpu_arm_getflags_cpuinfo ()
 {
@@ -162,7 +113,7 @@ orc_cpu_arm_getflags_cpuinfo ()
     return 0;
   }
 
-  cpuinfo_line = get_cpuinfo_line(cpuinfo, "CPU architecture");
+  cpuinfo_line = get_tag_value(cpuinfo, "CPU architecture");
   if (cpuinfo_line) {
     int arm_arch = strtoul (cpuinfo_line, NULL, 0);
     if (arm_arch >= 8L) {
@@ -175,7 +126,7 @@ orc_cpu_arm_getflags_cpuinfo ()
     free(cpuinfo_line);
   }
 
-  cpuinfo_line = get_cpuinfo_line(cpuinfo, "Features");
+  cpuinfo_line = get_tag_value(cpuinfo, "Features");
   if (cpuinfo_line == NULL) {
     free (cpuinfo);
     return 0;
index a37129d..5b58c60 100644 (file)
@@ -31,6 +31,9 @@
 #include <orc/orcdebug.h>
 #include <orc/orcutils.h>
 
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
  * @short_description: Orc utility functions
  */
 
+#if defined(__arm__) || defined(__mips__)
+char *
+get_proc_cpuinfo (void)
+{
+  char *cpuinfo;
+  int fd;
+  int n;
+
+  cpuinfo = malloc(4096);
+  if (cpuinfo == NULL) return NULL;
+
+  fd = open("/proc/cpuinfo", O_RDONLY);
+  if (fd < 0) {
+    free (cpuinfo);
+    return NULL;
+  }
+
+  n = read(fd, cpuinfo, 4095);
+  if (n < 0) {
+    free (cpuinfo);
+    close (fd);
+    return NULL;
+  }
+  cpuinfo[n] = 0;
+
+  close (fd);
+
+  return cpuinfo;
+}
+#endif
+
 char *
 _strndup (const char *s, int n)
 {
index 2225a12..177cd90 100644 (file)
@@ -214,6 +214,10 @@ ORC_BEGIN_DECLS
 
 #ifdef ORC_ENABLE_UNSTABLE_API
 
+#if defined(__arm__) || defined(__mips__)
+char * get_proc_cpuinfo (void);
+#endif
+
 char * _strndup (const char *s, int n);
 char ** strsplit (const char *s, char delimiter);
 char * get_tag_value (char *s, const char *tag);