[PATCH][NIH] Better semantics for NIH_{SHOULD,ZERO,MUST}

Casey Dahlin cdahlin at redhat.com
Tue Feb 17 21:16:59 GMT 2009


This patch makes NIH_{SHOULD,ZERO,MUST} return the values of their 
arguments, so

	NIH_MUST (foo = bar());

becomes

	foo = NIH_MUST (bar());

--CJD


=== modified file 'nih/error.h'
--- nih/error.h	2008-05-18 11:38:29 +0000
+++ nih/error.h	2009-02-17 21:09:01 +0000
@@ -95,18 +95,20 @@


  /* Force a true value, checking for ENOMEM on a false one */
-#define NIH_SHOULD(_e)						  \
-	while (! (_e)) {					  \
-		NihError *_nih_should_err;			  \
-								  \
-		_nih_should_err = nih_error_get ();		  \
-		if (_nih_should_err->number == ENOMEM) {	  \
-			nih_free (_nih_should_err);		  \
-		} else {					  \
-			nih_error_raise_again (_nih_should_err);  \
-			break;					  \
-		}						  \
-	}
+#define NIH_SHOULD(_e) ({			\
+	typeof (_e) ret;			\
+	while (! (ret = (_e))) {		\
+		NihError *err;			\
+						\
+		err = nih_error_get ();		\
+		if (err->number != ENOMEM) {	\
+			nih_free (err);		\
+			ret = (typeof (ret))0;	\
+		}				\
+		nih_free (err);			\
+	}					\
+	ret;					\
+)}


  NIH_BEGIN_EXTERN

=== modified file 'nih/macros.h'
--- nih/macros.h	2007-03-08 12:07:16 +0000
+++ nih/macros.h	2009-02-17 21:09:16 +0000
@@ -65,10 +65,17 @@
  #define NIH_LIKELY(_e)   __builtin_expect ((_e) ? TRUE : FALSE, TRUE)
  #define NIH_UNLIKELY(_e) __builtin_expect ((_e) ? TRUE : FALSE, FALSE)

-/* Force a true or false value, _e must be an assignment expression */
-#define NIH_MUST(_e) while (! (_e))
-#define NIH_ZERO(_e) while ((_e))
-
+/* Force a true or false value */
+#define NIH_MUST(_e) ({		\
+	typeof (_e) ret;	\
+	while (! (ret = (_e)));	\
+	ret;			\
+})
+#define NIH_ZERO(_e) ({		\
+	typeof (_e) ret;	\
+	while (ret = (_e));	\
+	ret;			\
+})

  /* Make gettext friendlier */
  #if ENABLE_NLS




More information about the upstart-devel mailing list