Module Name:    src
Committed By:   pgoyette
Date:           Wed Dec 15 17:17:17 UTC 2010

Modified Files:
        src/sys/dev/sysmon: sysmon_envsys.c sysmon_envsys_events.c
            sysmon_envsys_tables.c sysmon_envsysvar.h

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.109 -r1.110 src/sys/dev/sysmon/sysmon_envsys.c
cvs rdiff -u -r1.95 -r1.96 src/sys/dev/sysmon/sysmon_envsys_events.c
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/sysmon/sysmon_envsys_tables.c
cvs rdiff -u -r1.36 -r1.37 src/sys/dev/sysmon/sysmon_envsysvar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/sysmon/sysmon_envsys.c
diff -u src/sys/dev/sysmon/sysmon_envsys.c:1.109 src/sys/dev/sysmon/sysmon_envsys.c:1.110
--- src/sys/dev/sysmon/sysmon_envsys.c:1.109	Sat Dec 11 15:17:15 2010
+++ src/sys/dev/sysmon/sysmon_envsys.c	Wed Dec 15 17:17:16 2010
@@ -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 @@
 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 @@
 
 	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 @@
 	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 @@
 				/*
 				 * 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 @@
 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 @@
 	 * 		<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 @@
 	 * 		<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 @@
 	 * 		...
 	 */
 	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 @@
 	 * 		...
 	 */
 	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_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 @@
 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 @@
 		/* 
 		 * 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 @@
 		 * 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 @@
 		 * 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 @@
 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 @@
 			}
 		}
 
-		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 @@
 					(edata->flags & ENVSYS_FPERCENT)?
 						PENVSYS_EVENT_CAPACITY:
 						PENVSYS_EVENT_LIMITS,
-					sdt[i].crittype);
+					sdt->crittype);
 			if (error == EEXIST)
 				error = 0;
 			if (error) 

Index: src/sys/dev/sysmon/sysmon_envsys_events.c
diff -u src/sys/dev/sysmon/sysmon_envsys_events.c:1.95 src/sys/dev/sysmon/sysmon_envsys_events.c:1.96
--- src/sys/dev/sysmon/sysmon_envsys_events.c:1.95	Wed Dec  8 00:09:14 2010
+++ src/sys/dev/sysmon/sysmon_envsys_events.c	Wed Dec 15 17:17:17 2010
@@ -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 @@
 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 @@
 
 		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 @@
 			    __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)

Index: src/sys/dev/sysmon/sysmon_envsys_tables.c
diff -u src/sys/dev/sysmon/sysmon_envsys_tables.c:1.5 src/sys/dev/sysmon/sysmon_envsys_tables.c:1.6
--- src/sys/dev/sysmon/sysmon_envsys_tables.c:1.5	Thu Feb 28 16:21:34 2008
+++ src/sys/dev/sysmon/sysmon_envsys_tables.c	Wed Dec 15 17:17:17 2010
@@ -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 @@
 /*
  * 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 @@
 /*
  * 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 @@
 /*
  * 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 @@
 /*
  * 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 @@
 };
 
 /*
- * 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;
 }

Index: src/sys/dev/sysmon/sysmon_envsysvar.h
diff -u src/sys/dev/sysmon/sysmon_envsysvar.h:1.36 src/sys/dev/sysmon/sysmon_envsysvar.h:1.37
--- src/sys/dev/sysmon/sysmon_envsysvar.h:1.36	Sat Apr 10 19:01:01 2010
+++ src/sys/dev/sysmon/sysmon_envsysvar.h	Wed Dec 15 17:17:17 2010
@@ -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 @@
 	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_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_ */

Reply via email to