GNU Radio Manual and C++ API Reference  3.7.9.2
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
buffer.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2009-2011,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef INCLUDED_GR_RUNTIME_BUFFER_H
24 #define INCLUDED_GR_RUNTIME_BUFFER_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/runtime_types.h>
28 #include <gnuradio/tags.h>
29 #include <boost/weak_ptr.hpp>
30 #include <gnuradio/thread/thread.h>
31 #include <map>
32 
33 namespace gr {
34 
35  class vmcircbuf;
36 
37  /*!
38  * \brief Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
39  *
40  * The total size of the buffer will be rounded up to a system
41  * dependent boundary. This is typically the system page size, but
42  * under MS windows is 64KB.
43  *
44  * \param nitems is the minimum number of items the buffer will hold.
45  * \param sizeof_item is the size of an item in bytes.
46  * \param link is the block that writes to this buffer.
47  */
48  GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item,
49  block_sptr link=block_sptr());
50 
51  /*!
52  * \brief Single writer, multiple reader fifo.
53  * \ingroup internal
54  */
56  {
57  public:
58  virtual ~buffer();
59 
60  /*!
61  * \brief return number of items worth of space available for writing
62  */
63  int space_available();
64 
65  /*!
66  * \brief return size of this buffer in items
67  */
68  int bufsize() const { return d_bufsize; }
69 
70  /*!
71  * \brief return the base address of the buffer
72  */
73  const char* base() const { return static_cast<const char*>(d_base); }
74 
75  /*!
76  * \brief return pointer to write buffer.
77  *
78  * The return value points at space that can hold at least
79  * space_available() items.
80  */
81  void *write_pointer();
82 
83  /*!
84  * \brief tell buffer that we wrote \p nitems into it
85  */
86  void update_write_pointer(int nitems);
87 
88  void set_done(bool done);
89  bool done() const { return d_done; }
90 
91  /*!
92  * \brief Return the block that writes to this buffer.
93  */
94  block_sptr link() { return block_sptr(d_link); }
95 
96  size_t nreaders() const { return d_readers.size(); }
97  buffer_reader* reader(size_t index) { return d_readers[index]; }
98 
99  gr::thread::mutex *mutex() { return &d_mutex; }
100 
101  uint64_t nitems_written() { return d_abs_write_offset; }
102 
103  size_t get_sizeof_item() { return d_sizeof_item; }
104 
105  /*!
106  * \brief Adds a new tag to the buffer.
107  *
108  * \param tag the new tag
109  */
110  void add_item_tag(const tag_t &tag);
111 
112  /*!
113  * \brief Removes an existing tag from the buffer.
114  *
115  * If no such tag is found, does nothing.
116  * Note: Doesn't actually physically delete the tag, but
117  * marks it as deleted. For the user, this has the same effect:
118  * Any subsequent calls to get_tags_in_range() will not return
119  * the tag.
120  *
121  * \param tag the tag that needs to be removed
122  * \param id the unique ID of the block calling this function
123  */
124  void remove_item_tag(const tag_t &tag, long id);
125 
126  /*!
127  * \brief Removes all tags before \p max_time from buffer
128  *
129  * \param max_time the time (item number) to trim up until.
130  */
131  void prune_tags(uint64_t max_time);
132 
133  std::multimap<uint64_t,tag_t>::iterator get_tags_begin() { return d_item_tags.begin(); }
134  std::multimap<uint64_t,tag_t>::iterator get_tags_end() { return d_item_tags.end(); }
135  std::multimap<uint64_t,tag_t>::iterator get_tags_lower_bound(uint64_t x) { return d_item_tags.lower_bound(x); }
136  std::multimap<uint64_t,tag_t>::iterator get_tags_upper_bound(uint64_t x) { return d_item_tags.upper_bound(x); }
137 
138  // -------------------------------------------------------------------------
139 
140  private:
141  friend class buffer_reader;
142  friend GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item, block_sptr link);
143  friend GR_RUNTIME_API buffer_reader_sptr buffer_add_reader
144  (buffer_sptr buf, int nzero_preload, block_sptr link, int delay);
145 
146  protected:
147  char *d_base; // base address of buffer
148  unsigned int d_bufsize; // in items
149 
150  // Keep track of maximum sample delay of any reader; Only prune tags past this.
152 
153  private:
154  gr::vmcircbuf *d_vmcircbuf;
155  size_t d_sizeof_item; // in bytes
156  std::vector<buffer_reader *> d_readers;
157  boost::weak_ptr<block> d_link; // block that writes to this buffer
158 
159  //
160  // The mutex protects d_write_index, d_abs_write_offset, d_done, d_item_tags
161  // and the d_read_index's and d_abs_read_offset's in the buffer readers.
162  //
163  gr::thread::mutex d_mutex;
164  unsigned int d_write_index; // in items [0,d_bufsize)
165  uint64_t d_abs_write_offset; // num items written since the start
166  bool d_done;
167  std::multimap<uint64_t,tag_t> d_item_tags;
168  uint64_t d_last_min_items_read;
169 
170  unsigned index_add(unsigned a, unsigned b)
171  {
172  unsigned s = a + b;
173 
174  if(s >= d_bufsize)
175  s -= d_bufsize;
176 
177  assert(s < d_bufsize);
178  return s;
179  }
180 
181  unsigned index_sub(unsigned a, unsigned b)
182  {
183  int s = a - b;
184 
185  if(s < 0)
186  s += d_bufsize;
187 
188  assert((unsigned) s < d_bufsize);
189  return s;
190  }
191 
192  virtual bool allocate_buffer(int nitems, size_t sizeof_item);
193 
194  /*!
195  * \brief constructor is private. Use gr_make_buffer to create instances.
196  *
197  * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
198  *
199  * \param nitems is the minimum number of items the buffer will hold.
200  * \param sizeof_item is the size of an item in bytes.
201  * \param link is the block that writes to this buffer.
202  *
203  * The total size of the buffer will be rounded up to a system
204  * dependent boundary. This is typically the system page size, but
205  * under MS windows is 64KB.
206  */
207  buffer(int nitems, size_t sizeof_item, block_sptr link);
208 
209  /*!
210  * \brief disassociate \p reader from this buffer
211  */
212  void drop_reader(buffer_reader *reader);
213  };
214 
215  /*!
216  * \brief Create a new gr::buffer_reader and attach it to buffer \p buf
217  * \param buf is the buffer the \p gr::buffer_reader reads from.
218  * \param nzero_preload -- number of zero items to "preload" into buffer.
219  * \param link is the block that reads from the buffer using this gr::buffer_reader.
220  * \param delay Optional setting to declare the buffer's sample delay.
221  */
222  GR_RUNTIME_API buffer_reader_sptr
223  buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link=block_sptr(), int delay=0);
224 
225  //! returns # of buffers currently allocated
227 
228 
229  // ---------------------------------------------------------------------------
230 
231  /*!
232  * \brief How we keep track of the readers of a gr::buffer.
233  * \ingroup internal
234  */
236  {
237  public:
238  ~buffer_reader();
239 
240  /*!
241  * Declares the sample delay for this reader.
242  *
243  * See gr::block::declare_sample_delay for details.
244  *
245  * \param delay The new sample delay
246  */
247  void declare_sample_delay(unsigned delay);
248 
249  /*!
250  * Gets the sample delay for this reader.
251  *
252  * See gr::block::sample_delay for details.
253  */
254  unsigned sample_delay() const;
255 
256  /*!
257  * \brief Return number of items available for reading.
258  */
259  int items_available() const;
260 
261  /*!
262  * \brief Return buffer this reader reads from.
263  */
264  buffer_sptr buffer() const { return d_buffer; }
265 
266  /*!
267  * \brief Return maximum number of items that could ever be available for reading.
268  * This is used as a sanity check in the scheduler to avoid looping forever.
269  */
270  int max_possible_items_available() const { return d_buffer->d_bufsize - 1; }
271 
272  /*!
273  * \brief return pointer to read buffer.
274  *
275  * The return value points to items_available() number of items
276  */
277  const void *read_pointer();
278 
279  /*
280  * \brief tell buffer we read \p items from it
281  */
282  void update_read_pointer(int nitems);
283 
284  void set_done(bool done) { d_buffer->set_done(done); }
285  bool done() const { return d_buffer->done(); }
286 
287  gr::thread::mutex *mutex() { return d_buffer->mutex(); }
288 
289  uint64_t nitems_read() { return d_abs_read_offset; }
290 
291  size_t get_sizeof_item() { return d_buffer->get_sizeof_item(); }
292 
293  /*!
294  * \brief Return the block that reads via this reader.
295  *
296  */
297  block_sptr link() { return block_sptr(d_link); }
298 
299  /*!
300  * \brief Given a [start,end), returns a vector all tags in the range.
301  *
302  * Get a vector of tags in given range. Range of counts is from start to end-1.
303  *
304  * Tags are tuples of:
305  * (item count, source id, key, value)
306  *
307  * \param v a vector reference to return tags into
308  * \param abs_start a uint64 count of the start of the range of interest
309  * \param abs_end a uint64 count of the end of the range of interest
310  * \param id the unique ID of the block to make sure already deleted tags are not returned
311  */
312  void get_tags_in_range(std::vector<tag_t> &v,
313  uint64_t abs_start,
314  uint64_t abs_end,
315  long id);
316 
317  // -------------------------------------------------------------------------
318 
319  private:
320  friend class buffer;
321  friend GR_RUNTIME_API buffer_reader_sptr
322  buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link, int delay);
323 
324  buffer_sptr d_buffer;
325  unsigned int d_read_index; // in items [0,d->buffer.d_bufsize)
326  uint64_t d_abs_read_offset; // num items seen since the start
327  boost::weak_ptr<block> d_link; // block that reads via this buffer reader
328  unsigned d_attr_delay; // sample delay attribute for tag propagation
329 
330  //! constructor is private. Use gr::buffer::add_reader to create instances
331  buffer_reader(buffer_sptr buffer, unsigned int read_index,
332  block_sptr link);
333  };
334 
335  //! returns # of buffer_readers currently allocated
337 
338 } /* namespace gr */
339 
340 #endif /* INCLUDED_GR_RUNTIME_BUFFER_H */
buffer_sptr buffer() const
Return buffer this reader reads from.
Definition: buffer.h:264
Definition: tags.h:31
How we keep track of the readers of a gr::buffer.
Definition: buffer.h:235
const char * base() const
return the base address of the buffer
Definition: buffer.h:73
unsigned d_max_reader_delay
Definition: buffer.h:151
void set_done(bool done)
Definition: buffer.h:284
char * d_base
Definition: buffer.h:147
uint64_t nitems_read()
Definition: buffer.h:289
gr::thread::mutex * mutex()
Definition: buffer.h:99
std::multimap< uint64_t, tag_t >::iterator get_tags_lower_bound(uint64_t x)
Definition: buffer.h:135
int max_possible_items_available() const
Return maximum number of items that could ever be available for reading. This is used as a sanity che...
Definition: buffer.h:270
GR_RUNTIME_API buffer_reader_sptr buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link=block_sptr(), int delay=0)
Create a new gr::buffer_reader and attach it to buffer buf.
Definition: cc_common.h:45
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
bool done() const
Definition: buffer.h:285
Single writer, multiple reader fifo.
Definition: buffer.h:55
Include this header to use the message passing features.
Definition: logger.h:131
block_sptr link()
Return the block that reads via this reader.
Definition: buffer.h:297
size_t get_sizeof_item()
Definition: buffer.h:103
size_t get_sizeof_item()
Definition: buffer.h:291
std::multimap< uint64_t, tag_t >::iterator get_tags_end()
Definition: buffer.h:134
gr::thread::mutex * mutex()
Definition: buffer.h:287
GR_RUNTIME_API long buffer_ncurrently_allocated()
returns # of buffers currently allocated
block_sptr link()
Return the block that writes to this buffer.
Definition: buffer.h:94
buffer_reader * reader(size_t index)
Definition: buffer.h:97
size_t nreaders() const
Definition: buffer.h:96
uint64_t nitems_written()
Definition: buffer.h:101
std::multimap< uint64_t, tag_t >::iterator get_tags_upper_bound(uint64_t x)
Definition: buffer.h:136
std::multimap< uint64_t, tag_t >::iterator get_tags_begin()
Definition: buffer.h:133
boost::mutex mutex
Definition: thread.h:46
bool done() const
Definition: buffer.h:89
int bufsize() const
return size of this buffer in items
Definition: buffer.h:68
unsigned int d_bufsize
Definition: buffer.h:148
GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item, block_sptr link=block_sptr())
Allocate a buffer that holds at least nitems of size sizeof_item.
GR_RUNTIME_API long buffer_reader_ncurrently_allocated()
returns # of buffer_readers currently allocated