3 * Copyright 2004,2009 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
23 #ifndef INCLUDED_GR_BUFFER_H
24 #define INCLUDED_GR_BUFFER_H
26 #include <gr_runtime_types.h>
27 #include <boost/weak_ptr.hpp>
28 #include <gruel/thread.h>
33 * \brief Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
35 * The total size of the buffer will be rounded up to a system
36 * dependent boundary. This is typically the system page size, but
37 * under MS windows is 64KB.
39 * \param nitems is the minimum number of items the buffer will hold.
40 * \param sizeof_item is the size of an item in bytes.
41 * \param link is the block that writes to this buffer.
43 gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link=gr_block_sptr());
47 * \brief Single writer, multiple reader fifo.
53 virtual ~gr_buffer ();
56 * \brief return number of items worth of space available for writing
58 int space_available ();
61 * \brief return size of this buffer in items
63 int bufsize() const { return d_bufsize; }
66 * \brief return pointer to write buffer.
68 * The return value points at space that can hold at least
69 * space_available() items.
71 void *write_pointer ();
74 * \brief tell buffer that we wrote \p nitems into it
76 void update_write_pointer (int nitems);
78 void set_done (bool done);
79 bool done () const { return d_done; }
82 * \brief Return the block that writes to this buffer.
84 gr_block_sptr link() { return gr_block_sptr(d_link); }
86 size_t nreaders() const { return d_readers.size(); }
87 gr_buffer_reader* reader(size_t index) { return d_readers[index]; }
89 gruel::mutex *mutex() { return &d_mutex; }
91 // -------------------------------------------------------------------------
95 friend class gr_buffer_reader;
96 friend gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
97 friend gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link);
100 char *d_base; // base address of buffer
101 unsigned int d_bufsize; // in items
103 gr_vmcircbuf *d_vmcircbuf;
104 size_t d_sizeof_item; // in bytes
105 std::vector<gr_buffer_reader *> d_readers;
106 boost::weak_ptr<gr_block> d_link; // block that writes to this buffer
109 // The mutex protects d_write_index, d_done and the d_read_index's in the buffer readers.
111 gruel::mutex d_mutex;
112 unsigned int d_write_index; // in items [0,d_bufsize)
116 index_add (unsigned a, unsigned b)
123 assert (s < d_bufsize);
128 index_sub (unsigned a, unsigned b)
135 assert ((unsigned) s < d_bufsize);
139 virtual bool allocate_buffer (int nitems, size_t sizeof_item);
142 * \brief constructor is private. Use gr_make_buffer to create instances.
144 * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
146 * \param nitems is the minimum number of items the buffer will hold.
147 * \param sizeof_item is the size of an item in bytes.
148 * \param link is the block that writes to this buffer.
150 * The total size of the buffer will be rounded up to a system
151 * dependent boundary. This is typically the system page size, but
152 * under MS windows is 64KB.
154 gr_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
157 * \brief disassociate \p reader from this buffer
159 void drop_reader (gr_buffer_reader *reader);
164 * \brief Create a new gr_buffer_reader and attach it to buffer \p buf
165 * \param buf is the buffer the \p gr_buffer_reader reads from.
166 * \param nzero_preload -- number of zero items to "preload" into buffer.
167 * \param link is the block that reads from the buffer using this gr_buffer_reader.
169 gr_buffer_reader_sptr
170 gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link=gr_block_sptr());
172 //! returns # of gr_buffers currently allocated
173 long gr_buffer_ncurrently_allocated ();
176 // ---------------------------------------------------------------------------
179 * \brief How we keep track of the readers of a gr_buffer.
183 class gr_buffer_reader {
186 ~gr_buffer_reader ();
189 * \brief Return number of items available for reading.
191 int items_available () const;
194 * \brief Return buffer this reader reads from.
196 gr_buffer_sptr buffer () const { return d_buffer; }
200 * \brief Return maximum number of items that could ever be available for reading.
201 * This is used as a sanity check in the scheduler to avoid looping forever.
203 int max_possible_items_available () const { return d_buffer->d_bufsize - 1; }
206 * \brief return pointer to read buffer.
208 * The return value points to items_available() number of items
210 const void *read_pointer ();
213 * \brief tell buffer we read \p items from it
215 void update_read_pointer (int nitems);
217 void set_done (bool done) { d_buffer->set_done (done); }
218 bool done () const { return d_buffer->done (); }
220 gruel::mutex *mutex() { return d_buffer->mutex(); }
224 * \brief Return the block that reads via this reader.
226 gr_block_sptr link() { return gr_block_sptr(d_link); }
228 // -------------------------------------------------------------------------
232 friend class gr_buffer;
233 friend gr_buffer_reader_sptr
234 gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link);
237 gr_buffer_sptr d_buffer;
238 unsigned int d_read_index; // in items [0,d->buffer.d_bufsize)
239 boost::weak_ptr<gr_block> d_link; // block that reads via this buffer reader
241 //! constructor is private. Use gr_buffer::add_reader to create instances
242 gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index, gr_block_sptr link);
245 //! returns # of gr_buffer_readers currently allocated
246 long gr_buffer_reader_ncurrently_allocated ();
249 #endif /* INCLUDED_GR_BUFFER_H */