Nemo  2.4.2
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
binarystoragebuffer.h
Go to the documentation of this file.
1 
27 #ifndef BINARYSTORAGEBUFFER_H
28 #define BINARYSTORAGEBUFFER_H
29 
30 #include <iostream>
31 #include <string.h>
32 #include "output.h"
33 
34 #define MAX_BUFF 10000000 //10MB
35 
36 #define MAX_BUCKET 500000000 //500MB
37 
38 class BinaryDataSaver; //forward declaration
39 
43 
44 private:
45  char* _buff;
46  unsigned int _num_buckets;
48 
50 
51 public:
52 
55  ~BinaryStorageBuffer() {if(_buff != NULL) delete[]_buff;}
56 
57  char* getBuffer ( ) const {return _buff;}
58  off_t getBuffLength ( ) const {return _bytes_in;}
59  off_t getTotByteRecorded ( ) const {return _tot_bytes_in;}
60  unsigned int getBytesOut ( ) const {return _bytes_out;}
61  void clear ( )
62  {
63  if(_buff != NULL) delete [] _buff;
64  _buff = NULL;
65  _len = _bytes_in = _bytes_out = 0;
66  _num_buckets = 0; _tot_bytes_in = 0L;
67  }
68 
69  void emptyBuffer ( )
70  {
71  memset(_buff,'\0',_len);
72  _bytes_in = _bytes_out = 0;
73  }
74  // ----------------------------------------------------------------------------------------
75  // store
76  // ----------------------------------------------------------------------------------------
77  void store (void* stream, unsigned int nb_bytes);
78  // ----------------------------------------------------------------------------------------
79  // set_buff
80  // ----------------------------------------------------------------------------------------
81  inline void set_buff(BinaryDataSaver* owner)
82  {
83 #ifdef _DEBUG_
84  std::cout<<"BinaryStorageBuffer::set_buff";
85 #endif
86 
87  _myDataSaver = owner;
88 
89  if(_buff != NULL) delete [] _buff;
90 
91  _buff = new char[MAX_BUFF];
92 
93  if(_buff == NULL) fatal("BinaryStorageBuffer::set_buff::memory exhausted !!\n");
94 
95  _len = MAX_BUFF;
96 
97  _bytes_in = 0;
98 
99  _num_buckets = 0;
100 
101  _tot_bytes_in = 0L;
102 
103  memset(_buff,'\0',MAX_BUFF);
104 
105 #ifdef _DEBUG_
106  std::cout<<"[ok]"<<std::endl;
107 #endif
108  }
109  // ----------------------------------------------------------------------------------------
110  // set_buff
111  // ----------------------------------------------------------------------------------------
112  inline void set_buff(void* zone, size_t length)
113  {
114  if(_buff != NULL) delete [] _buff;
115 
116  _buff = new char [length];
117 
118  memcpy(_buff, zone, length);
119 
120  _bytes_in = _len = length ;
121 
123 
124  _bytes_out = 0;
125 
126  _num_buckets = 0;
127  }
128  // ----------------------------------------------------------------------------------------
129  // set_buff
130  // ----------------------------------------------------------------------------------------
131  inline void set_buff(size_t length)
132  {
133  if(_buff != NULL) delete [] _buff;
134 
135  _buff = new char [length];
136 
137  memset(_buff,'\0', length);
138 
139  _bytes_in = _len = length ;
140 
142 
143  _bytes_out = 0;
144 
145  _num_buckets = 0;
146  }
147  // ----------------------------------------------------------------------------------------
148  // set_buff_from_file
149  // ----------------------------------------------------------------------------------------
150  inline off_t set_buff_from_file(int FD, size_t length)
151  {
152  if(_buff != NULL) delete [] _buff;
153 
154  _buff = new char [length];
155 
156  _len = length;
157 
158  off_t rout = -1; // , rcount = 0;
159 // off_t rest = length;
160 
161  //@TODO need to find a solution for the call to read, replace with fread ?
162 
163 // while(rest != 0 && rout != 0) {
164 //
165 // if( (rout = read(FD, &_buff[rcount], rest)) == -1)
166 // fatal("Binary file appears corrupted:\n >>>> error while reading data: %s (reading in %li bytes, read %li so far)\n", strerror(errno), rest, rout);
167 //
168 // rcount += rout;
169 //
170 // rest -= rout;
171 //
172 //#ifdef _DEBUG_
173 // message("BinaryStorageBuffer::set_buff_from_file:read %i bytes from file\n", rout);
174 //#endif
175 // }
176 
177  _bytes_in = rout;
178 
180 
181  _bytes_out = 0;
182 
183  _num_buckets = 0;
184 
185  return rout;
186  }
187  // ----------------------------------------------------------------------------------------
188  // extend_buff
189  // ----------------------------------------------------------------------------------------
190  inline void extend_buff()
191  {
192 #ifdef _DEBUG_
193  std::cout<<"BinaryStorageBuffer::extend_buff"<<std::flush;
194 #endif
195 
196  char *old_buff, *new_buff;
197 
198  old_buff = _buff;
199 
200  new_buff = new char[_len + MAX_BUFF];
201 
202  if(new_buff == NULL) fatal("BinaryStorageBuffer::extend_buff::memory exhausted !!\n");
203 
204  memcpy(new_buff,_buff,_len);
205 
206  _buff = new_buff;
207 
208  _len += MAX_BUFF;
209 
210  delete [] old_buff;
211 
212 #ifdef _DEBUG_
213  std::cout<<"["<<_len<<" B]"<<std::endl;
214 #endif
215  }
216  // ----------------------------------------------------------------------------------------
217  // read
218  // ----------------------------------------------------------------------------------------
219  inline void read (void *out, unsigned int nb_bytes)
220  {
221 
222  if(((_bytes_out + nb_bytes) > _bytes_in) )
223  fatal("BinaryStorageBuffer::read::attempt to read beyond buffer length (asked %i bytes)\n",nb_bytes);
224  else {
225  char *tab = (char*)out;
226  if(nb_bytes == 1) {
227  (*tab) = _buff[_bytes_out];
228  } else {
229  for(unsigned int i = 0; i < nb_bytes; ++i)
230  tab[i] = _buff[_bytes_out + i];
231  }
232  }
233  _bytes_out += nb_bytes;
234  }
235 
236  void BSBread(void *out, unsigned int nb_bytes)
237  { read(out,nb_bytes); }
238 };
239 
240 
241 #endif
#define MAX_BUFF
Definition: binarystoragebuffer.h:34
A class to handle simulation data saving in binary format.
Definition: binarydatasaver.h:42
A class to store any kind of data in a char buffer before unloading it in a binary data file.
Definition: binarystoragebuffer.h:42
void read(void *out, unsigned int nb_bytes)
Definition: binarystoragebuffer.h:219
off_t getBuffLength() const
Definition: binarystoragebuffer.h:58
off_t _tot_bytes_in
Definition: binarystoragebuffer.h:47
char * getBuffer() const
Definition: binarystoragebuffer.h:57
unsigned int _num_buckets
Definition: binarystoragebuffer.h:46
void emptyBuffer()
Definition: binarystoragebuffer.h:69
unsigned int getBytesOut() const
Definition: binarystoragebuffer.h:60
char * _buff
Definition: binarystoragebuffer.h:45
void set_buff(size_t length)
Definition: binarystoragebuffer.h:131
off_t _len
Definition: binarystoragebuffer.h:47
void set_buff(BinaryDataSaver *owner)
Definition: binarystoragebuffer.h:81
void clear()
Definition: binarystoragebuffer.h:61
off_t getTotByteRecorded() const
Definition: binarystoragebuffer.h:59
BinaryDataSaver * _myDataSaver
Definition: binarystoragebuffer.h:49
off_t _bytes_out
Definition: binarystoragebuffer.h:47
BinaryStorageBuffer()
Definition: binarystoragebuffer.h:53
off_t _bytes_in
Definition: binarystoragebuffer.h:47
off_t set_buff_from_file(int FD, size_t length)
Definition: binarystoragebuffer.h:150
void store(void *stream, unsigned int nb_bytes)
Definition: binarystoragebuffer.cc:36
void set_buff(void *zone, size_t length)
Definition: binarystoragebuffer.h:112
void BSBread(void *out, unsigned int nb_bytes)
Definition: binarystoragebuffer.h:236
~BinaryStorageBuffer()
Definition: binarystoragebuffer.h:55
void extend_buff()
Definition: binarystoragebuffer.h:190
void fatal(const char *str,...)
Definition: output.cc:98
Nemo2.

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR