summaryrefslogtreecommitdiff
path: root/DSLogic-gui/pv/data/snapshot.cpp
blob: f05452a0394fb2f77be2b0ad9dbbc2a2119ab8a7 (plain)
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
122
/*
 * This file is part of the DSLogic-gui project.
 * DSLogic-gui is based on PulseView.
 *
 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
 * Copyright (C) 2013 DreamSourceLab <dreamsourcelab@dreamsourcelab.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */


#include "snapshot.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

using namespace boost;

namespace pv {
namespace data {

Snapshot::Snapshot(int unit_size, uint64_t total_sample_count, unsigned int channel_num) :
    _data(NULL),
    _channel_num(channel_num),
    _sample_count(0),
    _total_sample_count(total_sample_count * channel_num),
    _ring_sample_count(0),
    _unit_size(unit_size)
{
	boost::lock_guard<boost::recursive_mutex> lock(_mutex);
	assert(_unit_size > 0);
}

Snapshot::~Snapshot()
{
	boost::lock_guard<boost::recursive_mutex> lock(_mutex);
    if (_data != NULL)
        free(_data);
    _data = NULL;
}

int Snapshot::init(uint64_t _total_sample_len)
{
    _data = malloc(_total_sample_len * _unit_size +
        sizeof(uint64_t));

    if (_data == NULL)
        return SR_ERR_MALLOC;
    else
        return SR_OK;
}

bool Snapshot::buf_null() const
{
    if (_data == NULL)
        return true;
    else
        return false;
}

uint64_t Snapshot::get_sample_count() const
{
	boost::lock_guard<boost::recursive_mutex> lock(_mutex);
    return _sample_count / _channel_num;
}

void* Snapshot::get_data() const
{
    boost::lock_guard<boost::recursive_mutex> lock(_mutex);
    return _data;
}

int Snapshot::get_unit_size() const
{
    boost::lock_guard<boost::recursive_mutex> lock(_mutex);
    return _unit_size;
}

unsigned int Snapshot::get_channel_num() const
{
    boost::lock_guard<boost::recursive_mutex> lock(_mutex);
    return _channel_num;
}

void Snapshot::append_data(void *data, uint64_t samples)
{
//	boost::lock_guard<boost::recursive_mutex> lock(_mutex);
//	_data = realloc(_data, (_sample_count + samples) * _unit_size +
//		sizeof(uint64_t));
    if (_sample_count + samples < _total_sample_count)
        _sample_count += samples;
    else
        _sample_count = _total_sample_count;

    if (_ring_sample_count + samples > _total_sample_count) {
        memcpy((uint8_t*)_data + _ring_sample_count * _unit_size,
            data, (_total_sample_count - _ring_sample_count) * _unit_size);
        _ring_sample_count = (samples + _ring_sample_count - _total_sample_count) % _total_sample_count;
        memcpy((uint8_t*)_data,
            data, _ring_sample_count * _unit_size);
    } else {
        memcpy((uint8_t*)_data + _ring_sample_count * _unit_size,
            data, samples * _unit_size);
        _ring_sample_count += samples;
    }
}

} // namespace data
} // namespace pv