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