| 45 | } |
| 46 | |
| 47 | qint64 PipeReader::readData(char* data, qint64 maxSize) |
| 48 | { |
| 49 | int n = qMin(int(maxSize), m_usedBytes.available()); |
| 50 | if (n <= 0) |
| 51 | return -1; |
| 52 | m_usedBytes.acquire(n); |
| 53 | |
| 54 | // Copy the first (possibly the only) block of data |
| 55 | int size1 = qMin(int(m_bufEnd - m_start), n); |
| 56 | memcpy(data, m_start, size_t(size1)); |
| 57 | m_start += size1; |
| 58 | |
| 59 | Q_ASSERT(m_start <= m_bufEnd); |
| 60 | if (m_start == m_bufEnd) |
| 61 | m_start = m_buf; |
| 62 | |
| 63 | // Copy the second block of data from the beginning |
| 64 | int size2 = n - size1; |
| 65 | if (size2 > 0) |
| 66 | { |
| 67 | memcpy(data + size1, m_start, size_t(size2)); |
| 68 | m_start += size2; |
| 69 | |
| 70 | Q_ASSERT(m_start <= m_bufEnd); |
| 71 | if (m_start == m_bufEnd) |
| 72 | m_start = m_buf; |
| 73 | } |
| 74 | Q_ASSERT(n == size1 + size2); |
| 75 | |
| 76 | m_freeBytes.release(n); |
| 77 | return n; |
| 78 | } |
| 79 | |
| 80 | void PipeReader::run() |
| 81 | { |