[PATCH 09/10] Make the initialization of Milena wrappers deterministic.

* src/wrappers/milena.cc (dyn::mln::init_type): Remove class. Replace it by... * src/wrappers/milena.hh, src/wrappers/milena.cc (dyn::mln::initialize): ...this (new) function. * test/wrappers/test-milena.cc (main): Call dyn::mln::initialize() before calling wrappers. --- dynamic-use-of-static-c++/ChangeLog | 11 +++++++ dynamic-use-of-static-c++/src/wrappers/milena.cc | 32 ++++--------------- dynamic-use-of-static-c++/src/wrappers/milena.hh | 21 +++++++++++++ .../test/wrappers/test-milena.cc | 2 + 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/dynamic-use-of-static-c++/ChangeLog b/dynamic-use-of-static-c++/ChangeLog index 400c9bb..dfbb444 100644 --- a/dynamic-use-of-static-c++/ChangeLog +++ b/dynamic-use-of-static-c++/ChangeLog @@ -1,5 +1,16 @@ 2009-11-03 Roland Levillain <roland@lrde.epita.fr> + Make the initialization of Milena wrappers deterministic. + + * src/wrappers/milena.cc (dyn::mln::init_type): Remove class. + Replace it by... + * src/wrappers/milena.hh, src/wrappers/milena.cc + (dyn::mln::initialize): ...this (new) function. + * test/wrappers/test-milena.cc (main): Call dyn::mln::initialize() + before calling wrappers. + +2009-11-03 Roland Levillain <roland@lrde.epita.fr> + Help Mac OS X's Mach-O dynamic linker (dyld) find libdynmilena. * swig/run.in: Remove symlink, and replace it by an actual copy of diff --git a/dynamic-use-of-static-c++/src/wrappers/milena.cc b/dynamic-use-of-static-c++/src/wrappers/milena.cc index 3ad7a95..9baf0e7 100644 --- a/dynamic-use-of-static-c++/src/wrappers/milena.cc +++ b/dynamic-use-of-static-c++/src/wrappers/milena.cc @@ -41,32 +41,14 @@ namespace dyn | Initialization. | `-----------------*/ - /* This initialization relies on ctors of the fact that - constructors of global objects called before `main', or more - precisely: - - [...] the constructor of a global object is called either - before the first statement of main or may be deferred until - after the first statement of main but before the first use of - any object or function in the same translation unit as the - global object is defined in. - - according to this page: - http://en.allexperts.com/q/C-1040/Constructors-Global-Object.htm. */ - - struct init_type + void initialize() { - init_type() - { - dyn::include_dir(MILENA_DIR); - dyn::include("mln/core/image/image2d.hh"); - dyn::include("mln/data/fill.hh"); - dyn::include("mln/debug/iota.hh"); - dyn::include("mln/debug/println.hh"); - } - }; - - init_type init; + dyn::include_dir(MILENA_DIR); + dyn::include("mln/core/image/image2d.hh"); + dyn::include("mln/data/fill.hh"); + dyn::include("mln/debug/iota.hh"); + dyn::include("mln/debug/println.hh"); + } /*-------------------------. diff --git a/dynamic-use-of-static-c++/src/wrappers/milena.hh b/dynamic-use-of-static-c++/src/wrappers/milena.hh index b0ba1ed..ed4b702 100644 --- a/dynamic-use-of-static-c++/src/wrappers/milena.hh +++ b/dynamic-use-of-static-c++/src/wrappers/milena.hh @@ -40,6 +40,27 @@ namespace dyn namespace mln { + /*-----------------. + | Initialization. | + `-----------------*/ + + /* FIXME: This is not really elegant, but this is a lot safer than + the previous approach relying on the (implementation-defined) + order of initialization of global objects' ctors. We can + probably improve this by reworking dyn::function_loader. See + also hints and advice from + http://en.allexperts.com/q/C-1040/Constructors-Global-Object.htm. */ + + // This function *must* be called prior to any call to the wrapped + // routines below in order to find the headers of the functions + // they wrap. + void initialize(); + + + /*-------------------------. + | A few wrapped routines. | + `-------------------------*/ + extern dyn::language::ctor mk_image2d_int; extern dyn::language::fun fill; diff --git a/dynamic-use-of-static-c++/test/wrappers/test-milena.cc b/dynamic-use-of-static-c++/test/wrappers/test-milena.cc index d24d216..831c9db 100644 --- a/dynamic-use-of-static-c++/test/wrappers/test-milena.cc +++ b/dynamic-use-of-static-c++/test/wrappers/test-milena.cc @@ -30,6 +30,8 @@ int main() { + dyn::mln::initialize(); + dyn::language::var ima = dyn::mln::mk_image2d_int(3, 3); dyn::mln::fill(ima, 0); dyn::mln::println("ima (before) =", ima); -- 1.6.5
participants (1)
-
Roland Levillain