Nemo  2.3.56
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
binarystoragebuffer.h
Go to the documentation of this file.
1
29#ifndef BINARYSTORAGEBUFFER_H
30#define BINARYSTORAGEBUFFER_H
31
32#include <iostream>
33#include <string.h>
34#include "output.h"
35
36#define MAX_BUFF 10000000 //10MB
37
38#define MAX_BUCKET 500000000 //500MB
39
40class BinaryDataSaver; //forward declaration
41
45
46private:
47 char* _buff;
48 unsigned int _num_buckets;
50
52
53public:
54
57 ~BinaryStorageBuffer() {if(_buff != NULL) delete[]_buff;}
58
59 char* getBuffer ( ) const {return _buff;}
60 off_t getBuffLength ( ) const {return _bytes_in;}
61 off_t getTotByteRecorded ( ) const {return _tot_bytes_in;}
62 unsigned int getBytesOut ( ) const {return _bytes_out;}
63 void clear ( )
64 {
65 if(_buff != NULL) delete [] _buff;
66 _buff = NULL;
69 }
70
71 void emptyBuffer ( )
72 {
73 memset(_buff,'\0',_len);
75 }
76 // ----------------------------------------------------------------------------------------
77 // store
78 // ----------------------------------------------------------------------------------------
79 void store (void* stream, unsigned int nb_bytes);
80 // ----------------------------------------------------------------------------------------
81 // set_buff
82 // ----------------------------------------------------------------------------------------
83 inline void set_buff(BinaryDataSaver* owner)
84 {
85#ifdef _DEBUG_
86 std::cout<<"BinaryStorageBuffer::set_buff";
87#endif
88
89 _myDataSaver = owner;
90
91 if(_buff != NULL) delete [] _buff;
92
93 _buff = new char[MAX_BUFF];
94
95 if(_buff == NULL) fatal("BinaryStorageBuffer::set_buff::memory exhausted !!\n");
96
97 _len = MAX_BUFF;
98
99 _bytes_in = 0;
100
101 _num_buckets = 0;
102
103 _tot_bytes_in = 0L;
104
105 memset(_buff,'\0',MAX_BUFF);
106
107#ifdef _DEBUG_
108 std::cout<<"[ok]"<<std::endl;
109#endif
110 }
111 // ----------------------------------------------------------------------------------------
112 // set_buff
113 // ----------------------------------------------------------------------------------------
114 inline void set_buff(void* zone, size_t length)
115 {
116 if(_buff != NULL) delete [] _buff;
117
118 _buff = new char [length];
119
120 memcpy(_buff, zone, length);
121
122 _bytes_in = _len = length ;
123
125
126 _bytes_out = 0;
127
128 _num_buckets = 0;
129 }
130 // ----------------------------------------------------------------------------------------
131 // extend_buff
132 // ----------------------------------------------------------------------------------------
133 inline void extend_buff()
134 {
135#ifdef _DEBUG_
136 std::cout<<"BinaryStorageBuffer::extend_buff"<<std::flush;
137#endif
138
139 char *old_buff, *new_buff;
140
141 old_buff = _buff;
142
143 new_buff = new char[_len + MAX_BUFF];
144
145 if(new_buff == NULL) fatal("BinaryStorageBuffer::extend_buff::memory exhausted !!\n");
146
147 memcpy(new_buff,_buff,_len);
148
149 _buff = new_buff;
150
151 _len += MAX_BUFF;
152
153 delete [] old_buff;
154
155#ifdef _DEBUG_
156 std::cout<<"["<<_len<<" B]"<<std::endl;
157#endif
158 }
159 // ----------------------------------------------------------------------------------------
160 // read
161 // ----------------------------------------------------------------------------------------
162 inline void read (void *out, unsigned int nb_bytes)
163 {
164
165 if(((_bytes_out + nb_bytes) > _bytes_in) )
166 fatal("BinaryStorageBuffer::read::attempt to read beyond buffer length (asked %i bytes)\n",nb_bytes);
167 else {
168 char *tab = (char*)out;
169 if(nb_bytes == 1) {
170 (*tab) = _buff[_bytes_out];
171 } else {
172 for(unsigned int i = 0; i < nb_bytes; ++i)
173 tab[i] = _buff[_bytes_out + i];
174 }
175 }
176 _bytes_out += nb_bytes;
177 }
178
179 void BSBread(void *out, unsigned int nb_bytes)
180 { read(out,nb_bytes); }
181};
182
183
184#endif
#define MAX_BUFF
Definition: binarystoragebuffer.h:36
A class to handle simulation data saving in binary format.
Definition: binarydatasaver.h:44
A class to store any kind of data in a char buffer before unloading it in a binary data file.
Definition: binarystoragebuffer.h:44
void read(void *out, unsigned int nb_bytes)
Definition: binarystoragebuffer.h:162
off_t getBuffLength() const
Definition: binarystoragebuffer.h:60
off_t _tot_bytes_in
Definition: binarystoragebuffer.h:49
unsigned int _num_buckets
Definition: binarystoragebuffer.h:48
void emptyBuffer()
Definition: binarystoragebuffer.h:71
unsigned int getBytesOut() const
Definition: binarystoragebuffer.h:62
char * _buff
Definition: binarystoragebuffer.h:47
off_t _len
Definition: binarystoragebuffer.h:49
void set_buff(BinaryDataSaver *owner)
Definition: binarystoragebuffer.h:83
void clear()
Definition: binarystoragebuffer.h:63
off_t getTotByteRecorded() const
Definition: binarystoragebuffer.h:61
BinaryDataSaver * _myDataSaver
Definition: binarystoragebuffer.h:51
off_t _bytes_out
Definition: binarystoragebuffer.h:49
BinaryStorageBuffer()
Definition: binarystoragebuffer.h:55
off_t _bytes_in
Definition: binarystoragebuffer.h:49
char * getBuffer() const
Definition: binarystoragebuffer.h:59
void store(void *stream, unsigned int nb_bytes)
Definition: binarystoragebuffer.cc:16
void set_buff(void *zone, size_t length)
Definition: binarystoragebuffer.h:114
void BSBread(void *out, unsigned int nb_bytes)
Definition: binarystoragebuffer.h:179
~BinaryStorageBuffer()
Definition: binarystoragebuffer.h:57
void extend_buff()
Definition: binarystoragebuffer.h:133
void fatal(const char *str,...)
Definition: output.cc:96

Generated for Nemo v2.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR