From 6e04e69bff8e92a8df0f7663f9f7d3aac1b7fd31 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Tue, 30 Jan 2024 01:48:13 +0100 Subject: gccrs: libformat_parser: Send boxed values across FFI properly gcc/rust/ChangeLog: * ast/rust-fmt.cc (Pieces::~Pieces): Call libformat_parser's release function in destructor. * ast/rust-fmt.h (struct PieceSlice): Add capacity. (destroy_pieces): New. (struct Pieces): Add destructor. libgrust/ChangeLog: * libformat_parser/src/lib.rs: Leak Boxes properly for C++ to see them, add memory release function. --- gcc/rust/ast/rust-fmt.cc | 4 +++- gcc/rust/ast/rust-fmt.h | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-fmt.cc b/gcc/rust/ast/rust-fmt.cc index a7c4341..f6ee8a2 100644 --- a/gcc/rust/ast/rust-fmt.cc +++ b/gcc/rust/ast/rust-fmt.cc @@ -34,8 +34,10 @@ Pieces::collect (const std::string &to_parse) // auto pieces = std::vector (piece_slice.base_ptr, // piece_slice.base_ptr + piece_slice.len); - return Pieces{}; + return Pieces (piece_slice); } +Pieces::~Pieces () { destroy_pieces (slice); } + } // namespace Fmt } // namespace Rust diff --git a/gcc/rust/ast/rust-fmt.h b/gcc/rust/ast/rust-fmt.h index 7ec9a2a..50aeff6 100644 --- a/gcc/rust/ast/rust-fmt.h +++ b/gcc/rust/ast/rust-fmt.h @@ -237,6 +237,7 @@ struct PieceSlice { const Piece *base_ptr; size_t len; + size_t cap; }; extern "C" { @@ -244,11 +245,19 @@ extern "C" { PieceSlice collect_pieces (const char *input); +void destroy_pieces (PieceSlice); + } // extern "C" struct Pieces { static Pieces collect (const std::string &to_parse); + ~Pieces (); + +private: + Pieces (PieceSlice slice) : slice (slice) {} + + PieceSlice slice; }; } // namespace Fmt -- cgit v1.1