refactor(3p/nix/libexpr): state->allocBindings -> Bindings::NewGC

EvalState::allocBindings had little to do with Bindings, other than
returning them, and didn't belong in that class.
This commit is contained in:
Vincent Ambo 2020-05-22 16:48:30 +01:00
parent e24466c795
commit b3c9166b23
10 changed files with 14 additions and 15 deletions

View file

@ -49,10 +49,7 @@ void Bindings::merge(Bindings* other) {
attributes_.swap(other->attributes_); attributes_.swap(other->attributes_);
} }
// Allocate a new attribute set, making it visible to the garbage collector. Bindings* Bindings::NewGC() { return new (GC) Bindings; }
Bindings* EvalState::allocBindings(size_t _capacity) {
return new (GC) Bindings;
}
void EvalState::mkAttrs(Value& v, size_t capacity) { void EvalState::mkAttrs(Value& v, size_t capacity) {
if (capacity == 0) { if (capacity == 0) {
@ -61,7 +58,7 @@ void EvalState::mkAttrs(Value& v, size_t capacity) {
} }
clearValue(v); clearValue(v);
v.type = tAttrs; v.type = tAttrs;
v.attrs = new (GC) Bindings; v.attrs = Bindings::NewGC();
nrAttrsets++; nrAttrsets++;
nrAttrsInAttrsets += capacity; nrAttrsInAttrsets += capacity;
} }

View file

@ -33,6 +33,10 @@ class Bindings {
public: public:
typedef absl::btree_map<Symbol, Attr>::iterator iterator; typedef absl::btree_map<Symbol, Attr>::iterator iterator;
// Allocate a new attribute set that is visible to the garbage
// collector.
static Bindings* NewGC();
// Return the number of contained elements. // Return the number of contained elements.
size_t size(); size_t size();

View file

@ -33,7 +33,7 @@ MixEvalArgs::MixEvalArgs() {
} }
Bindings* MixEvalArgs::getAutoArgs(EvalState& state) { Bindings* MixEvalArgs::getAutoArgs(EvalState& state) {
Bindings* res = state.allocBindings(autoArgs.size()); Bindings* res = Bindings::NewGC();
for (auto& i : autoArgs) { for (auto& i : autoArgs) {
Value* v = state.allocValue(); Value* v = state.allocValue();
if (i.second[0] == 'E') { if (i.second[0] == 'E') {

View file

@ -372,7 +372,7 @@ EvalState::EvalState(const Strings& _searchPath, const ref<Store>& store)
clearValue(vEmptySet); clearValue(vEmptySet);
vEmptySet.type = tAttrs; vEmptySet.type = tAttrs;
vEmptySet.attrs = allocBindings(0); vEmptySet.attrs = Bindings::NewGC();
createBaseEnv(); createBaseEnv();
} }
@ -857,7 +857,7 @@ void ExprAttrs::eval(EvalState& state, Env& env, Value& v) {
if (hasOverrides) { if (hasOverrides) {
Value* vOverrides = v.attrs->find(overrides->first)->second.value; Value* vOverrides = v.attrs->find(overrides->first)->second.value;
state.forceAttrs(*vOverrides); state.forceAttrs(*vOverrides);
Bindings* newBnds = state.allocBindings(/* capacity = */ 0); Bindings* newBnds = Bindings::NewGC();
for (auto& i : *v.attrs) { // TODO(tazjin): copy constructor? for (auto& i : *v.attrs) { // TODO(tazjin): copy constructor?
newBnds->push_back(i.second); newBnds->push_back(i.second);
} }

View file

@ -258,8 +258,6 @@ class EvalState {
Value* allocAttr(Value& vAttrs, const Symbol& name); Value* allocAttr(Value& vAttrs, const Symbol& name);
[[deprecated]] static Bindings* allocBindings(size_t capacity);
void mkList(Value& v, size_t size); void mkList(Value& v, size_t size);
void mkAttrs(Value& v, size_t capacity); void mkAttrs(Value& v, size_t capacity);
void mkThunk_(Value& v, Expr* expr); void mkThunk_(Value& v, Expr* expr);

View file

@ -293,7 +293,7 @@ bool DrvInfo::queryMetaBool(const std::string& name, bool def) {
void DrvInfo::setMeta(const std::string& name, Value* v) { void DrvInfo::setMeta(const std::string& name, Value* v) {
getMeta(); getMeta();
Bindings* old = meta; Bindings* old = meta;
meta = state->allocBindings(1 + (old != nullptr ? old->size() : 0)); meta = Bindings::NewGC();
Symbol sym = state->symbols.Create(name); Symbol sym = state->symbols.Create(name);
if (old != nullptr) { if (old != nullptr) {
for (auto i : *old) { for (auto i : *old) {

View file

@ -19,7 +19,7 @@ DrvInfos queryInstalled(EvalState& state, const Path& userEnv) {
if (pathExists(manifestFile)) { if (pathExists(manifestFile)) {
Value v; Value v;
state.evalFile(manifestFile, v); state.evalFile(manifestFile, v);
Bindings& bindings(*state.allocBindings(0)); Bindings& bindings(*Bindings::NewGC());
getDerivations(state, v, "", bindings, elems, false); getDerivations(state, v, "", bindings, elems, false);
} }
return elems; return elems;

View file

@ -29,7 +29,7 @@ struct CmdEdit : InstallableCommand {
Value* v2; Value* v2;
try { try {
auto dummyArgs = state->allocBindings(0); auto dummyArgs = Bindings::NewGC();
v2 = findAlongAttrPath(*state, "meta.position", *dummyArgs, *v); v2 = findAlongAttrPath(*state, "meta.position", *dummyArgs, *v);
} catch (Error&) { } catch (Error&) {
throw Error("package '%s' has no source location information", throw Error("package '%s' has no source location information",

View file

@ -110,7 +110,7 @@ struct CmdSearch : SourceExprCommand, MixJSON {
if (v->type == tLambda && toplevel) { if (v->type == tLambda && toplevel) {
Value* v2 = state->allocValue(); Value* v2 = state->allocValue();
state->autoCallFunction(*state->allocBindings(1), *v, *v2); state->autoCallFunction(*Bindings::NewGC(), *v, *v2);
v = v2; v = v2;
state->forceValue(*v); state->forceValue(*v);
} }

View file

@ -151,7 +151,7 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand {
auto state = std::make_unique<EvalState>(Strings(), store); auto state = std::make_unique<EvalState>(Strings(), store);
auto v = state->allocValue(); auto v = state->allocValue();
state->eval(state->parseExprFromString(*res.data, "/no-such-path"), *v); state->eval(state->parseExprFromString(*res.data, "/no-such-path"), *v);
Bindings& bindings(*state->allocBindings(0)); Bindings& bindings(*Bindings::NewGC());
auto v2 = findAlongAttrPath(*state, settings.thisSystem, bindings, *v); auto v2 = findAlongAttrPath(*state, settings.thisSystem, bindings, *v);
return state->forceString(*v2); return state->forceString(*v2);