]> www.ginac.de Git - cln.git/commitdiff
Replace CL_REQUIRE/CL_PROVIDE(cl_C_ring) with portable code.
authorAlexei Sheplyakov <varg@theor.jinr.ru>
Thu, 21 Aug 2008 11:18:34 +0000 (15:18 +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/complex_ring.h
include/cln/univpoly_complex.h
src/complex/ring/cl_C_ring.cc

index ea7ca98d8f795397ff1d55c380ecc6f8751cbab8..881867c1e390b6d592280c8a35b71a5b9e6e94f2 100644 (file)
@@ -11,7 +11,15 @@ namespace cln {
 typedef cl_specialized_number_ring<cl_N> cl_complex_ring;
 extern const cl_complex_ring cl_C_ring;                // math. C
 extern cl_class cl_class_complex_ring;
-//CL_REQUIRE(cl_C_ring)
+
+class cl_C_ring_init_helper
+{
+       static int count;
+public:
+       cl_C_ring_init_helper();
+       ~cl_C_ring_init_helper();
+};
+static cl_C_ring_init_helper cl_C_ring_init_helper_instance;
 
 }  // namespace cln
 
index 3970d9dfac9128a095f6145ac27725aba1b9dc37..eb2991a55f593b226f88695698fc42d3500a7e7f 100644 (file)
@@ -223,8 +223,6 @@ inline const cl_UP_N deriv (const cl_UP_N& x)
 
 #endif
 
-CL_REQUIRE(cl_C_ring)
-
 }  // namespace cln
 
 #endif /* _CL_UNIVPOLY_COMPLEX_H */
index 296971aaa327c64e34af28a6aae503723cb5f400..32d6ccfe44ea1e64bb0117d48a4bd34a679ac5c1 100644 (file)
@@ -3,8 +3,6 @@
 // General includes.
 #include "cl_sysdep.h"
 
-CL_PROVIDE(cl_C_ring)
-
 // Specification.
 #include "cln/complex_ring.h"
 
@@ -144,19 +142,34 @@ static void cl_complex_ring_dprint (cl_heap* pointer)
        fprint(cl_debugout, "(cl_complex_ring) cl_C_ring");
 }
 
-cl_class cl_class_complex_ring = {
-       cl_complex_ring_destructor,
-       cl_class_flags_number_ring,
-       cl_complex_ring_dprint
-};
+cl_class cl_class_complex_ring;
+static cl_heap_complex_ring* cl_heap_complex_ring_instance;
+const cl_complex_ring cl_C_ring = cl_C_ring;
 
 // Constructor.
 template <>
 inline cl_complex_ring::cl_specialized_number_ring ()
-       : cl_number_ring (new cl_heap_complex_ring()) {}
+       : cl_number_ring(cl_heap_complex_ring_instance) { }
+
+int cl_C_ring_init_helper::count = 0;
+
+cl_C_ring_init_helper::cl_C_ring_init_helper()
+{
+       if (count++ == 0) {
+               cl_class_complex_ring.destruct = cl_complex_ring_destructor;
+               cl_class_complex_ring.flags = cl_class_flags_number_ring;
+               cl_class_complex_ring.dprint = cl_complex_ring_dprint;
+               cl_heap_complex_ring_instance = new cl_heap_complex_ring();
+               new ((void *)&cl_C_ring) cl_complex_ring();
+       }
+}
 
-const cl_complex_ring cl_C_ring;
+cl_C_ring_init_helper::~cl_C_ring_init_helper()
+{
+       if (--count == 0) {
+               delete cl_heap_complex_ring_instance;
+       }
+}
 
 }  // namespace cln
 
-CL_PROVIDE_END(cl_C_ring)