33 :m_stripe(stripe), m_storage(Q_NULLPTR), m_length(stripe.length())
56 unsigned int rest = cnt;
58 (*this)[dst++] = (*this)[src++];
62 if (!_samples)
return 0;
64 memmove(&(_samples[dst]), &(_samples[src]), cnt *
sizeof(
sample_t));
72 unsigned int dstoff,
unsigned int offset,
unsigned int length)
81 buffer[dstoff++] = (*this)[offset++];
85 if (!_samples)
return 0;
86 MEMCPY(&(buffer[dstoff]), &(_samples[offset]), length *
sizeof(
sample_t));
96 m_map_count(0), m_mapped_storage(Q_NULLPTR)
117 qWarning(
"StripeStorage: DEEP COPY (%u) FAILED - OOM!",
m_length);
129 qWarning(
"StripeStorage: DEEP COPY (%u) MMAP FAILED - " 130 "FALLING BACK TO SLOW COPY!",
m_length);
134 unsigned int ofs = 0;
137 unsigned int len = qMin(remaining,
Kwave::toUint(
sizeof(buf)));
140 Q_ASSERT(read == len);
141 if (read != len)
break;
144 Q_ASSERT(written == len);
145 if (written != len)
break;
150 qWarning(
"StripeStorage: DEEP COPY (%u) FAILED - DATA LOST!",
160 QMutexLocker lock(&
m_lock);
178 QMutexLocker lock(&
m_lock);
241 Q_ASSERT(offset < stripe.
length());
242 if (offset >= stripe.
length())
return;
249 Q_ASSERT(_samples.
data());
250 if (!_samples.
data())
return;
251 if (!stripe.
read(_samples, 0, offset, length))
resize(0);
258 QMutexLocker lock(&
m_lock);
270 QMutexLocker lock(&
m_lock);
297 Q_ASSERT(!
m_data->mapCount());
316 qWarning(
"Stripe::resizeStorage(%u) failed! (1)", length);
320 Q_ASSERT(!
m_data->m_storage);
321 m_data->m_storage = new_storage;
329 qWarning(
"Stripe::resizeStorage(%u) failed! (2)", length);
341 QMutexLocker lock(&
m_lock);
345 unsigned int old_length =
m_data->m_length;
346 if (
m_data->m_length == length)
return old_length;
349 Q_ASSERT(!
m_data->mapCount());
351 qWarning(
"Stripe::resize(%u) failed, out of memory ?", length);
356 if (initialize && length) {
357 Q_ASSERT(!
m_data->mapCount());
358 unsigned int pos = old_length;
362 if (_samples.
size() !=
m_data->m_length)
return 0;
364 while (pos < length) {
370 if (!samples)
return 0;
372 memset(&(samples[pos]), 0, (length - pos) *
sizeof(
sample_t));
385 if (!count)
return 0;
386 Q_ASSERT(offset + count <= samples.
size());
387 if (offset + count > samples.
size())
return 0;
390 QMutexLocker lock(&
m_lock);
394 unsigned int old_length =
m_data->m_length;
395 unsigned int new_length = old_length + count;
396 Q_ASSERT(!
m_data->mapCount());
401 unsigned int cnt = new_length - old_length;
402 Q_ASSERT(!
m_data->mapCount());
406 &(samples[offset]), cnt *
sizeof(
sample_t)
410 return (bytes_appended > 0) ? (bytes_appended /
sizeof(
sample_t)) : 0;
419 QMutexLocker lock(&
m_lock);
423 const unsigned int size =
m_data->m_length;
426 unsigned int first = offset;
427 unsigned int last = offset + length - 1;
431 Q_ASSERT(first < size);
432 if (first >= size)
return;
435 if (last >= size) last = size - 1;
436 Q_ASSERT(last >= first);
437 if (last < first)
return;
440 unsigned int dst = first;
441 unsigned int src = last+1;
442 unsigned int len = size - src;
447 Q_ASSERT(src + len <= size);
448 Q_ASSERT(dst + len <= size);
449 if (!_samples.
copy(dst, src, len))
return;
459 QMutexLocker lock(&
m_lock);
461 if (!
m_data)
return false;
463 if (
m_data->mapCount())
return false;
465 const unsigned int old_len =
m_data->m_length;
466 const unsigned int combined_len = offset + other.
length();
467 if (old_len < combined_len) {
479 if (!src || !dst)
return false;
481 MEMCPY(dst + offset, src, len);
489 unsigned int srcoff,
unsigned int srclen)
491 QMutexLocker lock(&
m_lock);
495 Q_ASSERT(!
m_data->mapCount());
498 &(source[srcoff]), srclen *
sizeof(
sample_t));
507 QMutexLocker lock(&
m_lock);
508 if (!length || !
m_data)
return 0;
516 Q_ASSERT(offset < m_data->m_length);
517 if (offset >=
m_data->m_length)
return 0;
518 if ((offset + length) >
m_data->m_length)
519 length =
m_data->m_length - offset;
523 if (!length)
return 0;
529 &buffer[dstoff], length *
sizeof(
sample_t)
533 return (bytes_read > 0) ? (bytes_read /
sizeof(
sample_t)) : 0;
540 QMutexLocker lock(&
m_lock);
550 Q_ASSERT(first < m_data->m_length);
551 Q_ASSERT(first <= last);
552 Q_ASSERT(last < m_data->m_length);
554 unsigned int remaining = last - first + 1;
557 const unsigned int block = 8;
558 while (Q_LIKELY(remaining >= block)) {
559 for (
unsigned int count = 0; Q_LIKELY(count < block); count++) {
561 if (Q_UNLIKELY(s < lo)) lo = s;
562 if (Q_UNLIKELY(s > hi)) hi = s;
566 while (Q_LIKELY(remaining)) {
568 if (Q_UNLIKELY(s < lo)) lo = s;
569 if (Q_UNLIKELY(s > hi)) hi = s;
579 unsigned int appended =
append(samples, 0, samples.
size());
580 if (appended != samples.
size()) {
581 qWarning(
"Stripe::operator << FAILED");
bool combine(unsigned int offset, Kwave::Stripe &other)
unsigned int append(const Kwave::SampleArray &samples, unsigned int offset, unsigned int count)
unsigned int length() const
int readFrom(Kwave::Handle handle, unsigned int offset, void *buffer, unsigned int length) Q_DECL_EXPORT
unsigned int read(Kwave::SampleArray &buffer, unsigned int dstoff, unsigned int offset, unsigned int length)
static MemoryManager & instance() Q_DECL_EXPORT
void overwrite(unsigned int offset, const Kwave::SampleArray &source, unsigned int srcoff, unsigned int srclen)
sample_t * m_mapped_storage
unsigned int read(Kwave::SampleArray &buffer, unsigned int dstoff, unsigned int offset, unsigned int length)
Stripe & operator<<(const Kwave::SampleArray &samples)
bool operator==(const Stripe &other) const
void deleteRange(unsigned int offset, unsigned int length)
unsigned int resizeStorage(unsigned int length)
void free(Kwave::Handle &handle) Q_DECL_EXPORT
unsigned int resize(unsigned int length, bool initialize=true)
sample_index_t start() const
MappedArray(Stripe &stripe)
sample_index_t end() const
Stripe & operator=(const Stripe &other)
void setRawData(sample_t *data, unsigned int size)
void setStart(sample_index_t start)
const sample_t * constData() const
unsigned int copy(unsigned int dst, unsigned int src, unsigned int cnt)
void unmap(Kwave::Handle handle) Q_DECL_EXPORT
unsigned int size() const
void * map(Kwave::Handle handle) Q_DECL_EXPORT
int writeTo(Kwave::Handle handle, unsigned int offset, const void *buffer, unsigned int length) Q_DECL_EXPORT
Kwave::Handle allocate(size_t size) Q_DECL_EXPORT
void minMax(unsigned int first, unsigned int last, sample_t &min, sample_t &max)
QExplicitlySharedDataPointer< StripeStorage > m_data