forked from miek/inspectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_source_impl.cc
76 lines (64 loc) · 2.09 KB
/
memory_source_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright (C) 2015, Mike Walters <[email protected]>
*
* This file is part of inspectrum.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "memory_source_impl.h"
#include <gnuradio/io_signature.h>
#include <stdexcept>
namespace gr {
namespace blocks {
memory_source::sptr
memory_source::make(size_t itemsize)
{
return gnuradio::get_initial_sptr
(new memory_source_impl(itemsize));
}
memory_source_impl::memory_source_impl(size_t itemsize)
: sync_block("memory_source",
io_signature::make(0, 0, 0),
io_signature::make(1, 1, itemsize)),
d_itemsize(itemsize)
{
}
memory_source_impl::~memory_source_impl()
{
}
void
memory_source_impl::set_source(void *source, size_t length)
{
d_source = source;
d_length = length;
d_ptr = 0;
}
int
memory_source_impl::work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
char *outbuf = (char*)output_items[0];
long nwritten = 0;
if(!d_source)
return noutput_items;
nwritten = std::min((long)(d_length - d_ptr), (long)noutput_items);
if (nwritten >= 0) {
memcpy(outbuf, (char*)d_source + d_ptr * d_itemsize, nwritten * d_itemsize);
}
d_ptr += nwritten;
return (nwritten == 0) ? -1 : nwritten;
}
} /* namespace blocks */
} /* namespace gr */