diff --git a/libmaxsi/Makefile b/libmaxsi/Makefile index d2314ee6..2cda163e 100644 --- a/libmaxsi/Makefile +++ b/libmaxsi/Makefile @@ -67,6 +67,7 @@ string.o \ error.o \ format.o \ access.o \ +_assert.o \ chdir.o \ chmod.o \ close.o \ diff --git a/libmaxsi/_assert.cpp b/libmaxsi/_assert.cpp new file mode 100644 index 00000000..90b30306 --- /dev/null +++ b/libmaxsi/_assert.cpp @@ -0,0 +1,35 @@ +/****************************************************************************** + + Copyright(C) Jonas 'Sortie' Termansen 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + more details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + _assert.cpp + Reports the occurence of an assertion failure. + +******************************************************************************/ + +#include +#include +#include + +void _assert(const char* filename, unsigned int line, const char* functionname, + const char* expression) +{ + fprintf(stderr, "Assertion failure: %s:%u: %s: %s\n", filename, line, + functionname, expression); + abort(); +} diff --git a/libmaxsi/include/assert.h b/libmaxsi/include/assert.h index 321e58ea..fbd7927a 100644 --- a/libmaxsi/include/assert.h +++ b/libmaxsi/include/assert.h @@ -1,6 +1,6 @@ /****************************************************************************** - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012. + Copyright(C) Jonas 'Sortie' Termansen 2012. This file is part of LibMaxsi. @@ -27,18 +27,40 @@ #include +/* stdlib.h is not needed, but GCC fixincludes thinks it is, so fool it. */ +#if 0 +#include +#endif + __BEGIN_DECLS -#ifdef assert -#undef assert -#endif - -#ifndef NDEBUG -/* #warning The assert macro is not implemented */ -#endif - -#define assert(ignore)((void) 0) +/* The actual implementation of assert. */ +void _assert(const char* filename, unsigned int line, const char* functionname, + const char* expression) __attribute__ ((noreturn)); __END_DECLS #endif + +/* Rid ourselves of any previous declaration of assert. */ +#ifdef assert +#undef assert +#endif + +/* Redefine the assert macro on each inclusion. */ +#ifdef NDEBUG + +#define assert(ignore) ((void) 0) + +#else /* !NDEBUG */ + +/* Use __builtin_expect to tell the compiler that we don't expect a failure to + happen and thus it can do better branch prediction. Naturally we don't + optimize for the case where the program is about to abort(). */ +#define assert(invariant) \ + if ( __builtin_expect(!(invariant), 0) ) \ + { \ + _assert(__FILE__, __LINE__, __PRETTY_FUNCTION__, #invariant); \ + } + +#endif /* !NDEBUG */