Module Name:    src
Committed By:   pgoyette
Date:           Tue Dec 11 15:39:06 UTC 2012

Modified Files:
        src/sys/dev/sysmon: sysmon_envsys_events.c sysmonvar.h

Log Message:
Replace a couple of many-line #define with equivalent code loops.

No functional change intended, and atf tests (using swsensor(4)) still
pass 100%


To generate a diff of this commit:
cvs rdiff -u -r1.106 -r1.107 src/sys/dev/sysmon/sysmon_envsys_events.c
cvs rdiff -u -r1.43 -r1.44 src/sys/dev/sysmon/sysmonvar.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_events.c
diff -u src/sys/dev/sysmon/sysmon_envsys_events.c:1.106 src/sys/dev/sysmon/sysmon_envsys_events.c:1.107
--- src/sys/dev/sysmon/sysmon_envsys_events.c:1.106	Wed Oct 31 05:42:47 2012
+++ src/sys/dev/sysmon/sysmon_envsys_events.c	Tue Dec 11 15:39:06 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: sysmon_envsys_events.c,v 1.106 2012/10/31 05:42:47 macallan Exp $ */
+/* $NetBSD: sysmon_envsys_events.c,v 1.107 2012/12/11 15:39:06 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.106 2012/10/31 05:42:47 macallan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.107 2012/12/11 15:39:06 pgoyette Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -65,6 +65,37 @@ static const struct sme_sensor_event sme
 	{ -1, 					-1 }
 };
 
+struct op_t {
+	const char *name;
+	enum envsys_lims idx;
+	uint32_t prop;
+} limit_ops[] = {
+	/* Value-based limits */
+	{ "critical-max", ENVSYS_LIM_CRITMAX, PROP_CRITMAX },
+	{ "warning-max",  ENVSYS_LIM_WARNMAX, PROP_WARNMAX },
+	{ "warning-min",  ENVSYS_LIM_WARNMIN, PROP_WARNMIN },
+	{ "critical-min", ENVSYS_LIM_CRITMIN, PROP_CRITMIN },
+
+	/* %Capacity-based limits */
+	{ "maximum-capacity",  ENVSYS_LIM_CRITMAX,  PROP_BATTMAX },
+	{ "high-capacity",     ENVSYS_LIM_WARNMAX,  PROP_BATTHIGH },
+	{ "warning-capacity",  ENVSYS_LIM_WARNMIN,  PROP_BATTWARN },
+	{ "critical-capacity", ENVSYS_LIM_CRITMIN,  PROP_BATTCAP },
+	{ NULL, 0, 0 }
+};
+
+struct ev_reg_t {
+	uint32_t crittype;
+	uint32_t powertype;
+	const char *name;
+} reg_events[] = {
+	{ ENVSYS_FMONCRITICAL,  PENVSYS_EVENT_CRITICAL,      "critical" },
+	{ ENVSYS_FMONSTCHANGED,	PENVSYS_EVENT_STATE_CHANGED, "state-changed" },
+	{ ENVSYS_FMONLIMITS,    PENVSYS_EVENT_LIMITS,        "hw-range-limits" },
+	{ ENVSYS_FHAS_ENTROPY,  PENVSYS_EVENT_NULL,          "refresh-event" },
+	{ 0, 0, NULL }
+};
+
 static bool sysmon_low_power;
 
 #define SME_EVTIMO	(SME_EVENTS_DEFTIMEOUT * hz)
