Changeset 4538

Show
Ignore:
Timestamp:
07/03/07 10:18:21 (6 years ago)
Author:
khali
Message:

Use sscanf instead of regex to match the feature names. This is much faster.
As a side effect, this fixes a memory leak (the regex engine data was never
freed.)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/branches/lm-sensors-3.0.0/lib/access.c

    r4520 r4538  
    2121#include <string.h> 
    2222#include <math.h> 
    23 #include <regex.h> 
    2423#include "access.h" 
    2524#include "sensors.h" 
     
    2827#include "proc.h" 
    2928#include "general.h" 
    30  
    31 #define GET_TYPE_REGEX "\\([[:alpha:]]\\{1,\\}\\)[[:digit:]]\\{0,\\}\\(_\\([[:alpha:]]\\{1,\\}\\)\\)\\{0,1\\}" 
    3229 
    3330static int sensors_do_this_chip_sets(sensors_chip_name name); 
     
    542539 
    543540static struct feature_type_match matches[] = {  
    544         { "temp", SENSORS_FEATURE_TEMP, temp_matches }, 
    545         { "in", SENSORS_FEATURE_IN, in_matches }, 
    546         { "fan", SENSORS_FEATURE_FAN, fan_matches }, 
    547         { "cpu", SENSORS_FEATURE_UNKNOWN, cpu_matches }, 
    548         { "vrm", SENSORS_FEATURE_VRM, 0 }, 
     541        { "temp%d%c", SENSORS_FEATURE_TEMP, temp_matches }, 
     542        { "in%d%c", SENSORS_FEATURE_IN, in_matches }, 
     543        { "fan%d%c", SENSORS_FEATURE_FAN, fan_matches }, 
     544        { "cpu%d%c", SENSORS_FEATURE_UNKNOWN, cpu_matches }, 
    549545        { 0 } 
    550546}; 
     
    554550        const sensors_feature_data *feature) 
    555551{ 
    556         regmatch_t pmatch[4]; 
    557         int size_first, size_second, retval, i; 
     552        char c; 
     553        int i, nr, count; 
    558554        struct feature_type_match *submatches; 
    559         static regex_t reg; 
    560         static regex_t *preg = NULL; 
    561555         
    562         if (!preg) { 
    563                 regcomp(&reg, GET_TYPE_REGEX, 0); 
    564                 preg = &reg; 
    565         } 
    566          
    567         retval = regexec(preg, feature->name, 4, pmatch, 0); 
    568          
    569         if (retval == -1) 
    570                 return SENSORS_FEATURE_UNKNOWN; 
    571          
    572         size_first = pmatch[1].rm_eo - pmatch[1].rm_so; 
    573         size_second = pmatch[3].rm_eo - pmatch[3].rm_so; 
     556        /* vrm is special */ 
     557        if (!strcmp(feature->name, "vrm")) 
     558                return SENSORS_FEATURE_VRM; 
    574559         
    575560        for(i = 0; matches[i].name != 0; i++) 
    576                 if (!strncmp(feature->name, matches[i].name, size_first)) 
     561                if ((count = sscanf(feature->name, matches[i].name, &nr, &c))) 
    577562                        break; 
    578563         
    579564        if (matches[i].name == NULL) /* no match */ 
    580565                return SENSORS_FEATURE_UNKNOWN; 
    581         else if (size_second == 0) /* single type */ 
     566        else if (count == 1) /* single type */ 
    582567                return matches[i].type; 
    583         else if (matches[i].submatches == NULL) /* not single type, but no submatches */ 
     568 
     569        /* assert: count == 2 */ 
     570        if (c != '_') 
    584571                return SENSORS_FEATURE_UNKNOWN; 
    585572 
    586573        submatches = matches[i].submatches; 
    587574        for(i = 0; submatches[i].name != 0; i++) 
    588                 if (!strcmp(feature->name + pmatch[3].rm_so, submatches[i].name)) 
     575                if (!strcmp(strchr(feature->name, '_') + 1, submatches[i].name)) 
    589576                        return submatches[i].type; 
    590577