From 2c19bd666280c33eb63d3f3b9063b07b62023c9d Mon Sep 17 00:00:00 2001 From: Kane York Date: Sat, 12 Sep 2020 08:09:18 -0700 Subject: refactor(3p/tvix): convert NixList usage to shared_ptr Starting to reclaim memory. Fairly simple mechanical replacement. Change-Id: I6b4c5c5596729470d1a049eba61e69e8097decf4 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1971 Tested-by: BuildkiteCI Reviewed-by: glittershark --- third_party/nix/src/libexpr/eval.cc | 12 ++++++------ third_party/nix/src/libexpr/eval.hh | 2 +- third_party/nix/src/libexpr/primops.cc | 8 ++++---- third_party/nix/src/libexpr/value.hh | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) (limited to 'third_party/nix') diff --git a/third_party/nix/src/libexpr/eval.cc b/third_party/nix/src/libexpr/eval.cc index 28ae46bdfd8c..2a0aba1a4c8b 100644 --- a/third_party/nix/src/libexpr/eval.cc +++ b/third_party/nix/src/libexpr/eval.cc @@ -598,15 +598,15 @@ Env& EvalState::allocEnv(size_t size) { return *env; } -void EvalState::mkList(Value& v, NixList* list) { +void EvalState::mkList(Value& v, std::shared_ptr list) { + nrListElems += list->size(); clearValue(v); v.type = tList; v.list = list; - nrListElems += list->size(); } void EvalState::mkList(Value& v, size_t size) { - EvalState::mkList(v, new NixList(size)); + EvalState::mkList(v, std::make_shared(size)); } unsigned long nrThunks = 0; @@ -1224,7 +1224,7 @@ void ExprOpConcatLists::eval(EvalState& state, Env& env, Value& v) { void EvalState::concatLists(Value& v, const NixList& lists, const Pos& pos) { nrListConcats++; - auto outlist = new NixList(); + auto outlist = std::make_shared(); for (Value* list : lists) { forceList(*list, pos); @@ -1816,8 +1816,8 @@ size_t valueSize(const Value& v) { } break; case tList: - if (seenLists.find(v.list) == seenLists.end()) { - seenLists.insert(v.list); + if (seenLists.find(v.list.get()) == seenLists.end()) { + seenLists.insert(v.list.get()); sz += v.listSize() * sizeof(Value*); for (const Value* v : *v.list) { sz += doValue(*v); diff --git a/third_party/nix/src/libexpr/eval.hh b/third_party/nix/src/libexpr/eval.hh index f36d11b92b99..0352a89a2ab8 100644 --- a/third_party/nix/src/libexpr/eval.hh +++ b/third_party/nix/src/libexpr/eval.hh @@ -258,7 +258,7 @@ class EvalState { Value* allocAttr(Value& vAttrs, const Symbol& name); // Create a list value from the specified vector. - void mkList(Value& v, NixList* list); + void mkList(Value& v, std::shared_ptr list); // Create a list value, allocating as many elements as specified in // size. This is used for the many cases in this codebase where diff --git a/third_party/nix/src/libexpr/primops.cc b/third_party/nix/src/libexpr/primops.cc index dd8a509df32d..761ff954a433 100644 --- a/third_party/nix/src/libexpr/primops.cc +++ b/third_party/nix/src/libexpr/primops.cc @@ -1603,7 +1603,7 @@ static void prim_sort(EvalState& state, const Pos& pos, Value** args, // Copy of the input list which can be sorted in place. v.type = tList; - v.list = new NixList(*args[1]->list); + v.list = std::make_shared(*args[1]->list); std::for_each(v.list->begin(), v.list->end(), [&](Value* val) { state.forceValue(*val); }); @@ -1633,8 +1633,8 @@ static void prim_partition(EvalState& state, const Pos& pos, Value** args, state.forceFunction(*args[0], pos); state.forceList(*args[1], pos); - NixList* right = new NixList(); - NixList* wrong = new NixList(); + std::shared_ptr right = std::make_shared(); + std::shared_ptr wrong = std::make_shared(); for (Value* elem : *args[1]->list) { state.forceValue(*elem, pos); @@ -1664,7 +1664,7 @@ static void prim_concatMap(EvalState& state, const Pos& pos, Value** args, state.forceFunction(*args[0], pos); state.forceList(*args[1], pos); - NixList* outlist = new NixList; + std::shared_ptr outlist = std::make_shared(); for (Value* elem : *args[1]->list) { auto out = state.allocValue(); diff --git a/third_party/nix/src/libexpr/value.hh b/third_party/nix/src/libexpr/value.hh index 82360c566813..b5a1a33cba45 100644 --- a/third_party/nix/src/libexpr/value.hh +++ b/third_party/nix/src/libexpr/value.hh @@ -96,7 +96,7 @@ struct Value { NixString string; const char* path; std::shared_ptr attrs; - NixList* list; + std::shared_ptr list; NixThunk thunk; NixApp app; // TODO(tazjin): "app"? NixLambda lambda; -- cgit 1.4.1