Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/libcmd/common-eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
? state.rootPath(absPath(getCommandBaseDir()))
: state.rootPath(".")));
},
[&](const AutoArgString & arg) { v->mkString(arg.s); },
[&](const AutoArgFile & arg) { v->mkString(readFile(arg.path.string())); },
[&](const AutoArgStdin & arg) { v->mkString(readFile(STDIN_FILENO)); }},
[&](const AutoArgString & arg) { v->mkString(arg.s, state.mem); },
[&](const AutoArgFile & arg) { v->mkString(readFile(arg.path.string()), state.mem); },
[&](const AutoArgStdin & arg) { v->mkString(readFile(STDIN_FILENO), state.mem); }},
arg);
res.insert(state.symbols.create(name), v);
}
Expand Down
6 changes: 3 additions & 3 deletions src/libexpr-c/nix_api_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,13 @@ nix_err nix_init_bool(nix_c_context * context, nix_value * value, bool b)
}

// todo string context
nix_err nix_init_string(nix_c_context * context, nix_value * value, const char * str)
nix_err nix_init_string(nix_c_context * context, EvalState * s, nix_value * value, const char * str)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto & v = check_value_out(value);
v.mkString(std::string_view(str));
v.mkString(std::string_view(str), s->state.mem);
}
NIXC_CATCH_ERRS
}
Expand All @@ -514,7 +514,7 @@ nix_err nix_init_path_string(nix_c_context * context, EvalState * s, nix_value *
context->last_err_code = NIX_OK;
try {
auto & v = check_value_out(value);
v.mkPath(s->state.rootPath(nix::CanonPath(str)));
v.mkPath(s->state.rootPath(nix::CanonPath(str)), s->state.mem);
}
NIXC_CATCH_ERRS
}
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr-c/nix_api_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ nix_err nix_init_bool(nix_c_context * context, nix_value * value, bool b);
* @param[in] str the string, copied
* @return error code, NIX_OK on success.
*/
nix_err nix_init_string(nix_c_context * context, nix_value * value, const char * str);
nix_err nix_init_string(nix_c_context * context, EvalState * s, nix_value * value, const char * str);

/** @brief Set a path
* @ingroup value_create
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr-tests/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ TEST_F(JSONValueTest, StringQuotes)
TEST_F(JSONValueTest, DISABLED_Path)
{
Value v;
v.mkPath(state.rootPath(CanonPath("/test")));
v.mkPath(state.rootPath(CanonPath("/test")), state.mem);
ASSERT_EQ(getJSONValue(v), "\"/nix/store/g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-x\"");
}
} /* namespace nix */
6 changes: 3 additions & 3 deletions src/libexpr-tests/nix_api_expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ primop_repeat(void * user_data, nix_c_context * context, EvalState * state, nix_
for (int i = 0; i < n; ++i)
result += s;

nix_init_string(context, ret, result.c_str());
nix_init_string(context, state, ret, result.c_str());
}

TEST_F(nix_api_expr_test, nix_expr_primop_arity_2_multiple_calls)
Expand All @@ -305,7 +305,7 @@ TEST_F(nix_api_expr_test, nix_expr_primop_arity_2_multiple_calls)

nix_value * hello = nix_alloc_value(ctx, state);
assert_ctx_ok();
nix_init_string(ctx, hello, "hello");
nix_init_string(ctx, state, hello, "hello");
assert_ctx_ok();

nix_value * three = nix_alloc_value(ctx, state);
Expand Down Expand Up @@ -340,7 +340,7 @@ TEST_F(nix_api_expr_test, nix_expr_primop_arity_2_single_call)

nix_value * hello = nix_alloc_value(ctx, state);
assert_ctx_ok();
nix_init_string(ctx, hello, "hello");
nix_init_string(ctx, state, hello, "hello");
assert_ctx_ok();

