Skip to content

Commit

Permalink
runtime: replace the DEFINE_CUSTOM_BUFFER_TYPE() macro function with
Browse files Browse the repository at this point in the history
some advanced template magic; also a few minor type corrections for
consistency

Signed-off-by: David Sorber <[email protected]>
  • Loading branch information
David Sorber committed Oct 18, 2021
1 parent 6fe74d5 commit ad391b0
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 85 deletions.
5 changes: 2 additions & 3 deletions gnuradio-runtime/include/gnuradio/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,7 @@ class GR_RUNTIME_API block : public basic_block
* that is incompatible with the default buffer type created by this block.
*
*/
buffer_sptr
replace_buffer(uint32_t src_port, uint32_t dst_port, block_sptr block_owner);
buffer_sptr replace_buffer(size_t src_port, size_t dst_port, block_sptr block_owner);

// --------------- Performance counter functions -------------

Expand Down Expand Up @@ -943,7 +942,7 @@ class GR_RUNTIME_API block : public basic_block
* that the downstream max number of items must be passed in to this
* function for consideration.
*/
buffer_sptr allocate_buffer(int port,
buffer_sptr allocate_buffer(size_t port,
int downstream_max_nitems,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult);
Expand Down
21 changes: 7 additions & 14 deletions gnuradio-runtime/include/gnuradio/buffer_double_mapped.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ namespace gr {

class vmcircbuf;

/*!
* \brief Note this function is only used and really intended to be used in
* qa_buffer.cc for the unit tests of buffer_double_mapped.
*
*/
GR_RUNTIME_API buffer_sptr make_buffer_double_mapped(int nitems,
size_t sizeof_item,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult,
block_sptr link = block_sptr(),
block_sptr buf_owner = block_sptr());

DEFINE_CUSTOM_BUFFER_TYPE(DEFAULT_NON_CUSTOM, make_buffer_double_mapped);

/*!
* \brief Single writer, multiple reader fifo.
* \ingroup internal
Expand All @@ -44,6 +30,13 @@ class GR_RUNTIME_API buffer_double_mapped : public buffer
public:
static buffer_type type;

static buffer_sptr make_buffer(int nitems,
size_t sizeof_item,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult,
block_sptr link = block_sptr(),
block_sptr buf_owner = block_sptr());

gr::logger_ptr d_logger;
gr::logger_ptr d_debug_logger;

Expand Down
79 changes: 40 additions & 39 deletions gnuradio-runtime/include/gnuradio/buffer_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,11 @@
#include <functional>
#include <mutex>
#include <string>
#include <typeinfo>
#include <vector>

namespace gr {

// This is the function pointer declaration for the factory-like functions
// used to create buffer subclasses
typedef buffer_sptr (*factory_func_ptr)(int nitems,
size_t sizeof_item,
uint64_t downstrea_lcm_nitems,
uint32_t downstream_max_out_mult,
block_sptr link,
block_sptr buf_owner);


/*!
* \brief Base class for describing a buffer's type.
*/
Expand All @@ -45,10 +36,7 @@ class GR_RUNTIME_API buffer_type_base

// Temporarily define copy constructor to work around pybind issue with
// default non-copyable function argument
buffer_type_base(buffer_type_base const& other)
: d_name(other.d_name), d_factory(other.d_factory)
{
}
buffer_type_base(buffer_type_base const& other) : d_name(other.d_name) {}

void operator=(buffer_type_base const&) = delete;

Expand Down Expand Up @@ -76,42 +64,55 @@ class GR_RUNTIME_API buffer_type_base
/*!
* \brief Make and return a buffer subclass of the corresponding type
*/
inline buffer_sptr make_buffer(int nitems,
size_t sizeof_item,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult,
block_sptr link,
block_sptr buf_owner) const
virtual buffer_sptr
make_buffer([[maybe_unused]] int nitems,
[[maybe_unused]] size_t sizeof_item,
[[maybe_unused]] uint64_t downstream_lcm_nitems,
[[maybe_unused]] uint32_t downstream_max_out_mult,
[[maybe_unused]] block_sptr link = block_sptr(),
[[maybe_unused]] block_sptr buf_owner = block_sptr()) const
{
// Delegate call to factory function
return d_factory(nitems,
sizeof_item,
downstream_lcm_nitems,
downstream_max_out_mult,
link,
buf_owner);
// NOTE: this is *never* intended to be called directly, instead the overridden
// version from a derived class (created from the template below) will be
// called.
return nullptr;
}

protected:
const std::string d_name;
factory_func_ptr d_factory;

// Protected constructor
buffer_type_base(const char* name, factory_func_ptr factory_func)
: d_name(name), d_factory(factory_func)
{
}
buffer_type_base(const std::string name) : d_name(name) {}
};

typedef const buffer_type_base& buffer_type;
typedef std::vector<std::reference_wrapper<const buffer_type_base>> gr_vector_buffer_type;

#define DEFINE_CUSTOM_BUFFER_TYPE(CLASSNAME, FACTORY_FUNC_PTR) \
class GR_RUNTIME_API buftype_##CLASSNAME : public buffer_type_base \
{ \
public: \
buftype_##CLASSNAME() : buffer_type_base(#CLASSNAME, FACTORY_FUNC_PTR) {} \
};
/*!
* \brief Template used to create buffer types. Note that the factory_class parameter
* must contain a static function make_buffer() that matches the signature below
* and will be used to create instances of the corresponding buffer type.
*/
template <typename classname, typename factory_class>
struct buftype : public buffer_type_base {
public:
using factory = factory_class;
buffer_sptr make_buffer(int nitems,
size_t sizeof_item,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult,
block_sptr link = block_sptr(),
block_sptr buf_owner = block_sptr()) const override
{
return factory::make_buffer(nitems,
sizeof_item,
downstream_lcm_nitems,
downstream_max_out_mult,
link,
buf_owner);
}

buftype<classname, factory_class>() : buffer_type_base(typeid(classname).name()) {}
};

} // namespace gr

