diff options
| author | pgoyette <pgoyette@NetBSD.org> | 2010-12-15 17:17:16 +0000 |
|---|---|---|
| committer | pgoyette <pgoyette@NetBSD.org> | 2010-12-15 17:17:16 +0000 |
| commit | 123e95b68ac2ee87ecc566d733d467b2ec857e41 (patch) | |
| tree | 2f00d979836d00764245aac1ca64967a68064c07 /sys/dev | |
| parent | 7be8ce5ea6456d293338295a62d7bb7948094002 (diff) | |
Extract searching of description tables into a single function, rather
than duplicating the code every time. Minor reduction in code size
(about 1200 bytes on amd64), no change in functionality.
Diffstat (limited to 'sys/dev')
| -rw-r--r-- | sys/dev/sysmon/sysmon_envsys.c | 125 | ||||
| -rw-r--r-- | sys/dev/sysmon/sysmon_envsys_events.c | 21 | ||||
| -rw-r--r-- | sys/dev/sysmon/sysmon_envsys_tables.c | 45 | ||||
| -rw-r--r-- | sys/dev/sysmon/sysmon_envsysvar.h | 8 |
4 files changed, 82 insertions, 117 deletions
diff --git a/sys/dev/sysmon/sysmon_envsys.c b/sys/dev/sysmon/sysmon_envsys.c index 869823e245e..859f1d7b41f 100644 --- a/sys/dev/sysmon/sysmon_envsys.c +++ b/sys/dev/sysmon/sysmon_envsys.c @@ -1,4 +1,4 @@ -/* $NetBSD: sysmon_envsys.c,v 1.109 2010/12/11 15:17:15 pgoyette Exp $ */ +/* $NetBSD: sysmon_envsys.c,v 1.110 2010/12/15 17:17:16 pgoyette Exp $ */ /*- * Copyright (c) 2007, 2008 Juan Romero Pardines. @@ -64,7 +64,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.109 2010/12/11 15:17:15 pgoyette Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.110 2010/12/15 17:17:16 pgoyette Exp $"); #include <sys/param.h> #include <sys/types.h> @@ -520,21 +520,16 @@ sysmon_envsys_destroy(struct sysmon_envsys *sme) int sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata) { - const struct sme_description_table *sdt_units; + const struct sme_descr_entry *sdt_units; envsys_data_t *oedata; - int i; KASSERT(sme != NULL || edata != NULL); /* * Find the correct units for this sensor. */ - sdt_units = sme_get_description_table(SME_DESC_UNITS); - for (i = 0; sdt_units[i].type != -1; i++) - if (sdt_units[i].type == edata->units) - break; - - if (strcmp(sdt_units[i].desc, "unknown") == 0) + sdt_units = sme_find_table_entry(SME_DESC_UNITS, edata->units); + if (sdt_units->type == -1) return EINVAL; /* @@ -567,7 +562,7 @@ sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata) DPRINTF(("%s: attached #%d (%s), units=%d (%s)\n", __func__, edata->sensor, edata->desc, - sdt_units[i].type, sdt_units[i].desc)); + sdt_units->type, sdt_units->desc)); return 0; } @@ -1054,9 +1049,9 @@ sme_remove_userprops(void) envsys_data_t *edata = NULL; char tmp[ENVSYS_DESCLEN]; sysmon_envsys_lim_t lims; - const struct sme_description_table *sdt_units; + const struct sme_descr_entry *sdt_units; uint32_t props; - int ptype, i; + int ptype; mutex_enter(&sme_global_mtx); LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { @@ -1166,15 +1161,12 @@ sme_remove_userprops(void) /* * Find the correct units for this sensor. */ - sdt_units = - sme_get_description_table(SME_DESC_UNITS); - for (i = 0; sdt_units[i].type != -1; i++) - if (sdt_units[i].type == edata->units) - break; + sdt_units = sme_find_table_entry(SME_DESC_UNITS, + edata->units); sme_event_register(sdict, edata, sme, &lims, props, PENVSYS_EVENT_LIMITS, - sdt_units[i].crittype); + sdt_units->crittype); } } @@ -1267,18 +1259,15 @@ static sme_event_drv_t * sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, prop_dictionary_t dict, envsys_data_t *edata) { - const struct sme_description_table *sdt, *sdt_units; + const struct sme_descr_entry *sdt_state, *sdt_units, *sdt_battcap; + const struct sme_descr_entry *sdt_drive; sme_event_drv_t *sme_evdrv_t = NULL; - int i, j; char indexstr[ENVSYS_DESCLEN]; /* * Find the correct units for this sensor. */ - sdt_units = sme_get_description_table(SME_DESC_UNITS); - for (i = 0; sdt_units[i].type != -1; i++) - if (sdt_units[i].type == edata->units) - break; + sdt_units = sme_find_table_entry(SME_DESC_UNITS, edata->units); /* * Add the index sensor string. @@ -1300,7 +1289,7 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, * <string>blah blah</string> * ... */ - if (sme_sensor_upstring(dict, "type", sdt_units[i].desc)) + if (sme_sensor_upstring(dict, "type", sdt_units->desc)) goto bad; if (sme_sensor_upstring(dict, "description", edata->desc)) @@ -1314,15 +1303,12 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, * <string>valid</string> * ... */ - sdt = sme_get_description_table(SME_DESC_STATES); - for (j = 0; sdt[j].type != -1; j++) - if (sdt[j].type == edata->state) - break; + sdt_state = sme_find_table_entry(SME_DESC_STATES, edata->state); DPRINTF(("%s: sensor desc=%s type=%d state=%d\n", __func__, edata->desc, edata->units, edata->state)); - if (sme_sensor_upstring(dict, "state", sdt[j].desc)) + if (sme_sensor_upstring(dict, "state", sdt_state->desc)) goto bad; /* @@ -1391,12 +1377,10 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, * ... */ if (edata->units == ENVSYS_BATTERY_CAPACITY) { - sdt = sme_get_description_table(SME_DESC_BATTERY_CAPACITY); - for (j = 0; sdt[j].type != -1; j++) - if (sdt[j].type == edata->value_cur) - break; - - if (sme_sensor_upstring(dict, "battery-capacity", sdt[j].desc)) + sdt_battcap = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY, + edata->value_cur); + if (sme_sensor_upstring(dict, "battery-capacity", + sdt_battcap->desc)) goto out; } @@ -1409,12 +1393,9 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, * ... */ if (edata->units == ENVSYS_DRIVE) { - sdt = sme_get_description_table(SME_DESC_DRIVE_STATES); - for (j = 0; sdt[j].type != -1; j++) - if (sdt[j].type == edata->value_cur) - break; - - if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc)) + sdt_drive = sme_find_table_entry(SME_DESC_DRIVE_STATES, + edata->value_cur); + if (sme_sensor_upstring(dict, "drive-state", sdt_drive->desc)) goto out; } @@ -1494,7 +1475,7 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, sme_evdrv_t->sed_sdict = dict; sme_evdrv_t->sed_edata = edata; sme_evdrv_t->sed_sme = sme; - sme_evdrv_t->sed_powertype = sdt_units[i].crittype; + sme_evdrv_t->sed_powertype = sdt_units->crittype; } out: @@ -1579,10 +1560,10 @@ sme_get_max_value(struct sysmon_envsys *sme, int sme_update_dictionary(struct sysmon_envsys *sme) { - const struct sme_description_table *sdt; + const struct sme_descr_entry *sdt; envsys_data_t *edata; prop_object_t array, dict, obj, obj2; - int j, error = 0; + int error = 0; /* * Retrieve the array of dictionaries in device. @@ -1652,31 +1633,25 @@ sme_update_dictionary(struct sysmon_envsys *sme) /* * update sensor's state. */ - sdt = sme_get_description_table(SME_DESC_STATES); - for (j = 0; sdt[j].type != -1; j++) - if (sdt[j].type == edata->state) - break; + sdt = sme_find_table_entry(SME_DESC_STATES, edata->state); DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n", - __func__, edata->sensor, sdt[j].type, sdt[j].desc, + __func__, edata->sensor, sdt->type, sdt->desc, edata->flags)); - error = sme_sensor_upstring(dict, "state", sdt[j].desc); + error = sme_sensor_upstring(dict, "state", sdt->desc); if (error) break; /* * update sensor's type. */ - sdt = sme_get_description_table(SME_DESC_UNITS); - for (j = 0; sdt[j].type != -1; j++) - if (sdt[j].type == edata->units) - break; + sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units); DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n", - __func__, edata->sensor, sdt[j].type, sdt[j].desc)); + __func__, edata->sensor, sdt->type, sdt->desc)); - error = sme_sensor_upstring(dict, "type", sdt[j].desc); + error = sme_sensor_upstring(dict, "type", sdt->desc); if (error) break; @@ -1763,14 +1738,10 @@ sme_update_dictionary(struct sysmon_envsys *sme) * update 'drive-state' only for ENVSYS_DRIVE sensors. */ if (edata->units == ENVSYS_DRIVE) { - sdt = sme_get_description_table(SME_DESC_DRIVE_STATES); - for (j = 0; sdt[j].type != -1; j++) - if (sdt[j].type == edata->value_cur) - break; - - error = sme_sensor_upstring(dict, - "drive-state", - sdt[j].desc); + sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES, + edata->value_cur); + error = sme_sensor_upstring(dict, "drive-state", + sdt->desc); if (error) break; } @@ -1780,15 +1751,10 @@ sme_update_dictionary(struct sysmon_envsys *sme) * sensors. */ if (edata->units == ENVSYS_BATTERY_CAPACITY) { - sdt = - sme_get_description_table(SME_DESC_BATTERY_CAPACITY); - for (j = 0; sdt[j].type != -1; j++) - if (sdt[j].type == edata->value_cur) - break; - - error = sme_sensor_upstring(dict, - "battery-capacity", - sdt[j].desc); + sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY, + edata->value_cur); + error = sme_sensor_upstring(dict, "battery-capacity", + sdt->desc); if (error) break; } @@ -1807,7 +1773,7 @@ int sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict, prop_array_t array) { - const struct sme_description_table *sdt; + const struct sme_descr_entry *sdt; envsys_data_t *edata; prop_dictionary_t dict, tdict = NULL; prop_object_t obj, obj1, obj2, tobj = NULL; @@ -1940,10 +1906,7 @@ sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict, } } - sdt = sme_get_description_table(SME_DESC_UNITS); - for (i = 0; sdt[i].type != -1; i++) - if (sdt[i].type == edata->units) - break; + sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units); /* * did the user want to set a critical capacity event? @@ -2035,7 +1998,7 @@ sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict, (edata->flags & ENVSYS_FPERCENT)? PENVSYS_EVENT_CAPACITY: PENVSYS_EVENT_LIMITS, - sdt[i].crittype); + sdt->crittype); if (error == EEXIST) error = 0; if (error) diff --git a/sys/dev/sysmon/sysmon_envsys_events.c b/sys/dev/sysmon/sysmon_envsys_events.c index 217f91aa7ab..2ff75137fc4 100644 --- a/sys/dev/sysmon/sysmon_envsys_events.c +++ b/sys/dev/sysmon/sysmon_envsys_events.c @@ -1,4 +1,4 @@ -/* $NetBSD: sysmon_envsys_events.c,v 1.95 2010/12/08 00:09:14 pgoyette Exp $ */ +/* $NetBSD: sysmon_envsys_events.c,v 1.96 2010/12/15 17:17:17 pgoyette Exp $ */ /*- * Copyright (c) 2007, 2008 Juan Romero Pardines. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.95 2010/12/08 00:09:14 pgoyette Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.96 2010/12/15 17:17:17 pgoyette Exp $"); #include <sys/param.h> #include <sys/types.h> @@ -751,7 +751,7 @@ void sme_deliver_event(sme_event_t *see) { envsys_data_t *edata = see->see_edata; - const struct sme_description_table *sdt = NULL; + const struct sme_descr_entry *sdt = NULL; const struct sme_sensor_event *sse = sme_sensor_event; int i, state = 0; @@ -819,12 +819,13 @@ sme_deliver_event(sme_event_t *see) switch (edata->units) { case ENVSYS_DRIVE: - sdt = sme_get_description_table(SME_DESC_DRIVE_STATES); + sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES, + edata->value_cur); state = ENVSYS_DRIVE_ONLINE; break; case ENVSYS_BATTERY_CAPACITY: - sdt = sme_get_description_table( - SME_DESC_BATTERY_CAPACITY); + sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY, + edata->value_cur); state = ENVSYS_BATTERY_CAPACITY_NORMAL; break; default: @@ -832,17 +833,13 @@ sme_deliver_event(sme_event_t *see) __func__); } - for (i = 0; sdt[i].type != -1; i++) - if (sdt[i].type == edata->value_cur) - break; - - if (sdt[i].type == -1) + if (sdt->type == -1) break; /* * copy current state description. */ - (void)strlcpy(see->see_pes.pes_statedesc, sdt[i].desc, + (void)strlcpy(see->see_pes.pes_statedesc, sdt->desc, sizeof(see->see_pes.pes_statedesc)); if (edata->value_cur == state) diff --git a/sys/dev/sysmon/sysmon_envsys_tables.c b/sys/dev/sysmon/sysmon_envsys_tables.c index f5eb7471633..61b71cbe70b 100644 --- a/sys/dev/sysmon/sysmon_envsys_tables.c +++ b/sys/dev/sysmon/sysmon_envsys_tables.c @@ -1,4 +1,4 @@ -/* $NetBSD: sysmon_envsys_tables.c,v 1.5 2008/02/28 16:21:34 xtraeme Exp $ */ +/* $NetBSD: sysmon_envsys_tables.c,v 1.6 2010/12/15 17:17:17 pgoyette Exp $ */ /*- * Copyright (c) 2007 Juan Romero Pardines. @@ -26,7 +26,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_tables.c,v 1.5 2008/02/28 16:21:34 xtraeme Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_tables.c,v 1.6 2010/12/15 17:17:17 pgoyette Exp $"); #include <sys/types.h> @@ -36,7 +36,7 @@ __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_tables.c,v 1.5 2008/02/28 16:21:34 xtr /* * Available units type descriptions. */ -static const struct sme_description_table sme_units_description[] = { +static const struct sme_descr_entry sme_units_description[] = { { ENVSYS_STEMP, PENVSYS_TYPE_TEMP, "Temperature" }, { ENVSYS_SFANRPM, PENVSYS_TYPE_FAN, "Fan" }, { ENVSYS_SVOLTS_AC, PENVSYS_TYPE_VOLTAGE, "Voltage AC" }, @@ -57,7 +57,7 @@ static const struct sme_description_table sme_units_description[] = { /* * Available sensor state descriptions. */ -static const struct sme_description_table sme_state_description[] = { +static const struct sme_descr_entry sme_state_description[] = { { ENVSYS_SVALID, -1, "valid" }, { ENVSYS_SINVALID, -1, "invalid" }, { ENVSYS_SCRITICAL, -1, "critical" }, @@ -71,7 +71,7 @@ static const struct sme_description_table sme_state_description[] = { /* * Available drive state descriptions. */ -static const struct sme_description_table sme_drivestate_description[] = { +static const struct sme_descr_entry sme_drivestate_description[] = { { ENVSYS_DRIVE_EMPTY, -1, "unknown" }, { ENVSYS_DRIVE_READY, -1, "ready" }, { ENVSYS_DRIVE_POWERUP, -1, "powering up" }, @@ -92,7 +92,7 @@ static const struct sme_description_table sme_drivestate_description[] = { /* * Available battery capacity descriptions. */ -static const struct sme_description_table sme_batterycap_description[] = { +static const struct sme_descr_entry sme_batterycap_description[] = { { ENVSYS_BATTERY_CAPACITY_NORMAL, -1, "NORMAL" }, { ENVSYS_BATTERY_CAPACITY_WARNING, -1, "WARNING" }, { ENVSYS_BATTERY_CAPACITY_CRITICAL, -1, "CRITICAL" }, @@ -101,26 +101,31 @@ static const struct sme_description_table sme_batterycap_description[] = { }; /* - * Returns the table associated with type. + * Returns the entry from specified table with type == key */ -const struct sme_description_table * -sme_get_description_table(int type) +const struct sme_descr_entry * +sme_find_table_entry(enum sme_descr_type table_id, int key) { - const struct sme_description_table *ud = sme_units_description; - const struct sme_description_table *sd = sme_state_description; - const struct sme_description_table *dd = sme_drivestate_description; - const struct sme_description_table *bd = sme_batterycap_description; + const struct sme_descr_entry *table = NULL; - switch (type) { + switch (table_id) { case SME_DESC_UNITS: - return ud; + table = sme_units_description; + break; case SME_DESC_STATES: - return sd; + table = sme_state_description; + break; case SME_DESC_DRIVE_STATES: - return dd; + table = sme_drivestate_description; + break; case SME_DESC_BATTERY_CAPACITY: - return bd; - default: - return NULL; + table = sme_batterycap_description; + break; } + + for (; table->type != -1; table++) + if (table->type == key) + break; + + return table; } diff --git a/sys/dev/sysmon/sysmon_envsysvar.h b/sys/dev/sysmon/sysmon_envsysvar.h index 3a57ad16ca8..680e20d29f1 100644 --- a/sys/dev/sysmon/sysmon_envsysvar.h +++ b/sys/dev/sysmon/sysmon_envsysvar.h @@ -1,4 +1,4 @@ -/* $NetBSD: sysmon_envsysvar.h,v 1.36 2010/04/10 19:01:01 pgoyette Exp $ */ +/* $NetBSD: sysmon_envsysvar.h,v 1.37 2010/12/15 17:17:17 pgoyette Exp $ */ /*- * Copyright (c) 2007, 2008 Juan Romero Pardines. @@ -40,7 +40,7 @@ #include <dev/sysmon/sysmonvar.h> #include <prop/proplib.h> -enum sme_description_types { +enum sme_descr_type { SME_DESC_UNITS = 1, SME_DESC_STATES, SME_DESC_DRIVE_STATES, @@ -90,7 +90,7 @@ typedef struct sme_event_drv { int sed_powertype; } sme_event_drv_t; -struct sme_description_table { +struct sme_descr_entry { int type; int crittype; const char *desc; @@ -142,6 +142,6 @@ int sme_sensor_upint32(prop_dictionary_t, const char *, int32_t); int sme_sensor_upuint32(prop_dictionary_t, const char *, uint32_t); int sme_sensor_upstring(prop_dictionary_t, const char *, const char *); -const struct sme_description_table *sme_get_description_table(int); +const struct sme_descr_entry *sme_find_table_entry(enum sme_descr_type, int); #endif /* _DEV_SYSMON_ENVSYSVAR_H_ */ |
