changed call interfaces for get_freq/get_freq_dynamic, eliminating mallocs. bug...
authorPhilip Kovacs <pkovacs@users.sourceforge.net>
Sun, 13 Nov 2005 03:33:26 +0000 (03:33 +0000)
committerPhilip Kovacs <pkovacs@users.sourceforge.net>
Sun, 13 Nov 2005 03:33:26 +0000 (03:33 +0000)
git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@410 7f574dfc-610e-0410-a909-a81674777703

ChangeLog
src/conky.c
src/conky.h
src/freebsd.c
src/linux.c

index 73ff180..9d740ca 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,9 +3,9 @@
 2005-11-12
        * Replaced bitwise copy of tcp_connection_t with function
        copy_tcp_connection().
-       * Changed call interfaces for get_acpi_fan() /
-       get_acpi_ac_adapter() to eliminate need to malloc.  
-       More functions need same correction.  See bug 1355470.
+       * Changed call interfaces for get_acpi_fan(), get_acpi_ac_adapter(),   
+       get_freq(), get_freq_dynamic(), eliminating mallocs.  pkovacs.
+       More to come.  See bug 1355470.
 
 2005-11-11
        * moved hash sizing code into portmon lib, where it belongs
index da229d2..1a248b4 100644 (file)
@@ -2041,19 +2041,16 @@ static void generate_text()
                                                                        i)+ 40) * 9.0 / 5 - 40));
                        }
                        OBJ(freq) {
-                               snprintf(p, n, "%.0f", get_freq());
+                               get_freq(p, n, "%.0f", 1); /* pk */
                        }
                        OBJ(freq_g) {
-                               float ghz = (float)(get_freq()/1000);
-                               //printf("%f\n", ghz);
-                               snprintf(p, n, "%'.2f", ghz);
+                               get_freq(p, n, "%'.2f", 1000); /* pk */
                        }
                        OBJ(freq_dyn) {
-                               snprintf(p, n, "%.0f", get_freq_dynamic());
+                               get_freq_dynamic(p, n, "%.0f", 1 ); /* pk */
                        }
                        OBJ(freq_dyn_g) {
-                               float ghz = (float)(get_freq_dynamic()/1000);
-                               snprintf(p, n, "%'.2f", ghz);
+                               get_freq_dynamic(p, n, "%'.2f", 1000); /* pk */
                        }
                        OBJ(adt746xcpu) {
                                snprintf(p, n, "%s", get_adt746x_cpu());
@@ -2062,12 +2059,9 @@ static void generate_text()
                                snprintf(p, n, "%s", get_adt746x_fan());
                        }
                        OBJ(acpifan) {
-                               /*snprintf(p, n, "%s", get_acpi_fan()); */
                                get_acpi_fan(p, n);  /* pk */
                        }
                        OBJ(acpiacadapter) {
-                               /* snprintf(p, n, "%s",
-                                        get_acpi_ac_adapter()); */
                                get_acpi_ac_adapter(p, n);
                        }
                        OBJ(battery) {
index d59c23b..04ab308 100644 (file)
@@ -319,8 +319,8 @@ void update_cpu_usage(void);
 void update_total_processes(void);
 void update_running_processes(void);
 void update_i8k(void);
-float get_freq();
-float get_freq_dynamic();
+void get_freq( char *, size_t, char *, int ); /* pk */
+void get_freq_dynamic( char *, size_t, char *, int ); /* pk */
 void update_load_average();
 int open_i2c_sensor(const char *dev, const char *type, int n, int *div,
                    char *devtype);
index 056f206..c1b0429 100644 (file)
@@ -372,11 +372,9 @@ int open_acpi_temperature(const char *name)
        return 0;
 }
 
-/*char *get_acpi_ac_adapter(void)*/
 void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
 {
        int state;
-       /*char *acstate = (char *) malloc(100);*/
 
        if ( !p_client_buffer !! client_buffer_size <= 0 )
                return;
@@ -384,23 +382,18 @@ void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
        if (GETSYSCTL("hw.acpi.acline", state)) {
                (void) fprintf(stderr,
                               "Cannot read sysctl \"hw.acpi.acline\"\n");
-               /*return "n\\a";*/
                return;
        }
 
 
        if (state)
-               /*strcpy(acstate, "Running on AC Power");*/
                strncpy( p_client_buffer, client_buffer_size, "Running on AC Power" );
        else
-               /*strcpy(acstate, "Running on battery");*/
                strncpy( p_client_buffer, client_buffer_size, "Running on battery" );
 
-       /*return ac_state;*/
        return;
 }
 
-/*char *get_acpi_fan()*/
 void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
 {
        if ( !p_client_buffer !! client_buffer_size <= 0 )
@@ -432,7 +425,8 @@ __inline__ unsigned long long int rdtsc()
 }
 #endif
 
