File: SampleArray.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (169 lines) | stat: -rw-r--r-- 5,440 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*************************************************************************
        SampleArray.cpp  -  array with Kwave's internal sample_t
                             -------------------
    begin                : Wed Jan 02 2008
    copyright            : (C) 2008 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <new>
#include <stdlib.h>

#include "libkwave/SampleArray.h"
#include "libkwave/memcpy.h"

//***************************************************************************
Kwave::SampleArray::SampleArray()
{
    m_storage = new(std::nothrow) SampleStorage;
}

//***************************************************************************
Kwave::SampleArray::SampleArray(unsigned int size)
{
    m_storage = new(std::nothrow) SampleStorage;
    bool ok = resize(size);
    if (!ok)
        qWarning("Kwave::SampleArray::SampleArray(%u) - FAILED, OOM?", size);
}

//***************************************************************************
Kwave::SampleArray::~SampleArray()
{
}

//***************************************************************************
void Kwave::SampleArray::fill(sample_t value)
{
    if (!m_storage) return;
    sample_t *p = data();
    Q_ASSERT(p);
    if (!p) return;
    for (unsigned int count = m_storage->m_size; Q_LIKELY(count); count--) {
        *p = value;
        p++;
    }
}

//***************************************************************************
sample_t & Kwave::SampleArray::operator [] (unsigned int index)
{
    static sample_t dummy = 0;
    sample_t *p = data();

    if (Q_LIKELY(p))
        return (*(p + index));
    else
        return dummy;
}

//***************************************************************************
const sample_t & Kwave::SampleArray::operator [] (unsigned int index) const
{
    static const sample_t dummy = 0;
    const sample_t *p = constData();

    if (Q_LIKELY(p))
        return (*(p + index));
    else
        return dummy;
}

//***************************************************************************
bool Kwave::SampleArray::resize(unsigned int size)
{
    if (!m_storage) return false;
    if (size == m_storage->m_size) return true;

    m_storage->resize(size);
    if (size && (m_storage->m_size > size)) {
        qWarning("Kwave::SampleArray::resize(): shrinking from %u to %u "
                 "failed, keeping old memory", m_storage->m_size, size);
        return true;
    }
    return (m_storage->m_size == size);
}

//***************************************************************************
unsigned int Kwave::SampleArray::size() const
{
    return (m_storage) ? m_storage->m_size : 0;
}

//***************************************************************************
Kwave::SampleArray::SampleStorage::SampleStorage()
    :QSharedData()
{
    m_size     = 0;
    m_data     = nullptr;
}

//***************************************************************************
Kwave::SampleArray::SampleStorage::SampleStorage(const SampleStorage &other)
    :QSharedData(other)
{
    m_size     = 0;
    m_data     = nullptr;

    if (other.m_size) {
        m_data = static_cast<sample_t *>(
            ::malloc(other.m_size * sizeof(sample_t))
        );
        if (m_data) {
            m_size = other.m_size;
            MEMCPY(m_data, other.m_data, m_size * sizeof(sample_t));
        }
    }
}

//***************************************************************************
Kwave::SampleArray::SampleStorage::~SampleStorage()
{
    if (m_data) ::free(m_data);
}

//***************************************************************************
void Kwave::SampleArray::SampleStorage::resize(unsigned int size)
{
    if (size) {
        // resize using realloc, keep existing data
        sample_t *new_data = static_cast<sample_t *>(
            ::realloc(m_data, size * sizeof(sample_t)));
        if (new_data) {
            // successful
            m_data = new_data;
            if (size > m_size) {
                // initialize the new data
                unsigned int count = size - m_size;
                sample_t *p = m_data + m_size;
                while (count--)
                    *(p++) = 0;
            }
            m_size = size;
        } else {
            qWarning("Kwave::SampleArray::SampleStorage::resize(%u): OOM! "
                     "- keeping old size %u", size, m_size);
        }
    } else {
        // resize to zero == delete/free memory
        Q_ASSERT(m_data);
        sample_t *t = m_data;
        m_data = nullptr;
        m_size = 0;
        ::free(t);
    }
}

//***************************************************************************
//***************************************************************************