rtl, osmosdr: reorder buffer allocation to prevent memory leak

the leak would occur if the requested device could not be opened
This commit is contained in:
Dimitri Stolnikov 2012-06-02 22:50:48 +02:00
parent 237385a0c3
commit 05700e6092
2 changed files with 29 additions and 47 deletions

View File

@ -65,7 +65,10 @@ osmosdr_make_src_c (const std::string &args)
osmosdr_src_c::osmosdr_src_c (const std::string &args)
: gr_sync_block ("osmosdr_src_c",
gr_make_io_signature (0, 0, sizeof (gr_complex)),
args_to_io_signature(args))
args_to_io_signature(args)),
_running(true),
_auto_gain(false),
_skipped(0)
{
int ret;
unsigned int dev_index = 0, mcr = 0;
@ -80,15 +83,17 @@ osmosdr_src_c::osmosdr_src_c (const std::string &args)
mcr = (unsigned int) boost::lexical_cast< double >( dict["mcr"] );
if (mcr != 0)
throw std::runtime_error("Setting the master clock rate is not supported.");
throw std::runtime_error("FIXME: Setting the MCR is not supported.");
if (dict.count("nchan"))
nchan = boost::lexical_cast< size_t >( dict["nchan"] );
if (nchan != 1)
throw std::runtime_error("Values of nchan != 1 are not supported.");
throw std::runtime_error("FIXME: Values of nchan != 1 are not supported.");
_buf_num = BUF_NUM;
_buf_head = _buf_used = _buf_offset = 0;
_samp_avail = BUF_SIZE / BYTES_PER_SAMPLE;
if (dict.count("buffers")) {
_buf_num = (unsigned int)boost::lexical_cast< double >( dict["buffers"] );
@ -98,14 +103,6 @@ osmosdr_src_c::osmosdr_src_c (const std::string &args)
<< std::endl;
}
_buf = (unsigned short **) malloc(_buf_num * sizeof(unsigned short *));
for(unsigned int i = 0; i < _buf_num; ++i)
_buf[i] = (unsigned short *) malloc(BUF_SIZE);
_buf_head = _buf_used = _buf_offset = 0;
_samp_avail = BUF_SIZE / BYTES_PER_SAMPLE;
if ( dev_index >= osmosdr_get_device_count() )
throw std::runtime_error("Wrong osmosdr device index given.");
@ -118,23 +115,22 @@ osmosdr_src_c::osmosdr_src_c (const std::string &args)
if (ret < 0)
throw std::runtime_error("Failed to open osmosdr device.");
ret = osmosdr_set_sample_rate( _dev, 500000 );
ret = osmosdr_set_sample_rate( _dev, 1000000 );
if (ret < 0)
throw std::runtime_error("Failed to set default samplerate.");
ret = osmosdr_set_tuner_gain_mode(_dev, int(!_auto_gain));
if (ret < 0)
throw std::runtime_error("Failed to enable manual gain mode.");
ret = osmosdr_reset_buffer( _dev );
if (ret < 0)
throw std::runtime_error("Failed to reset usb buffers.");
ret = osmosdr_set_tuner_gain_mode(_dev, 1);
if (ret < 0)
throw std::runtime_error("Failed to enable manual gain mode.");
_buf = (unsigned short **) malloc(_buf_num * sizeof(unsigned short *));
_running = true;
_auto_gain = false;
_skipped = 0;
for(unsigned int i = 0; i < _buf_num; ++i)
_buf[i] = (unsigned short *) malloc(BUF_SIZE);
_thread = gruel::thread(_osmosdr_wait, this);
}
@ -174,11 +170,6 @@ void osmosdr_src_c::osmosdr_callback(unsigned char *buf, uint32_t len)
return;
}
if (len != BUF_SIZE) {
printf("U(%d)\n", len); fflush(stdout);
return;
}
{
boost::mutex::scoped_lock lock( _buf_mutex );

View File

@ -79,7 +79,10 @@ static const int MAX_OUT = 1; // maximum number of output streams
rtl_source_c::rtl_source_c (const std::string &args)
: gr_sync_block ("rtl_source_c",
gr_make_io_signature (MIN_IN, MAX_IN, sizeof (gr_complex)),
gr_make_io_signature (MIN_OUT, MAX_OUT, sizeof (gr_complex)))
gr_make_io_signature (MIN_OUT, MAX_OUT, sizeof (gr_complex))),
_running(true),
_auto_gain(false),
_skipped(0)
{
int ret;
unsigned int dev_index = 0, rtl_freq = 0, tuner_freq = 0;
@ -96,6 +99,8 @@ rtl_source_c::rtl_source_c (const std::string &args)
tuner_freq = (unsigned int)boost::lexical_cast< double >( dict["tuner_xtal"] );
_buf_num = BUF_NUM;
_buf_head = _buf_used = _buf_offset = 0;
_samp_avail = BUF_SIZE / BYTES_PER_SAMPLE;
if (dict.count("buffers")) {
_buf_num = (unsigned int)boost::lexical_cast< double >( dict["buffers"] );
@ -105,14 +110,6 @@ rtl_source_c::rtl_source_c (const std::string &args)
<< std::endl;
}
_buf = (unsigned short **) malloc(_buf_num * sizeof(unsigned short *));
for(unsigned int i = 0; i < _buf_num; ++i)
_buf[i] = (unsigned short *) malloc(BUF_SIZE);
_buf_head = _buf_used = _buf_offset = 0;
_samp_avail = BUF_SIZE / BYTES_PER_SAMPLE;
// create a lookup table for gr_complex values
for (unsigned int i = 0; i <= 0xffff; i++) {
#if 1 // little endian
@ -152,19 +149,18 @@ rtl_source_c::rtl_source_c (const std::string &args)
if (ret < 0)
throw std::runtime_error("Failed to set default samplerate.");
ret = rtlsdr_set_tuner_gain_mode(_dev, int(!_auto_gain));
if (ret < 0)
throw std::runtime_error("Failed to enable manual gain mode.");
ret = rtlsdr_reset_buffer( _dev );
if (ret < 0)
throw std::runtime_error("Failed to reset usb buffers.");
ret = rtlsdr_set_tuner_gain_mode(_dev, 1);
if (ret < 0)
throw std::runtime_error("Failed to enable manual gain mode.");
_buf = (unsigned short **) malloc(_buf_num * sizeof(unsigned short *));
_running = true;
_auto_gain = false;
_skipped = 0;
for(unsigned int i = 0; i < _buf_num; ++i)
_buf[i] = (unsigned short *) malloc(BUF_SIZE);
_thread = gruel::thread(_rtlsdr_wait, this);
}
@ -204,11 +200,6 @@ void rtl_source_c::rtlsdr_callback(unsigned char *buf, uint32_t len)
return;
}
if (len != BUF_SIZE) {
printf("U(%d)\n", len); fflush(stdout);
return;
}
{
boost::mutex::scoped_lock lock( _buf_mutex );