implemented qsort() in top.c instead of using my lame algorithm
authorBrenden Matthews <brenden1@rty.ca>
Thu, 25 Aug 2005 01:13:01 +0000 (01:13 +0000)
committerBrenden Matthews <brenden1@rty.ca>
Thu, 25 Aug 2005 01:13:01 +0000 (01:13 +0000)
git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@176 7f574dfc-610e-0410-a909-a81674777703

src/conky.c
src/linux.c
src/top.c

index 31b457a..b778e14 100644 (file)
@@ -2423,10 +2423,7 @@ static void generate_text()
                                    && obj->data.top.num < 10) {
                                        // if we limit the buffer and add a bunch of space after, it stops the thing from
                                        // moving other shit around, which is really fucking annoying
-                                       snprintf(p, 17,
-                                                "%s                              ",
-                                                cur->cpu[obj->data.top.
-                                                         num]->name);
+                                       snprintf(p, 17, "%s                              ", cur->cpu[obj->data.top.num]->name);
                                } else if (obj->data.top.type == TOP_CPU
                                           && obj->data.top.num >= 0
                                           && obj->data.top.num < 10) {
index f25e8e9..3bf9f3a 100644 (file)
@@ -687,9 +687,7 @@ float get_freq()
        microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
            (tvstop.tv_usec - tvstart.tv_usec);
 
-       sprintf(buffer, "%lld", (cycles[1] - cycles[0]) / microseconds);
-
-       return strtod(buffer, (char **)NULL);
+       return (cycles[1] - cycles[0]) / microseconds;
 #else
        FILE *f;
        char s[1000];
index 511e3b5..25fd24d 100644 (file)
--- a/src/top.c
+++ b/src/top.c
@@ -375,6 +375,28 @@ inline static void calc_cpu_each(int total)
 static struct process **sorttmp;
 static size_t sorttmp_size = 10;
 
+int comparecpu(const void * a, const void * b)
+{
+       if ((*(struct process **)a)->amount > (*(struct process **)b)->amount) {
+               return -1;
+       }
+       if ((*(struct process **)a)->amount < (*(struct process **)b)->amount) {
+               return 1;
+       }
+       return 0;
+}
+
+int comparemem(const void * a, const void * b)
+{
+       if ((*(struct process **)a)->totalmem > (*(struct process **)b)->totalmem) {
+               return -1;
+       }
+       if ((*(struct process **)a)->totalmem < (*(struct process **)b)->totalmem) {
+               return 1;
+       }
+       return 0;
+}
+
 inline void process_find_top(struct process **cpu, struct process **mem)
 {
        struct process *pr;
@@ -383,7 +405,7 @@ inline void process_find_top(struct process **cpu, struct process **mem)
                assert(sorttmp != NULL);
        }
        int total;
-       unsigned int i, max;
+       unsigned int i/*, max*/;
 
        total = calc_cpu_total();       /* calculate the total of the processor */
 
@@ -425,34 +447,9 @@ inline void process_find_top(struct process **cpu, struct process **mem)
                            realloc(sorttmp,
                                    sizeof(struct process) * sorttmp_size);
                }
-               max = i;
-               for (i = 0; i < max - 1; i++) {
-                       while (sorttmp[i + 1]->amount > sorttmp[i]->amount) {
-                               pr = sorttmp[i];
-                               sorttmp[i] = sorttmp[i + 1];
-                               sorttmp[i + 1] = pr;
-                               if (i > 0)
-                                       i--;
-                               else
-                                       break;
-                       }
-
-               }
-               for (i = max; i > 1; i--);
-               {
-                       while (sorttmp[i]->amount > sorttmp[i - 1]->amount) {
-                               pr = sorttmp[i];
-                               sorttmp[i] = sorttmp[i - 1];
-                               sorttmp[i - 1] = pr;
-                               if (i < max)
-                                       i++;
-                               else
-                                       break;
-                       }
-               }
+               qsort(sorttmp, i, sizeof(struct process *), comparecpu);
                for (i = 0; i < 10; i++) {
                        cpu[i] = sorttmp[i];
-
                }
        }
        if (top_mem) {
@@ -479,33 +476,7 @@ inline void process_find_top(struct process **cpu, struct process **mem)
                            realloc(sorttmp,
                                    sizeof(struct process) * sorttmp_size);
                }
-               max = i;
-               for (i = 0; i < max - 1; i++) {
-                       while (sorttmp[i + 1]->totalmem >
-                              sorttmp[i]->totalmem) {
-                               pr = sorttmp[i];
-                               sorttmp[i] = sorttmp[i + 1];
-                               sorttmp[i + 1] = pr;
-                               if (i > 0)
-                                       i--;
-                               else
-                                       break;
-                       }
-
-               }
-               for (i = max; i > 1; i--);
-               {
-                       while (sorttmp[i]->totalmem >
-                              sorttmp[i - 1]->totalmem) {
-                               pr = sorttmp[i];
-                               sorttmp[i] = sorttmp[i - 1];
-                               sorttmp[i - 1] = pr;
-                               if (i < max)
-                                       i++;
-                               else
-                                       break;
-                       }
-               }
+               qsort(sorttmp, i, sizeof(struct process *), comparemem);
                for (i = 0; i < 10; i++) {
                        mem[i] = sorttmp[i];