10 typedef unsigned long long bufsize_type;
11 const int FLOATSIZE = sizeof(float);
15 Buffer(int nMB, int nSamples, int cvlen, const char* tmpF) {
17 size = bufsize_type(nMB) * 1024 * 1024 / FLOATSIZE / cvlen;
18 if (size > (bufsize_type)nSamples) size = nSamples;
19 general_assert(size > 0, "Memory allocated for credibility intervals is not enough!");
22 buffer = new float[size];
23 ftmpOut.open(tmpF, std::ios::binary);
24 pthread_mutex_init(&lock, NULL);
27 this->nSamples = nSamples;
32 if (fr < to) flushToTempFile();
35 pthread_mutex_destroy(&lock);
39 void write(int n, float **vecs) {
40 pthread_assert(pthread_mutex_lock(&lock), "pthread_mutex_lock", "Error occurred while acquiring the lock!");
41 for (int i = 0; i < n; i++) {
42 if (size - cpos < bufsize_type(cvlen)) flushToTempFile();
43 memcpy(buffer + cpos, vecs[i], FLOATSIZE * cvlen);
47 pthread_assert(pthread_mutex_unlock(&lock), "pthread_mutex_unlock", "Error occurred while releasing the lock!");
51 bufsize_type size, cpos; // cpos : current position
54 std::ofstream ftmpOut;
57 int fr, to; // each flush, sample fr .. to - 1
60 void flushToTempFile() {
61 std::streampos gap1 = std::streampos(fr) * FLOATSIZE;
62 std::streampos gap2 = std::streampos(nSamples - to) * FLOATSIZE;
65 ftmpOut.seekp(0, std::ios::beg);
66 for (int i = 0; i < cvlen; i++) {
68 ftmpOut.seekp(gap1, std::ios::cur);
69 for (int j = fr; j < to; j++) {
70 ftmpOut.write((char*)p, FLOATSIZE);
73 ftmpOut.seekp(gap2, std::ios::cur);
81 #endif /* BUFFER_H_ */