]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Context struct added, returned by initialize()
authorThomas Otto <thomas.otto@pdv-fs.de>
Sun, 19 Jan 2020 17:53:41 +0000 (18:53 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 10 Feb 2020 20:08:27 +0000 (21:08 +0100)
The goal is to eventually move almost all global state into Context.

Makefile.in
src/Context.cpp [new file with mode: 0644]
src/Context.hpp [new file with mode: 0644]
src/ccache.cpp
src/exitfn.cpp
src/exitfn.hpp

index 2d0e367eee28e4f3c55ca4273ed1e1d8d51ed10e..cf2c81fbe204709a391d265c8872f5cf72336686 100644 (file)
@@ -39,6 +39,7 @@ non_third_party_sources = \
     src/Compression.cpp \
     src/Compressor.cpp \
     src/Config.cpp \
+    src/Context.cpp \
     src/Decompressor.cpp \
     src/NullCompressor.cpp \
     src/NullDecompressor.cpp \
diff --git a/src/Context.cpp b/src/Context.cpp
new file mode 100644 (file)
index 0000000..27ab9c1
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright (C) 2020 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "Context.hpp"
+
+Context::~Context()
+{
+}
diff --git a/src/Context.hpp b/src/Context.hpp
new file mode 100644 (file)
index 0000000..a3ff59b
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright (C) 2020 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#pragma once
+
+#include "system.hpp"
+
+#include "ArgsInfo.hpp"
+#include "NonCopyable.hpp"
+
+struct Context : NonCopyable
+{
+  Context() = default;
+  ~Context();
+};
index 8ba339a7e4ecd7aa0dbe1482ece007be632ba1a4..30017d1cebef2335b4de62092b07442ab0ae3b5b 100644 (file)
@@ -20,6 +20,7 @@
 #include "ccache.hpp"
 
 #include "ArgsInfo.hpp"
+#include "Context.hpp"
 #include "FormatNonstdStringView.hpp"
 #include "ProgressBar.hpp"
 #include "ScopeGuard.hpp"
@@ -3476,25 +3477,29 @@ set_up_config(Config& config)
 }
 
 // Initialize ccache, must be called once before anything else is run.
-static void
+static Context&
 initialize()
 {
-  bool enable_internal_trace = getenv("CCACHE_INTERNAL_TRACE");
-  if (enable_internal_trace) {
-#ifdef MTR_ENABLED
-    // We don't have any conf yet, so we can't use temp_dir() here.
-    trace_init(format("%s/tmp.ccache-trace.%d", tmpdir(), (int)getpid()));
-#endif
-  }
+  // This object is placed onto the heap so it is available in exit functions
+  // which run after main(). It is cleaned up by the last exit function.
+  Context* ctx = new Context{};
 
   set_up_config(g_config);
 
   init_log(g_config);
 
   exitfn_init();
+  exitfn_delete_context(ctx);
   exitfn_add_nullary(stats_flush);
   exitfn_add_nullary(clean_up_pending_tmp_files);
 
+  bool enable_internal_trace = getenv("CCACHE_INTERNAL_TRACE");
+  if (enable_internal_trace) {
+#ifdef MTR_ENABLED
+    // We don't have any conf yet, so we can't use temp_dir() here.
+    trace_init(format("%s/tmp.ccache-trace.%d", tmpdir(), (int)getpid()));
+#endif
+  }
 
   cc_log("=== CCACHE %s STARTED =========================================",
          CCACHE_VERSION);
@@ -3507,6 +3512,8 @@ initialize()
     cc_log("Error: tracing is not enabled!");
 #endif
   }
+
+  return *ctx;
 }
 
 template<class T>
@@ -3613,7 +3620,8 @@ cache_compilation(int argc, char* argv[])
 
   orig_args = args_init(argc, argv);
 
-  initialize();
+  Context& ctx = initialize();
+  (void)ctx;
 
   MTR_BEGIN("main", "find_compiler");
   find_compiler(argv);
@@ -3903,7 +3911,8 @@ handle_main_options(int argc, char* argv[])
     {"zero-stats", no_argument, 0, 'z'},
     {0, 0, 0, 0}};
 
-  initialize();
+  Context& ctx = initialize();
+  (void)ctx;
 
   int c;
   while ((c = getopt_long(argc, argv, "cCk:hF:M:po:sVxX:z", options, NULL))
index 5f5d137580cdd46094c02374cb4715f88e1352cf..612a84ac033e7278a9783ba4e34651c4229d0d88 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "exitfn.hpp"
 
+#include "Context.hpp"
 #include "legacy_util.hpp"
 #include "logging.hpp"
 
@@ -34,6 +35,7 @@ struct nullary_exit_function
 };
 
 static struct exit_function* exit_functions;
+static struct Context* context_to_clean_up;
 
 static void
 call_nullary_exit_function(void* context)
@@ -92,6 +94,15 @@ exitfn_add_last(void (*function)(void*), void* context)
   *q = p;
 }
 
+// Remember a Context pointer to delete after all exit functions have run.
+// This function can only be called once.
+void
+exitfn_delete_context(Context* ctx)
+{
+  assert(context_to_clean_up == nullptr);
+  context_to_clean_up = ctx;
+}
+
 // Call added functions.
 void
 exitfn_call(void)
@@ -104,4 +115,7 @@ exitfn_call(void)
     p = p->next;
     free(q);
   }
+  if (context_to_clean_up) {
+    delete context_to_clean_up;
+  }
 }
index c8ad8383acba460e6c506c3c19bc7babfe78c0ac..15c136f5b9bcbc6367409ca0f741d6c6d5cd0578 100644 (file)
 
 #pragma once
 
+struct Context;
+
 void exitfn_init();
 void exitfn_add_nullary(void (*function)());
 void exitfn_add(void (*function)(void*), void* context);
 void exitfn_add_last(void (*function)(void*), void* context);
+void exitfn_delete_context(Context* ctx);
 void exitfn_call();