From 98f795a7d25cd0afd71386389c3c42ba8f05d206 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sun, 18 Aug 2024 19:42:07 +0200 Subject: [PATCH] cleanup: fix leak + sbuf stuff --- src/base/sbuf.c | 9 +++++++++ src/base/sbuf.h | 10 ++++++++-- src/layout/layered.c | 5 ++--- src/layout/segmented.c | 12 +++++------- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/base/sbuf.c b/src/base/sbuf.c index 6250d1e1..c0c07945 100644 --- a/src/base/sbuf.c +++ b/src/base/sbuf.c @@ -1,3 +1,4 @@ +#include #include #include "../util.h" #include "sbuf.h" @@ -74,3 +75,11 @@ void sbuf_copy_layers(sample_t* dst, int dst_channels, sample_t* src, int src_ch void sbuf_silence(sample_t* dst, int samples, int channels, int filled) { memset(dst + filled * channels, 0, (samples - filled) * channels * sizeof(sample_t)); } + +bool sbuf_realloc(sample_t** dst, int samples, int channels) { + sample_t* outbuf_re = realloc(*dst, samples * channels * sizeof(sample_t)); + if (!outbuf_re) return false; + + *dst = outbuf_re; + return true; +} diff --git a/src/base/sbuf.h b/src/base/sbuf.h index 044de9e4..3d11b4df 100644 --- a/src/base/sbuf.h +++ b/src/base/sbuf.h @@ -1,5 +1,5 @@ -#ifndef _SBUF_H -#define _SBUF_H +#ifndef _SBUF_H_ +#define _SBUF_H_ #include "../streamtypes.h" @@ -27,6 +27,11 @@ typedef struct { } sbuf_t; void sbuf_init16(sbuf_t* sbuf, int16_t* buf, int samples, int channels); + +void sbuf_clamp(sbuf_t* sbuf, int samples); + +/* skips N samples from current sbuf */ +void sbuf_consume(sbuf_t* sbuf, int samples); #endif /* it's probably slightly faster to make those inline'd, but aren't called that often to matter (given big enough total samples) */ @@ -42,5 +47,6 @@ void sbuf_copy_layers(sample_t* dst, int dst_channels, sample_t* src, int src_ch void sbuf_silence(sample_t* dst, int samples, int channels, int filled); +bool sbuf_realloc(sample_t** dst, int samples, int channels); #endif diff --git a/src/layout/layered.c b/src/layout/layered.c index b374afea..746b959c 100644 --- a/src/layout/layered.c +++ b/src/layout/layered.c @@ -179,9 +179,8 @@ bool setup_layout_layered(layered_layout_data* data) { return false; /* create internal buffer big enough for mixing all layers */ - sample_t* outbuf_re = realloc(data->buffer, VGMSTREAM_LAYER_SAMPLE_BUFFER * max_input_channels * sizeof(sample_t)); - if (!outbuf_re) goto fail; - data->buffer = outbuf_re; + if (!sbuf_realloc(&data->buffer, VGMSTREAM_LAYER_SAMPLE_BUFFER, max_input_channels)) + goto fail; data->input_channels = max_input_channels; data->output_channels = max_output_channels; diff --git a/src/layout/segmented.c b/src/layout/segmented.c index 6b412b5f..c411fa33 100644 --- a/src/layout/segmented.c +++ b/src/layout/segmented.c @@ -14,7 +14,7 @@ * (like one part for intro and other for loop segments, which may even use different codecs). */ void render_vgmstream_segmented(sample_t* outbuf, int32_t sample_count, VGMSTREAM* vgmstream) { segmented_layout_data* data = vgmstream->layout_data; - bool use_internal_buffer = 0; + bool use_internal_buffer = false; /* normally uses outbuf directly (faster?) but could need internal buffer if downmixing */ if (vgmstream->channels != data->input_channels || data->mixed_channels) { @@ -146,7 +146,6 @@ fail: bool setup_layout_segmented(segmented_layout_data* data) { int max_input_channels = 0, max_output_channels = 0, mixed_channels = 0; - sample_t* outbuf_re = NULL; /* setup each VGMSTREAM (roughly equivalent to vgmstream.c's init_vgmstream_internal stuff) */ @@ -211,12 +210,11 @@ bool setup_layout_segmented(segmented_layout_data* data) { } if (max_output_channels > VGMSTREAM_MAX_CHANNELS || max_input_channels > VGMSTREAM_MAX_CHANNELS) - goto fail; + return false; /* create internal buffer big enough for mixing */ - outbuf_re = realloc(data->buffer, VGMSTREAM_SEGMENT_SAMPLE_BUFFER*max_input_channels*sizeof(sample_t)); - if (!outbuf_re) goto fail; - data->buffer = outbuf_re; + if (!sbuf_realloc(&data->buffer, VGMSTREAM_SEGMENT_SAMPLE_BUFFER, max_input_channels)) + goto fail; data->input_channels = max_input_channels; data->output_channels = max_output_channels; @@ -232,7 +230,7 @@ void free_layout_segmented(segmented_layout_data* data) { return; for (int i = 0; i < data->segment_count; i++) { - bool is_repeat = true; + bool is_repeat = false; /* segments are allowed to be repeated so don't close the same thing twice */ for (int j = 0; j < i; j++) {