add deinit() calls to custom plugins

This commit is contained in:
Luna 2019-08-14 09:10:09 -03:00
parent 0ad1c88274
commit f8d8ed067d
3 changed files with 19 additions and 5 deletions

View file

@ -9,7 +9,8 @@ const RunBuffers = plugins.RunBuffers;
pub const RandomNoise = struct {
r: std.rand.DefaultPrng,
rand_buf: ?[]f32,
rand_buf: ?[]f32 = null,
allocator: ?*std.mem.Allocator = null,
cnt: usize = 0,
pub fn init(
@ -30,16 +31,22 @@ pub const RandomNoise = struct {
return RandomNoise{
.r = r,
.allocator = allocator,
.rand_buf = rand_buf,
};
} else {
return RandomNoise{
.r = r,
.rand_buf = null,
};
}
}
pub fn deinit(self: *RandomNoise) void {
if (self.allocator == null) return;
if (self.rand_buf == null) return;
self.allocator.?.free(self.rand_buf.?);
}
pub fn run(self: *RandomNoise, bufs: *RunBuffers) void {
if (self.rand_buf) |rand_buf| {
if (self.cnt >= rand_buf.len) self.cnt = 0;
@ -53,7 +60,8 @@ pub const RandomNoise = struct {
pub const WildNoise = struct {
r: std.rand.DefaultPrng,
rand_buf: ?[]f32,
rand_buf: ?[]f32 = null,
allocator: ?*std.mem.Allocator = null,
cnt: usize = 0,
pub fn init(
@ -79,11 +87,16 @@ pub const WildNoise = struct {
} else {
return WildNoise{
.r = r,
.rand_buf = null,
};
}
}
pub fn deinit(self: *RandomNoise) void {
if (self.allocator == null) return;
if (self.rand_buf == null) return;
self.allocator.?.free(self.rand_buf.?);
}
pub fn run(self: *WildNoise, bufs: *RunBuffers) void {
if (self.rand_buf) |rand_buf| {
if (self.cnt >= rand_buf.len) self.cnt = 0;
@ -107,7 +120,6 @@ pub const Write = struct {
params: *plugins.ParamMap,
) Write {
const data = params.get("data").?;
return Write{
.data = data.value,
};

View file

@ -69,6 +69,7 @@ fn sf_tell(file: *c.SNDFILE) i64 {
return -frames;
}
/// Caller owns the returned memory.
pub fn temporaryName(allocator: *std.mem.Allocator) ![]u8 {
const template_start = "/temp/temp_";
const template = "/tmp/temp_XXXXXXXXXXX";

View file

@ -141,6 +141,7 @@ pub const RunContext = struct {
pub fn makeContext(allocator: *std.mem.Allocator, plugin_uri: []const u8) !Context {
const cstr_plugin_uri = try std.cstr.addNullByte(allocator, plugin_uri);
defer allocator.free(cstr_plugin_uri);
var world: *c.LilvWorld = c.lilv_world_new().?;
errdefer c.lilv_world_free(world);