@@ -91,6 +122,7 @@ sme_event_register(prop_dictionary_t sdi
 	prop_object_t obj;
 	int error = 0;
 	const char *objkey;
+	struct op_t *op;
 
 	KASSERT(sdict != NULL);
 	KASSERT(edata != NULL);
@@ -244,41 +276,30 @@ sme_event_register(prop_dictionary_t sdi
 	/*
 	 * Limit operation requested.
 	 */
-#define	LIMIT_OP(k, l, p)						\
-	if (props & p) {						\
-		objkey = k;						\
-		obj = prop_dictionary_get(sdict, objkey);		\
-		if (obj != NULL &&					\
-		    prop_object_type(obj) != PROP_TYPE_NUMBER) {	\
-			DPRINTF(("%s: (%s) %s object no TYPE_NUMBER\n",	\
-			    __func__, sme->sme_name, objkey));		\
-			error = ENOTSUP;				\
-		} else {						\
-			edata->limits.l = lims->l;			\
-			error = sme_sensor_upint32(sdict, objkey,lims->l); \
-			DPRINTF(("%s: (%s) event [sensor=%s type=%d] "	\
-			    "(%s updated)\n", __func__, sme->sme_name,	\
-			    edata->desc, crittype, objkey));		\
-		}							\
-		if (error && error != EEXIST)				\
-			goto out;					\
-		edata->upropset |= p;					\
+	for (op = limit_ops; op->name != NULL; op++) {
+		if (props & op->prop) {
+			objkey = op->name;
+			obj = prop_dictionary_get(sdict, objkey);
+			if (obj != NULL &&
+			    prop_object_type(obj) != PROP_TYPE_NUMBER) {
+				DPRINTF(("%s: (%s) %s object not TYPE_NUMBER\n",
+				    __func__, sme->sme_name, objkey));
+				error = ENOTSUP;
+			} else {
+				edata->limits.sel_limit_list[op->idx] =
+				    lims->sel_limit_list[op->idx];
+				error = sme_sensor_upint32(sdict, objkey,
+					   lims->sel_limit_list[op->idx]);
+				DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
+				    "(%s updated)\n", __func__, sme->sme_name,
+				    edata->desc, crittype, objkey));
+			}
+			if (error && error != EEXIST)
+				goto out;
+			edata->upropset |= op->prop;
+		}
 	}
 
-	/* Value-based limits */
-	LIMIT_OP("critical-max", sel_critmax, PROP_CRITMAX);
-	LIMIT_OP("warning-max",  sel_warnmax, PROP_WARNMAX);
-	LIMIT_OP("warning-min",  sel_warnmin, PROP_WARNMIN);
-	LIMIT_OP("critical-min", sel_critmin, PROP_CRITMIN);
-
-	/* %Capacity-based limits */
-	LIMIT_OP("maximum-capacity",  sel_critmax,  PROP_BATTMAX);
-	LIMIT_OP("high-capacity",     sel_warnmax,  PROP_BATTHIGH);
-	LIMIT_OP("warning-capacity",  sel_warnmin,  PROP_BATTWARN);
-	LIMIT_OP("critical-capacity", sel_critmin,  PROP_BATTCAP);
-
-#undef LIMIT_OP
-
 	if (props & PROP_DRIVER_LIMITS)
 		edata->upropset |= PROP_DRIVER_LIMITS;
 	else
@@ -474,34 +495,10 @@ sme_event_drvadd(void *arg)
 	sysmon_envsys_lim_t lims;
 	uint32_t props;
 	int error = 0;
+	struct ev_reg_t *reg;
 
 	KASSERT(sed_t != NULL);
 
-#define SEE_REGEVENT(a, b, c)						\
-do {									\
-	if (sed_t->sed_edata->flags & (a)) {				\
-		char str[ENVSYS_DESCLEN] = "monitoring-state-";		\
-									\
-		error = sme_event_register(sed_t->sed_sdict,		\
-				      sed_t->sed_edata,			\
-				      sed_t->sed_sme,			\
-				      &lims, props,			\
-				      (b),				\
-				      sed_t->sed_powertype);		\
-		if (error && error != EEXIST)				\
-			printf("%s: failed to add event! "		\
-			    "error=%d sensor=%s event=%s\n",		\
-			    __func__, error,				\
-			    sed_t->sed_edata->desc, (c));		\
-		else {							\
-			(void)strlcat(str, (c), sizeof(str));		\
-			prop_dictionary_set_bool(sed_t->sed_sdict,	\
-						 str,			\
-						 true);			\
-		}							\
-	}								\
-} while (/* CONSTCOND */ 0)
-
 	/*
 	 * If driver provides a method to retrieve its internal limit
 	 * values, call it and use those returned values as initial
@@ -522,21 +519,28 @@ do {									\
 
 	/* Register the events that were specified */
 
-	SEE_REGEVENT(ENVSYS_FMONCRITICAL,
-		     PENVSYS_EVENT_CRITICAL,
-		     "critical");
-
-	SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
-		     PENVSYS_EVENT_STATE_CHANGED,
-		     "state-changed");
-
-	SEE_REGEVENT(ENVSYS_FMONLIMITS,
-		     PENVSYS_EVENT_LIMITS,
-		     "hw-range-limits");
-
-	SEE_REGEVENT(ENVSYS_FHAS_ENTROPY,
-		     PENVSYS_EVENT_NULL,
-		     "refresh-event");
+	for (reg = reg_events; reg->name != NULL; reg++) {
+		if (sed_t->sed_edata->flags & reg->crittype) {
+
+			error = sme_event_register(sed_t->sed_sdict,
+					      sed_t->sed_edata,
+					      sed_t->sed_sme,
+					      &lims, props,
+					      reg->powertype,
+					      sed_t->sed_powertype);
+			if (error && error != EEXIST)
+				printf("%s: failed to add event! "
+				    "error=%d sensor=%s event=%s\n",
+				    __func__, error,
+				    sed_t->sed_edata->desc, reg->name);
+			else {
+				char str[ENVSYS_DESCLEN] = "monitoring-state-";
+				(void)strlcat(str, reg->name, sizeof(str));
+				prop_dictionary_set_bool(sed_t->sed_sdict,
+							 str, true);
+			}
+		}
+	}
 
 	/* 
 	 * we are done, free memory now.

Index: src/sys/dev/sysmon/sysmonvar.h
diff -u src/sys/dev/sysmon/sysmonvar.h:1.43 src/sys/dev/sysmon/sysmonvar.h:1.44
--- src/sys/dev/sysmon/sysmonvar.h:1.43	Mon Jul 16 13:55:01 2012
+++ src/sys/dev/sysmon/sysmonvar.h	Tue Dec 11 15:39:06 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: sysmonvar.h,v 1.43 2012/07/16 13:55:01 pgoyette Exp $	*/
+/*	$NetBSD: sysmonvar.h,v 1.44 2012/12/11 15:39:06 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2000 Zembu Labs, Inc.
@@ -63,14 +63,31 @@ struct workqueue;
 /*
  * Thresholds/limits that are being monitored
  */
+
+enum envsys_lims {
+	ENVSYS_LIM_CRITMAX,
+	ENVSYS_LIM_WARNMAX,
+	ENVSYS_LIM_WARNMIN,
+	ENVSYS_LIM_CRITMIN,
+	ENVSYS_LIM_LASTLIM
+};
+
 struct sysmon_envsys_lim {
-	int32_t		sel_critmax;
-	int32_t		sel_warnmax;
-	int32_t		sel_warnmin;
-	int32_t		sel_critmin;
+	int32_t critmax;
+	int32_t warnmax;
+	int32_t warnmin;
+	int32_t critmin;
 };
 
-typedef struct sysmon_envsys_lim sysmon_envsys_lim_t;
+typedef union {
+	int32_t sel_limit_list[ENVSYS_LIM_LASTLIM];
+	struct sysmon_envsys_lim sel_limits;
+} sysmon_envsys_lim_t;
+
+#define	sel_critmax sel_limits.critmax
+#define	sel_warnmax sel_limits.warnmax
+#define	sel_warnmin sel_limits.warnmin
+#define	sel_critmin sel_limits.critmin
 
 /* struct used by a sensor */
 struct envsys_data {

Reply via email to