00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef INCLUDED_GR_BUFFER_H
00024 #define INCLUDED_GR_BUFFER_H
00025
00026 #include <gr_runtime.h>
00027
00028 class gr_vmcircbuf;
00029
00037 gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item);
00038
00039
00044 class gr_buffer {
00045 public:
00046 virtual ~gr_buffer ();
00047
00051 int space_available () const;
00052
00059 void *write_pointer ();
00060
00064 void update_write_pointer (int nitems);
00065
00066
00067 void set_done (bool done) { d_done = done; }
00068 bool done () const { return d_done; }
00069
00070
00071
00072 private:
00073
00074 friend class gr_buffer_reader;
00075 friend gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item);
00076 friend gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int history);
00077
00078 protected:
00079 char *d_base;
00080 unsigned int d_bufsize;
00081 private:
00082 gr_vmcircbuf *d_vmcircbuf;
00083 size_t d_sizeof_item;
00084 unsigned int d_write_index;
00085 std::vector<gr_buffer_reader *> d_readers;
00086 bool d_done;
00087
00088 unsigned
00089 index_add (unsigned a, unsigned b)
00090 {
00091 unsigned s = a + b;
00092
00093 if (s >= d_bufsize)
00094 s -= d_bufsize;
00095
00096 assert (s < d_bufsize);
00097 return s;
00098 }
00099
00100 unsigned
00101 index_sub (unsigned a, unsigned b)
00102 {
00103 int s = a - b;
00104
00105 if (s < 0)
00106 s += d_bufsize;
00107
00108 assert ((unsigned) s < d_bufsize);
00109 return s;
00110 }
00111
00112 virtual bool allocate_buffer (int nitems, size_t sizeof_item);
00113
00123 gr_buffer (int nitems, size_t sizeof_item);
00124
00128 void drop_reader (gr_buffer_reader *reader);
00129
00130 };
00131
00133 gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int history);
00134
00136 long gr_buffer_ncurrently_allocated ();
00137
00138
00139
00140
00146 class gr_buffer_reader {
00147
00148 public:
00149 ~gr_buffer_reader ();
00150
00154 int items_available () const;
00155
00160 int max_possible_items_available () const { return d_buffer->d_bufsize - 1; }
00161
00167 const void *read_pointer ();
00168
00169
00170
00171
00172 void update_read_pointer (int nitems);
00173
00174 void set_done (bool done) { d_buffer->set_done (done); }
00175 bool done () const { return d_buffer->done (); }
00176
00177
00178
00179 private:
00180
00181 friend class gr_buffer;
00182 friend gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int history);
00183
00184
00185 gr_buffer_sptr d_buffer;
00186 unsigned int d_read_index;
00187
00189 gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index);
00190 };
00191
00193 long gr_buffer_reader_ncurrently_allocated ();
00194
00195
00196 #endif