Expand Down
2 changes: 1 addition & 1 deletion gnuradio-runtime/include/gnuradio/custom_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ class custom_lock

} /* namespace gr */

#endif /* INCLUDED_GR_CUSTOM_LOCK_H */
#endif /* INCLUDED_GR_CUSTOM_LOCK_H */
11 changes: 7 additions & 4 deletions gnuradio-runtime/include/gnuradio/host_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class GR_RUNTIME_API host_buffer : public buffer_single_mapped

static buffer_type type;

static buffer_sptr make_buffer(int nitems,
size_t sizeof_item,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult,
block_sptr link = block_sptr(),
block_sptr buf_owner = block_sptr());

virtual ~host_buffer();

/*!
Expand Down Expand Up @@ -117,10 +124,6 @@ class GR_RUNTIME_API host_buffer : public buffer_single_mapped
block_sptr buf_owner);
};

// See buffer_type.h for details on this macro. It is used here to generate
// compile-time class representing the host_buffer classes "type".
DEFINE_CUSTOM_BUFFER_TYPE(HOST_BUFFER, &host_buffer::make_host_buffer);

} // namespace gr

#endif /* INCLUDED_HOST_BUFFER_H */
3 changes: 2 additions & 1 deletion gnuradio-runtime/include/gnuradio/io_signature.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ class GR_RUNTIME_API io_signature
*
* \param min_streams specify minimum number of streams (>= 0)
* \param max_streams specify maximum number of streams (>= min_streams or -1 ->
* infinite) \param sizeof_stream_items specify the size of the items in the streams
* infinite)
* \param sizeof_stream_items specify the size of the items in the streams
* \param buftypes the type of buffer each stream will should use
*
* If there are more streams than there are entries in
Expand Down
4 changes: 2 additions & 2 deletions gnuradio-runtime/lib/block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ void block::allocate_detail(int ninputs,
}

