]> www.ginac.de Git - cln.git/commitdiff
Replace CL_REQUIRE/CL_PROVIDE(cl_SV_number) with portable code.
authorAlexei Sheplyakov <varg@theor.jinr.ru>
Thu, 21 Aug 2008 17:18:11 +0000 (21:18 +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_number.h
src/vector/cl_SV_number.cc
src/vector/cl_SV_number_debug.cc

index 845142d631728e712d0480059fbec8f197b50c35..8233f2607391649d736206cd40848d03f9be2531 100644 (file)
@@ -43,7 +43,14 @@ inline cl_SV_number::operator cl_heap_SV_number* () const
 extern const cl_SV_number cl_null_SV_number;
 inline cl_SV_number::cl_SV_number ()
        : cl_SV<cl_number,cl_SV_any> ((cl_heap_SV_number*) cl_null_SV_number) {}
-CL_REQUIRE(cl_SV_number)
+class cl_SV_number_init_helper
+{
+       static int count;
+public:
+       cl_SV_number_init_helper();
+       ~cl_SV_number_init_helper();
+};
+static cl_SV_number_init_helper cl_SV_number_init_helper_instance;
 
 // Copy a simple vector.
 inline const cl_SV_number copy (const cl_SV_number& vector)
index 55801a493c8071122e52f9b95686f9e91c92b6ef..87e45c6366609b0984eb05c4a3666e625c111e28 100644 (file)
@@ -3,12 +3,9 @@
 // General includes.
 #include "cl_sysdep.h"
 
-CL_PROVIDE(cl_SV_number)
-
 // Specification.
 #include "cln/SV_number.h"
 
-
 // Implementation.
 
 namespace cln {
@@ -22,16 +19,22 @@ static void cl_svector_number_destructor (cl_heap* pointer)
 #endif
 }
 
-cl_class cl_class_svector_number = {
-       cl_svector_number_destructor,
-       0
-};
+// XXX: ugh, this needs to be non-static (and non-const) so redefining
+// debugging function is possible (see cl_SV_number_debug.cc) 
+cl_class& cl_class_svector_number()
+{
+       static cl_class instance = {
+               cl_svector_number_destructor,
+               0
+       };
+       return instance;
+}
 
 cl_heap_SV_number* cl_make_heap_SV_number_uninit (uintC len)
 {
        var cl_heap_SV_number* hv = (cl_heap_SV_number*) malloc_hook(sizeof(cl_heap_SV_number)+sizeof(cl_number)*len);
        hv->refcount = 1;
-       hv->type = &cl_class_svector_number;
+       hv->type = &cl_class_svector_number();
        new (&hv->v) cl_SV_inner<cl_number> (len);
        // Have to fill hv->v[i] (0 <= i < len) yourself.
        return hv;
@@ -41,7 +44,7 @@ cl_heap_SV_number* cl_make_heap_SV_number (uintC len)
 {
        var cl_heap_SV_number* hv = (cl_heap_SV_number*) malloc_hook(sizeof(cl_heap_SV_number)+sizeof(cl_number)*len);
        hv->refcount = 1;
-       hv->type = &cl_class_svector_number;
+       hv->type = &cl_class_svector_number();
        new (&hv->v) cl_SV_inner<cl_number> (len);
        for (var uintC i = 0; i < len; i++)
                init1(cl_number, hv->v[i]) (0);
@@ -49,8 +52,22 @@ cl_heap_SV_number* cl_make_heap_SV_number (uintC len)
 }
 
 // An empty vector.
-const cl_SV_number cl_null_SV_number = cl_SV_number((uintC)0);
+const cl_SV_number cl_null_SV_number = cl_null_SV_number;
+
+int cl_SV_number_init_helper::count = 0;
+
+cl_SV_number_init_helper::cl_SV_number_init_helper()
+{
+       if (count++ == 0)
+               new ((void *)&cl_null_SV_number) cl_SV_number((uintC)0);
+}
+
+cl_SV_number_init_helper::~cl_SV_number_init_helper()
+{
+       if (--count == 0) {
+               // Nothing to clean up
+       }
+}
 
 }  // namespace cln
 
-CL_PROVIDE_END(cl_SV_number)
index 16528911de5f6d06408590fb4451a25736a60b72..bd1aedee1b158a3903c87282b7308c3e61ee38da 100644 (file)
@@ -29,7 +29,7 @@ static void dprint (cl_heap* pointer)
        print_vector(cl_debugout,default_print_flags,&print_for_debug,obj);
 }
 AT_INITIALIZATION(dprint_SV_number)
-{ cl_register_type_printer(cl_class_svector_number,dprint); }
+{ extern cl_class& cl_class_svector_number(); cl_class_svector_number().dprint = dprint; }
 
 // This dummy links in this module when <cln/SV_number.h> requires it.
 int cl_SV_number_debug_module;