]> www.ginac.de Git - cln.git/commitdiff
Replace CL_REQUIRE/CL_PROVIDE(cl_DF_globals) with portable code.
authorAlexei Sheplyakov <varg@theor.jinr.ru>
Thu, 21 Aug 2008 11:24:30 +0000 (15:24 +0400)
committerAlexei Sheplyakov <varg@theor.jinr.ru>
Wed, 27 Aug 2008 04:41:04 +0000 (08:41 +0400)
The order of initialization of non-local objects in different compilation units
is not specified in C++. Hence special care should be taken to avoid static
initialization order fiasco. CLN solved the problem with some evil (GCC
specific, and even GCC-version-specific) hack. Replace it with a technique
similar to one used in STL to initialize std::cout and friends.

include/cln/dfloat_class.h
src/float/dfloat/elem/cl_DF_globals.cc

index c189f3905ca150f41347aa9f5168d644cc91b4a2..9d0b9c74e7f49e5108b7d1c06c944038d0b88412 100644 (file)
@@ -52,7 +52,15 @@ inline cl_DF::operator struct cl_heap_dfloat * () const
 extern const cl_DF cl_DF_0;
 inline cl_DF::cl_DF ()
        : cl_F ((cl_private_thing) (struct cl_heap_dfloat *) cl_DF_0) {}
 extern const cl_DF cl_DF_0;
 inline cl_DF::cl_DF ()
        : cl_F ((cl_private_thing) (struct cl_heap_dfloat *) cl_DF_0) {}
-CL_REQUIRE(cl_DF_globals)
+class cl_DF_globals_init_helper
+{
+       static int count;
+public:
+       cl_DF_globals_init_helper();
+       ~cl_DF_globals_init_helper();
+};
+static cl_DF_globals_init_helper cl_DF_globals_init_helper_instance;
+
 #if 0 // see cl_DF.h
 inline cl_DF::cl_DF (struct cl_heap_dfloat * ptr)
        : cl_F ((cl_private_thing) ptr) {}
 #if 0 // see cl_DF.h
 inline cl_DF::cl_DF (struct cl_heap_dfloat * ptr)
        : cl_F ((cl_private_thing) ptr) {}
index 985299cf8f58cbe647cd0ce3a2f6a20febd24d14..b959c76183a986fe70f398f71dd3a92dbc1d69f8 100644 (file)
@@ -3,8 +3,6 @@
 // General includes.
 #include "cl_sysdep.h"
 
 // General includes.
 #include "cl_sysdep.h"
 
-CL_PROVIDE(cl_DF_globals)
-
 // Specification.
 #include "cl_DF.h"
 
 // Specification.
 #include "cl_DF.h"
 
@@ -13,24 +11,32 @@ CL_PROVIDE(cl_DF_globals)
 
 namespace cln {
 
 
 namespace cln {
 
-#if (cl_word_size==64)
-
-const cl_DF cl_DF_0 = allocate_dfloat(0); // 0.0d0
+const cl_DF cl_DF_0 = cl_DF_0;
+const cl_DF cl_DF_1 = cl_DF_1;
+const cl_DF cl_DF_minus1 = cl_DF_minus1;
 
 
-const cl_DF cl_DF_1 = encode_DF(0,1,bit(DF_mant_len)); // 1.0d0
-
-const cl_DF cl_DF_minus1 = encode_DF(-1,1,bit(DF_mant_len)); // -1.0d0
+int cl_DF_globals_init_helper::count = 0;
 
 
+cl_DF_globals_init_helper::cl_DF_globals_init_helper()
+{
+       if (count++ == 0) {
+#if (cl_word_size == 64)
+               new ((void *)&cl_DF_0) cl_DF(allocate_dfloat(0)); // 0.0d0
+               new ((void *)&cl_DF_1) cl_DF(encode_DF(0, 1, bit(DF_mant_len))); // 1.0d0
+               new ((void *)&cl_DF_minus1) cl_DF(encode_DF(-1,1,bit(DF_mant_len))); // -1.0d0
 #else
 #else
-
-const cl_DF cl_DF_0 = allocate_dfloat(0,0); // 0.0d0
-
-const cl_DF cl_DF_1 = encode_DF(0,1,bit(DF_mant_len-32),0); // 1.0d0
-
-const cl_DF cl_DF_minus1 = encode_DF(-1,1,bit(DF_mant_len-32),0); // -1.0d0
-
+               new ((void *)&cl_DF_0) cl_DF(allocate_dfloat(0, 0)); // 0.0d0
+               new ((void *)&cl_DF_1) cl_DF(encode_DF(0, 1, bit(DF_mant_len - 32), 0)); // 1.0d0
+               new ((void *)&cl_DF_minus1) cl_DF(encode_DF(-1, 1, bit(DF_mant_len - 32), 0)); // -1.0d0
 #endif
 #endif
+       }
+}
+cl_DF_globals_init_helper::~cl_DF_globals_init_helper()
+{
+       if (--count == 0) {
+               // Nothing to clean up
+       }
+}
 
 }  // namespace cln
 
 
 }  // namespace cln
 
-CL_PROVIDE_END(cl_DF_globals)