From 92b5982180762edc5b9dacc333fb49a1422a5fbe Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Thu, 28 Jun 2007 14:39:34 +0000 Subject: [PATCH] With the new dynamic chip features enumeration code, the sensors_proc_chips and sensors_chip_features_list arrays tend to track the same information. So it's much more simple, and more efficient, to have a single array. I decided to keep sensors_proc_chips, because it already has memory management helpers. One nice side effect is that the arbitrary limit to the number of supported chips is now gone. Another one is that some of the memory leaks were fixed. git-svn-id: http://lm-sensors.org/svn/lm-sensors/branches/lm-sensors-3.0.0@4510 7894878c-1315-0410-8ee3-d5d059ff63e0 --- lib/access.c | 20 ++++++++--------- lib/data.c | 9 +------- lib/data.h | 9 ++------ lib/init.c | 16 ++++++++++++-- lib/sysfs.c | 61 +++++++++++++++++++++------------------------------- 5 files changed, 51 insertions(+), 64 deletions(-) diff --git a/lib/access.c b/lib/access.c index 17ee7768..946c6062 100644 --- a/lib/access.c +++ b/lib/access.c @@ -105,9 +105,9 @@ const sensors_chip_feature *sensors_lookup_feature_nr(const sensors_chip_name *c int i, j; const sensors_chip_feature *features; - for (i = 0; sensors_chip_features_list[i].chip.prefix; i++) - if (sensors_match_chip(sensors_chip_features_list[i].chip, *chip)) { - features = sensors_chip_features_list[i].feature; + for (i = 0; i < sensors_proc_chips_count; i++) + if (sensors_match_chip(sensors_proc_chips[i].chip, *chip)) { + features = sensors_proc_chips[i].feature; for (j = 0; features[j].data.name; j++) if (features[j].data.number == feature) return features + j; @@ -124,9 +124,9 @@ const sensors_chip_feature *sensors_lookup_feature_name(const sensors_chip_name int i, j; const sensors_chip_feature *features; - for (i = 0; sensors_chip_features_list[i].chip.prefix; i++) - if (sensors_match_chip(sensors_chip_features_list[i].chip, *chip)) { - features = sensors_chip_features_list[i].feature; + for (i = 0; i < sensors_proc_chips_count; i++) + if (sensors_match_chip(sensors_proc_chips[i].chip, *chip)) { + features = sensors_proc_chips[i].feature; for (j = 0; features[j].data.name; j++) if (!strcasecmp(features[j].data.name, feature)) return features + j; @@ -304,7 +304,7 @@ const sensors_chip_name *sensors_get_detected_chips(int *nr) { const sensors_chip_name *res; res = (*nr >= sensors_proc_chips_count ? - NULL : &sensors_proc_chips[*nr]); + NULL : &sensors_proc_chips[*nr].chip); (*nr)++; return res; } @@ -332,9 +332,9 @@ const sensors_feature_data *sensors_get_all_features(sensors_chip_name name, sensors_chip_feature *feature_list; int i; - for (i = 0; sensors_chip_features_list[i].chip.prefix; i++) - if (sensors_match_chip(sensors_chip_features_list[i].chip, name)) { - feature_list = sensors_chip_features_list[i].feature; + for (i = 0; i < sensors_proc_chips_count; i++) + if (sensors_match_chip(sensors_proc_chips[i].chip, name)) { + feature_list = sensors_proc_chips[i].feature; if (!*nr1 && !*nr2) { /* Return the first entry */ if (!feature_list[0].data.name) /* The list may be empty */ return NULL; diff --git a/lib/data.c b/lib/data.c index b618f994..3f9b7013 100644 --- a/lib/data.c +++ b/lib/data.c @@ -36,7 +36,7 @@ sensors_bus *sensors_config_busses = NULL; int sensors_config_busses_count = 0; int sensors_config_busses_max = 0; -sensors_chip_name *sensors_proc_chips = NULL; +sensors_chip_features *sensors_proc_chips = NULL; int sensors_proc_chips_count = 0; int sensors_proc_chips_max = 0; @@ -44,13 +44,6 @@ sensors_bus *sensors_proc_bus = NULL; int sensors_proc_bus_count = 0; int sensors_proc_bus_max = 0; -#define PLACEHOLDER_ELEMENTS {{0}}, {{0}}, {{0}}, {{0}}, {{0}}, {{0}}, {{0}}, {{0}}, {{0}}, {{0}} - -sensors_chip_features sensors_chip_features_list[] = { - PLACEHOLDER_ELEMENTS, - { { 0 } } -}; - static int sensors_substitute_chip(sensors_chip_name *name,int lineno); /* Wow, this must be one of the ugliest functions I have ever written. diff --git a/lib/data.h b/lib/data.h index b6ad36fe..799feddc 100644 --- a/lib/data.h +++ b/lib/data.h @@ -159,13 +159,13 @@ extern sensors_bus *sensors_config_busses; extern int sensors_config_busses_count; extern int sensors_config_busses_max; -extern sensors_chip_name *sensors_proc_chips; +extern sensors_chip_features *sensors_proc_chips; extern int sensors_proc_chips_count; extern int sensors_proc_chips_max; #define sensors_add_proc_chips(el) sensors_add_array_el( \ (el), &sensors_proc_chips, &sensors_proc_chips_count,\ - &sensors_proc_chips_max, sizeof(struct sensors_chip_name)) + &sensors_proc_chips_max, sizeof(struct sensors_chip_features)) extern sensors_bus *sensors_proc_bus; extern int sensors_proc_bus_count; @@ -175,9 +175,4 @@ extern int sensors_proc_bus_max; (el), &sensors_proc_bus, &sensors_proc_bus_count,\ &sensors_proc_bus_max, sizeof(struct sensors_bus)) -extern sensors_chip_features sensors_chip_features_list[]; - -/* this should match the total number of elements in PLACEHOLDER_ELEMENTS */ -#define N_PLACEHOLDER_ELEMENTS 10 - #endif /* def LIB_SENSORS_DATA_H */ diff --git a/lib/init.c b/lib/init.c index dfc4cdf8..865232c4 100644 --- a/lib/init.c +++ b/lib/init.c @@ -28,6 +28,7 @@ #include "scanner.h" static void free_chip_name(sensors_chip_name name); +static void free_chip_features(sensors_chip_feature *features); static void free_bus(sensors_bus bus); static void free_chip(sensors_chip chip); static void free_label(sensors_label label); @@ -59,8 +60,10 @@ void sensors_cleanup(void) sensors_scanner_exit(); - for (i = 0; i < sensors_proc_chips_count; i++) - free_chip_name(sensors_proc_chips[i]); + for (i = 0; i < sensors_proc_chips_count; i++) { + free_chip_name(sensors_proc_chips[i].chip); + free_chip_features(sensors_proc_chips[i].feature); + } free(sensors_proc_chips); sensors_proc_chips = NULL; sensors_proc_chips_count = sensors_proc_chips_max = 0; @@ -90,6 +93,15 @@ void free_chip_name(sensors_chip_name name) free(name.busname); } +void free_chip_features(sensors_chip_feature *features) +{ + int i; + + for (i = 0; features[i].data.name; i++) + free(features[i].data.name); + free(features); +} + void free_bus(sensors_bus bus) { free(bus.adapter); diff --git a/lib/sysfs.c b/lib/sysfs.c index 1665ae4a..3ebe22b5 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -61,13 +61,12 @@ int get_type_scaling(int type) return 0; } -static -sensors_chip_features sensors_read_dynamic_chip(struct sysfs_device *sysdir) +static int sensors_read_dynamic_chip(sensors_chip_features *chip, + struct sysfs_device *sysdir) { int i, type, fnum = 1; struct sysfs_attribute *attr; struct dlist *attrs; - sensors_chip_features ret = {{0}, 0}; /* room for all 3 (in, fan, temp) types, with all their subfeatures + misc features. We use a large sparse table at first to store all found features, so that we can store them sorted at type and index @@ -81,7 +80,7 @@ sensors_chip_features sensors_read_dynamic_chip(struct sysfs_device *sysdir) attrs = sysfs_get_device_attributes(sysdir); if (attrs == NULL) - return ret; + return -ENOENT; memset(features, 0, sizeof(features)); @@ -90,7 +89,7 @@ sensors_chip_features sensors_read_dynamic_chip(struct sysfs_device *sysdir) name = attr->name; if (!strcmp(name, "name")) { - ret.chip.prefix = strndup(attr->value, strlen(attr->value) - 1); + /* Already done */ continue; } @@ -192,9 +191,9 @@ sensors_chip_features sensors_read_dynamic_chip(struct sysfs_device *sysdir) } } - ret.feature = dyn_features; + chip->feature = dyn_features; - return ret; + return 0; } /* returns !0 if sysfs filesystem was found, 0 otherwise */ @@ -215,11 +214,10 @@ int sensors_init_sysfs(void) /* returns: 0 if successful, !0 otherwise */ static int sensors_read_one_sysfs_chip(struct sysfs_device *dev) { - static int total_dynamic = 0; - int domain, bus, slot, fn, i; + int domain, bus, slot, fn; struct sysfs_attribute *attr, *bus_attr; char bus_path[SYSFS_PATH_MAX]; - sensors_chip_name name; + sensors_chip_features entry; /* ignore any device without name attribute */ if (!(attr = sysfs_get_device_attr(dev, "name"))) @@ -231,22 +229,22 @@ static int sensors_read_one_sysfs_chip(struct sysfs_device *dev) return 0; /* NB: attr->value[attr->len-1] == '\n'; chop that off */ - name.prefix = strndup(attr->value, attr->len - 1); - if (!name.prefix) + entry.chip.prefix = strndup(attr->value, attr->len - 1); + if (!entry.chip.prefix) sensors_fatal_error(__FUNCTION__, "out of memory"); - name.busname = strdup(dev->path); - if (!name.busname) + entry.chip.busname = strdup(dev->path); + if (!entry.chip.busname) sensors_fatal_error(__FUNCTION__, "out of memory"); - if (sscanf(dev->name, "%d-%x", &name.bus, &name.addr) == 2) { + if (sscanf(dev->name, "%d-%x", &entry.chip.bus, &entry.chip.addr) == 2) { /* find out if legacy ISA or not */ - if (name.bus == 9191) - name.bus = SENSORS_CHIP_NAME_BUS_ISA; + if (entry.chip.bus == 9191) + entry.chip.bus = SENSORS_CHIP_NAME_BUS_ISA; else { snprintf(bus_path, sizeof(bus_path), "%s/class/i2c-adapter/i2c-%d/device/name", - sensors_sysfs_mount, name.bus); + sensors_sysfs_mount, entry.chip.bus); if ((bus_attr = sysfs_open_attribute(bus_path))) { if (sysfs_read_attribute(bus_attr)) @@ -254,35 +252,24 @@ static int sensors_read_one_sysfs_chip(struct sysfs_device *dev) if (bus_attr->value && !strncmp(bus_attr->value, "ISA ", 4)) - name.bus = SENSORS_CHIP_NAME_BUS_ISA; + entry.chip.bus = SENSORS_CHIP_NAME_BUS_ISA; sysfs_close_attribute(bus_attr); } } - } else if (sscanf(dev->name, "%*[a-z0-9_].%d", &name.addr) == 1) { + } else if (sscanf(dev->name, "%*[a-z0-9_].%d", &entry.chip.addr) == 1) { /* must be new ISA (platform driver) */ - name.bus = SENSORS_CHIP_NAME_BUS_ISA; + entry.chip.bus = SENSORS_CHIP_NAME_BUS_ISA; } else if (sscanf(dev->name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn) == 4) { /* PCI */ - name.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn; - name.bus = SENSORS_CHIP_NAME_BUS_PCI; + entry.chip.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn; + entry.chip.bus = SENSORS_CHIP_NAME_BUS_PCI; } else return -SENSORS_ERR_PARSE; - if (total_dynamic < N_PLACEHOLDER_ELEMENTS) { - sensors_chip_features n_entry = sensors_read_dynamic_chip(dev); - n_entry.chip.addr = name.addr; - n_entry.chip.bus = name.bus; - - /* skip to end of list */ - for(i = 0; sensors_chip_features_list[i].chip.prefix; i++); - - sensors_chip_features_list[i] = n_entry; - - total_dynamic++; - } - - sensors_add_proc_chips(&name); + if (sensors_read_dynamic_chip(&entry, dev) < 0) + return -SENSORS_ERR_PARSE; + sensors_add_proc_chips(&entry); return 0; }