Nemo  2.4.2
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
BinaryStorageBuffer Class Reference

A class to store any kind of data in a char buffer before unloading it in a binary data file. More...

#include <binarystoragebuffer.h>

+ Collaboration diagram for BinaryStorageBuffer:

Public Member Functions

 BinaryStorageBuffer ()
 
 ~BinaryStorageBuffer ()
 
char * getBuffer () const
 
off_t getBuffLength () const
 
off_t getTotByteRecorded () const
 
unsigned int getBytesOut () const
 
void clear ()
 
void emptyBuffer ()
 
void store (void *stream, unsigned int nb_bytes)
 
void set_buff (BinaryDataSaver *owner)
 
void set_buff (void *zone, size_t length)
 
void set_buff (size_t length)
 
off_t set_buff_from_file (int FD, size_t length)
 
void extend_buff ()
 
void read (void *out, unsigned int nb_bytes)
 
void BSBread (void *out, unsigned int nb_bytes)
 

Private Attributes

char * _buff
 
unsigned int _num_buckets
 
off_t _len
 
off_t _bytes_in
 
off_t _bytes_out
 
off_t _tot_bytes_in
 
BinaryDataSaver_myDataSaver
 

Detailed Description

A class to store any kind of data in a char buffer before unloading it in a binary data file.

Constructor & Destructor Documentation

◆ BinaryStorageBuffer()

BinaryStorageBuffer::BinaryStorageBuffer ( )
inline
53  : _buff(NULL),_len(0),_bytes_in(0),_bytes_out(0),
off_t _tot_bytes_in
Definition: binarystoragebuffer.h:47
unsigned int _num_buckets
Definition: binarystoragebuffer.h:46
char * _buff
Definition: binarystoragebuffer.h:45
off_t _len
Definition: binarystoragebuffer.h:47
BinaryDataSaver * _myDataSaver
Definition: binarystoragebuffer.h:49
off_t _bytes_out
Definition: binarystoragebuffer.h:47
off_t _bytes_in
Definition: binarystoragebuffer.h:47

◆ ~BinaryStorageBuffer()

BinaryStorageBuffer::~BinaryStorageBuffer ( )
inline
55 {if(_buff != NULL) delete[]_buff;}

References _buff.

Member Function Documentation

◆ BSBread()

void BinaryStorageBuffer::BSBread ( void *  out,
unsigned int  nb_bytes 
)
inline
237  { read(out,nb_bytes); }
void read(void *out, unsigned int nb_bytes)
Definition: binarystoragebuffer.h:219

References read().

Referenced by BinaryDataLoader::extractPop().

+ Here is the caller graph for this function:

◆ clear()

void BinaryStorageBuffer::clear ( )
inline
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  }

References _buff, _bytes_in, _bytes_out, _len, _num_buckets, and _tot_bytes_in.

Referenced by BinaryDataLoader::clear(), BinaryDataLoader::extractPop(), and BinaryDataSaver::printOffsetTable().

+ Here is the caller graph for this function:

◆ emptyBuffer()

void BinaryStorageBuffer::emptyBuffer ( )
inline
70  {
71  memset(_buff,'\0',_len);
72  _bytes_in = _bytes_out = 0;
73  }

References _buff, _bytes_in, _bytes_out, and _len.

Referenced by BinaryDataSaver::printData().

+ Here is the caller graph for this function:

◆ extend_buff()

void BinaryStorageBuffer::extend_buff ( )
inline
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  }
#define MAX_BUFF
Definition: binarystoragebuffer.h:34
void fatal(const char *str,...)
Definition: output.cc:98

References _buff, _len, fatal(), and MAX_BUFF.

Referenced by store().

+ Here is the caller graph for this function:

◆ getBuffer()

char* BinaryStorageBuffer::getBuffer ( ) const
inline
57 {return _buff;}

References _buff.

Referenced by BinaryDataLoader::extractPop(), BinaryDataSaver::printData(), and BinaryDataSaver::printOffsetTable().

+ Here is the caller graph for this function:

◆ getBuffLength()

off_t BinaryStorageBuffer::getBuffLength ( ) const
inline
58 {return _bytes_in;}

References _bytes_in.

Referenced by BinaryDataSaver::FHwrite(), BinaryDataSaver::printData(), BinaryDataSaver::printOffsetTable(), and BinaryDataSaver::storeData().

+ Here is the caller graph for this function:

◆ getBytesOut()

unsigned int BinaryStorageBuffer::getBytesOut ( ) const
inline
60 {return _bytes_out;}

References _bytes_out.

Referenced by Metapop::retrieve_data().

+ Here is the caller graph for this function:

◆ getTotByteRecorded()

off_t BinaryStorageBuffer::getTotByteRecorded ( ) const
inline
59 {return _tot_bytes_in;}

References _tot_bytes_in.

Referenced by Metapop::store_data().

+ Here is the caller graph for this function:

◆ read()

