Thursday, October 11, 2012

Understanding a Singal Processing Block in GNU Radio : Boost Smart/Shared Pointers

Boost Smart Pointers

GNU Radio uses Boost smart pointers instead of regular C++ pointers. Boost is a high-quality software library with many extensions to the basic C++ language. For our purposes, Boost provides a smart implementation of C++ pointers that offers garbage collection, i.e., it deletes dynamically allocated objects when they are no longer needed.

Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners. Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed.

In fact, the smart pointers are defined as class templates(a class template provides a specification for generating classes based on parameters)

The library smart_ptr provides five smart pointer class templates, but in GNU Radio, we only use one of them: shared_ptr, defined in

To use the smarter pointer shared_ptr in GNU Radio, first we need to include the header file . Then we can use it to define a smart pointer as:

typedef boost::shared_ptr   pointer_name

There are actually many different types of smart pointers, but GNU radio uses just one of them, called a shared_ptr, which is used specifically when our dynamically allocated object has ownership shared by several pointers.

In order to declare a regular C++ pointer to an object of type gr_io_signature, we would use the followig command:

gr_io_signature* ptr;

Whereas with Boost, we would use this command:

typedef boost::shared_ptr gr_io_signature_sptr;

gr_io_signature_sptr ptr;

Now ptr is a boost smart pointer to the type of object gr_io_signature


As shown in the above code, GNU Radio uses the convention of type defining Boost smart pointers to an object of type X as X_sptr. This format makes it explicit to the user that X_sptr is a Boost smart pointer.

Lets open gr_runtime_types.h You can find it in /usr/local/include/gnuradio/gr_runtime_types.h
or (

Here you can see the typedefs for smart pointers we use throughout the runtime system

First all classes are declared and then their boost smart pointers have been typedef

class gr_basic_block;
class gr_block;
class gr_block_detail;
class gr_hier_block2;
class gr_io_signature;
class gr_buffer;
class gr_buffer_reader;
class gr_flowgraph;
class gr_flat_flowgraph;
class gr_top_block;
class gr_top_block_detail;

typedef boost::shared_ptr       gr_basic_block_sptr;
typedef boost::shared_ptr             gr_block_sptr;
typedef boost::shared_ptr    gr_block_detail_sptr;
typedef boost::shared_ptr    gr_hier_block2_sptr;
typedef boost::shared_ptr      gr_io_signature_sptr;
typedef boost::shared_ptr        gr_buffer_sptr;
typedef boost::shared_ptr    gr_buffer_reader_sptr;
typedef boost::shared_ptr         gr_flowgraph_sptr;
typedef boost::shared_ptr    gr_flat_flowgraph_sptr;
typedef boost::shared_ptr         gr_top_block_sptr;

gr_runtime.h includes gr_type.h first and in gr_type.h, we include the header file:


Reference :

No comments:

Post a Comment