From 022b918db171c9614e7c27f633452fb2bf9e6c57 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 27 Jul 2023 15:58:18 +0200 Subject: [PATCH] nix_api_expr: remove bindingsbuilder refcounting --- src/libexpr/nix_api_expr_internal.h | 7 ++++++- src/libexpr/nix_api_value.cc | 27 ++++++++++++++------------- src/libexpr/nix_api_value.h | 14 +++++++------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/libexpr/nix_api_expr_internal.h b/src/libexpr/nix_api_expr_internal.h index 424ca2874..e9031d311 100644 --- a/src/libexpr/nix_api_expr_internal.h +++ b/src/libexpr/nix_api_expr_internal.h @@ -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 diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index 6a2f19de9..74e8395fc 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -386,16 +386,13 @@ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) { NIXC_CATCH_ERRS } -typedef std::shared_ptr 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( - traceable_allocator(), 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 } diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index 22ecfa86b..6aae5cf3c 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -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