-float get_freq_dynamic()
+/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
+void get_freq_dynamic( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor )
 {
 #if  defined(__i386) || defined(__x86_64)
         struct timezone tz;
@@ -454,20 +448,32 @@ float get_freq_dynamic()
         microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
             (tvstop.tv_usec - tvstart.tv_usec);
                              
-        return (cycles[1] - cycles[0]) / microseconds;
+       snprintf( p_client_buffer, client_buffer_size, p_format, (float)((cycles[1] - cycles[0]) / microseconds) / divisor );
+        return;
 #else
-        return get_freq();
+       get_freq( p_client_buffer, client_buffer_size, p_format, divisor );
+        return;
 #endif
 }
 
-float get_freq()
+/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
+void get_freq( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor )
 {
        int freq;
+
+       if ( !p_client_buffer || client_buffer_size <= 0 || !p_format || divisor <= 0 )
+              return;
        
        if (GETSYSCTL("dev.cpu.0.freq", freq) == 0)
-               return (float)freq;
+       {
+               snprintf( p_client_buffer, client_buffer_size, p_format, freq/divisor );
+       }
        else
-               return (float)0;
+       {
+               snprintf( p_client_buffer, client_buffer_size, p_format, (float)0 );
+       }
+
+       return;
 }
 
 void update_top()
index 619ed55..25e88d3 100644 (file)
@@ -128,10 +128,6 @@ void update_meminfo()
 
        info.bufmem = info.cached + info.buffers;
 
-       /*if (no_buffers) {
-               info.mem -= info.bufmem;
-       }*/
-
        info.mask |= (1 << INFO_MEM) | (1 << INFO_BUFFERS);
 }
 
@@ -749,19 +745,20 @@ __inline__ unsigned long long int rdtsc()
        __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
        return x;
 }
-static char *buffer = NULL;
 #endif
 
-float get_freq_dynamic()
+/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
+void get_freq_dynamic( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor )
 {
 #if  defined(__i386) || defined(__x86_64)
-       if (buffer == NULL)
-               buffer = malloc(64);
        struct timezone tz;
        struct timeval tvstart, tvstop;
        unsigned long long cycles[2];   /* gotta be 64 bit */
        unsigned int microseconds;      /* total time taken */
 
+       if ( !p_client_buffer || client_buffer_size <= 0 || !p_format || divisor <= 0 )
+            return;
+
        memset(&tz, 0, sizeof(tz));
 
        /* get this function in cached memory */
@@ -776,55 +773,60 @@ float get_freq_dynamic()
        microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
            (tvstop.tv_usec - tvstart.tv_usec);
 
-       return (cycles[1] - cycles[0]) / microseconds;
+       snprintf( p_client_buffer, client_buffer_size, p_format, (float)((cycles[1] - cycles[0]) / microseconds) / divisor );
+       return;
 #else
-       return get_freq();
+       get_freq( p_client_buffer, client_buffer_size, p_format, divisor );
+       return;
 #endif
 }
 
 #define CPUFREQ_CURRENT "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
 
-static char *frequency;
-       
-float get_freq()
+/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
+void get_freq( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor )
 {
        FILE *f;
-       char s[1000];
-       if (frequency == NULL) {
-               frequency = (char *) malloc(100);
-               assert(frequency != NULL);
-       }
+       char frequency[32];
+       char s[256];
+       double freq = 0;
+
+       if ( !p_client_buffer || client_buffer_size <= 0 || !p_format || divisor <= 0 )
+               return;
+
        f = fopen(CPUFREQ_CURRENT, "r");
        if (f) {
-               /* if there's a cpufreq /sys node, read the current
-                * frequency there from this node; divice by 1000 to
-                * get MHz
-                */
-               double freq = 0;
-               if (fgets(s, 1000,f)) {
+               /* if there's a cpufreq /sys node, read the current frequency from this node;
+                * divide by 1000 to get Mhz. */
+               if (fgets(s, sizeof(s), f)) {
                        s[strlen(s)-1] = '\0';
                        freq = strtod(s, NULL);
                }
                fclose(f);
-               return (freq/1000);
+               snprintf( p_client_buffer, client_buffer_size, p_format, (freq/1000)/divisor );
+               return;
        }
        
-       f = fopen("/proc/cpuinfo", "r");        //open the CPU information file
+       f = fopen("/proc/cpuinfo", "r");                //open the CPU information file
        if (!f)
-           return 0;
-       while (fgets(s, 1000, f) != NULL){      //read the file
+           return;
+
+       while (fgets(s, sizeof(s), f) != NULL){         //read the file
 #if defined(__i386) || defined(__x86_64)
-               if (strncmp(s, "cpu MHz", 5) == 0) {    //and search for the cpu mhz
+               if (strncmp(s, "cpu MHz", 7) == 0) {    //and search for the cpu mhz
 #else
                if (strncmp(s, "clock", 5) == 0) {      // this is different on ppc for some reason
 #endif
                strcpy(frequency, strchr(s, ':') + 2);  //copy just the number
-               frequency[strlen(frequency) - 1] = '\0';        // strip \n
+               frequency[strlen(frequency) - 1] = '\0'; // strip \n
+               freq = strtod(frequency, NULL);
                break;
                }
        }
-               fclose(f);
-               return strtod(frequency, (char **)NULL);
+       
+       fclose(f);
+       snprintf( p_client_buffer, client_buffer_size, p_format, (float)freq/divisor );
+       return;
 }