From: bernie Date: Thu, 16 Feb 2006 22:49:55 +0000 (+0000) Subject: Macros for intermodule dependency checks. X-Git-Tag: 1.0.0~723 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=bd7e7624e20c538f2bc3c3d670cc4ae8e02cffd5;p=bertos.git Macros for intermodule dependency checks. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@518 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/cfg/module.h b/cfg/module.h new file mode 100755 index 00000000..31bb07f2 --- /dev/null +++ b/cfg/module.h @@ -0,0 +1,99 @@ +/*! + * \file + * + * + * \brief Debug macros for inter-module dependency checking. + * + * These macros expand to nothing in release builds. In debug + * builds, they perform run-time dependency checks for modules. + * + * The usage pattern looks like this: + * + * \code + * MOD_DEFINE(phaser) + * + * void phaser_init(void) + * { + * MOD_CHECK(computer); + * MOD_CHECK(warp_core); + * + * [...charge weapons...] + * + * MOD_INIT(phaser); + * } + * + * void phaser_cleanup(void) + * { + * MOD_CLEANUP(phaser); + * + * [...disarm phaser...] + * } + * \endcode + * + * \version $Id$ + * \author Bernardo Innocenti + */ +#ifndef CFG_MODULE_H +#define CFG_MODULE_H + +#include + +/** + * Declare a global variable for module dependency check. + * + * \see MOD_INIT(), MOD_CHECK() + */ +#define MOD_DEFINE(module) DB(bool module ## _initialized;) + +/** + * Check that \a module was already initialized. + * + * Put this check just before accessing any facility + * provided by a module that requires prior initialization. + * + * \see MOD_INIT() + */ + +#define MOD_CHECK(module) \ +do { \ + DB(extern bool module ## _initialized;) \ + ASSERT(module ## _initialized); \ +} while (0) + +/** + * Mark module as initialized. + * + * Marking initialization requires the global data + * previously defined by MOD_DEFINE(). + * + * To prevent double initialization bugs, an initialized + * module must first be cleaned up with MOD_CLEANUP() before + * calling MOD_INIT() another time. + * + * \see MOD_CLEANUP(), MOD_CHECK(), MOD_DEFINE() + */ +#define MOD_INIT(module) \ +do { \ + ASSERT(!module ## _initialized); \ + DB(module ## _initialized = true;) \ +} while (0) + +/** + * Mark module as being no longer initialized. + * + * Marking initialization requires the global data + * previously defined by MOD_DEFINE(). + * + * \see MOD_INIT(), MOD_CHECK(), MOD_DEFINE() + */ +#define MOD_CLEANUP(module) \ +do { \ + ASSERT(module ## _initialized); \ + DB(module ## _initialized = false;) \ +} while (0) + +#endif /* CFG_MODULE_H */ +