Browse Source

Replace CL_REQUIRE/CL_PROVIDE(cl_LF_globals) 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.
master
Alexei Sheplyakov 16 years ago
parent
commit
7f619754e2
  1. 9
      include/cln/lfloat_class.h
  2. 19
      src/float/lfloat/elem/cl_LF_globals.cc

9
include/cln/lfloat_class.h

@ -53,7 +53,14 @@ inline cl_LF::operator struct cl_heap_lfloat * () const
extern const cl_LF cl_LF_0;
inline cl_LF::cl_LF ()
: cl_F ((cl_private_thing) (struct cl_heap_lfloat *) cl_LF_0) {}
CL_REQUIRE(cl_LF_globals)
class cl_LF_globals_init_helper
{
static int count;
public:
cl_LF_globals_init_helper();
~cl_LF_globals_init_helper();
};
static cl_LF_globals_init_helper cl_LF_globals_init_helper_instance;
#if 0 // see cl_LF_impl.h
inline cl_LF::cl_LF (struct cl_heap_lfloat * ptr)
: cl_F ((cl_private_thing) ptr) {}

19
src/float/lfloat/elem/cl_LF_globals.cc

@ -3,8 +3,6 @@
// General includes.
#include "cl_sysdep.h"
CL_PROVIDE(cl_LF_globals)
// Specification.
#include "cln/number.h"
@ -17,8 +15,21 @@ CL_PROVIDE(cl_LF_globals)
namespace cln {
// Only needed for the default constructor of cl_LF.
const cl_LF cl_LF_0 = encode_LF0(LF_minlen); // 0.0L0
const cl_LF cl_LF_0 = cl_LF_0; // 0.0L0
int cl_LF_globals_init_helper::count = 0;
cl_LF_globals_init_helper::cl_LF_globals_init_helper()
{
if (count++ == 0)
new ((void *)&cl_LF_0) cl_LF(encode_LF0(LF_minlen)); // 0.0L0
}
cl_LF_globals_init_helper::~cl_LF_globals_init_helper()
{
if (--count == 0) {
// Nothing to clean up
}
}
} // namespace cln
CL_PROVIDE_END(cl_LF_globals)
Loading…
Cancel
Save