diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index ffb745742..f7aeb739b 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -14,12 +14,10 @@ namespace nix { -RegisterCommand::Commands * RegisterCommand::commands = nullptr; - nix::Commands RegisterCommand::getCommandsFor(const std::vector & prefix) { nix::Commands res; - for (auto & [name, command] : *RegisterCommand::commands) + for (auto & [name, command] : RegisterCommand::commands()) if (name.size() == prefix.size() + 1) { bool equal = true; for (size_t i = 0; i < prefix.size(); ++i) diff --git a/src/libcmd/include/nix/cmd/command.hh b/src/libcmd/include/nix/cmd/command.hh index 45739fdb2..9139594ac 100644 --- a/src/libcmd/include/nix/cmd/command.hh +++ b/src/libcmd/include/nix/cmd/command.hh @@ -285,13 +285,16 @@ struct StorePathCommand : public StorePathsCommand struct RegisterCommand { typedef std::map, std::function()>> Commands; - static Commands * commands; + + static Commands & commands() + { + static Commands commands; + return commands; + } RegisterCommand(std::vector && name, std::function()> command) { - if (!commands) - commands = new Commands; - commands->emplace(name, command); + commands().emplace(name, command); } static nix::Commands getCommandsFor(const std::vector & prefix); diff --git a/src/libcmd/include/nix/cmd/legacy.hh b/src/libcmd/include/nix/cmd/legacy.hh index 357500a4d..0c375a7d2 100644 --- a/src/libcmd/include/nix/cmd/legacy.hh +++ b/src/libcmd/include/nix/cmd/legacy.hh @@ -12,12 +12,15 @@ typedef std::function MainFunction; struct RegisterLegacyCommand { typedef std::map Commands; - static Commands * commands; + + static Commands & commands() { + static Commands commands; + return commands; + } RegisterLegacyCommand(const std::string & name, MainFunction fun) { - if (!commands) commands = new Commands; - (*commands)[name] = fun; + commands()[name] = fun; } }; diff --git a/src/libcmd/legacy.cc b/src/libcmd/legacy.cc deleted file mode 100644 index 69b066831..000000000 --- a/src/libcmd/legacy.cc +++ /dev/null @@ -1,7 +0,0 @@ -#include "nix/cmd/legacy.hh" - -namespace nix { - -RegisterLegacyCommand::Commands * RegisterLegacyCommand::commands = 0; - -} diff --git a/src/libcmd/meson.build b/src/libcmd/meson.build index 32f44697d..216d4df9c 100644 --- a/src/libcmd/meson.build +++ b/src/libcmd/meson.build @@ -71,7 +71,6 @@ sources = files( 'installable-flake.cc', 'installable-value.cc', 'installables.cc', - 'legacy.cc', 'markdown.cc', 'misc-store-flags.cc', 'network-proxy.cc', diff --git a/src/libexpr/include/nix/expr/primops.hh b/src/libexpr/include/nix/expr/primops.hh index f0742a138..0b4ecdd50 100644 --- a/src/libexpr/include/nix/expr/primops.hh +++ b/src/libexpr/include/nix/expr/primops.hh @@ -27,7 +27,12 @@ constexpr size_t conservativeStackReservation = 16; struct RegisterPrimOp { typedef std::vector PrimOps; - static PrimOps * primOps; + + static PrimOps & primOps() + { + static PrimOps primOps; + return primOps; + } /** * You can register a constant by passing an arity of 0. fun diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index b7b027fba..535a9a501 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -4713,13 +4713,9 @@ static RegisterPrimOp primop_splitVersion({ *************************************************************/ -RegisterPrimOp::PrimOps * RegisterPrimOp::primOps; - - RegisterPrimOp::RegisterPrimOp(PrimOp && primOp) { - if (!primOps) primOps = new PrimOps; - primOps->push_back(std::move(primOp)); + primOps().push_back(std::move(primOp)); } @@ -4973,14 +4969,12 @@ void EvalState::createBaseEnv(const EvalSettings & evalSettings) )", }); - if (RegisterPrimOp::primOps) - for (auto & primOp : *RegisterPrimOp::primOps) - if (experimentalFeatureSettings.isEnabled(primOp.experimentalFeature)) - { - auto primOpAdjusted = primOp; - primOpAdjusted.arity = std::max(primOp.args.size(), primOp.arity); - addPrimOp(std::move(primOpAdjusted)); - } + for (auto & primOp : RegisterPrimOp::primOps()) + if (experimentalFeatureSettings.isEnabled(primOp.experimentalFeature)) { + auto primOpAdjusted = primOp; + primOpAdjusted.arity = std::max(primOp.args.size(), primOp.arity); + addPrimOp(std::move(primOpAdjusted)); + } for (auto & primOp : evalSettings.extraPrimOps) { auto primOpAdjusted = primOp; diff --git a/src/libfetchers/fetchers.cc b/src/libfetchers/fetchers.cc index 3ae45dcf8..e0161dee8 100644 --- a/src/libfetchers/fetchers.cc +++ b/src/libfetchers/fetchers.cc @@ -12,24 +12,26 @@ namespace nix::fetchers { using InputSchemeMap = std::map>; -std::unique_ptr inputSchemes = nullptr; +static InputSchemeMap & inputSchemes() +{ + static InputSchemeMap inputSchemeMap; + return inputSchemeMap; +} void registerInputScheme(std::shared_ptr && inputScheme) { - if (!inputSchemes) - inputSchemes = std::make_unique(); auto schemeName = inputScheme->schemeName(); - if (inputSchemes->count(schemeName) > 0) + if (!inputSchemes().emplace(schemeName, std::move(inputScheme)).second) throw Error("Input scheme with name %s already registered", schemeName); - inputSchemes->insert_or_assign(schemeName, std::move(inputScheme)); } -nlohmann::json dumpRegisterInputSchemeInfo() { +nlohmann::json dumpRegisterInputSchemeInfo() +{ using nlohmann::json; auto res = json::object(); - for (auto & [name, scheme] : *inputSchemes) { + for (auto & [name, scheme] : inputSchemes()) { auto & r = res[name] = json::object(); r["allowedAttrs"] = scheme->allowedAttrs(); } @@ -57,7 +59,7 @@ Input Input::fromURL( const Settings & settings, const ParsedURL & url, bool requireTree) { - for (auto & [_, inputScheme] : *inputSchemes) { + for (auto & [_, inputScheme] : inputSchemes()) { auto res = inputScheme->inputFromURL(settings, url, requireTree); if (res) { experimentalFeatureSettings.require(inputScheme->experimentalFeature()); @@ -91,8 +93,8 @@ Input Input::fromAttrs(const Settings & settings, Attrs && attrs) }; std::shared_ptr inputScheme = ({ - auto i = inputSchemes->find(schemeName); - i == inputSchemes->end() ? nullptr : i->second; + auto i = get(inputSchemes(), schemeName); + i ? *i : nullptr; }); if (!inputScheme) return raw(); diff --git a/src/libstore/include/nix/store/store-api.hh b/src/libstore/include/nix/store/store-api.hh index d5e3d5214..fa1873502 100644 --- a/src/libstore/include/nix/store/store-api.hh +++ b/src/libstore/include/nix/store/store-api.hh @@ -902,12 +902,11 @@ struct StoreFactory struct Implementations { - static std::vector * registered; + static std::vector & registered(); template static void add() { - if (!registered) registered = new std::vector(); StoreFactory factory{ .uriSchemes = TConfig::uriSchemes(), .create = @@ -919,7 +918,7 @@ struct Implementations -> std::shared_ptr { return std::make_shared(StringMap({})); }) }; - registered->push_back(factory); + registered().push_back(factory); } }; diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index e9e982e61..7e5ce6e2e 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -1355,7 +1355,7 @@ ref openStore(StoreReference && storeURI) return std::make_shared(params); }, [&](const StoreReference::Specified & g) { - for (const auto & implem : *Implementations::registered) + for (const auto & implem : Implementations::registered()) if (implem.uriSchemes.count(g.scheme)) return implem.create(g.scheme, g.authority, params); @@ -1399,6 +1399,10 @@ std::list> getDefaultSubstituters() return stores; } -std::vector * Implementations::registered = 0; +std::vector & Implementations::registered() +{ + static std::vector registered; + return registered; +} } diff --git a/src/libutil/config-global.cc b/src/libutil/config-global.cc index 10d176c51..94d715443 100644 --- a/src/libutil/config-global.cc +++ b/src/libutil/config-global.cc @@ -6,7 +6,7 @@ namespace nix { bool GlobalConfig::set(const std::string & name, const std::string & value) { - for (auto & config : *configRegistrations) + for (auto & config : configRegistrations()) if (config->set(name, value)) return true; @@ -17,20 +17,20 @@ bool GlobalConfig::set(const std::string & name, const std::string & value) void GlobalConfig::getSettings(std::map & res, bool overriddenOnly) { - for (auto & config : *configRegistrations) + for (auto & config : configRegistrations()) config->getSettings(res, overriddenOnly); } void GlobalConfig::resetOverridden() { - for (auto & config : *configRegistrations) + for (auto & config : configRegistrations()) config->resetOverridden(); } nlohmann::json GlobalConfig::toJSON() { auto res = nlohmann::json::object(); - for (const auto & config : *configRegistrations) + for (const auto & config : configRegistrations()) res.update(config->toJSON()); return res; } @@ -47,19 +47,15 @@ std::string GlobalConfig::toKeyValue() void GlobalConfig::convertToArgs(Args & args, const std::string & category) { - for (auto & config : *configRegistrations) + for (auto & config : configRegistrations()) config->convertToArgs(args, category); } GlobalConfig globalConfig; -GlobalConfig::ConfigRegistrations * GlobalConfig::configRegistrations; - GlobalConfig::Register::Register(Config * config) { - if (!configRegistrations) - configRegistrations = new ConfigRegistrations; - configRegistrations->emplace_back(config); + configRegistrations().emplace_back(config); } ExperimentalFeatureSettings experimentalFeatureSettings; diff --git a/src/libutil/include/nix/util/config-global.hh b/src/libutil/include/nix/util/config-global.hh index b47ee0ad1..44f89e06d 100644 --- a/src/libutil/include/nix/util/config-global.hh +++ b/src/libutil/include/nix/util/config-global.hh @@ -8,7 +8,12 @@ namespace nix { struct GlobalConfig : public AbstractConfig { typedef std::vector ConfigRegistrations; - static ConfigRegistrations * configRegistrations; + + static ConfigRegistrations & configRegistrations() + { + static ConfigRegistrations configRegistrations; + return configRegistrations; + } bool set(const std::string & name, const std::string & value) override; diff --git a/src/nix/main.cc b/src/nix/main.cc index f229ba2a4..351fcd1b6 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -193,7 +193,7 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs, virtual RootArgs res["args"] = toJSON(); auto stores = nlohmann::json::object(); - for (auto & implem : *Implementations::registered) { + for (auto & implem : Implementations::registered()) { auto storeConfig = implem.getConfig(); auto storeName = storeConfig->name(); auto & j = stores[storeName]; @@ -373,7 +373,7 @@ void mainWrapped(int argc, char * * argv) } { - auto legacy = (*RegisterLegacyCommand::commands)[programName]; + auto legacy = RegisterLegacyCommand::commands()[programName]; if (legacy) return legacy(argc, argv); }