buffer_sptr
block::replace_buffer(uint32_t src_port, uint32_t dst_port, block_sptr block_owner)
block::replace_buffer(size_t src_port, size_t dst_port, block_sptr block_owner)
{
block_detail_sptr detail_ = detail();
buffer_sptr orig_buffer = detail_->output(src_port);
Expand All @@ -440,7 +440,7 @@ bool block::update_rate() const { return d_update_rate; }

void block::enable_update_rate(bool en) { d_update_rate = en; }

buffer_sptr block::allocate_buffer(int port,
buffer_sptr block::allocate_buffer(size_t port,
int downstream_max_nitems,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult)
Expand Down
17 changes: 9 additions & 8 deletions gnuradio-runtime/lib/buffer_double_mapped.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ namespace gr {
*
* type_size * nitems == k * page_size
*/
static inline long minimum_buffer_items(long type_size, long page_size)
static inline long minimum_buffer_items(size_t type_size, size_t page_size)
{
return page_size / GR_GCD(type_size, page_size);
}

buffer_type buffer_double_mapped::type(buftype_DEFAULT_NON_CUSTOM{});
buffer_type
buffer_double_mapped::type(buftype<buffer_double_mapped, buffer_double_mapped>{});

buffer_double_mapped::buffer_double_mapped(int nitems,
size_t sizeof_item,
Expand Down Expand Up @@ -67,12 +68,12 @@ buffer_double_mapped::buffer_double_mapped(int nitems,
// NB: Added the extra 'block_sptr unused' parameter so that the
// call signature matches the other factory-like functions used to create
// the buffer_single_mapped subclasses
buffer_sptr make_buffer_double_mapped(int nitems,
size_t sizeof_item,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult,
block_sptr link,
block_sptr unused)
buffer_sptr buffer_double_mapped::make_buffer(int nitems,
size_t sizeof_item,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult,
block_sptr link,
block_sptr unused)
{
return buffer_sptr(new buffer_double_mapped(
nitems, sizeof_item, downstream_lcm_nitems, downstream_max_out_mult, link));
Expand Down
18 changes: 16 additions & 2 deletions gnuradio-runtime/lib/host_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

namespace gr {

buffer_type host_buffer::type(buftype_HOST_BUFFER{});

void* host_buffer::device_memcpy(void* dest, const void* src, std::size_t count)
{
// There is no spoon...er... device so fake it out using regular memcpy
Expand All @@ -28,6 +26,22 @@ void* host_buffer::device_memmove(void* dest, const void* src, std::size_t count
return std::memmove(dest, src, count);
}

buffer_type host_buffer::type(buftype<host_buffer, host_buffer>{});

buffer_sptr host_buffer::make_buffer(int nitems,
size_t sizeof_item,
uint64_t downstream_lcm_nitems,
uint32_t downstream_max_out_mult,
block_sptr link,
block_sptr buf_owner)
{
return buffer_sptr(new host_buffer(nitems,
sizeof_item,
downstream_lcm_nitems,
downstream_max_out_mult,
link,
buf_owner));
}

host_buffer::host_buffer(int nitems,
size_t sizeof_item,
Expand Down
16 changes: 8 additions & 8 deletions gnuradio-runtime/lib/qa_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ static void t0_body()
int nitems = 4000 / sizeof(int);
int counter = 0;

gr::buffer_sptr buf(
gr::make_buffer_double_mapped(nitems, sizeof(int), nitems, 1, gr::block_sptr()));
gr::buffer_sptr buf(gr::buffer_double_mapped::make_buffer(
nitems, sizeof(int), nitems, 1, gr::block_sptr()));

int last_sa;
int sa;
Expand Down Expand Up @@ -77,8 +77,8 @@ static void t1_body()
int write_counter = 0;
int read_counter = 0;

gr::buffer_sptr buf(
gr::make_buffer_double_mapped(nitems, sizeof(int), nitems, 1, gr::block_sptr()));
gr::buffer_sptr buf(gr::buffer_double_mapped::make_buffer(
nitems, sizeof(int), nitems, 1, gr::block_sptr()));
gr::buffer_reader_sptr r1(gr::buffer_add_reader(buf, 0, gr::block_sptr()));

int sa;
Expand Down Expand Up @@ -149,8 +149,8 @@ static void t2_body()

int nitems = (64 * (1L << 10)) / sizeof(int); // 64K worth of ints

gr::buffer_sptr buf(
gr::make_buffer_double_mapped(nitems, sizeof(int), nitems, 1, gr::block_sptr()));
gr::buffer_sptr buf(gr::buffer_double_mapped::make_buffer(
nitems, sizeof(int), nitems, 1, gr::block_sptr()));
gr::buffer_reader_sptr r1(gr::buffer_add_reader(buf, 0, gr::block_sptr()));

int read_counter = 0;
Expand Down Expand Up @@ -215,8 +215,8 @@ static void t3_body()
int nitems = (64 * (1L << 10)) / sizeof(int);

static const int N = 5;
gr::buffer_sptr buf(
gr::make_buffer_double_mapped(nitems, sizeof(int), nitems, 1, gr::block_sptr()));
gr::buffer_sptr buf(gr::buffer_double_mapped::make_buffer(
nitems, sizeof(int), nitems, 1, gr::block_sptr()));
gr::buffer_reader_sptr reader[N];
int read_counter[N];
int write_counter = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/* BINDTOOL_GEN_AUTOMATIC(0) */
/* BINDTOOL_USE_PYGCCXML(0) */
/* BINDTOOL_HEADER_FILE(block.h) */
/* BINDTOOL_HEADER_FILE_HASH(23fce54cc3292f62ca2551fb3f409f77) */
/* BINDTOOL_HEADER_FILE_HASH(1c265259ee70fb7b389cc57a9334a1f1) */
/***********************************************************************************/

#include <pybind11/complex.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/* BINDTOOL_GEN_AUTOMATIC(0) */
/* BINDTOOL_USE_PYGCCXML(0) */
/* BINDTOOL_HEADER_FILE(buffer_type.h) */
/* BINDTOOL_HEADER_FILE_HASH(6ed358bbc956244006073acb8c2efab2) */
/* BINDTOOL_HEADER_FILE_HASH(879f41aa2a2009fd5fb31bc24ecef768) */
/***********************************************************************************/

#include <pybind11/complex.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/* BINDTOOL_GEN_AUTOMATIC(0) */
/* BINDTOOL_USE_PYGCCXML(0) */
/* BINDTOOL_HEADER_FILE(io_signature.h) */
/* BINDTOOL_HEADER_FILE_HASH(baf27e696237b6542ec62a4c5627ea1d) */
/* BINDTOOL_HEADER_FILE_HASH(2d21df486462de11f4eb25681101c0c6) */
/***********************************************************************************/

#include <pybind11/complex.h>
Expand Down

0 comments on commit ad391b0

Please sign in to comment.