]> www.ginac.de Git - cln.git/commitdiff
Replace CL_REQUIRE/CL_PROVIDE(cl_SV_ringelt) with portable code.
authorAlexei Sheplyakov <varg@theor.jinr.ru>
Thu, 21 Aug 2008 17:16:50 +0000 (21:16 +0400)
committerAlexei Sheplyakov <varg@theor.jinr.ru>
Wed, 27 Aug 2008 04:41:06 +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/SV_ringelt.h
src/vector/cl_SV_ringelt.cc
src/vector/cl_SV_ringelt_debug.cc

index d370afd814cac71f09fd8d01609b8811eca64d85..be05eff8a350a775fa9ae7b305bb226ed1d3647e 100644 (file)
@@ -44,7 +44,15 @@ inline cl_SV_ringelt::operator cl_heap_SV_ringelt* () const
 extern const cl_SV_ringelt cl_null_SV_ringelt;
 inline cl_SV_ringelt::cl_SV_ringelt ()
        : cl_SV<_cl_ring_element,cl_SV_any> ((cl_heap_SV_ringelt*) cl_null_SV_ringelt) {}
-CL_REQUIRE(cl_SV_ringelt)
+
+class cl_SV_ringelt_init_helper
+{
+       static int count;
+public:
+       cl_SV_ringelt_init_helper();
+       ~cl_SV_ringelt_init_helper();
+};
+static cl_SV_ringelt_init_helper cl_SV_ringelt_init_helper_instance;
 
 // Copy a simple vector.
 inline const cl_SV_ringelt copy (const cl_SV_ringelt& vector)
index b867b727cd896dca1cb379d8672afa97a4ff93af..7a30c6b48eb61b5f66c696adad7fa31d2911dd1f 100644 (file)
@@ -3,8 +3,6 @@
 // General includes.
 #include "cl_sysdep.h"
 
-CL_PROVIDE(cl_SV_ringelt)
-
 // Specification.
 #include "cln/SV_ringelt.h"
 
@@ -22,16 +20,22 @@ static void cl_svector_ringelt_destructor (cl_heap* pointer)
 #endif
 }
 
-cl_class cl_class_svector_ringelt = {
-       cl_svector_ringelt_destructor,
-       0
-};
+// XXX: this ought to be static const, but it would be impossible to
+// set the printing function (see cl_SV_ringelt_debug.cc)
+cl_class& cl_class_svector_ringelt()
+{
+       static cl_class instance = {
+               cl_svector_ringelt_destructor,
+               0
+       };
+       return instance;
+}
 
 cl_heap_SV_ringelt* cl_make_heap_SV_ringelt_uninit (uintC len)
 {
        var cl_heap_SV_ringelt* hv = (cl_heap_SV_ringelt*) malloc_hook(sizeof(cl_heap_SV_ringelt)+sizeof(_cl_ring_element)*len);
        hv->refcount = 1;
-       hv->type = &cl_class_svector_ringelt;
+       hv->type = &cl_class_svector_ringelt();
        new (&hv->v) cl_SV_inner<_cl_ring_element> (len);
        // Have to fill hv->v[i] (0 <= i < len) yourself.
        return hv;
@@ -41,7 +45,7 @@ cl_heap_SV_ringelt* cl_make_heap_SV_ringelt (uintC len)
 {
        var cl_heap_SV_ringelt* hv = (cl_heap_SV_ringelt*) malloc_hook(sizeof(cl_heap_SV_ringelt)+sizeof(_cl_ring_element)*len);
        hv->refcount = 1;
-       hv->type = &cl_class_svector_ringelt;
+       hv->type = &cl_class_svector_ringelt();
        new (&hv->v) cl_SV_inner<_cl_ring_element> (len);
        for (var uintC i = 0; i < len; i++)
                init1(_cl_ring_element, hv->v[i]) ();
@@ -49,8 +53,22 @@ cl_heap_SV_ringelt* cl_make_heap_SV_ringelt (uintC len)
 }
 
 // An empty vector.
-const cl_SV_ringelt cl_null_SV_ringelt = cl_SV_ringelt((uintC)0);
+const cl_SV_ringelt cl_null_SV_ringelt = cl_null_SV_ringelt;
+
+int cl_SV_ringelt_init_helper::count = 0;
+
+cl_SV_ringelt_init_helper::cl_SV_ringelt_init_helper()
+{
+       if (count++ == 0)
+               new ((void *)&cl_null_SV_ringelt) cl_SV_ringelt((uintC)0);
+}
+
+cl_SV_ringelt_init_helper::~cl_SV_ringelt_init_helper()
+{
+       if (--count == 0) {
+               // Nothing to clean up here
+       }
+}
 
 }  // namespace cln
 
-CL_PROVIDE_END(cl_SV_ringelt)
index 0133fc0747309d2c3f07709e88de58a97cb555c4..6c65cd95a7dc2fbfc6997658b421eb64a0e741d4 100644 (file)
@@ -21,7 +21,7 @@ static void dprint (cl_heap* pointer)
        cl_dprint_unknown(pointer);
 }
 AT_INITIALIZATION(dprint_SV_ringelt)
-{ cl_register_type_printer(cl_class_svector_ringelt,dprint); }
+{ extern cl_class& cl_class_svector_ringelt(); cl_class_svector_ringelt().dprint = dprint; }
 
 // This dummy links in this module when <cln/SV_ringelt.h> requires it.
 int cl_SV_ringelt_debug_module;