From f8d8ed067de6e96710b8a199bc34684555f44910 Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 14 Aug 2019 09:10:09 -0300 Subject: [PATCH] add deinit() calls to custom plugins --- src/custom.zig | 22 +++++++++++++++++----- src/image.zig | 1 + src/plugin.zig | 1 + 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/custom.zig b/src/custom.zig index 2fc5a09..1b8b3a6 100644 --- a/src/custom.zig +++ b/src/custom.zig @@ -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, }; diff --git a/src/image.zig b/src/image.zig index 1898f28..e42bca7 100644 --- a/src/image.zig +++ b/src/image.zig @@ -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"; diff --git a/src/plugin.zig b/src/plugin.zig index c403eb7..1de854f 100644 --- a/src/plugin.zig +++ b/src/plugin.zig @@ -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);