From 5b4ec0e1fe48f85383a308aa535cae952032b572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6gberg?= Date: Fri, 16 Oct 2015 20:09:14 +0200 Subject: [PATCH] Share memory between fm buffer and temporary buffer The temporary buffer and the fm buffer are not used at the same time, so the allocated memory can be shared between the two buffers. This reduces memory usage by 8MB (~30 %) --- src/rtl_433.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/rtl_433.c b/src/rtl_433.c index 842da32f..93946749 100644 --- a/src/rtl_433.c +++ b/src/rtl_433.c @@ -46,8 +46,11 @@ struct dm_state { FILE *out_file; int32_t level_limit; int16_t am_buf[MAXIMAL_BUF_LENGTH]; // AM demodulated signal (for OOK decoding) - int16_t fm_buf[MAXIMAL_BUF_LENGTH]; // FM demodulated signal (for FSK decoding) - uint16_t temp_buf[MAXIMAL_BUF_LENGTH]; // Temporary buffer (to be optimized out..) + union { + // These buffers aren't used at the same time, so let's use a union to save some memory + int16_t fm_buf[MAXIMAL_BUF_LENGTH]; // FM demodulated signal (for FSK decoding) + uint16_t temp_buf[MAXIMAL_BUF_LENGTH]; // Temporary buffer (to be optimized out..) + }; FilterState lowpass_filter_state; DemodFM_State demod_FM_state; int enable_FM_demod; @@ -640,13 +643,14 @@ static void rtlsdr_callback(unsigned char *iq_buf, uint32_t len, void *ctx) { demod->sg_index = 0; } + // AM demodulation + envelope_detect(iq_buf, demod->temp_buf, len/2); + baseband_low_pass_filter(demod->temp_buf, demod->am_buf, len/2, &demod->lowpass_filter_state); + // FM demodulation if (demod->enable_FM_demod) { baseband_demod_FM(iq_buf, demod->fm_buf, len/2, &demod->demod_FM_state); } - // AM demodulation - envelope_detect(iq_buf, demod->temp_buf, len/2); - baseband_low_pass_filter(demod->temp_buf, demod->am_buf, len/2, &demod->lowpass_filter_state); // Handle special input formats if(!demod->out_file) { // If output file is specified we always assume I/Q input