nix_api_expr: remove bindingsbuilder refcounting

This commit is contained in:
Yorick van Pelt 2023-07-27 15:58:18 +02:00 committed by José Luis Lafuente
parent aa85f7d917
commit 022b918db1
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
3 changed files with 27 additions and 21 deletions

View file

@ -4,7 +4,8 @@
// forward declaration
namespace nix {
class EvalState;
};
class BindingsBuilder;
}; // namespace nix
struct State {
nix::EvalState state;
@ -14,4 +15,8 @@ struct GCRef {
void *ptr;
};
struct BindingsBuilder {
nix::BindingsBuilder builder;
};
#endif // NIX_API_EXPR_INTERNAL_H

View file

@ -386,16 +386,13 @@ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) {
NIXC_CATCH_ERRS
}
typedef std::shared_ptr<nix::BindingsBuilder> BindingsBuilder_Inner;
nix_err nix_make_attrs(nix_c_context *context, Value *value,
BindingsBuilder *b) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
nix::BindingsBuilder &builder = **(BindingsBuilder_Inner *)b;
v.mkAttrs(builder);
v.mkAttrs(b->builder);
}
NIXC_CATCH_ERRS
}
@ -406,10 +403,11 @@ BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state,
context->last_err_code = NIX_OK;
try {
auto bb = state->state.buildBindings(capacity);
auto res = new BindingsBuilder_Inner();
*res = std::allocate_shared<nix::BindingsBuilder>(
traceable_allocator<nix::BindingsBuilder>(), bb);
return res;
return new
#if HAVE_BOEHMGC
(NoGC)
#endif
BindingsBuilder{std::move(bb)};
}
NIXC_CATCH_ERRS_NULL
}
@ -419,14 +417,17 @@ nix_err nix_bindings_builder_insert(nix_c_context *context, BindingsBuilder *b,
if (context)
context->last_err_code = NIX_OK;
try {
nix::BindingsBuilder &builder = **(BindingsBuilder_Inner *)b;
auto &v = check_value_not_null(value);
nix::Symbol s = builder.state.symbols.create(name);
builder.insert(s, &v);
nix::Symbol s = b->builder.state.symbols.create(name);
b->builder.insert(s, &v);
}
NIXC_CATCH_ERRS
}
void nix_bindings_builder_unref(BindingsBuilder *bb) {
delete (BindingsBuilder_Inner *)bb;
void nix_bindings_builder_free(BindingsBuilder *bb) {
#if HAVE_BOEHMGC
GC_FREE((nix::BindingsBuilder *)bb);
#else
delete (nix::BindingsBuilder *)bb;
#endif
}

View file

@ -36,11 +36,12 @@ typedef struct State State;
typedef struct GCRef GCRef;
// type defs
/** @brief Stores an under-construction set of bindings
* Reference-counted
* @see nix_make_bindings_builder, nix_bindings_builder_unref, nix_make_attrs
*
* Do not reuse.
* @see nix_make_bindings_builder, nix_bindings_builder_free, nix_make_attrs
* @see nix_bindings_builder_insert
*/
typedef void BindingsBuilder;
typedef struct BindingsBuilder BindingsBuilder;
/** @brief PrimOp function
*
@ -328,13 +329,12 @@ BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state,
nix_err nix_bindings_builder_insert(nix_c_context *context,
BindingsBuilder *builder, const char *name,
Value *value);
/** @brief Unref a bindings builder
/** @brief Free a bindings builder
*
* Does not fail.
* It'll be deallocated when all references are gone.
* @param[in] builder the builder to unref
* @param[in] builder the builder to free
*/
void nix_bindings_builder_unref(BindingsBuilder *builder);
void nix_bindings_builder_free(BindingsBuilder *builder);
// cffi end
#ifdef __cplusplus