Nemo  2.4.0
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
54  : _buff(NULL),_len(0),_bytes_in(0),_bytes_out(0),
off_t _tot_bytes_in
Definition: binarystoragebuffer.h:48
unsigned int _num_buckets
Definition: binarystoragebuffer.h:47
char * _buff
Definition: binarystoragebuffer.h:46
off_t _len
Definition: binarystoragebuffer.h:48
BinaryDataSaver * _myDataSaver
Definition: binarystoragebuffer.h:50
off_t _bytes_out
Definition: binarystoragebuffer.h:48
off_t _bytes_in
Definition: binarystoragebuffer.h:48

◆ ~BinaryStorageBuffer()

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

References _buff.

Member Function Documentation

◆ BSBread()

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

References read().

Referenced by BinaryDataLoader::extractPop().

+ Here is the caller graph for this function:

◆ clear()

void BinaryStorageBuffer::clear ( )
inline
63  {
64  if(_buff != NULL) delete [] _buff;
65  _buff = NULL;
66  _len = _bytes_in = _bytes_out = 0;
67  _num_buckets = 0; _tot_bytes_in = 0L;
68  }

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
71  {
72  memset(_buff,'\0',_len);
73  _bytes_in = _bytes_out = 0;
74  }

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
192  {
193 #ifdef _DEBUG_
194  std::cout<<"BinaryStorageBuffer::extend_buff"<<std::flush;
195 #endif
196 
197  char *old_buff, *new_buff;
198 
199  old_buff = _buff;
200 
201  new_buff = new char[_len + MAX_BUFF];
202 
203  if(new_buff == NULL) fatal("BinaryStorageBuffer::extend_buff::memory exhausted !!\n");
204 
205  memcpy(new_buff,_buff,_len);
206 
207  _buff = new_buff;
208 
209  _len += MAX_BUFF;
210 
211  delete [] old_buff;
212 
213 #ifdef _DEBUG_
214  std::cout<<"["<<_len<<" B]"<<std::endl;
215 #endif
216  }
#define MAX_BUFF
Definition: binarystoragebuffer.h:35
void fatal(const char *str,...)
Definition: output.cc:99

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

Referenced by store().

+ Here is the caller graph for this function:

◆ getBuffer()

char* BinaryStorageBuffer::getBuffer ( ) const
inline
58 {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
59 {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
61 {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
60 {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
221  {
222 
223  if(((_bytes_out + nb_bytes) > _bytes_in) )
224  fatal("BinaryStorageBuffer::read::attempt to read beyond buffer length (asked %i bytes)\n",nb_bytes);
225  else {
226  char *tab = (char*)out;
227  if(nb_bytes == 1) {
228  (*tab) = _buff[_bytes_out];
229  } else {
230  for(unsigned int i = 0; i < nb_bytes; ++i)
231  tab[i] = _buff[_bytes_out + i];
232  }
233  }
234  _bytes_out += nb_bytes;
235  }

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
83  {
84 #ifdef _DEBUG_
85  std::cout<<"BinaryStorageBuffer::set_buff";
86 #endif
87 
88  _myDataSaver = owner;
89 
90  if(_buff != NULL) delete [] _buff;
91 
92  _buff = new char[MAX_BUFF];
93 
94  if(_buff == NULL) fatal("BinaryStorageBuffer::set_buff::memory exhausted !!\n");
95 
96  _len = MAX_BUFF;
97 
98  _bytes_in = 0;
99 
100  _num_buckets = 0;
101 
102  _tot_bytes_in = 0L;
103 
104  memset(_buff,'\0',MAX_BUFF);
105 
106 #ifdef _DEBUG_
107  std::cout<<"[ok]"<<std::endl;
108 #endif
109  }

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
133  {
134  if(_buff != NULL) delete [] _buff;
135 
136  _buff = new char [length];
137 
138  memset(_buff,'\0', length);
139 
140  _bytes_in = _len = length ;
141 
143 
144  _bytes_out = 0;
145 
146  _num_buckets = 0;
147  }

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
114  {
115  if(_buff != NULL) delete [] _buff;
116 
117  _buff = new char [length];
118 
119  memcpy(_buff, zone, length);
120 
121  _bytes_in = _len = length ;
122 
124 
125  _bytes_out = 0;
126 
127  _num_buckets = 0;
128  }

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
152  {
153  if(_buff != NULL) delete [] _buff;
154 
155  _buff = new char [length];
156 
157  _len = length;
158 
159  off_t rout = -1; // , rcount = 0;
160 // off_t rest = length;
161 
162  //@TODO need to find a solution for the call to read, replace with fread ?
163 
164 // while(rest != 0 && rout != 0) {
165 //
166 // if( (rout = read(FD, &_buff[rcount], rest)) == -1)
167 // fatal("Binary file appears corrupted:\n >>>> error while reading data: %s (reading in %li bytes, read %li so far)\n", strerror(errno), rest, rout);
168 //
169 // rcount += rout;
170 //
171 // rest -= rout;
172 //
173 //#ifdef _DEBUG_
174 // message("BinaryStorageBuffer::set_buff_from_file:read %i bytes from file\n", rout);
175 //#endif
176 // }
177 
178  _bytes_in = rout;
179 
181 
182  _bytes_out = 0;
183 
184  _num_buckets = 0;
185 
186  return rout;
187  }

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

◆ store()

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

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.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR