GNU Radio Manual and C++ API Reference  3.7.13.4
The Free & Open Software Radio Ecosystem
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  void reset_nitem_counter() { d_abs_write_offset = 0; }
104 
105  size_t get_sizeof_item() { return d_sizeof_item; }
106 
107  /*!
108  * \brief Adds a new tag to the buffer.
109  *
110  * \param tag the new tag
111  */
112  void add_item_tag(const tag_t &tag);
113 
114  /*!
115  * \brief Removes an existing tag from the buffer.
116  *
117  * If no such tag is found, does nothing.
118  * Note: Doesn't actually physically delete the tag, but
119  * marks it as deleted. For the user, this has the same effect:
120  * Any subsequent calls to get_tags_in_range() will not return
121  * the tag.
122  *
123  * \param tag the tag that needs to be removed
124  * \param id the unique ID of the block calling this function
125  */
126  void remove_item_tag(const tag_t &tag, long id);
127 
128  /*!
129  * \brief Removes all tags before \p max_time from buffer
130  *
131  * \param max_time the time (item number) to trim up until.
132  */
133  void prune_tags(uint64_t max_time);
134 
135  std::multimap<uint64_t,tag_t>::iterator get_tags_begin() { return d_item_tags.begin(); }
136  std::multimap<uint64_t,tag_t>::iterator get_tags_end() { return d_item_tags.end(); }
137  std::multimap<uint64_t,tag_t>::iterator get_tags_lower_bound(uint64_t x) { return d_item_tags.lower_bound(x); }
138  std::multimap<uint64_t,tag_t>::iterator get_tags_upper_bound(uint64_t x) { return d_item_tags.upper_bound(x); }
139 
140  // -------------------------------------------------------------------------
141 
142  private:
143  friend class buffer_reader;
144  friend GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item, block_sptr link);
145  friend GR_RUNTIME_API buffer_reader_sptr buffer_add_reader
146  (buffer_sptr buf, int nzero_preload, block_sptr link, int delay);
147 
148  protected:
149  char *d_base; // base address of buffer
150  unsigned int d_bufsize; // in items
151 
152  // Keep track of maximum sample delay of any reader; Only prune tags past this.
154 
155  private:
156  gr::vmcircbuf *d_vmcircbuf;
157  size_t d_sizeof_item; // in bytes
158  std::vector<buffer_reader *> d_readers;
159  boost::weak_ptr<block> d_link; // block that writes to this buffer
160 
161  //
162  // The mutex protects d_write_index, d_abs_write_offset, d_done, d_item_tags
163  // and the d_read_index's and d_abs_read_offset's in the buffer readers.
164  //
165  gr::thread::mutex d_mutex;
166  unsigned int d_write_index; // in items [0,d_bufsize)
167  uint64_t d_abs_write_offset; // num items written since the start
168  bool d_done;
169  std::multimap<uint64_t,tag_t> d_item_tags;
170  uint64_t d_last_min_items_read;
171 
172  unsigned index_add(unsigned a, unsigned b)
173  {
174  unsigned s = a + b;
175 
176  if(s >= d_bufsize)
177  s -= d_bufsize;
178 
179  assert(s < d_bufsize);
180  return s;
181  }
182 
183  unsigned index_sub(unsigned a, unsigned b)
184  {
185  int s = a - b;
186 
187  if(s < 0)
188  s += d_bufsize;
189 
190  assert((unsigned) s < d_bufsize);
191  return s;
192  }
193 
194  virtual bool allocate_buffer(int nitems, size_t sizeof_item);
195 
196  /*!
197  * \brief constructor is private. Use gr_make_buffer to create instances.
198  *
199  * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
200  *
201  * \param nitems is the minimum number of items the buffer will hold.
202  * \param sizeof_item is the size of an item in bytes.
203  * \param link is the block that writes to this buffer.
204  *
205  * The total size of the buffer will be rounded up to a system
206  * dependent boundary. This is typically the system page size, but
207  * under MS windows is 64KB.
208  */
209  buffer(int nitems, size_t sizeof_item, block_sptr link);
210 
211  /*!
212  * \brief disassociate \p reader from this buffer
213  */
214  void drop_reader(buffer_reader *reader);
215  };
216 
217  /*!
218  * \brief Create a new gr::buffer_reader and attach it to buffer \p buf
219  * \param buf is the buffer the \p gr::buffer_reader reads from.
220  * \param nzero_preload -- number of zero items to "preload" into buffer.
221  * \param link is the block that reads from the buffer using this gr::buffer_reader.
222  * \param delay Optional setting to declare the buffer's sample delay.
223  */
224  GR_RUNTIME_API buffer_reader_sptr
225  buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link=block_sptr(), int delay=0);
226 
227  //! returns # of buffers currently allocated
229 
230 
231  // ---------------------------------------------------------------------------
232 
233  /*!
234  * \brief How we keep track of the readers of a gr::buffer.
235  * \ingroup internal
236  */
238  {
239  public:
240  ~buffer_reader();
241 
242  /*!
243  * Declares the sample delay for this reader.
244  *
245  * See gr::block::declare_sample_delay for details.
246  *
247  * \param delay The new sample delay
248  */
249  void declare_sample_delay(unsigned delay);
250 
251  /*!
252  * Gets the sample delay for this reader.
253  *
254  * See gr::block::sample_delay for details.
255  */
256  unsigned sample_delay() const;
257 
258  /*!
259  * \brief Return number of items available for reading.
260  */
261  int items_available() const;
262 
263  /*!
264  * \brief Return buffer this reader reads from.
265  */
266  buffer_sptr buffer() const { return d_buffer; }
267 
268  /*!
269  * \brief Return maximum number of items that could ever be available for reading.
270  * This is used as a sanity check in the scheduler to avoid looping forever.
271  */
272  int max_possible_items_available() const { return d_buffer->d_bufsize - 1; }
273 
274  /*!
275  * \brief return pointer to read buffer.
276  *
277  * The return value points to items_available() number of items
278  */
279  const void *read_pointer();
280 
281  /*
282  * \brief tell buffer we read \p items from it
283  */
284  void update_read_pointer(int nitems);
285 
286  void set_done(bool done) { d_buffer->set_done(done); }
287  bool done() const { return d_buffer->done(); }
288 
289  gr::thread::mutex *mutex() { return d_buffer->mutex(); }
290 
291  uint64_t nitems_read() { return d_abs_read_offset; }
292 
293  void reset_nitem_counter() { d_abs_read_offset = 0; }
294 
295  size_t get_sizeof_item() { return d_buffer->get_sizeof_item(); }
296 
297  /*!
298  * \brief Return the block that reads via this reader.
299  *
300  */
301  block_sptr link() { return block_sptr(d_link); }
302 
303  /*!
304  * \brief Given a [start,end), returns a vector all tags in the range.
305  *
306  * Get a vector of tags in given range. Range of counts is from start to end-1.
307  *
308  * Tags are tuples of:
309  * (item count, source id, key, value)
310  *
311  * \param v a vector reference to return tags into
312  * \param abs_start a uint64 count of the start of the range of interest
313  * \param abs_end a uint64 count of the end of the range of interest
314  * \param id the unique ID of the block to make sure already deleted tags are not returned
315  */
316  void get_tags_in_range(std::vector<tag_t> &v,
317  uint64_t abs_start,
318  uint64_t abs_end,
319  long id);
320 
321  // -------------------------------------------------------------------------
322 
323  private:
324  friend class buffer;
325  friend GR_RUNTIME_API buffer_reader_sptr
326  buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link, int delay);
327 
328  buffer_sptr d_buffer;
329  unsigned int d_read_index; // in items [0,d->buffer.d_bufsize)
330  uint64_t d_abs_read_offset; // num items seen since the start
331  boost::weak_ptr<block> d_link; // block that reads via this buffer reader
332  unsigned d_attr_delay; // sample delay attribute for tag propagation
333 
334  //! constructor is private. Use gr::buffer::add_reader to create instances
335  buffer_reader(buffer_sptr buffer, unsigned int read_index,
336  block_sptr link);
337  };
338 
339  //! returns # of buffer_readers currently allocated
341 
342 } /* namespace gr */
343 
344 #endif /* INCLUDED_GR_RUNTIME_BUFFER_H */
buffer_sptr buffer() const
Return buffer this reader reads from.
Definition: buffer.h:266
Definition: tags.h:31
How we keep track of the readers of a gr::buffer.
Definition: buffer.h:237
const char * base() const
return the base address of the buffer
Definition: buffer.h:73
unsigned d_max_reader_delay
Definition: buffer.h:153
void set_done(bool done)
Definition: buffer.h:286
char * d_base
Definition: buffer.h:149
uint64_t nitems_read()
Definition: buffer.h:291
void reset_nitem_counter()
Definition: buffer.h:293
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:137
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:272
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.
void reset_nitem_counter()
Definition: buffer.h:103
Definition: cc_common.h:45
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
bool done() const
Definition: buffer.h:287
Single writer, multiple reader fifo.
Definition: buffer.h:55
Include this header to use the message passing features.
Definition: logger.h:695
block_sptr link()
Return the block that reads via this reader.
Definition: buffer.h:301
size_t get_sizeof_item()
Definition: buffer.h:105
size_t get_sizeof_item()
Definition: buffer.h:295
std::multimap< uint64_t, tag_t >::iterator get_tags_end()
Definition: buffer.h:136
gr::thread::mutex * mutex()
Definition: buffer.h:289
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:138
std::multimap< uint64_t, tag_t >::iterator get_tags_begin()
Definition: buffer.h:135
boost::mutex mutex
Definition: thread.h:48
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:150
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