10 typedef unsigned long long bufsize_type;
11 const int FLOATSIZE = sizeof(float);
15 // in_mem_arr must be allocated memory before the Buffer is constructed
16 Buffer(int nMB, int nSamples, int vlen, float* in_mem_arr, const char* tmpF) {
18 size = bufsize_type(nMB) * 1024 * 1024 / FLOATSIZE / vlen;
19 if (size > (bufsize_type)nSamples) size = nSamples;
20 general_assert(size > 0, "Memory allocated for credibility intervals is not enough!");
23 buffer = new float[size];
24 ftmpOut.open(tmpF, std::ios::binary);
25 pthread_mutex_init(&lock, NULL);
28 this->nSamples = nSamples;
30 this->in_mem_arr = in_mem_arr;
34 if (fr < to) flushToTempFile();
37 pthread_mutex_destroy(&lock);
41 void write(float value, float *vec) {
42 pthread_assert(pthread_mutex_lock(&lock), "pthread_mutex_lock", "Error occurred while acquiring the lock!");
43 if (size - cpos < bufsize_type(vlen)) flushToTempFile();
44 in_mem_arr[to] = value;
45 memcpy(buffer + cpos, vec, FLOATSIZE * vlen);
48 pthread_assert(pthread_mutex_unlock(&lock), "pthread_mutex_unlock", "Error occurred while releasing the lock!");
52 bufsize_type size, cpos; // cpos : current position
56 std::ofstream ftmpOut;
59 int fr, to; // each flush, sample fr .. to - 1
60 int nSamples, vlen; // vlen : vector length
62 void flushToTempFile() {
63 std::streampos gap1 = std::streampos(fr) * FLOATSIZE;
64 std::streampos gap2 = std::streampos(nSamples - to) * FLOATSIZE;
67 ftmpOut.seekp(0, std::ios::beg);
68 for (int i = 0; i < vlen; i++) {
70 ftmpOut.seekp(gap1, std::ios::cur);
71 for (int j = fr; j < to; j++) {
72 ftmpOut.write((char*)p, FLOATSIZE);
75 ftmpOut.seekp(gap2, std::ios::cur);
83 #endif /* BUFFER_H_ */