[SLOF] [PATCH v2 1/3] libc: Add a simple implementation of an assert() function

Thomas Huth thuth at redhat.com
Tue Jun 5 20:11:47 AEST 2018


... useful for "this should never happen" situations, where
you want to make sure that it really never happens.

Signed-off-by: Thomas Huth <thuth at redhat.com>
---
 lib/libc/include/assert.h | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 lib/libc/include/assert.h

diff --git a/lib/libc/include/assert.h b/lib/libc/include/assert.h
new file mode 100644
index 0000000..e0e02ac
--- /dev/null
+++ b/lib/libc/include/assert.h
@@ -0,0 +1,37 @@
+/*****************************************************************************
+ * assert() macro definition
+ *
+ * Copyright 2018 Red Hat, Inc.
+ *
+ * This program and the accompanying materials are made available under
+ * the terms of the BSD License which accompanies this distribution, and
+ * is available at http://www.opensource.org/licenses/bsd-license.php
+ *
+ * Contributors:
+ *     Thomas Huth, Red Hat Inc. - initial implementation
+ *****************************************************************************/
+
+#ifndef SLIMLINE_ASSERT_H
+#define SLIMLINE_ASSERT_H
+
+
+#ifdef NDEBUG
+
+#define assert(cond) (void)
+
+#else
+
+#define assert(cond) \
+	do { \
+		if (!(cond)) { \
+			fprintf(stderr, \
+				"ERROR: Assertion '" #cond "' failed!\n" \
+				"(function %s, file " __FILE__ ", line %i)\n", \
+				__func__, __LINE__); \
+			while (1) {}; \
+		} \
+	}  while (0)
+
+#endif
+
+#endif /* SLIMLINE_ASSERT_H */
-- 
1.8.3.1



More information about the SLOF mailing list