kwave  18.07.70
SampleArray.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  SampleArray.cpp - array with Kwave's internal sample_t
3  -------------------
4  begin : Wed Jan 02 2008
5  copyright : (C) 2008 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <new>
21 #include <stdlib.h>
22 
23 #include "libkwave/SampleArray.h"
24 #include "libkwave/memcpy.h"
25 
26 //***************************************************************************
28 {
29  m_storage = new(std::nothrow) SampleStorage;
30 }
31 
32 //***************************************************************************
34 {
35  m_storage = new(std::nothrow) SampleStorage;
36  bool ok = resize(size);
37  if (!ok)
38  qWarning("Kwave::SampleArray::SampleArray(%u) - FAILED, OOM?", size);
39 }
40 
41 //***************************************************************************
43 {
44 }
45 
46 //***************************************************************************
48 {
49  if (!m_storage) return;
50  bool ok = resize(0);
51  if (!ok) qWarning("Kwave::SampleArray::setRawData(...) - OOM?");
52  Q_ASSERT(m_storage->m_raw_data == Q_NULLPTR);
53  Q_ASSERT(m_storage->m_data == Q_NULLPTR);
54  Q_ASSERT(m_storage->m_size == 0);
55  m_storage->m_raw_data = data;
56  m_storage->m_size = size;
57 }
58 
59 //***************************************************************************
61 {
62  if (!m_storage) return;
63  m_storage->m_raw_data = Q_NULLPTR;
64  m_storage->m_size = 0;
65 }
66 
67 //***************************************************************************
69 {
70  if (!m_storage) return;
71  sample_t *p = data();
72  Q_ASSERT(p);
73  if (!p) return;
74  for (unsigned int count = m_storage->m_size; Q_LIKELY(count); count--) {
75  *p = value;
76  p++;
77  }
78 }
79 
80 //***************************************************************************
82 {
83  static sample_t dummy = 0;
84  sample_t *p = data();
85 
86  if (Q_LIKELY(p))
87  return (*(p + index));
88  else
89  return dummy;
90 }
91 
92 //***************************************************************************
93 const sample_t & Kwave::SampleArray::operator [] (unsigned int index) const
94 {
95  static const sample_t dummy = 0;
96  const sample_t *p = constData();
97 
98  if (Q_LIKELY(p))
99  return (*(p + index));
100  else
101  return dummy;
102 }
103 
104 //***************************************************************************
106 {
107  if (!m_storage) return false;
108  if (size == m_storage->m_size) return true;
109 
110  Q_ASSERT(m_storage->m_raw_data == Q_NULLPTR);
111  m_storage->resize(size);
112  if (size && (m_storage->m_size > size)) {
113  qWarning("Kwave::SampleArray::resize(): shrinking from %u to %u "
114  "failed, keeping old memory", m_storage->m_size, size);
115  return true;
116  }
117  return (m_storage->m_size == size);
118 }
119 
120 //***************************************************************************
121 unsigned int Kwave::SampleArray::size() const
122 {
123  return (m_storage) ? m_storage->m_size : 0;
124 }
125 
126 //***************************************************************************
128  :QSharedData()
129 {
130  m_size = 0;
131  m_data = Q_NULLPTR;
132  m_raw_data = Q_NULLPTR;
133 }
134 
135 //***************************************************************************
137  :QSharedData(other)
138 {
139  m_size = 0;
140  m_data = Q_NULLPTR;
141  m_raw_data = Q_NULLPTR;
142  Q_ASSERT(other.m_raw_data == Q_NULLPTR);
143 
144  if (other.m_size) {
145  m_data = static_cast<sample_t *>(
146  ::malloc(other.m_size * sizeof(sample_t))
147  );
148  if (m_data) {
149  m_size = other.m_size;
150  MEMCPY(m_data,
151  (other.m_raw_data) ? other.m_raw_data : other.m_data,
152  m_size * sizeof(sample_t)
153  );
154  }
155  }
156 }
157 
158 //***************************************************************************
160 {
161  Q_ASSERT(m_raw_data == Q_NULLPTR);
162  if (m_data) ::free(m_data);
163 }
164 
165 //***************************************************************************
167 {
168  Q_ASSERT(m_raw_data == Q_NULLPTR);
169 
170  if (size) {
171  // resize using realloc, keep existing data
172  sample_t *new_data = static_cast<sample_t *>(
173  ::realloc(m_data, size * sizeof(sample_t)));
174  if (new_data) {
175  // successful
176  // NOTE: if we grew, the additional memory is *not* initialized!
177  m_data = new_data;
178  m_size = size;
179  } else {
180  qWarning("Kwave::SampleArray::SampleStorage::resize(%u): OOM! "
181  "- keeping old size %u", size, m_size);
182  }
183  } else {
184  // resize to zero == delete/free memory
185  Q_ASSERT(m_data);
186  ::free(m_data);
187  m_data = Q_NULLPTR;
188  m_size = 0;
189  }
190 }
191 
192 //***************************************************************************
193 //***************************************************************************
sample_t & operator[](unsigned int index)
Definition: SampleArray.cpp:81
virtual ~SampleArray()
Definition: SampleArray.cpp:42
void resize(unsigned int size)
QSharedDataPointer< SampleStorage > m_storage
Definition: SampleArray.h:152
#define MEMCPY
Definition: memcpy.h:37
void setRawData(sample_t *data, unsigned int size)
Definition: SampleArray.cpp:47
const sample_t * constData() const
Definition: SampleArray.h:54
void fill(sample_t value)
Definition: SampleArray.cpp:68
unsigned int size() const
sample_t * data()
Definition: SampleArray.h:62
bool resize(unsigned int size) Q_REQUIRED_RESULT
qint32 sample_t
Definition: Sample.h:37