This is an automated email from the ASF dual-hosted git repository.

bneradt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git


The following commit(s) were added to refs/heads/master by this push:
     new a119c7e  Possible fix for the initialization issue with globals. (#5)
a119c7e is described below

commit a119c7e4e2e0752ee8a30f141bb604cc2981031b
Author: Damian Meden <[email protected]>
AuthorDate: Fri Jan 10 17:19:53 2025 +0100

    Possible fix for the initialization issue with globals. (#5)
    
    This is an attempt to fix some ordering creation issues for some global 
variables within lib swoc.
    The is to get swoc::bwf::Global_Names initialized first.
---
 code/include/swoc/bwf_base.h | 10 +++++-----
 code/src/bw_format.cc        |  6 +++++-
 unit_tests/ex_bw_format.cc   |  8 ++++----
 3 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/code/include/swoc/bwf_base.h b/code/include/swoc/bwf_base.h
index d5c426d..e76ccc7 100644
--- a/code/include/swoc/bwf_base.h
+++ b/code/include/swoc/bwf_base.h
@@ -467,7 +467,7 @@ protected:
  * This nameset is used if no other is provided. Therefore bindings added to 
this nameset will be
  * available in the default formatting use.
  */
-extern ExternalNames Global_Names;
+extern ExternalNames& Global_Names();
 
 // --------------- Implementation --------------------
 /// --- Spec ---
@@ -919,25 +919,25 @@ BufferWriter::print_nfv(Binding &&names, Extractor &&ex, 
bwf::ArgPack const &arg
 template <typename... Args>
 BufferWriter &
 BufferWriter::print(const TextView &fmt, Args &&...args) {
-  return this->print_nfv(bwf::Global_Names.bind(), bwf::Format::bind(fmt), 
bwf::ArgTuple{std::forward_as_tuple(args...)});
+  return this->print_nfv(bwf::Global_Names().bind(), bwf::Format::bind(fmt), 
bwf::ArgTuple{std::forward_as_tuple(args...)});
 }
 
 template <typename... Args>
 BufferWriter &
 BufferWriter::print(bwf::Format const &fmt, Args &&...args) {
-  return this->print_nfv(bwf::Global_Names.bind(), fmt.bind(), 
bwf::ArgTuple{std::forward_as_tuple(args...)});
+  return this->print_nfv(bwf::Global_Names().bind(), fmt.bind(), 
bwf::ArgTuple{std::forward_as_tuple(args...)});
 }
 
 template <typename... Args>
 BufferWriter &
 BufferWriter::print_v(TextView const &fmt, std::tuple<Args...> const &args) {
-  return this->print_nfv(bwf::Global_Names.bind(), bwf::Format::bind(fmt), 
bwf::ArgTuple{args});
+  return this->print_nfv(bwf::Global_Names().bind(), bwf::Format::bind(fmt), 
bwf::ArgTuple{args});
 }
 
 template <typename... Args>
 BufferWriter &
 BufferWriter::print_v(const bwf::Format &fmt, const std::tuple<Args...> &args) 
{
-  return this->print_nfv(bwf::Global_Names.bind(), fmt.bind(), 
bwf::ArgTuple{args});
+  return this->print_nfv(bwf::Global_Names().bind(), fmt.bind(), 
bwf::ArgTuple{args});
 }
 
 template <typename Binding, typename Extractor>
diff --git a/code/src/bw_format.cc b/code/src/bw_format.cc
index 60ceacc..a5ccbb7 100644
--- a/code/src/bw_format.cc
+++ b/code/src/bw_format.cc
@@ -24,12 +24,16 @@
 using namespace std::literals;
 using namespace swoc::literals;
 
-swoc::bwf::ExternalNames swoc::bwf::Global_Names;
+
 using swoc::svto_radix;
 
 namespace swoc { inline namespace SWOC_VERSION_NS {
 
 namespace bwf {
+ExternalNames& Global_Names() {
+  static swoc::bwf::ExternalNames Global_Names;
+  return Global_Names;
+}
 
 const Spec Spec::DEFAULT;
 
diff --git a/unit_tests/ex_bw_format.cc b/unit_tests/ex_bw_format.cc
index fa33238..d26b70c 100644
--- a/unit_tests/ex_bw_format.cc
+++ b/unit_tests/ex_bw_format.cc
@@ -127,10 +127,10 @@ struct Context {
 
 void
 EX_BWF_Format_Init() {
-  swoc::bwf::Global_Names.assign("timestamp", &BWF_Timestamp);
-  swoc::bwf::Global_Names.assign("now", &BWF_Now);
-  swoc::bwf::Global_Names.assign("version", &BWF_Version);
-  swoc::bwf::Global_Names.assign("dave", &BWF_EvilDave);
+  swoc::bwf::Global_Names().assign("timestamp", &BWF_Timestamp);
+  swoc::bwf::Global_Names().assign("now", &BWF_Now);
+  swoc::bwf::Global_Names().assign("version", &BWF_Version);
+  swoc::bwf::Global_Names().assign("dave", &BWF_EvilDave);
 }
 
 // Work with external / global names.

Reply via email to