Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

namespace nix {

RegisterCommand::Commands * RegisterCommand::commands = nullptr;

nix::Commands RegisterCommand::getCommandsFor(const std::vector<std::string> & 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)
Expand Down
11 changes: 7 additions & 4 deletions src/libcmd/include/nix/cmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,16 @@ struct StorePathCommand : public StorePathsCommand
struct RegisterCommand
{
typedef std::map<std::vector<std::string>, std::function<ref<Command>()>> Commands;
static Commands * commands;

static Commands & commands()
{
static Commands commands;
return commands;
}

RegisterCommand(std::vector<std::string> && name, std::function<ref<Command>()> command)
{
if (!commands)
commands = new Commands;
commands->emplace(name, command);
commands().emplace(name, command);
}

static nix::Commands getCommandsFor(const std::vector<std::string> & prefix);
Expand Down
9 changes: 6 additions & 3 deletions src/libcmd/include/nix/cmd/legacy.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ typedef std::function<void(int, char * *)> MainFunction;
struct RegisterLegacyCommand
{
typedef std::map<std::string, MainFunction> 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;
}
};

Expand Down
7 changes: 0 additions & 7 deletions src/libcmd/legacy.cc

This file was deleted.

1 change: 0 additions & 1 deletion src/libcmd/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 6 additions & 1 deletion src/libexpr/include/nix/expr/primops.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ constexpr size_t conservativeStackReservation = 16;
struct RegisterPrimOp
{
typedef std::vector<PrimOp> PrimOps;
static PrimOps * primOps;

static PrimOps & primOps()
{
static PrimOps primOps;
return primOps;
}

/**
* You can register a constant by passing an arity of 0. fun
Expand Down
20 changes: 7 additions & 13 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down Expand Up @@ -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;
Expand Down
22 changes: 12 additions & 10 deletions src/libfetchers/fetchers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ namespace nix::fetchers {

using InputSchemeMap = std::map<std::string_view, std::shared_ptr<InputScheme>>;

std::unique_ptr<InputSchemeMap> inputSchemes = nullptr;
static InputSchemeMap & inputSchemes()
{
static InputSchemeMap inputSchemeMap;
return inputSchemeMap;
}

void registerInputScheme(std::shared_ptr<InputScheme> && inputScheme)
{
if (!inputSchemes)
inputSchemes = std::make_unique<InputSchemeMap>();
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();
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -91,8 +93,8 @@ Input Input::fromAttrs(const Settings & settings, Attrs && attrs)
};

std::shared_ptr<InputScheme> inputScheme = ({
auto i = inputSchemes->find(schemeName);
i == inputSchemes->end() ? nullptr : i->second;
auto i = get(inputSchemes(), schemeName);
i ? *i : nullptr;
});

if (!inputScheme) return raw();
Expand Down
5 changes: 2 additions & 3 deletions src/libstore/include/nix/store/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -902,12 +902,11 @@ struct StoreFactory

struct Implementations
{
static std::vector<StoreFactory> * registered;
static std::vector<StoreFactory> & registered();

template<typename T, typename TConfig>
static void add()
{
if (!registered) registered = new std::vector<StoreFactory>();
StoreFactory factory{
.uriSchemes = TConfig::uriSchemes(),
.create =
Expand All @@ -919,7 +918,7 @@ struct Implementations
-> std::shared_ptr<StoreConfig>
{ return std::make_shared<TConfig>(StringMap({})); })
};
registered->push_back(factory);
registered().push_back(factory);
}
};

Expand Down
8 changes: 6 additions & 2 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ ref<Store> openStore(StoreReference && storeURI)
return std::make_shared<LocalStore>(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);

Expand Down Expand Up @@ -1399,6 +1399,10 @@ std::list<ref<Store>> getDefaultSubstituters()
return stores;
}

std::vector<StoreFactory> * Implementations::registered = 0;
std::vector<StoreFactory> & Implementations::registered()
{
static std::vector<StoreFactory> registered;
return registered;
}

}
16 changes: 6 additions & 10 deletions src/libutil/config-global.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,20 +17,20 @@ bool GlobalConfig::set(const std::string & name, const std::string & value)

void GlobalConfig::getSettings(std::map<std::string, SettingInfo> & 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;
}
Expand All @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/libutil/include/nix/util/config-global.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ namespace nix {
struct GlobalConfig : public AbstractConfig
{
typedef std::vector<Config *> ConfigRegistrations;
static ConfigRegistrations * configRegistrations;

static ConfigRegistrations & configRegistrations()
{
static ConfigRegistrations configRegistrations;
return configRegistrations;
}

bool set(const std::string & name, const std::string & value) override;

Expand Down
4 changes: 2 additions & 2 deletions src/nix/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}

Expand Down
Loading