nix_value * three = nix_alloc_value(ctx, state);
Expand Down
6 changes: 3 additions & 3 deletions src/libexpr-tests/nix_api_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ TEST_F(nix_api_expr_test, nix_value_set_get_string)
{
std::string string_value;
const char * myString = "some string";
nix_init_string(ctx, value, myString);
nix_init_string(ctx, state, value, myString);

nix_get_string(ctx, value, OBSERVE_STRING(string_value));
ASSERT_STREQ(myString, string_value.c_str());
Expand Down Expand Up @@ -306,7 +306,7 @@ TEST_F(nix_api_expr_test, nix_build_and_init_attr)
nix_init_int(ctx, intValue, 42);

nix_value * stringValue = nix_alloc_value(ctx, state);
nix_init_string(ctx, stringValue, "foo");
nix_init_string(ctx, state, stringValue, "foo");

nix_bindings_builder_insert(ctx, builder, "a", intValue);
nix_bindings_builder_insert(ctx, builder, "b", stringValue);
Expand Down Expand Up @@ -625,7 +625,7 @@ TEST_F(nix_api_expr_test, nix_value_init)
TEST_F(nix_api_expr_test, nix_value_init_apply_error)
{
nix_value * some_string = nix_alloc_value(ctx, state);
nix_init_string(ctx, some_string, "some string");
nix_init_string(ctx, state, some_string, "some string");
assert_ctx_ok();

nix_value * v = nix_alloc_value(ctx, state);
Expand Down
4 changes: 2 additions & 2 deletions src/libexpr-tests/value/print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ struct StringPrintingTests : LibExprTest
void test(std::string_view literal, std::string_view expected, unsigned int maxLength, A... args)
{
Value v;
v.mkString(literal);
v.mkString(literal, state.mem);

std::stringstream out;
printValue(state, out, v, PrintOptions{.maxStringLength = maxLength});
Expand Down Expand Up @@ -353,7 +353,7 @@ TEST_F(ValuePrintingTests, ansiColorsStringElided)
TEST_F(ValuePrintingTests, ansiColorsPath)
{
Value v;
v.mkPath(state.rootPath(CanonPath("puppy")));
v.mkPath(state.rootPath(CanonPath("puppy")), state.mem);

test(v, ANSI_GREEN "/puppy" ANSI_NORMAL, PrintOptions{.ansiColors = true});
}
Expand Down
26 changes: 13 additions & 13 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ static const char * makeImmutableString(std::string_view s)
return t;
}

StringData & StringData::alloc(size_t size)
StringData & StringData::alloc(EvalMemory & mem, size_t size)
{
void * t = GC_MALLOC_ATOMIC(sizeof(StringData) + size + 1);
void * t = mem.allocBytes(sizeof(StringData) + size + 1);
if (!t)
throw std::bad_alloc();
auto res = new (t) StringData(size);
return *res;
}

const StringData & StringData::make(std::string_view s)
const StringData & StringData::make(EvalMemory & mem, std::string_view s)
{
if (s.empty())
return ""_sds;
auto & res = alloc(s.size());
auto & res = alloc(mem, s.size());
std::memcpy(&res.data_, s.data(), s.size());
res.data_[s.size()] = '\0';
return res;
Expand Down Expand Up @@ -849,9 +849,9 @@ DebugTraceStacker::DebugTraceStacker(EvalState & evalState, DebugTrace t)
evalState.runDebugRepl(nullptr, trace.env, trace.expr);
}

void Value::mkString(std::string_view s)
void Value::mkString(std::string_view s, EvalMemory & mem)
{
mkStringNoCopy(StringData::make(s));
mkStringNoCopy(StringData::make(mem, s));
}

Value::StringWithContext::Context *
Expand All @@ -862,23 +862,23 @@ Value::StringWithContext::Context::fromBuilder(const NixStringContext & context,

auto ctx = new (mem.allocBytes(sizeof(Context) + context.size() * sizeof(value_type))) Context(context.size());
std::ranges::transform(
context, ctx->elems, [](const NixStringContextElem & elt) { return &StringData::make(elt.to_string()); });
context, ctx->elems, [&](const NixStringContextElem & elt) { return &StringData::make(mem, elt.to_string()); });
return ctx;
}

void Value::mkString(std::string_view s, const NixStringContext & context, EvalMemory & mem)
{
mkStringNoCopy(StringData::make(s), Value::StringWithContext::Context::fromBuilder(context, mem));
mkStringNoCopy(StringData::make(mem, s), Value::StringWithContext::Context::fromBuilder(context, mem));
}

void Value::mkStringMove(const StringData & s, const NixStringContext & context, EvalMemory & mem)
{
mkStringNoCopy(s, Value::StringWithContext::Context::fromBuilder(context, mem));
}

void Value::mkPath(const SourcePath & path)
void Value::mkPath(const SourcePath & path, EvalMemory & mem)
{
mkPath(&*path.accessor, StringData::make(path.path.abs()));
mkPath(&*path.accessor, StringData::make(mem, path.path.abs()));
}

inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval)
Expand Down Expand Up @@ -943,7 +943,7 @@ void EvalState::mkPos(Value & v, PosIdx p)
auto origin = positions.originOf(p);
if (auto path = std::get_if<SourcePath>(&origin)) {
auto attrs = buildBindings(3);
attrs.alloc(s.file).mkString(path->path.abs());
attrs.alloc(s.file).mkString(path->path.abs(), mem);
makePositionThunks(*this, p, attrs.alloc(s.line), attrs.alloc(s.column));
v.mkAttrs(attrs);
} else
Expand Down Expand Up @@ -2139,9 +2139,9 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
for (const auto & part : strings) {
resultStr += *part;
}
v.mkPath(state.rootPath(CanonPath(resultStr)));
v.mkPath(state.rootPath(CanonPath(resultStr)), state.mem);
} else {
auto & resultStr = StringData::alloc(sSize);
auto & resultStr = StringData::alloc(state.mem, sSize);
auto * tmp = resultStr.data();
for (const auto & part : strings) {
std::memcpy(tmp, part->data(), part->size());
Expand Down
8 changes: 4 additions & 4 deletions src/libexpr/include/nix/expr/value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ public:
* Allocate StringData on the (possibly) GC-managed heap and copy
* the contents of s to it.
*/
static const StringData & make(std::string_view s);
static const StringData & make(EvalMemory & mem, std::string_view s);

/**
* Allocate StringData on the (possibly) GC-managed heap.
* @param size Length of the string (without the NUL terminator).
*/
static StringData & alloc(size_t size);
static StringData & alloc(EvalMemory & mem, size_t size);

size_t size() const
{
Expand Down Expand Up @@ -1147,13 +1147,13 @@ public:
setStorage(StringWithContext{.str = &s, .context = context});
}

void mkString(std::string_view s);
void mkString(std::string_view s, EvalMemory & mem);

void mkString(std::string_view s, const NixStringContext & context, EvalMemory & mem);

void mkStringMove(const StringData & s, const NixStringContext & context, EvalMemory & mem);

void mkPath(const SourcePath & path);
void mkPath(const SourcePath & path, EvalMemory & mem);

inline void mkPath(SourceAccessor * accessor, const StringData & path) noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/json-to-value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class JSONSax : nlohmann::json_sax<json>
bool string(string_t & val) override
{
forceNoNullByte(val);
rs->value(state).mkString(val);
rs->value(state).mkString(val, state.mem);
rs->add();
return true;
}
Expand Down
Loading
Loading