From 22799ef1cd61de1183b85572815affba7d03cf08 Mon Sep 17 00:00:00 2001 From: Alexei Sheplyakov Date: Thu, 21 Aug 2008 19:51:59 +0400 Subject: [PATCH] Replace CL_REQUIRE/CL_PROVIDE(cl_UP_no_ring) with portable code. 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/univpoly.h | 10 +++++++++- src/polynomial/elem/cl_UP_no_ring.cc | 29 ++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/include/cln/univpoly.h b/include/cln/univpoly.h index d716da4..f7c655d 100644 --- a/include/cln/univpoly.h +++ b/include/cln/univpoly.h @@ -449,7 +449,15 @@ extern const cl_UP deriv (const cl_UP& x); extern const cl_univpoly_ring cl_no_univpoly_ring; extern cl_class cl_class_no_univpoly_ring; -CL_REQUIRE(cl_UP_no_ring) + +class cl_UP_no_ring_init_helper +{ + static int count; +public: + cl_UP_no_ring_init_helper(); + ~cl_UP_no_ring_init_helper(); +}; +static cl_UP_no_ring_init_helper cl_UP_no_ring_init_helper_instance; inline cl_univpoly_ring::cl_univpoly_ring () : cl_ring (as_cl_private_thing(cl_no_univpoly_ring)) {} diff --git a/src/polynomial/elem/cl_UP_no_ring.cc b/src/polynomial/elem/cl_UP_no_ring.cc index 53aaf2c..892f963 100644 --- a/src/polynomial/elem/cl_UP_no_ring.cc +++ b/src/polynomial/elem/cl_UP_no_ring.cc @@ -3,8 +3,6 @@ // General includes. #include "cl_sysdep.h" -CL_PROVIDE(cl_UP_no_ring) - // Specification. #include "cln/univpoly.h" @@ -174,13 +172,28 @@ static void cl_no_univpoly_ring_destructor (cl_heap* pointer) (*(cl_heap_no_univpoly_ring*)pointer).~cl_heap_no_univpoly_ring(); } -cl_class cl_class_no_univpoly_ring = { - cl_no_univpoly_ring_destructor, - 0 -}; +cl_class cl_class_no_univpoly_ring; +static cl_heap_no_univpoly_ring* cl_heap_no_univpoly_ring_instance; +const cl_univpoly_ring cl_no_univpoly_ring = cl_no_univpoly_ring; + +int cl_UP_no_ring_init_helper::count = 0; + +cl_UP_no_ring_init_helper::cl_UP_no_ring_init_helper() +{ + if (count++ == 0) { + cl_class_no_univpoly_ring.destruct = cl_no_univpoly_ring_destructor; + cl_class_no_univpoly_ring.flags = 0; + cl_heap_no_univpoly_ring_instance = new cl_heap_no_univpoly_ring(); + new ((void *)&cl_no_univpoly_ring) cl_univpoly_ring(cl_heap_no_univpoly_ring_instance); + } +} -const cl_univpoly_ring cl_no_univpoly_ring = cl_univpoly_ring (new cl_heap_no_univpoly_ring()); +cl_UP_no_ring_init_helper::~cl_UP_no_ring_init_helper() +{ + if (--count == 0) { + delete cl_heap_no_univpoly_ring_instance; + } +} } // namespace cln -CL_PROVIDE_END(cl_UP_no_ring)