Improved heap memory management in M17 demodulator class

pull/79/head
Silvano Seva 2022-06-05 12:05:51 +02:00
rodzic 8b5b6ed571
commit a1888ae19e
3 zmienionych plików z 27 dodań i 28 usunięć

Wyświetl plik

@ -27,17 +27,15 @@
#error This header is C++ only! #error This header is C++ only!
#endif #endif
#include <array>
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
#include <interfaces/audio_stream.h> #include <memory>
#include <hwconfig.h> #include <array>
#include <dsp.h> #include <dsp.h>
#include <deque> #include <cmath>
#include <stdio.h> #include <interfaces/audio_stream.h>
#include <M17/M17Datatypes.h> #include <M17/M17Datatypes.h>
#include <M17/M17Constants.h> #include <M17/M17Constants.h>
#include <cmath>
namespace M17 namespace M17
{ {
@ -146,18 +144,18 @@ private:
/* /*
* Buffers * Buffers
*/ */
streamId basebandId; ///< Id of the baseband input stream. std::unique_ptr< int16_t[] > baseband_buffer; ///< Buffer for baseband audio handling.
int16_t *baseband_buffer; ///< Buffer for baseband audio handling. streamId basebandId; ///< Id of the baseband input stream.
dataBlock_t baseband; ///< Half buffer, free to be processed. dataBlock_t baseband; ///< Data block with samples to be processed.
uint16_t frame_index; ///< Index for filling the raw frame. uint16_t frame_index; ///< Index for filling the raw frame.
frame_t *demodFrame; ///< Frame being demodulated. std::unique_ptr<frame_t > demodFrame; ///< Frame being demodulated.
frame_t *readyFrame; ///< Fully demodulated frame to be returned. std::unique_ptr<frame_t > readyFrame; ///< Fully demodulated frame to be returned.
bool syncDetected; ///< A syncword was detected. bool syncDetected; ///< A syncword was detected.
bool locked; ///< A syncword was correctly demodulated. bool locked; ///< A syncword was correctly demodulated.
bool newFrame; ///< A new frame has been fully decoded. bool newFrame; ///< A new frame has been fully decoded.
int16_t basebandBridge[M17_BRIDGE_SIZE] = { 0 }; ///< Bridge buffer int16_t basebandBridge[M17_BRIDGE_SIZE] = { 0 }; ///< Bridge buffer
int16_t phase; ///< Phase of the signal w.r.t. sampling int16_t phase; ///< Phase of the signal w.r.t. sampling
bool invPhase; ///< Invert signal phase bool invPhase; ///< Invert signal phase
/* /*
* State variables * State variables

Wyświetl plik

@ -155,8 +155,7 @@ M17Demodulator::M17Demodulator()
M17Demodulator::~M17Demodulator() M17Demodulator::~M17Demodulator()
{ {
// TODO terminate();
// terminate();
} }
void M17Demodulator::init() void M17Demodulator::init()
@ -167,10 +166,10 @@ void M17Demodulator::init()
* placement new. * placement new.
*/ */
baseband_buffer = new int16_t[2 * M17_SAMPLE_BUF_SIZE]; baseband_buffer = std::make_unique< int16_t[] >(2 * M17_SAMPLE_BUF_SIZE);
demodFrame = std::make_unique< frame_t >();
readyFrame = std::make_unique< frame_t >();
baseband = { nullptr, 0 }; baseband = { nullptr, 0 };
demodFrame = new frame_t;
readyFrame = new frame_t;
frame_index = 0; frame_index = 0;
phase = 0; phase = 0;
syncDetected = false; syncDetected = false;
@ -193,9 +192,9 @@ void M17Demodulator::terminate()
inputStream_stop(basebandId); inputStream_stop(basebandId);
// Delete the buffers and deallocate memory. // Delete the buffers and deallocate memory.
delete[] baseband_buffer; baseband_buffer.reset();
delete demodFrame; demodFrame.reset();
delete readyFrame; readyFrame.reset();
#ifdef ENABLE_DEMOD_LOG #ifdef ENABLE_DEMOD_LOG
logRunning = false; logRunning = false;
@ -205,7 +204,7 @@ void M17Demodulator::terminate()
void M17Demodulator::startBasebandSampling() void M17Demodulator::startBasebandSampling()
{ {
basebandId = inputStream_start(SOURCE_RTX, PRIO_RX, basebandId = inputStream_start(SOURCE_RTX, PRIO_RX,
baseband_buffer, baseband_buffer.get(),
2 * M17_SAMPLE_BUF_SIZE, 2 * M17_SAMPLE_BUF_SIZE,
BUF_CIRC_DOUBLE, BUF_CIRC_DOUBLE,
M17_RX_SAMPLE_RATE); M17_RX_SAMPLE_RATE);
@ -555,7 +554,7 @@ bool M17Demodulator::update()
// If the frame buffer is full switch demod and ready frame // If the frame buffer is full switch demod and ready frame
if (frame_index == M17_FRAME_SYMBOLS) if (frame_index == M17_FRAME_SYMBOLS)
{ {
std::swap(demodFrame, readyFrame); demodFrame.swap(readyFrame);
frame_index = 0; frame_index = 0;
newFrame = true; newFrame = true;
} }

Wyświetl plik

@ -87,5 +87,7 @@ dataBlock_t inputStream_getData(streamId id)
void inputStream_stop(streamId id) void inputStream_stop(streamId id)
{ {
(void) id; (void) id;
if(baseband_file == NULL) return;
fclose(baseband_file); fclose(baseband_file);
baseband_file = NULL;
} }