From 4ceb0efdc490ad982e95afe7e6c1da557d942ca1 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Thu, 3 Mar 2022 15:06:13 +0100 Subject: [PATCH] Use Arc instead of Box for the wrapper We'll need this for the GuiContext. --- src/wrapper/clap/factory.rs | 6 +++++- src/wrapper/clap/plugin.rs | 7 ++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/wrapper/clap/factory.rs b/src/wrapper/clap/factory.rs index c5f1938e..8f14d6d5 100644 --- a/src/wrapper/clap/factory.rs +++ b/src/wrapper/clap/factory.rs @@ -4,6 +4,7 @@ use clap_sys::plugin_factory::clap_plugin_factory; use std::ffi::CStr; use std::os::raw::c_char; use std::ptr; +use std::sync::Arc; use super::descriptor::PluginDescriptor; use super::plugin::Wrapper; @@ -60,7 +61,10 @@ impl Factory

{ if !plugin_id.is_null() && CStr::from_ptr(plugin_id) == factory.plugin_descriptor.clap_id() { - &Box::leak(Wrapper::

::new(host)).clap_plugin + // Arc does not have a convenient leak function like Box, so this gets a bit awkward + // This pointer gets turned into an Arc and its reference count decremented in + // [Wrapper::destroy()] + &(*Arc::into_raw(Wrapper::

::new(host))).clap_plugin } else { ptr::null() } diff --git a/src/wrapper/clap/plugin.rs b/src/wrapper/clap/plugin.rs index 2e8c5103..e28ea2f8 100644 --- a/src/wrapper/clap/plugin.rs +++ b/src/wrapper/clap/plugin.rs @@ -40,6 +40,7 @@ use std::ffi::{c_void, CStr}; use std::os::raw::c_char; use std::ptr; use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; +use std::sync::Arc; use std::thread::{self, ThreadId}; use super::context::WrapperProcessContext; @@ -214,7 +215,7 @@ impl MainThreadExecutor for Wrapper

{ } impl Wrapper

{ - pub fn new(host_callback: *const clap_host) -> Box { + pub fn new(host_callback: *const clap_host) -> Arc { let plugin_descriptor = Box::new(PluginDescriptor::default()); assert!(!host_callback.is_null()); @@ -371,7 +372,7 @@ impl Wrapper

{ .map(|(_, hash, ptr)| (*ptr, hash)) .collect(); - Box::new(wrapper) + Arc::new(wrapper) } fn make_process_context(&self) -> WrapperProcessContext<'_, P> { @@ -515,7 +516,7 @@ impl Wrapper

{ } unsafe extern "C" fn destroy(plugin: *const clap_plugin) { - Box::from_raw(plugin as *mut Self); + Arc::from_raw(plugin as *mut Self); } unsafe extern "C" fn activate(