diff --git a/Modelica/Resources/BuildProjects/CMake/src.cmake b/Modelica/Resources/BuildProjects/CMake/src.cmake index 2a2a7e9002..062df7b6fb 100644 --- a/Modelica/Resources/BuildProjects/CMake/src.cmake +++ b/Modelica/Resources/BuildProjects/CMake/src.cmake @@ -70,7 +70,7 @@ set(EXTC_SOURCES "${MODELICA_SOURCE_DIR}/ModelicaRandom.h" "${MODELICA_SOURCE_DIR}/ModelicaStrings.c" "${MODELICA_SOURCE_DIR}/ModelicaStrings.h" - "${MODELICA_SOURCE_DIR}/gconstructor.h" + "${MODELICA_SOURCE_DIR}/g2constructor.h" "${MODELICA_SOURCE_DIR}/stdint_msvc.h" "${MODELICA_SOURCE_DIR}/stdint_wrap.h" "${MODELICA_SOURCE_DIR}/uthash.h" @@ -84,7 +84,7 @@ set(TABLES_SOURCES "${MODELICA_SOURCE_DIR}/ModelicaStandardTables.h" "${MODELICA_SOURCE_DIR}/ModelicaStandardTablesUsertab.c" "${MODELICA_SOURCE_DIR}/ModelicaMatIO.h" - "${MODELICA_SOURCE_DIR}/gconstructor.h" + "${MODELICA_SOURCE_DIR}/g2constructor.h" "${MODELICA_SOURCE_DIR}/stdint_msvc.h" "${MODELICA_SOURCE_DIR}/stdint_wrap.h" "${MODELICA_SOURCE_DIR}/uthash.h" diff --git a/Modelica/Resources/C-Sources/ModelicaInternal.c b/Modelica/Resources/C-Sources/ModelicaInternal.c index b97e0d4854..043a022048 100644 --- a/Modelica/Resources/C-Sources/ModelicaInternal.c +++ b/Modelica/Resources/C-Sources/ModelicaInternal.c @@ -279,7 +279,7 @@ void ModelicaInternal_setenv(_In_z_ const char* name, #define HASH_NO_STDINT 1 #define HASH_NONFATAL_OOM 1 #include "uthash.h" -#include "gconstructor.h" +#include "g2constructor.h" #include #include @@ -774,16 +774,16 @@ typedef struct FileCache { static FileCache* fileCache = NULL; #if defined(_POSIX_) && !defined(NO_MUTEX) #include -#if defined(G_HAS_CONSTRUCTORS) +#if defined(G2_HAS_CONSTRUCTORS) static pthread_mutex_t m; -G_DEFINE_CONSTRUCTOR(initializeMutex) -static void initializeMutex(void) { +G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(initializeMutex)) +static void G2_FUNCNAME(initializeMutex)(void) { if (pthread_mutex_init(&m, NULL) != 0) { ModelicaError("Initialization of mutex failed\n"); } } -G_DEFINE_DESTRUCTOR(destroyMutex) -static void destroyMutex(void) { +G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(destroyMutex)) +static void G2_FUNCNAME(destroyMutex)(void) { if (pthread_mutex_destroy(&m) != 0) { ModelicaError("Destruction of mutex failed\n"); } @@ -793,24 +793,24 @@ static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; #endif #define MUTEX_LOCK() pthread_mutex_lock(&m) #define MUTEX_UNLOCK() pthread_mutex_unlock(&m) -#elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS) +#elif defined(_WIN32) && defined(G2_HAS_CONSTRUCTORS) #if !defined(WIN32_LEAN_AND_MEAN) #define WIN32_LEAN_AND_MEAN #endif #include static CRITICAL_SECTION cs; -#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA -#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ModelicaInternal_initializeCS) +#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA +#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaInternal_initializeCS)) #endif -G_DEFINE_CONSTRUCTOR(ModelicaInternal_initializeCS) -static void ModelicaInternal_initializeCS(void) { +G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(ModelicaInternal_initializeCS)) +static void G2_FUNCNAME(ModelicaInternal_initializeCS)(void) { InitializeCriticalSection(&cs); } -#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA -#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(ModelicaInternal_deleteCS) +#ifdef G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA +#pragma G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaInternal_deleteCS)) #endif -G_DEFINE_DESTRUCTOR(ModelicaInternal_deleteCS) -static void ModelicaInternal_deleteCS(void) { +G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(ModelicaInternal_deleteCS)) +static void G2_FUNCNAME(ModelicaInternal_deleteCS)(void) { DeleteCriticalSection(&cs); } #define MUTEX_LOCK() EnterCriticalSection(&cs) diff --git a/Modelica/Resources/C-Sources/ModelicaRandom.c b/Modelica/Resources/C-Sources/ModelicaRandom.c index 7a2d5b18a4..122d623aae 100644 --- a/Modelica/Resources/C-Sources/ModelicaRandom.c +++ b/Modelica/Resources/C-Sources/ModelicaRandom.c @@ -48,7 +48,7 @@ #include #include "ModelicaInternal.h" #include "ModelicaUtilities.h" -#include "gconstructor.h" +#include "g2constructor.h" /* The standard way to detect POSIX is to check _POSIX_VERSION, * which is defined in @@ -68,24 +68,24 @@ static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; #define MUTEX_UNLOCK() pthread_mutex_unlock(&m) /* On Windows systems define a critical section using the single static variable "cs" */ -#elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS) +#elif defined(_WIN32) && defined(G2_HAS_CONSTRUCTORS) #if !defined(WIN32_LEAN_AND_MEAN) #define WIN32_LEAN_AND_MEAN #endif #include static CRITICAL_SECTION cs; -#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA -#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ModelicaRandom_initializeCS) +#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA +#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaRandom_initializeCS)) #endif -G_DEFINE_CONSTRUCTOR(ModelicaRandom_initializeCS) -static void ModelicaRandom_initializeCS(void) { +G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(ModelicaRandom_initializeCS)) +static void G2_FUNCNAME(ModelicaRandom_initializeCS)(void) { InitializeCriticalSection(&cs); } -#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA -#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(ModelicaRandom_deleteCS) +#ifdef G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA +#pragma G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaRandom_deleteCS)) #endif -G_DEFINE_DESTRUCTOR(ModelicaRandom_deleteCS) -static void ModelicaRandom_deleteCS(void) { +G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(ModelicaRandom_deleteCS)) +static void G2_FUNCNAME(ModelicaRandom_deleteCS)(void) { DeleteCriticalSection(&cs); } #define MUTEX_LOCK() EnterCriticalSection(&cs) diff --git a/Modelica/Resources/C-Sources/ModelicaStandardTables.c b/Modelica/Resources/C-Sources/ModelicaStandardTables.c index 61e7cee794..1b5bd3f884 100644 --- a/Modelica/Resources/C-Sources/ModelicaStandardTables.c +++ b/Modelica/Resources/C-Sources/ModelicaStandardTables.c @@ -194,7 +194,7 @@ #define uthash_strlen(s) key_strlen(s) #define HASH_NONFATAL_OOM 1 #include "uthash.h" -#include "gconstructor.h" +#include "g2constructor.h" #endif #include #include @@ -433,16 +433,16 @@ typedef struct TableShare { static TableShare* tableShare = NULL; #if defined(_POSIX_) && !defined(NO_MUTEX) #include -#if defined(G_HAS_CONSTRUCTORS) +#if defined(G2_HAS_CONSTRUCTORS) static pthread_mutex_t m; -G_DEFINE_CONSTRUCTOR(initializeMutex) -static void initializeMutex(void) { +G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(initializeMutex)) +static void G2_FUNCNAME(initializeMutex)(void) { if (pthread_mutex_init(&m, NULL) != 0) { ModelicaError("Initialization of mutex failed\n"); } } -G_DEFINE_DESTRUCTOR(destroyMutex) -static void destroyMutex(void) { +G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(destroyMutex)) +static void G2_FUNCNAME(destroyMutex)(void) { if (pthread_mutex_destroy(&m) != 0) { ModelicaError("Destruction of mutex failed\n"); } @@ -452,24 +452,24 @@ static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; #endif #define MUTEX_LOCK() pthread_mutex_lock(&m) #define MUTEX_UNLOCK() pthread_mutex_unlock(&m) -#elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS) +#elif defined(_WIN32) && defined(G2_HAS_CONSTRUCTORS) #if !defined(WIN32_LEAN_AND_MEAN) #define WIN32_LEAN_AND_MEAN #endif #include static CRITICAL_SECTION cs; -#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA -#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ModelicaStandardTables_initializeCS) +#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA +#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaStandardTables_initializeCS)) #endif -G_DEFINE_CONSTRUCTOR(ModelicaStandardTables_initializeCS) -static void ModelicaStandardTables_initializeCS(void) { +G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(ModelicaStandardTables_initializeCS)) +static void G2_FUNCNAME(ModelicaStandardTables_initializeCS)(void) { InitializeCriticalSection(&cs); } -#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA -#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(ModelicaStandardTables_deleteCS) +#ifdef G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA +#pragma G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaStandardTables_deleteCS)) #endif -G_DEFINE_DESTRUCTOR(ModelicaStandardTables_deleteCS) -static void ModelicaStandardTables_deleteCS(void) { +G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(ModelicaStandardTables_deleteCS)) +static void G2_FUNCNAME(ModelicaStandardTables_deleteCS)(void) { DeleteCriticalSection(&cs); } #define MUTEX_LOCK() EnterCriticalSection(&cs) diff --git a/Modelica/Resources/C-Sources/gconstructor.h b/Modelica/Resources/C-Sources/g2constructor.h similarity index 54% rename from Modelica/Resources/C-Sources/gconstructor.h rename to Modelica/Resources/C-Sources/g2constructor.h index a711232332..856565f017 100644 --- a/Modelica/Resources/C-Sources/gconstructor.h +++ b/Modelica/Resources/C-Sources/g2constructor.h @@ -1,35 +1,50 @@ -/* gconstructor.h - Module constructor and destructor helper header +/* g2constructor.h - Module constructor and destructor helper header - If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and + If G2_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and destructors, in a sane way, including e.g. on library unload. If not you're on your own. Some compilers need #pragma to handle this, which does not work with macros, so the way you need to use this is (for constructors): - #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA - #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor) + #ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA + #pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(my_constructor)) #endif - G_DEFINE_CONSTRUCTOR(my_constructor) - static void my_constructor(void) { + G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(my_constructor)) + static void G2_FUNCNAME(my_constructor)(void) { ... } */ -#ifndef G_CONSTRUCTOR_H_ -#define G_CONSTRUCTOR_H_ +#ifndef G2_CONSTRUCTOR_H_ +#define G2_CONSTRUCTOR_H_ + +#ifndef G2_MODEL_PREFIX +#ifdef MODEL_IDENTIFIER +#define G2_MODEL_PREFIX MODEL_IDENTIFIER +#else +#define G2_MODEL_PREFIX +#endif +#endif + +#define G2_CONCAT(a, b) a ## b +#define G2_CONCAT_(a, b) G2_CONCAT(a, b) +#define G2_FUNCNAME(name) G2_CONCAT_(G2_MODEL_PREFIX, name) #if defined(__cplusplus) -#define G_HAS_CONSTRUCTORS 1 +#define G2_HAS_CONSTRUCTORS 1 + +#define G2_DEFINE_CONSTRUCTOR(_func) G2_CXX_CTOR(_func) +#define G2_DEFINE_DESTRUCTOR(_func) G2_CXX_DTOR(_func) -#define G_DEFINE_CONSTRUCTOR(_func) \ +#define G2_CXX_CTOR(_func) \ static void _func(void); \ struct _func ## _wrapper_struct { _func ## _wrapper_struct() { _func(); } }; \ static _func ## _wrapper_struct _func ## _wrapper; -#define G_DEFINE_DESTRUCTOR(_func) \ +#define G2_CXX_DTOR(_func) \ static void _func(void); \ struct _func ## _wrapper_struct2 { ~_func ## _wrapper_struct2() { _func(); } }; \ static _func ## _wrapper_struct2 _func ## _wrapper2; @@ -37,26 +52,26 @@ #elif (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))) || \ defined(__clang__) -#define G_HAS_CONSTRUCTORS 1 +#define G2_HAS_CONSTRUCTORS 1 -#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void); -#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void); +#define G2_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void); +#define G2_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void); #elif defined(_MSC_VER) && (_MSC_VER >= 1500) /* Visual Studio 2008 and later has _pragma */ -#define G_HAS_CONSTRUCTORS 1 +#define G2_HAS_CONSTRUCTORS 1 #ifdef _WIN64 -#define G_MSVC_SYMBOL_PREFIX "" +#define G2_MSVC_SYMBOL_PREFIX "" #else -#define G_MSVC_SYMBOL_PREFIX "_" +#define G2_MSVC_SYMBOL_PREFIX "_" #endif -#define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX) -#define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX) +#define G2_DEFINE_CONSTRUCTOR(_func) G2_MSVC_CTOR(_func, G2_MSVC_SYMBOL_PREFIX) +#define G2_DEFINE_DESTRUCTOR(_func) G2_MSVC_DTOR(_func, G2_MSVC_SYMBOL_PREFIX) -#define G_MSVC_CTOR(_func,_sym_prefix) \ +#define G2_MSVC_CTOR(_func, _sym_prefix) \ static void _func(void); \ extern int (* _array ## _func)(void); \ int _func ## _wrapper(void) { _func(); return _array ## _func == NULL; } \ @@ -64,7 +79,7 @@ __pragma(section(".CRT$XCU",read)) \ __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper; -#define G_MSVC_DTOR(_func,_sym_prefix) \ +#define G2_MSVC_DTOR(_func, _sym_prefix) \ static void _func(void); \ extern int (* _array ## _func)(void); \ int _func ## _constructor(void) { atexit (_func); return _array ## _func == NULL; } \ @@ -74,22 +89,22 @@ #elif defined(_MSC_VER) && (_MSC_VER >= 1400) -#define G_HAS_CONSTRUCTORS 1 +#define G2_HAS_CONSTRUCTORS 1 /* Pre Visual Studio 2008 must use #pragma section */ -#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 -#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 +#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 +#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 -#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ +#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ section(".CRT$XCU",read) -#define G_DEFINE_CONSTRUCTOR(_func) \ +#define G2_DEFINE_CONSTRUCTOR(_func) \ static void _func(void); \ static int _func ## _wrapper(void) { _func(); return 0; } \ __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper; -#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ +#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ section(".CRT$XCU",read) -#define G_DEFINE_DESTRUCTOR(_func) \ +#define G2_DEFINE_DESTRUCTOR(_func) \ static void _func(void); \ static int _func ## _constructor(void) { atexit (_func); return 0; } \ __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor; @@ -100,19 +115,19 @@ * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c */ -#define G_HAS_CONSTRUCTORS 1 +#define G2_HAS_CONSTRUCTORS 1 -#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 -#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 +#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 +#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 -#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ +#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ init(_func) -#define G_DEFINE_CONSTRUCTOR(_func) \ +#define G2_DEFINE_CONSTRUCTOR(_func) \ static void _func(void); -#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ +#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ fini(_func) -#define G_DEFINE_DESTRUCTOR(_func) \ +#define G2_DEFINE_DESTRUCTOR(_func) \ static void _func(void); #else