Repository navigation
Expand file tree
/
Copy pathdata_buffer.cpp
More file actions
121 lines (107 loc) · 2.57 KB
/
Copy pathdata_buffer.cpp
File metadata and controls
121 lines (107 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "data_buffer.h"
#include <new>
DataBuffer::DataBuffer (int num_samples, size_t buffer_size)
{
this->buffer_size = buffer_size;
this->num_samples = num_samples;
first_free = first_used = count = 0;
if (buffer_size == 0)
{
data = NULL;
}
else
{
try
{
data = new double[buffer_size * num_samples];
}
catch (const std::bad_alloc &)
{
data = NULL;
}
}
}
DataBuffer::~DataBuffer ()
{
delete[] data;
}
bool DataBuffer::is_ready ()
{
return (data != NULL);
}
void DataBuffer::add_data (double *value)
{
if (!is_ready ())
{
return;
}
lock.lock ();
if (count == 0)
{
first_used = first_free = 0;
}
else if (first_free == first_used)
{
first_used = next (first_used);
count--;
}
memcpy (this->data + first_free * num_samples, value, sizeof (double) * num_samples);
first_free = next (first_free);
count++;
lock.unlock ();
}
void DataBuffer::get_chunk (size_t start, size_t size, double *data_buf)
{
if (start + size < buffer_size)
{
memcpy (data_buf, data + start * num_samples, size * sizeof (double) * num_samples);
}
else
{
size_t first_half = buffer_size - start;
size_t second_half = size - first_half;
memcpy (data_buf, data + start * num_samples, first_half * sizeof (double) * num_samples);
memcpy (
data_buf + first_half * num_samples, data, second_half * sizeof (double) * num_samples);
}
}
// Removes data from buffer
size_t DataBuffer::get_data (size_t max_count, double *data_buf)
{
lock.lock ();
size_t result_count = max_count;
if (result_count > count)
{
result_count = count;
}
if (result_count)
{
get_chunk (first_used, result_count, data_buf);
first_used = (first_used + result_count) % buffer_size;
count -= result_count;
}
lock.unlock ();
return result_count;
}
// Doesn't remove data from buffer
size_t DataBuffer::get_current_data (size_t max_count, double *data_buf)
{
lock.lock ();
size_t result_count = max_count;
if (result_count > count)
result_count = count;
if (result_count)
{
size_t first_return = (first_used + (count - result_count)) % buffer_size;
get_chunk (first_return, result_count, data_buf);
}
lock.unlock ();
return result_count;
}
size_t DataBuffer::get_data_count ()
{
lock.lock ();
size_t result = this->count;
lock.unlock ();
return result;
}