]> git.webhop.me Git - lcd4linux.git/commitdiff
[lcd4linux @ 2004-03-11 06:39:58 by reinelt]
authorreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Thu, 11 Mar 2004 06:39:59 +0000 (06:39 +0000)
committerreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Thu, 11 Mar 2004 06:39:59 +0000 (06:39 +0000)
big patch from Martin:
- reuse filehandles
- memory leaks fixed
- earlier busy-flag checking with HD44780
- reuse memory for strings in RESULT and hash
- netdev_fast to wavid time-consuming regex

git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@393 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

16 files changed:
cfg.c
drv_HD44780.c
evaluator.c
evaluator.h
hash.c
hash.h
lcd4linux.c
plugin_cfg.c
plugin_cpuinfo.c
plugin_loadavg.c
plugin_meminfo.c
plugin_netdev.c
plugin_proc_stat.c
widget_bar.c
widget_icon.c
widget_text.c

diff --git a/cfg.c b/cfg.c
index a39fbf4c26d2a626d71a0340a8dfa066ed82d66a..8d9388e58e30dc46da8dc2afb5127b97ee79f6ac 100644 (file)
--- a/cfg.c
+++ b/cfg.c
@@ -1,4 +1,4 @@
-/* $Id: cfg.c,v 1.38 2004/03/08 16:26:26 reinelt Exp $^
+/* $Id: cfg.c,v 1.39 2004/03/11 06:39:58 reinelt Exp $^
  *
  * config file stuff
  *
  *
  *
  * $Log: cfg.c,v $
+ * Revision 1.39  2004/03/11 06:39:58  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.38  2004/03/08 16:26:26  reinelt
  * re-introduced \nnn (octal) characters in strings
  * text widgets can have a 'update' speed of 0 which means 'never'
@@ -502,7 +510,7 @@ char *l4l_cfg_get (char *section, char *key, char *defval)
   char *expression;
   char *retval;
   void *tree = NULL;
-  RESULT result = {0, 0.0, NULL};
+  RESULT result = {0, 00, NULL};
   
   expression=cfg_lookup(section, key);
   
@@ -510,6 +518,7 @@ char *l4l_cfg_get (char *section, char *key, char *defval)
     if (*expression=='\0') return "";
     if (Compile(expression, &tree)==0 && Eval(tree, &result)==0) {
       retval=strdup(R2S(&result));
+      DelTree(tree);
       DelResult(&result);        
       return(retval);
     }
@@ -525,7 +534,7 @@ int l4l_cfg_number (char *section, char *key, int defval, int min, int max, int
 {
   char *expression;
   void *tree = NULL;
-  RESULT result = {0, 0.0, NULL};
+  RESULT result = {0, 00, NULL};
    
   // start with default value
   // in case of an (uncatched) error, you have the
@@ -537,8 +546,12 @@ int l4l_cfg_number (char *section, char *key, int defval, int min, int max, int
     return 0;
   }
   
-  if (Compile(expression, &tree) != 0) return -1;
+  if (Compile(expression, &tree) != 0) {
+    DelTree(tree);
+    return -1;
+  }
   if (Eval(tree, &result) != 0) {
+    DelTree(tree);
     DelResult(&result);
     return -1;
   }
index f30f0c7969be4ba04e75f0d2290a3a1da8e6ab18..3b1abecd97ac054bc7f06cbed72ee9d7580d7ce9 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: drv_HD44780.c,v 1.15 2004/03/03 03:47:04 reinelt Exp $
+/* $Id: drv_HD44780.c,v 1.16 2004/03/11 06:39:58 reinelt Exp $
  *
  * new style driver for HD44780-based displays
  *
  *
  *
  * $Log: drv_HD44780.c,v $
+ * Revision 1.16  2004/03/11 06:39:58  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.15  2004/03/03 03:47:04  reinelt
  * big patch from Martin Hejl:
  * - use qprintf() where appropriate
@@ -625,10 +633,6 @@ static int drv_HD_start (char *section)
     drv_HD_command (allControllers, 0x28, T_EXEC);         // 4 Bit mode, 1/16 duty cycle, 5x8 font
   }
 
-  drv_HD_command (allControllers, 0x08, T_EXEC);  // Display off, cursor off, blink off
-  drv_HD_command (allControllers, 0x0c, T_CLEAR); // Display on, cursor off, blink off, wait 1.64 ms
-  drv_HD_command (allControllers, 0x06, T_EXEC);  // curser moves to right, no shift
-  
   // maybe use busy-flag from now on 
   // (we can't use the busy flag during the init sequence)
   cfg_number(section, "UseBusy", 0, 0, 1, &UseBusy);
@@ -650,6 +654,10 @@ static int drv_HD_start (char *section)
   info("%s: %susing busy-flag checking", Name, UseBusy?"":"not ");
   free(strsize);
 
+  drv_HD_command (allControllers, 0x08, T_EXEC);  // Display off, cursor off, blink off
+  drv_HD_command (allControllers, 0x0c, T_CLEAR); // Display on, cursor off, blink off, wait 1.64 ms
+  drv_HD_command (allControllers, 0x06, T_EXEC);  // curser moves to right, no shift
+
   drv_HD_command (allControllers, 0x01, T_CLEAR); // clear *both* displays
   drv_HD_command (allControllers, 0x03, T_CLEAR); // return home
   
index 214de3e12da1bdd6310bdc1fad0c4bac51f1023c..502dc2de34f19c986798ef39fa235d2c1c07894c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: evaluator.c,v 1.17 2004/03/08 18:45:52 hejl Exp $
+/* $Id: evaluator.c,v 1.18 2004/03/11 06:39:58 reinelt Exp $
  *
  * expression evaluation
  *
  *
  *
  * $Log: evaluator.c,v $
+ * Revision 1.18  2004/03/11 06:39:58  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.17  2004/03/08 18:45:52  hejl
  * fixed segfault when using string concatenation
  *
 #include <dmalloc.h>
 #endif
 
+// string buffer chunk size
+#define CHUNK_SIZE 16
 
 typedef enum {
   T_NAME, 
@@ -266,18 +276,19 @@ static int      nFunction = 0;
 
 void DelResult (RESULT *result)
 {
-  result->type=0;
-  result->number=0.0;
+  result->type   = 0;
+  result->number = 0.0;
+  result->length = -1;
   if (result->string) {
     free (result->string);
-    result->string=NULL;
+    result->string = NULL;
   }
 }
 
 
 static void FreeResult (RESULT *result)
 {
-  if (result!=NULL) {
+  if (result != NULL) {
     DelResult(result);
     free (result);
   }
@@ -287,13 +298,14 @@ static void FreeResult (RESULT *result)
 static RESULT* NewResult (void)
 {
   RESULT *result = malloc(sizeof(RESULT));
-  if (result==NULL) {
+  if (result == NULL) {
     error ("Evaluator: cannot allocate result: out of memory!");
     return NULL;
   }
-  result->type=0;
-  result->number=0.0;
-  result->string=NULL;
+  result->type   = 0;
+  result->number = 0.0;
+  result->length = -1;
+  result->string = NULL;
   
   return result;
 }
@@ -303,15 +315,19 @@ static RESULT* DupResult (RESULT *result)
 {
   RESULT *result2;
   
-  if ((result2=NewResult())==NULL) 
-    return NULL;
+  if ((result2 = NewResult()) == NULL) return NULL;
   
-  result2->type=result->type;
-  result2->number=result->number;
-  if (result->string!=NULL)
-    result2->string=strdup(result->string);
-  else    
-    result2->string=NULL;
+  result2->type   = result->type;
+  result2->number = result->number;
+
+  if (result->length >= 0) {
+    result2->length = result->length;
+    result2->string = malloc(result2->length);
+    strcpy(result2->string, result->string);
+  } else {
+    result2->length = -1;
+    result2->string = NULL;
+  }
   
   return result2;
 }
@@ -319,21 +335,31 @@ static RESULT* DupResult (RESULT *result)
 
 RESULT* SetResult (RESULT **result, int type, void *value)
 {
-  if (*result) {
+  if (*result == NULL) {
+    if ((*result = NewResult()) == NULL)  return NULL;
+  } else if (type == R_NUMBER) {
     DelResult(*result);
-  } else {
-    if ((*result = NewResult()) == NULL) 
-      return NULL;
   }
   
   if (type == R_NUMBER) {
     (*result)->type   = R_NUMBER;
     (*result)->number = *(double*)value;
+    (*result)->length = -1;
     (*result)->string = NULL;
-  } else if (type == R_STRING) {
+  } 
+  else if (type == R_STRING) {
+    int len = strlen((char*)value);
     (*result)->type   = R_STRING;
     (*result)->number = 0.0;
-    (*result)->string = strdup(value);
+    if (len > (*result)->length) {
+      // buffer is either empty or too small
+      if ((*result)->string) free((*result)->string);
+      // allocate memory in multiples of CHUNK_SIZE
+      // note that length does not count the trailing \0
+      (*result)->length = CHUNK_SIZE*(len/CHUNK_SIZE+1)-1;
+      (*result)->string = malloc((*result)->length+1);
+    }
+    strcpy((*result)->string, value);
   } else {
     error ("Evaluator: internal error: invalid result type %d", type); 
     return NULL;
@@ -345,7 +371,7 @@ RESULT* SetResult (RESULT **result, int type, void *value)
 
 double R2N (RESULT *result)
 {
-  if (result==NULL) {
+  if (result == NULL) {
     error ("Evaluator: internal error: NULL result");
     return 0.0;
   }
@@ -356,7 +382,7 @@ double R2N (RESULT *result)
   
   if (result->type & R_STRING) {
     result->type |= R_NUMBER;
-    result->number=atof(result->string);
+    result->number = atof(result->string);
     return result->number;
   }
   
@@ -367,8 +393,6 @@ double R2N (RESULT *result)
 
 char* R2S (RESULT *result)
 {
-  char buffer[16];
-  
   if (result==NULL) {
     error ("Evaluator: internal error: NULL result");
     return NULL;
@@ -379,10 +403,11 @@ char* R2S (RESULT *result)
   }
   
   if (result->type & R_NUMBER) {
-    sprintf(buffer, "%g", result->number);
     result->type |= R_STRING;
     if (result->string) free(result->string);
-    result->string=strdup(buffer);
+    result->length = CHUNK_SIZE-1;
+    result->string = malloc(CHUNK_SIZE);
+    snprintf(result->string, CHUNK_SIZE, "%g", result->number);
     return result->string;
   }
   
@@ -426,25 +451,23 @@ int SetVariable (char *name, RESULT *value)
 
 int SetVariableNumeric (char *name, double value)
 {
-  RESULT result;
+  RESULT result = {0, 0, 0, NULL};
+  RESULT *rp = &result;
   
-  result.type   = R_NUMBER;
-  result.number = value;
-  result.string = NULL;
-  
-  return SetVariable (name, &result);
+  SetResult (&rp, R_NUMBER, &value); 
+
+  return SetVariable (name, rp);
 }
 
 
 int SetVariableString (char *name, char *value)
 {
-  RESULT result;
-  
-  result.type   =R_STRING;
-  result.number = 0.0;
-  result.string = strdup(value);
+  RESULT result = {0, 0, 0, NULL};
+  RESULT *rp = &result;
   
-  return SetVariable (name, &result);
+  SetResult(&rp, R_STRING, value );
+
+  return SetVariable (name, rp);
 }
 
 
@@ -1184,20 +1207,27 @@ int Eval (void *tree, RESULT *result)
   NODE *Tree = (NODE*)tree;
 
   DelResult (result);
-
+  
   if (Tree==NULL) {
     SetResult (&result, R_STRING, "");
     return 0;
   }
-
+  
   ret = EvalTree(Tree);
-
+  
   result->type   = Tree->Result->type;
   result->number = Tree->Result->number;
-  if (Tree->Result->string != NULL) {
-    result->string = strdup(Tree->Result->string);
+  result->length = Tree->Result->length;
+  if (result->length >= 0) {
+    result->string = malloc(result->length);
+    if (Tree->Result->string != NULL)
+      strcpy(result->string, Tree->Result->string);
+    else
+      result->string[0]='\0';
+  } else {
+    result->string = NULL;
   }
-
+  
   return ret;
 }
 
@@ -1206,13 +1236,13 @@ void DelTree (void *tree)
 {
   int i;
   NODE *Tree = (NODE*)tree;
-  if (Tree==NULL) return;
+  
+  if (Tree == NULL) return;
 
   for (i=0; i<Tree->Children; i++) {
     DelTree (Tree->Child[i]);
   }
  
-  FreeResult (Tree->Result);
-  
-  free (Tree);
+  if (Tree->Result) FreeResult (Tree->Result);
+  free(Tree);
 }
index 58bf4ca345927f546a2d6d6d60d10c29f48b1a38..12b52bad70e0ce4e8a31657a1d192385ed47b300 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: evaluator.h,v 1.5 2004/03/06 20:31:16 reinelt Exp $
+/* $Id: evaluator.h,v 1.6 2004/03/11 06:39:59 reinelt Exp $
  *
  * expression evaluation
  *
  *
  *
  * $Log: evaluator.h,v $
+ * Revision 1.6  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.5  2004/03/06 20:31:16  reinelt
  * Complete rewrite of the evaluator to get rid of the code
  * from mark Morley (because of license issues).
@@ -63,6 +71,7 @@
 typedef struct {
   int type;
   double number;
+  int    length;
   char  *string;
 } RESULT;
 
diff --git a/hash.c b/hash.c
index d71971b20d092053af3f6062788be2e21bb5b5b6..b73313aa561d4787ce75c67fc0bb92b08c38a670 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -1,4 +1,4 @@
-/* $Id: hash.c,v 1.16 2004/03/03 08:40:07 hejl Exp $
+/* $Id: hash.c,v 1.17 2004/03/11 06:39:59 reinelt Exp $
  *
  * hashes (associative arrays)
  *
  *
  *
  * $Log: hash.c,v $
+ * Revision 1.17  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.16  2004/03/03 08:40:07  hejl
  * Fixed memory leak in hash_get_regex
  *
 #include <dmalloc.h>
 #endif
 
-
+// number of slots for delta processing
 #define DELTA_SLOTS 64
 
+// string buffer chunk size
+#define CHUNK_SIZE 16
+
 
 // bsearch compare function for hash entries
 static int hash_lookup_f (const void *a, const void *b)
@@ -181,32 +192,44 @@ static HASH_ITEM* hash_lookup (HASH *Hash, char *key, int sortit)
 static HASH_ITEM* hash_set_string (HASH *Hash, char *key, char *val)
 {
   HASH_ITEM *Item;
+  int len = strlen(val);
   
-  Item=hash_lookup (Hash, key, 0);
+  Item = hash_lookup (Hash, key, 0);
 
   // entry already exists?
-  if (Item!=NULL) {
-    if (Item->val) free (Item->val);
-    Item->val = strdup(val);
+  if (Item != NULL) {
+    if (len > Item->len) {
+      // buffer is either empty or too small
+      if (Item->val) free (Item->val);
+      // allocate memory in multiples of CHUNK_SIZE
+      // note that length does not count the trailing \0
+      Item->len = CHUNK_SIZE*(len/CHUNK_SIZE+1)-1;
+      Item->val = malloc(Item->len+1);
+    }
+    strcpy(Item->val, val);
     goto hash_got_string;
   }
 
   // add entry
-  Hash->sorted=0;
+  Hash->sorted = 0;
   Hash->nItems++;
-  Hash->Items=realloc(Hash->Items,Hash->nItems*sizeof(HASH_ITEM));
+  Hash->Items = realloc(Hash->Items, Hash->nItems*sizeof(HASH_ITEM));
 
-  Item=&(Hash->Items[Hash->nItems-1]);
+  Item = &(Hash->Items[Hash->nItems-1]);
   
   Item->key  = strdup(key);
-  Item->val  = strdup(val);
+
+  Item->len  = CHUNK_SIZE*(len/CHUNK_SIZE+1)-1;
+  Item->val  = malloc(Item->len+1);
+  strcpy(Item->val, val);
+  
   Item->root = 0;
   Item->Slot = NULL;
 
  hash_got_string:
   // set timestamps
   gettimeofday(&Hash->time, NULL);
-  Item->time=Hash->time;
+  Item->time = Hash->time;
 
   return Item;
 }
@@ -236,12 +259,7 @@ void hash_set_delta (HASH *Hash, char *key, char *val)
     memset(Item->Slot, 0, DELTA_SLOTS*sizeof(HASH_SLOT));
   }
   
-  // shift delta table
-  // <--- don't move 63 bytes just to insert one byte - this is _very_
-  // slow on my Elan SC520 or GEODE SC1100
-  //memmove (Item->Slot+1, Item->Slot, (DELTA_SLOTS-1)*sizeof(HASH_SLOT));
-
-  // move the pointer to the next free slot (wrapping around if necessary
+  // move the pointer to the next free slot, wrap around if necessary
   if (--Item->root < 0) Item->root = DELTA_SLOTS-1;
 
   // set first entry
diff --git a/hash.h b/hash.h
index a06ec6a1da96f694fc530ab9949b811b2fbe6549..b7bdbc8d219f013663406019b0704717e2c9cc10 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -1,4 +1,4 @@
-/* $Id: hash.h,v 1.10 2004/03/03 04:44:16 reinelt Exp $
+/* $Id: hash.h,v 1.11 2004/03/11 06:39:59 reinelt Exp $
  *
  * hashes (associative arrays)
  *
  *
  *
  * $Log: hash.h,v $
+ * Revision 1.11  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.10  2004/03/03 04:44:16  reinelt
  * changes (cosmetics?) to the big patch from Martin
  * hash patch un-applied
@@ -85,6 +93,7 @@ typedef struct {
 typedef struct {
   char      *key;
   char      *val;
+  int       len; 
   timeval   time;
   int       root;
   HASH_SLOT *Slot;
index ab71fdbda4dc98f5ab6675681c3686b3bb1ba94f..cd48c991ef50bd05c36eeefa0c0a9a45152d94ac 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: lcd4linux.c,v 1.67 2004/03/06 20:31:16 reinelt Exp $
+/* $Id: lcd4linux.c,v 1.68 2004/03/11 06:39:59 reinelt Exp $
  *
  * LCD4Linux
  *
  *
  *
  * $Log: lcd4linux.c,v $
+ * Revision 1.68  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.67  2004/03/06 20:31:16  reinelt
  * Complete rewrite of the evaluator to get rid of the code
  * from mark Morley (because of license issues).
@@ -576,7 +584,7 @@ int main (int argc, char *argv[])
   if (interactive) {
     char line[1024];
     void *tree;
-    RESULT result = {0, 0.0, NULL};
+    RESULT result = {0, 00, NULL};
     
     printf("\neval> ");
     for(fgets(line, 1024, stdin); !feof(stdin); fgets(line, 1024, stdin)) {
@@ -590,7 +598,8 @@ int main (int argc, char *argv[])
            printf ("'%s'\n", R2S(&result));
          }
          DelResult (&result);
-       }
+        }
+       DelTree(tree);
       }
       printf("eval> ");
     }
index 5a853fb7f4b7a394742566e239fc9f5e9662de84..e2abcd7873342f5c5399d9ff74437f8de0177d8e 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: plugin_cfg.c,v 1.7 2004/03/06 20:31:16 reinelt Exp $
+/* $Id: plugin_cfg.c,v 1.8 2004/03/11 06:39:59 reinelt Exp $
  *
  * plugin for config file access
  *
  *
  *
  * $Log: plugin_cfg.c,v $
+ * Revision 1.8  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.7  2004/03/06 20:31:16  reinelt
  * Complete rewrite of the evaluator to get rid of the code
  * from mark Morley (because of license issues).
@@ -89,7 +97,7 @@ static void load_variables (void)
   char *list, *l, *p;
   char *expression;
   void *tree;
-  RESULT result = {0, 0.0, NULL};
+  RESULT result = {0, 00, NULL};
   
   list=cfg_list(section);
   l=list;
index 1bec66b53ef8e38f541d949c34d543b790819c6e..2fcb98db761679ca88c876a74389a2cb6ec2b39c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: plugin_cpuinfo.c,v 1.8 2004/03/03 03:47:04 reinelt Exp $
+/* $Id: plugin_cpuinfo.c,v 1.9 2004/03/11 06:39:59 reinelt Exp $
  *
  * plugin for /proc/cpuinfo parsing
  *
  *
  *
  * $Log: plugin_cpuinfo.c,v $
+ * Revision 1.9  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.8  2004/03/03 03:47:04  reinelt
  * big patch from Martin Hejl:
  * - use qprintf() where appropriate
 
 
 static HASH CPUinfo = { 0, };
-
+static FILE *stream = NULL;
 
 static int parse_cpuinfo (void)
 {
   int age;
-  FILE *stream;
   
   // reread every second only
   age=hash_age(&CPUinfo, NULL, NULL);
   if (age>0 && age<=1000) return 0;
   
-    
-  stream=fopen("/proc/cpuinfo", "r");
-  if (stream==NULL) {
+  if (stream == NULL) stream=fopen("/proc/cpuinfo", "r");
+  if (stream == NULL) {
     error ("fopen(/proc/cpuinfo) failed: %s", strerror(errno));
     return -1;
   }
-    
+  rewind(stream);
   while (!feof(stream)) {
     char buffer[256];
     char *c, *key, *val;
@@ -123,7 +129,6 @@ static int parse_cpuinfo (void)
     hash_set (&CPUinfo, key, val);
       
   }
-  fclose (stream);
   return 0;
 }
   
@@ -153,5 +158,9 @@ int plugin_init_cpuinfo (void)
 
 void plugin_exit_cpuinfo(void) 
 {
-       hash_destroy(&CPUinfo);
+  if (stream != NULL) {
+    fclose (stream);
+    stream = NULL;
+  }
+  hash_destroy(&CPUinfo);
 }
index 2c633108eb803c327c25ee92ae87c70797462412..4c868b0f188334e2cfbf260a37e907d989b07cae 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: plugin_loadavg.c,v 1.5 2004/03/08 18:46:21 hejl Exp $
+/* $Id: plugin_loadavg.c,v 1.6 2004/03/11 06:39:59 reinelt Exp $
  *
  * plugin for load average
  *
  *
  *
  * $Log: plugin_loadavg.c,v $
+ * Revision 1.6  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.5  2004/03/08 18:46:21  hejl
  * Fixed bug introduced with "caching" the loadavg values
  *
 #include "plugin.h"
 
 #ifndef HAVE_GETLOADAVG
+static int fd = -2;
 
 int getloadavg (double loadavg[], int nelem)
 {
-  static int fd=-2;
   char buf[65], *p;
   ssize_t nread;
   int i;
       
-  if (fd==-2) fd = open ("/proc/loadavg", O_RDONLY);
+  if (fd == -2) fd = open ("/proc/loadavg", O_RDONLY);
   if (fd < 0) return -1;
 
   lseek(fd,0,SEEK_SET);
   nread = read (fd, buf, sizeof buf - 1);
-  //close (fd);
   
   if (nread < 0) return -1;
   buf[nread - 1] = '\0';
@@ -149,7 +156,6 @@ static void my_loadavg (RESULT *result, RESULT *arg1)
   
 }
 
-
 int plugin_init_loadavg (void)
 {
   AddFunction ("loadavg", 1, my_loadavg);
@@ -158,4 +164,8 @@ int plugin_init_loadavg (void)
 
 void plugin_exit_loadavg(void) 
 {
+#ifndef HAVE_GETLOADAVG   
+  if (fd>0) close(fd);
+  fd=-2;
+#endif
 }
index fcc73a29e72099d62d9463d8f9b9c880ee5a8a6f..d9203de274c6a6ca67043014e13d2d625e0a71e3 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: plugin_meminfo.c,v 1.6 2004/03/03 03:47:04 reinelt Exp $
+/* $Id: plugin_meminfo.c,v 1.7 2004/03/11 06:39:59 reinelt Exp $
  *
  * plugin for /proc/meminfo parsing
  *
  *
  *
  * $Log: plugin_meminfo.c,v $
+ * Revision 1.7  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.6  2004/03/03 03:47:04  reinelt
  * big patch from Martin Hejl:
  * - use qprintf() where appropriate
 
 
 static HASH MemInfo = { 0, };
-
+static FILE *stream = NULL;
 
 static int parse_meminfo (void)
 {
   int age;
-  FILE *stream;
   
   // reread every 10 msec only
   age=hash_age(&MemInfo, NULL, NULL);
   if (age>0 && age<=10) return 0;
   
-  stream=fopen("/proc/meminfo", "r");
+  if (stream==NULL) stream=fopen("/proc/meminfo", "r");
   if (stream==NULL) {
     error ("fopen(/proc/meminfo) failed: %s", strerror(errno));
     return -1;
   }
-    
+  
+  rewind(stream);
   while (!feof(stream)) {
     char buffer[256];
     char *c, *key, *val;
@@ -116,9 +124,8 @@ static int parse_meminfo (void)
       hash_set (&MemInfo, key, val);
     }
   }
-  fclose (stream);
   return 0;
-}  
+} 
 
 static void my_meminfo (RESULT *result, RESULT *arg1)
 {
@@ -146,5 +153,9 @@ int plugin_init_meminfo (void)
 
 void plugin_exit_meminfo(void) 
 {
-       hash_destroy(&MemInfo);
+  if (stream != NULL) {
+    fclose(stream);
+    stream=NULL;
+  }
+  hash_destroy(&MemInfo);
 }
index 1668498291f83b212dd040b09005df4576185dfa..bba623a317d4a128c84a787bced35fd4175ffa56 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: plugin_netdev.c,v 1.6 2004/03/03 04:44:16 reinelt Exp $
+/* $Id: plugin_netdev.c,v 1.7 2004/03/11 06:39:59 reinelt Exp $
  *
  * plugin for /proc/net/dev parsing
  *
  *
  *
  * $Log: plugin_netdev.c,v $
+ * Revision 1.7  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.6  2004/03/03 04:44:16  reinelt
  * changes (cosmetics?) to the big patch from Martin
  * hash patch un-applied
@@ -72,7 +80,7 @@
 
 
 static HASH NetDev = { 0, };
-
+static FILE *stream = NULL;
 
 static void hash_set3 (char *key1, char *key2, char *key3, char *val) 
 {
@@ -88,19 +96,20 @@ static int parse_netdev (void)
   int age;
   int line;
   int  RxTx=0; // position of Receive/Transmit switch
-  FILE *stream;
   
   // reread every 10 msec only
   age=hash_age(&NetDev, NULL, NULL);
   if (age>0 && age<=10) return 0;
   
-  stream=fopen("/proc/net/dev", "r");
+  if (stream==NULL) stream=fopen("/proc/net/dev", "r");
   if (stream==NULL) {
     error ("fopen(/proc/net/dev) failed: %s", strerror(errno));
     return -1;
   }
-    
+
+  rewind(stream);    
   line=0;
+  
   while (!feof(stream)) {
     char buffer[256];
     char header[256], *h, *t;
@@ -142,7 +151,7 @@ static int parse_netdev (void)
       }
     }
   }
-  fclose (stream);
+  
   return 0;
 }  
 
@@ -165,14 +174,38 @@ static void my_netdev (RESULT *result, RESULT *arg1, RESULT *arg2)
   SetResult(&result, R_NUMBER, &value); 
 }
 
+static void my_netdev_fast(RESULT *result, RESULT *arg1, RESULT *arg2)
+{
+  char *key;
+  int delay;
+  double value;
+  
+  if (parse_netdev()<0) {
+    SetResult(&result, R_STRING, ""); 
+    return;
+  }
+
+  key   = R2S(arg1);
+  delay = R2N(arg2);
+  
+  value  = hash_get_delta(&NetDev, key, delay);
+  
+  SetResult(&result, R_NUMBER, &value); 
+}
+
 
 int plugin_init_netdev (void)
 {
   AddFunction ("netdev", 2, my_netdev);
+  AddFunction ("netdev_fast", 2, my_netdev_fast);
   return 0;
 }
 
 void plugin_exit_netdev(void) 
 {
-       hash_destroy(&NetDev);
+  if(stream!=NULL) {
+    fclose (stream);
+    stream=NULL;
+  }
+  hash_destroy(&NetDev);
 }
index 72de48e9017c27b2c72e664741f73f5e1726c33c..03cdfb06ad27e146594e3fa90881f46b7f004223 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: plugin_proc_stat.c,v 1.17 2004/03/03 04:44:16 reinelt Exp $
+/* $Id: plugin_proc_stat.c,v 1.18 2004/03/11 06:39:59 reinelt Exp $
  *
  * plugin for /proc/stat parsing
  *
  *
  *
  * $Log: plugin_proc_stat.c,v $
+ * Revision 1.18  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.17  2004/03/03 04:44:16  reinelt
  * changes (cosmetics?) to the big patch from Martin
  * hash patch un-applied
 
 
 static HASH Stat = { 0, };
+static FILE *stream = NULL;
+
 
 static void hash_set1 (char *key1, char *val) 
 {
@@ -140,18 +150,19 @@ static void hash_set3 (char *key1, char *key2, char *key3, char *val)
 static int parse_proc_stat (void)
 {
   int age;
-  FILE *stream;
   
   // reread every 10 msec only
   age=hash_age(&Stat, NULL, NULL);
   if (age>0 && age<=10) return 0;
   
-  stream=fopen("/proc/stat", "r");
+  if (stream==NULL) stream=fopen("/proc/stat", "r");
   if (stream==NULL) {
     error ("fopen(/proc/stat) failed: %s", strerror(errno));
     return -1;
   }
   
+  rewind(stream);
+  
   while (!feof(stream)) {
     char buffer[1024];
     if (fgets (buffer, sizeof(buffer), stream) == NULL) break;
@@ -175,7 +186,6 @@ static int parse_proc_stat (void)
        beg=end?end+1:NULL;
       }
     } 
-    
     else if (strncmp(buffer, "page ", 5)==0) {
       char *key[]  = { "in", "out" }; 
       char delim[] = " \t\n";
@@ -220,8 +230,6 @@ static int parse_proc_stat (void)
        beg=end?end+1:NULL;
       }
     } 
-    
-
     else if (strncmp(buffer, "disk_io:", 8)==0) {
       char *key[]  = { "io", "rio", "rblk", "wio", "wblk" }; 
       char delim[] = " ():,\t\n";
@@ -256,7 +264,6 @@ static int parse_proc_stat (void)
       hash_set1 (buffer, beg);
     } 
   }
-  fclose (stream);
   return 0;
 }
 
@@ -357,5 +364,9 @@ int plugin_init_proc_stat (void)
 
 void plugin_exit_proc_stat(void) 
 {
+  if (stream!=NULL) {
+    fclose (stream);
+    stream=NULL;
+  }
   hash_destroy(&Stat);
 }
index a3d443586c11a03510b5c2344ac9383204f41d2c..60d2de9a731e740dc4cf5525f9003adb5307d818 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: widget_bar.c,v 1.9 2004/03/06 20:31:16 reinelt Exp $
+/* $Id: widget_bar.c,v 1.10 2004/03/11 06:39:59 reinelt Exp $
  *
  * bar widget handling
  *
  *
  *
  * $Log: widget_bar.c,v $
+ * Revision 1.10  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.9  2004/03/06 20:31:16  reinelt
  * Complete rewrite of the evaluator to get rid of the code
  * from mark Morley (because of license issues).
@@ -93,7 +101,7 @@ void widget_bar_update (void *Self)
 {
   WIDGET      *W = (WIDGET*)Self;
   WIDGET_BAR *Bar = W->data;
-  RESULT result = {0, 0.0, NULL};
+  RESULT result = {0, 00, NULL};
 
   double val1, val2;
   double min, max;
@@ -226,12 +234,21 @@ int widget_bar_init (WIDGET *Self)
 }
 
 
-int widget_bar_quit (WIDGET *Self) {
+int widget_bar_quit (WIDGET *Self) 
+{
   if (Self ) {
-         if (Self->data) free(Self->data);
-      Self->data=NULL;
+    if (Self->data) {
+      WIDGET_BAR *Bar = Self->data;    
+      DelTree(Bar->tree1);
+      DelTree(Bar->tree2);
+      DelTree(Bar->tree_min);
+      DelTree(Bar->tree_max);
+      free(Self->data);
+    }
+    Self->data=NULL;
   }
   
+  
   return 0;
   
 }
index 421ca848e609ab554c616139fe4c11c3d0ff3d75..95edb8fb84aa934d32ebc3ff8dce877e821bfe53 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: widget_icon.c,v 1.10 2004/03/06 20:31:16 reinelt Exp $
+/* $Id: widget_icon.c,v 1.11 2004/03/11 06:39:59 reinelt Exp $
  *
  * icon widget handling
  *
  *
  *
  * $Log: widget_icon.c,v $
+ * Revision 1.11  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.10  2004/03/06 20:31:16  reinelt
  * Complete rewrite of the evaluator to get rid of the code
  * from mark Morley (because of license issues).
@@ -137,7 +145,7 @@ void widget_icon_update (void *Self)
 {
   WIDGET      *W = (WIDGET*)Self;
   WIDGET_ICON *Icon = W->data;
-  RESULT result = {0, 0.0, NULL};
+  RESULT result = {0, 00, NULL};
   
   // evaluate expressions
   Icon->speed = 100;
@@ -229,6 +237,8 @@ int widget_icon_quit (WIDGET *Self)
   if (Self) {
     if (Self->data) {
       WIDGET_ICON *Icon = Self->data;
+      DelTree(Icon->speed_tree);
+      DelTree(Icon->visible_tree);
       if (Icon->bitmap) free (Icon->bitmap); 
       free(Self->data);
       Self->data=NULL;
index 3c8d2dc2abf787e8d72fd7ce6d1122e8b40e561c..5840a346c6ddf3525d64f536a3c24ecddd7aa346 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: widget_text.c,v 1.16 2004/03/08 16:26:26 reinelt Exp $
+/* $Id: widget_text.c,v 1.17 2004/03/11 06:39:59 reinelt Exp $
  *
  * simple text widget handling
  *
  *
  *
  * $Log: widget_text.c,v $
+ * Revision 1.17  2004/03/11 06:39:59  reinelt
+ * big patch from Martin:
+ * - reuse filehandles
+ * - memory leaks fixed
+ * - earlier busy-flag checking with HD44780
+ * - reuse memory for strings in RESULT and hash
+ * - netdev_fast to wavid time-consuming regex
+ *
  * Revision 1.16  2004/03/08 16:26:26  reinelt
  * re-introduced \nnn (octal) characters in strings
  * text widgets can have a 'update' speed of 0 which means 'never'
@@ -217,7 +225,7 @@ void widget_text_update (void *Self)
 {
   WIDGET      *W = (WIDGET*)Self;
   WIDGET_TEXT *T = W->data;
-  RESULT result = {0, 0.0, NULL};
+  RESULT result = {0, 00, NULL};
   char *preval, *postval, *value;
   int update;
   
@@ -411,13 +419,17 @@ int widget_text_quit (WIDGET *Self) {
   if (Self) {
     Text=Self->data;
     if (Self->data) {    
-      if (Text->preval)      free(Text->preval);
-      if (Text->postval)     free(Text->postval);
-      if (Text->value)       free(Text->value);
-      if (Text->buffer)      free(Text->buffer);
+      DelTree(Text->pretree);
+      DelTree(Text->posttree);
+      DelTree(Text->tree);     
+      if (Text->preval)  free(Text->preval);
+      if (Text->postval) free(Text->postval);
+      if (Text->value)   free(Text->value);
+      if (Text->buffer)  free(Text->buffer);
       free (Self->data);
       Self->data=NULL;
     }
+  
   }
   return 0;