GNU Radio C++ API
gr_buffer.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2009,2010,2011 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_BUFFER_H
24 #define INCLUDED_GR_BUFFER_H
25 
26 #include <gr_core_api.h>
27 #include <gr_runtime_types.h>
28 #include <boost/weak_ptr.hpp>
29 #include <gruel/thread.h>
30 #include <gr_tags.h>
31 #include <deque>
32 
33 class gr_vmcircbuf;
34 
35 /*!
36  * \brief Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
37  *
38  * The total size of the buffer will be rounded up to a system
39  * dependent boundary. This is typically the system page size, but
40  * under MS windows is 64KB.
41  *
42  * \param nitems is the minimum number of items the buffer will hold.
43  * \param sizeof_item is the size of an item in bytes.
44  * \param link is the block that writes to this buffer.
45  */
46 GR_CORE_API gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link=gr_block_sptr());
47 
48 
49 /*!
50  * \brief Single writer, multiple reader fifo.
51  * \ingroup internal
52  */
54  public:
55 
56  virtual ~gr_buffer ();
57 
58  /*!
59  * \brief return number of items worth of space available for writing
60  */
61  int space_available ();
62 
63  /*!
64  * \brief return size of this buffer in items
65  */
66  int bufsize() const { return d_bufsize; }
67 
68  /*!
69  * \brief return pointer to write buffer.
70  *
71  * The return value points at space that can hold at least
72  * space_available() items.
73  */
74  void *write_pointer ();
75 
76  /*!
77  * \brief tell buffer that we wrote \p nitems into it
78  */
79  void update_write_pointer (int nitems);
80 
81  void set_done (bool done);
82  bool done () const { return d_done; }
83 
84  /*!
85  * \brief Return the block that writes to this buffer.
86  */
87  gr_block_sptr link() { return gr_block_sptr(d_link); }
88 
89  size_t nreaders() const { return d_readers.size(); }
90  gr_buffer_reader* reader(size_t index) { return d_readers[index]; }
91 
92  gruel::mutex *mutex() { return &d_mutex; }
93 
94  uint64_t nitems_written() { return d_abs_write_offset; }
95 
96 
97  /*!
98  * \brief Adds a new tag to the buffer.
99  *
100  * \param tag the new tag
101  */
102  void add_item_tag(const gr_tag_t &tag);
103 
104  /*!
105  * \brief Removes all tags before \p max_time from buffer
106  *
107  * \param max_time the time (item number) to trim up until.
108  */
109  void prune_tags(uint64_t max_time);
110 
111  std::deque<gr_tag_t>::iterator get_tags_begin() { return d_item_tags.begin(); }
112  std::deque<gr_tag_t>::iterator get_tags_end() { return d_item_tags.end(); }
113 
114  // -------------------------------------------------------------------------
115 
116  private:
117 
118  friend class gr_buffer_reader;
119  friend GR_CORE_API gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
121 
122  protected:
123  char *d_base; // base address of buffer
124  unsigned int d_bufsize; // in items
125  private:
126  gr_vmcircbuf *d_vmcircbuf;
127  size_t d_sizeof_item; // in bytes
128  std::vector<gr_buffer_reader *> d_readers;
129  boost::weak_ptr<gr_block> d_link; // block that writes to this buffer
130 
131  //
132  // The mutex protects d_write_index, d_abs_write_offset, d_done, d_item_tags
133  // and the d_read_index's and d_abs_read_offset's in the buffer readers.
134  //
135  gruel::mutex d_mutex;
136  unsigned int d_write_index; // in items [0,d_bufsize)
137  uint64_t d_abs_write_offset; // num items written since the start
138  bool d_done;
139  std::deque<gr_tag_t> d_item_tags;
140  uint64_t d_last_min_items_read;
141 
142  unsigned
143  index_add (unsigned a, unsigned b)
144  {
145  unsigned s = a + b;
146 
147  if (s >= d_bufsize)
148  s -= d_bufsize;
149 
150  assert (s < d_bufsize);
151  return s;
152  }
153 
154  unsigned
155  index_sub (unsigned a, unsigned b)
156  {
157  int s = a - b;
158 
159  if (s < 0)
160  s += d_bufsize;
161 
162  assert ((unsigned) s < d_bufsize);
163  return s;
164  }
165 
166  virtual bool allocate_buffer (int nitems, size_t sizeof_item);
167 
168  /*!
169  * \brief constructor is private. Use gr_make_buffer to create instances.
170  *
171  * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
172  *
173  * \param nitems is the minimum number of items the buffer will hold.
174  * \param sizeof_item is the size of an item in bytes.
175  * \param link is the block that writes to this buffer.
176  *
177  * The total size of the buffer will be rounded up to a system
178  * dependent boundary. This is typically the system page size, but
179  * under MS windows is 64KB.
180  */
181  gr_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
182 
183  /*!
184  * \brief disassociate \p reader from this buffer
185  */
186  void drop_reader (gr_buffer_reader *reader);
187 
188 };
189 
190 /*!
191  * \brief Create a new gr_buffer_reader and attach it to buffer \p buf
192  * \param buf is the buffer the \p gr_buffer_reader reads from.
193  * \param nzero_preload -- number of zero items to "preload" into buffer.
194  * \param link is the block that reads from the buffer using this gr_buffer_reader.
195  */
198 
199 //! returns # of gr_buffers currently allocated
201 
202 
203 // ---------------------------------------------------------------------------
204 
205 /*!
206  * \brief How we keep track of the readers of a gr_buffer.
207  * \ingroup internal
208  */
209 
211  public:
212 
213  ~gr_buffer_reader ();
214 
215  /*!
216  * \brief Return number of items available for reading.
217  */
218  int items_available () const;
219 
220  /*!
221  * \brief Return buffer this reader reads from.
222  */
223  gr_buffer_sptr buffer () const { return d_buffer; }
224 
225 
226  /*!
227  * \brief Return maximum number of items that could ever be available for reading.
228  * This is used as a sanity check in the scheduler to avoid looping forever.
229  */
230  int max_possible_items_available () const { return d_buffer->d_bufsize - 1; }
231 
232  /*!
233  * \brief return pointer to read buffer.
234  *
235  * The return value points to items_available() number of items
236  */
237  const void *read_pointer ();
238 
239  /*
240  * \brief tell buffer we read \p items from it
241  */
242  void update_read_pointer (int nitems);
243 
244  void set_done (bool done) { d_buffer->set_done (done); }
245  bool done () const { return d_buffer->done (); }
246 
247  gruel::mutex *mutex() { return d_buffer->mutex(); }
248 
249 
250  uint64_t nitems_read() { return d_abs_read_offset; }
251 
252  /*!
253  * \brief Return the block that reads via this reader.
254  *
255  */
256  gr_block_sptr link() { return gr_block_sptr(d_link); }
257 
258 
259  /*!
260  * \brief Given a [start,end), returns a vector all tags in the range.
261  *
262  * Get a vector of tags in given range. Range of counts is from start to end-1.
263  *
264  * Tags are tuples of:
265  * (item count, source id, key, value)
266  *
267  * \param v a vector reference to return tags into
268  * \param abs_start a uint64 count of the start of the range of interest
269  * \param abs_end a uint64 count of the end of the range of interest
270  */
271  void get_tags_in_range(std::vector<gr_tag_t> &v,
272  uint64_t abs_start,
273  uint64_t abs_end);
274 
275  // -------------------------------------------------------------------------
276 
277  private:
278 
279  friend class gr_buffer;
281  gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link);
282 
283 
284  gr_buffer_sptr d_buffer;
285  unsigned int d_read_index; // in items [0,d->buffer.d_bufsize)
286  uint64_t d_abs_read_offset; // num items seen since the start
287  boost::weak_ptr<gr_block> d_link; // block that reads via this buffer reader
288 
289  //! constructor is private. Use gr_buffer::add_reader to create instances
290  gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index, gr_block_sptr link);
291 };
292 
293 //! returns # of gr_buffer_readers currently allocated
295 
296 
297 #endif /* INCLUDED_GR_BUFFER_H */