1 /*
2 Z88DK Z80 Macro Assembler
3 
4 Macros to help define init_module() functions per module
5 
6 Copyright (C) Gunther Strube, InterLogic 1993-99
7 Copyright (C) Paulo Custodio, 2011-2020
8 License: The Artistic License 2.0, http://www.perlfoundation.org/artistic_license_2_0
9 Repository: https://github.com/z88dk/z88dk
10 */
11 
12 #pragma once
13 
14 #include "dbg.h"
15 #include "types.h"
16 
17 /*-----------------------------------------------------------------------------
18 *   Usage:
19 *
20 *	DEFINE_init_module()
21 *	{
22 *		... init code ...	// included in a new init_module() function
23 *	}
24 *	DEFINE_dtor_module()
25 *	{
26 *		... dtor code ...	// included in a new dtor() function
27 *							// init_module() calls atexit(dtor)
28 *	}
29 *
30 *	xxx func ( xxx )
31 *	{
32 *		init_module();				// call init_module() at the begin of every external function
33 *		...
34 *	}
35 *----------------------------------------------------------------------------*/
36 
37 /* DEFINE_init_module() */
38 #define DEFINE_init_module()									\
39 		static bool __init_called = false;				\
40 		static void __fini(void);						\
41 		static void __user_init(void);					\
42 		static void __init(void)						\
43 		{												\
44 			if ( ! __init_called )						\
45 			{											\
46 				__init_called = true;					\
47 				__user_init();							\
48 				xatexit( __fini );						\
49 			}											\
50 		}												\
51 		static void __user_init(void)
52 
53 /* DEFINE_dtor_module() */
54 #define DEFINE_dtor_module()									\
55 		static void __fini(void)
56 
57 /* init_module() */
58 #define init_module() do { if ( ! __init_called ) __init(); } while (0)
59