void BinaryStorageBuffer::read ( void *  out,
unsigned int  nb_bytes 
)
inline
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  }

References _buff, _bytes_in, _bytes_out, and fatal().

Referenced by BSBread(), Individual::retrieve_data(), Metapop::retrieve_data(), TProtoBDMI::retrieve_data(), TT_BDMI::retrieve_data(), TTDeletMutations_bitstring::retrieve_data(), TProtoDeletMutations_bitstring::retrieve_data(), TTDispersal::retrieve_data(), TProtoDispersal::retrieve_data(), TTNeutralGenes_byte::retrieve_data(), TProtoNeutralGenes::retrieve_data(), TTNeutralGenes_bitstring::retrieve_data(), TTQuanti_continuous::retrieve_data(), TTQuanti_diallelic::retrieve_data(), TProtoQuanti::retrieve_data(), TTQuanti_diallelic_bitstring::retrieve_data(), and TTWolbachia::retrieve_data().

+ Here is the caller graph for this function:

◆ set_buff() [1/3]

void BinaryStorageBuffer::set_buff ( BinaryDataSaver owner)
inline
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  }

References _buff, _bytes_in, _len, _myDataSaver, _num_buckets, _tot_bytes_in, fatal(), and MAX_BUFF.

Referenced by BinaryDataSaver::execute(), and BinaryDataLoader::extractPop().

+ Here is the caller graph for this function:

◆ set_buff() [2/3]

void BinaryStorageBuffer::set_buff ( size_t  length)
inline
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  }

References _buff, _bytes_in, _bytes_out, _len, _num_buckets, and _tot_bytes_in.

◆ set_buff() [3/3]

void BinaryStorageBuffer::set_buff ( void *  zone,
size_t  length 
)
inline
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  }

References _buff, _bytes_in, _bytes_out, _len, _num_buckets, and _tot_bytes_in.

◆ set_buff_from_file()

off_t BinaryStorageBuffer::set_buff_from_file ( int  FD,
size_t  length 
)
inline
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  }

References _buff, _bytes_in, _bytes_out, _len, _num_buckets, and _tot_bytes_in.

◆ store()

void BinaryStorageBuffer::store ( void *  stream,
unsigned int  nb_bytes 
)
37 {
38 
39  while( !((_bytes_in + nb_bytes) < _len) ) extend_buff();
40 
41  if(nb_bytes == 1) {
42 
43  _buff[_bytes_in] = *(char*)stream;
44 
45  } else {
46 
47  char *tab = (char*)stream;
48 
49  for(unsigned int i = 0; i < nb_bytes; i++)
50  _buff[_bytes_in + i] = tab[i];
51  }
52 
53  _bytes_in += nb_bytes;
54 
55  _tot_bytes_in += nb_bytes;
56 
57  if(_bytes_in > MAX_BUCKET) {
58 
59  // cout<<"\n BinaryStorageBuffer::store::bucket "<<_num_buckets<<" is full, emptying\n";
60 
61  // tell the binary data saver to empty to bucket to file
63 
64  // cout<<"wrote "<<_tot_bytes_in<<"B so far\n";
65 
66  _num_buckets++;
67 
68  _bytes_in = 0;
69  }
70 }
#define MAX_BUCKET
Definition: binarystoragebuffer.h:36
void printData()
Definition: binarydatasaver.cc:366
void extend_buff()
Definition: binarystoragebuffer.h:190

References _buff, _bytes_in, _len, _myDataSaver, _num_buckets, _tot_bytes_in, extend_buff(), MAX_BUCKET, and BinaryDataSaver::printData().

Referenced by BinaryDataSaver::printOffsetTable(), Individual::store_data(), Metapop::store_data(), TProtoBDMI::store_data(), TT_BDMI::store_data(), TTDeletMutations_bitstring::store_data(), TProtoDeletMutations_bitstring::store_data(), TTDispersal::store_data(), TProtoDispersal::store_data(), TTNeutralGenes_byte::store_data(), TProtoNeutralGenes::store_data(), TTNeutralGenes_bitstring::store_data(), TTQuanti_continuous::store_data(), TTQuanti_diallelic::store_data(), TProtoQuanti::store_data(), TTQuanti_diallelic_bitstring::store_data(), TTWolbachia::store_data(), and BinaryDataSaver::storeData().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _buff

char* BinaryStorageBuffer::_buff
private

◆ _bytes_in

off_t BinaryStorageBuffer::_bytes_in
private

◆ _bytes_out

off_t BinaryStorageBuffer::_bytes_out
private

◆ _len

off_t BinaryStorageBuffer::_len
private

◆ _myDataSaver

BinaryDataSaver* BinaryStorageBuffer::_myDataSaver
private

Referenced by set_buff(), and store().

◆ _num_buckets

unsigned int BinaryStorageBuffer::_num_buckets
private

◆ _tot_bytes_in

off_t BinaryStorageBuffer::_tot_bytes_in
private

The documentation for this class was generated from the following files:

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR