cleanup: fix leak + sbuf stuff

This commit is contained in:
bnnm 2024-08-18 19:42:07 +02:00
parent 03d8cb5329
commit 98f795a7d2
4 changed files with 24 additions and 12 deletions

View File

@ -1,3 +1,4 @@
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -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

View File

@ -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;

View File

@ -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++) {