[Xfce4-commits] <xfce4-cpugraph-plugin:master> Merge identical code
Florian
noreply at xfce.org
Sat Apr 17 16:16:08 CEST 2010
Updating branch refs/heads/master
to fc2d847918fcdd03016a09283888335e013c3f5e (commit)
from 6f90e71a8a3f472cfa20cfe07b2fb1d475fd00d3 (commit)
commit fc2d847918fcdd03016a09283888335e013c3f5e
Author: Florian Rivoal <frivoal at gmail.com>
Date: Mon Feb 8 10:23:59 2010 +0900
Merge identical code
Part of the cpu detection code was indentical on all OSes. Put it in a
shared function, rather than copy and paste.
panel-plugin/os.c | 114 +++++++++++++++--------------------------------------
1 files changed, 32 insertions(+), 82 deletions(-)
diff --git a/panel-plugin/os.c b/panel-plugin/os.c
index fefd5a3..4b539a6 100644
--- a/panel-plugin/os.c
+++ b/panel-plugin/os.c
@@ -16,6 +16,8 @@
CpuData *cpudata = NULL;
int nrCpus = 0;
+static int DetectCPUNumber();
+
void cpuData_free()
{
g_free( cpudata );
@@ -23,28 +25,18 @@ void cpuData_free()
nrCpus = 0;
}
-#if defined (__linux__)
int cpuData_init()
{
- FILE *fstat = NULL;
- char cpuStr[PROCMAXLNLEN];
int i, cpuNr = -1;
+
/* Check if previously initalized */
if( cpudata != NULL )
return -2;
- /* Open proc stat file */
- if( !(fstat = fopen( PROC_STAT, "r" )) )
+ cpuNr = DetectCPUNumber();
+ if( cpuNr < 1 )
return -1;
- /* Read each cpu line at time */
- do
- {
- if( !fgets( cpuStr, PROCMAXLNLEN, fstat ) )
- return cpuNr;
- cpuNr++;
- }
- while( strncmp( cpuStr, "cpu", 3 ) == 0 );
/* Alloc storage for cpu data stuff */
cpudata = (CpuData *) g_malloc0( cpuNr * sizeof( CpuData ) );
@@ -56,10 +48,30 @@ int cpuData_init()
cpudata[i].scalMaxFreq = -1;
}
- fclose( fstat );
return nrCpus = cpuNr;
}
+#if defined (__linux__)
+static int DetectCPUNumber()
+{
+ int cpuNr= -1;
+ FILE *fstat = NULL;
+ char cpuStr[PROCMAXLNLEN];
+ /* Open proc stat file */
+ if( !(fstat = fopen( PROC_STAT, "r" )) )
+ return -1;
+
+ /* Read each cpu line at time */
+ do
+ {
+ if( !fgets( cpuStr, PROCMAXLNLEN, fstat ) )
+ return cpuNr;
+ cpuNr++;
+ } while( strncmp( cpuStr, "cpu", 3 ) == 0 );
+ fclose( fstat );
+ return cpuNr;
+}
+
CpuData *cpuData_read()
{
FILE *fStat = NULL;
@@ -106,29 +118,9 @@ CpuData *cpuData_read()
}
#elif defined (__FreeBSD__)
-void cpuData_init()
+static int DetectCPUNumber()
{
- int i, cpuNr = -1;
-
- /* Check if previously initalized */
- if( cpudata != NULL )
- return -2;
-
- cpuNr = 1;
-
- /* Alloc storage for cpu data stuff */
- cpudata = (CpuData *) g_malloc0( cpuNr * sizeof( CpuData ) );
-
- /* init frequency */
- for( i=cpuNr-1; i>=0; i-- )
- {
- cpudata[i].scalCurFreq = 0;
- cpudata[i].scalMinFreq = 0;
- cpudata[i].scalMaxFreq = -1;
- }
-
- fclose( fstat );
- return nrCpus = cpuNr;
+ return 1;
}
CpuData *cpuData_read()
@@ -166,32 +158,11 @@ CpuData *cpuData_read()
}
#elif defined (__NetBSD__)
-void cpuData_init()
+static int DetectCPUNumber()
{
- int i, cpuNr = -1;
-
- /* Check if previously initalized */
- if( cpudata != NULL )
- return -2;
-
- cpuNr = 1;
-
- /* Alloc storage for cpu data stuff */
- cpudata = (CpuData *) g_malloc0( cpuNr * sizeof( CpuData ) );
-
- /* init frequency */
- for( i=cpuNr-1; i>=0; i-- )
- {
- cpudata[i].scalCurFreq = 0;
- cpudata[i].scalMinFreq = 0;
- cpudata[i].scalMaxFreq = -1;
- }
-
- fclose( fstat );
- return nrCpus = cpuNr;
+ return 1;
}
-
CpuData *cpuData_read()
{
long user, nice, sys, bsdidle, idle;
@@ -227,32 +198,11 @@ CpuData *cpuData_read()
}
#elif defined (__OpenBSD_)
-void cpuData_init()
+static int DetectCPUNumber()
{
- int i, cpuNr = -1;
-
- /* Check if previously initalized */
- if( cpudata != NULL )
- return -2;
-
- cpuNr = 1;
-
- /* Alloc storage for cpu data stuff */
- cpudata = (CpuData *) g_malloc0( cpuNr, sizeof( CpuData ) );
-
- /* init frequency */
- for( i=cpuNr-1; i>=0; i-- )
- {
- cpudata[i].scalCurFreq = 0;
- cpudata[i].scalMinFreq = 0;
- cpudata[i].scalMaxFreq = -1;
- }
-
- fclose( fstat );
- return nrCpus = cpuNr;
+ return 1;
}
-
CpuData *cpuData_read()
{
unsigned long user, nice, sys, bsdidle, idle;
More information about the Xfce4-commits
mailing list