uhd: Avoid reallocation of buffers every read

Buffer size is based on num of chans and rxBuffer size is based on num
of chans and rx_spp, and both are available and set during open(), so no
need to waste time allocating and freeing them everytime we read from
the device.

Change-Id: I8c881c9c303c80f323825d85a924d74b76d2ce47
This commit is contained in:
Pau Espin 2019-04-30 18:51:00 +02:00
parent ac927b2690
commit ef192d303c
2 changed files with 10 additions and 15 deletions

View File

@ -516,6 +516,11 @@ int uhd_device::open(const std::string &args, int ref, bool swap_channels)
for (size_t i = 0; i < rx_buffers.size(); i++)
rx_buffers[i] = new smpl_buf(buf_len, rx_rate);
// Create vector buffer
pkt_bufs = std::vector<std::vector<short> >(chans, std::vector<short>(2 * rx_spp));
for (size_t i = 0; i < pkt_bufs.size(); i++)
pkt_ptrs.push_back(&pkt_bufs[i].front());
// Initialize and shadow gain values
init_gains();
@ -549,13 +554,6 @@ bool uhd_device::flush_recv(size_t num_pkts)
size_t num_smpls;
float timeout = UHD_RESTART_TIMEOUT;
std::vector<std::vector<short> >
pkt_bufs(chans, std::vector<short>(2 * rx_spp));
std::vector<short *> pkt_ptrs;
for (size_t i = 0; i < pkt_bufs.size(); i++)
pkt_ptrs.push_back(&pkt_bufs[i].front());
ts_initial = 0;
while (!ts_initial || (num_pkts-- > 0)) {
num_smpls = rx_stream->recv(pkt_ptrs, rx_spp, md,
@ -720,14 +718,6 @@ int uhd_device::readSamples(std::vector<short *> &bufs, int len, bool *overrun,
return 0;
}
// Create vector buffer
std::vector<std::vector<short> >
pkt_bufs(chans, std::vector<short>(2 * rx_spp));
std::vector<short *> pkt_ptrs;
for (size_t i = 0; i < pkt_bufs.size(); i++)
pkt_ptrs.push_back(&pkt_bufs[i].front());
// Receive samples from the usrp until we have enough
while (rx_buffers[0]->avail_smpls(timestamp) < len) {
thread_enable_cancel(false);

View File

@ -152,6 +152,11 @@ private:
TIMESTAMP ts_initial, ts_offset;
std::vector<smpl_buf *> rx_buffers;
/* Sample buffers used to receive samples from UHD: */
std::vector<std::vector<short> > pkt_bufs;
/* Used to call UHD API: Buffer pointer of each elem in pkt_ptrs will
point to corresponding buffer of vector pkt_bufs. */
std::vector<short *> pkt_ptrs;
void init_gains();
void set_channels(bool swap);