diff options
author | reinelt <> | 2004-03-11 06:39:59 +0000 |
---|---|---|
committer | reinelt <> | 2004-03-11 06:39:59 +0000 |
commit | b241d736825d0469b7716847ee4ef5de269d2423 (patch) | |
tree | e3f51842280e06133de00726fa987765d4464f10 | |
parent | 888c37c1b1aa181c76253af8425b2bbbac4bbc4a (diff) | |
download | lcd4linux-b241d736825d0469b7716847ee4ef5de269d2423.tar.gz |
[lcd4linux @ 2004-03-11 06:39:58 by 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
Diffstat (limited to '')
-rw-r--r-- | cfg.c | 21 | ||||
-rw-r--r-- | drv_HD44780.c | 18 | ||||
-rw-r--r-- | evaluator.c | 132 | ||||
-rw-r--r-- | evaluator.h | 11 | ||||
-rw-r--r-- | hash.c | 52 | ||||
-rw-r--r-- | hash.h | 11 | ||||
-rw-r--r-- | lcd4linux.c | 15 | ||||
-rw-r--r-- | plugin_cfg.c | 12 | ||||
-rw-r--r-- | plugin_cpuinfo.c | 27 | ||||
-rw-r--r-- | plugin_loadavg.c | 20 | ||||
-rw-r--r-- | plugin_meminfo.c | 27 | ||||
-rw-r--r-- | plugin_netdev.c | 47 | ||||
-rw-r--r-- | plugin_proc_stat.c | 25 | ||||
-rw-r--r-- | widget_bar.c | 27 | ||||
-rw-r--r-- | widget_icon.c | 14 | ||||
-rw-r--r-- | widget_text.c | 24 |
16 files changed, 350 insertions, 133 deletions
@@ -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 * @@ -23,6 +23,14 @@ * * * $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, 0, 0, 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, 0, 0, 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; } diff --git a/drv_HD44780.c b/drv_HD44780.c index f30f0c7..3b1abec 100644 --- a/drv_HD44780.c +++ b/drv_HD44780.c @@ -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 * @@ -29,6 +29,14 @@ * * * $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 diff --git a/evaluator.c b/evaluator.c index 214de3e..502dc2d 100644 --- a/evaluator.c +++ b/evaluator.c @@ -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 * @@ -23,6 +23,14 @@ * * * $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 * @@ -156,6 +164,8 @@ #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); } diff --git a/evaluator.h b/evaluator.h index 58bf4ca..12b52ba 100644 --- a/evaluator.h +++ b/evaluator.h @@ -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 * @@ -23,6 +23,14 @@ * * * $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; @@ -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) * @@ -23,6 +23,14 @@ * * * $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 * @@ -112,9 +120,12 @@ #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 @@ -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) * @@ -23,6 +23,14 @@ * * * $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; diff --git a/lcd4linux.c b/lcd4linux.c index ab71fdb..cd48c99 100644 --- a/lcd4linux.c +++ b/lcd4linux.c @@ -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 * @@ -22,6 +22,14 @@ * * * $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, 0, 0, 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> "); } diff --git a/plugin_cfg.c b/plugin_cfg.c index 5a853fb..e2abcd7 100644 --- a/plugin_cfg.c +++ b/plugin_cfg.c @@ -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 * @@ -23,6 +23,14 @@ * * * $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, 0, 0, NULL}; list=cfg_list(section); l=list; diff --git a/plugin_cpuinfo.c b/plugin_cpuinfo.c index 1bec66b..2fcb98d 100644 --- a/plugin_cpuinfo.c +++ b/plugin_cpuinfo.c @@ -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 * @@ -23,6 +23,14 @@ * * * $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 @@ -84,24 +92,22 @@ 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); } diff --git a/plugin_loadavg.c b/plugin_loadavg.c index 2c63310..4c868b0 100644 --- a/plugin_loadavg.c +++ b/plugin_loadavg.c @@ -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 * @@ -23,6 +23,14 @@ * * * $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 * @@ -76,20 +84,19 @@ #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 } diff --git a/plugin_meminfo.c b/plugin_meminfo.c index fcc73a2..d9203de 100644 --- a/plugin_meminfo.c +++ b/plugin_meminfo.c @@ -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 * @@ -23,6 +23,14 @@ * * * $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 @@ -75,23 +83,23 @@ 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); } diff --git a/plugin_netdev.c b/plugin_netdev.c index 1668498..bba623a 100644 --- a/plugin_netdev.c +++ b/plugin_netdev.c @@ -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 * @@ -23,6 +23,14 @@ * * * $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); } diff --git a/plugin_proc_stat.c b/plugin_proc_stat.c index 72de48e..03cdfb0 100644 --- a/plugin_proc_stat.c +++ b/plugin_proc_stat.c @@ -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 * @@ -23,6 +23,14 @@ * * * $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 @@ -112,6 +120,8 @@ 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); } diff --git a/widget_bar.c b/widget_bar.c index a3d4435..60d2de9 100644 --- a/widget_bar.c +++ b/widget_bar.c @@ -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 * @@ -21,6 +21,14 @@ * * * $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, 0, 0, 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; } diff --git a/widget_icon.c b/widget_icon.c index 421ca84..95edb8f 100644 --- a/widget_icon.c +++ b/widget_icon.c @@ -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 * @@ -21,6 +21,14 @@ * * * $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, 0, 0, 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; diff --git a/widget_text.c b/widget_text.c index 3c8d2dc..5840a34 100644 --- a/widget_text.c +++ b/widget_text.c @@ -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 * @@ -21,6 +21,14 @@ * * * $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, 0, 0, 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; |