| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | /** | |||
| 2 | * @file buffered_writer.h | |||
| 3 | */ | |||
| 4 | ||||
| 5 | #pragma once | |||
| 6 | ||||
| 7 | #include <cstring> | |||
| 8 | ||||
| 9 | #include "writer.h" | |||
| 10 | ||||
| 11 | template <int TBufferSize> | |||
| 12 | class BufferedWriter : public Writer { | |||
| 13 | public: | |||
| 14 | 388625368 | size_t write(const char* buffer, size_t count) override { | ||
| 15 | 388625368 | size_t bytesFlushed = 0; | ||
| 16 | ||||
| 17 |
6/6BufferedWriter<50>::write(char const*, unsigned long):
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
BufferedWriter<10>::write(char const*, unsigned long):
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9 times.
BufferedWriter<512>::write(char const*, unsigned long):
✓ Branch 0 taken 389636379 times.
✓ Branch 1 taken 388625358 times.
|
2/2✓ Decision 'true' taken 389636379 times.
✓ Decision 'false' taken 388625358 times.
|
778261759 | while (count) { |
| 18 |
9/12BufferedWriter<50>::write(char const*, unsigned long):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
BufferedWriter<10>::write(char const*, unsigned long):
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5 times.
BufferedWriter<512>::write(char const*, unsigned long):
✓ Branch 0 taken 1684863 times.
✓ Branch 1 taken 387951516 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1684863 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 389636379 times.
|
389636391 | if (m_bytesUsed == 0 && count >= TBufferSize) { |
| 19 | // special case: write-thru, skip the copy | |||
| 20 | 3 | bytesFlushed += writeInternal(buffer, count); | ||
| 21 | 3 | count = 0; | ||
| 22 |
4/6BufferedWriter<50>::write(char const*, unsigned long):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
BufferedWriter<10>::write(char const*, unsigned long):
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
BufferedWriter<512>::write(char const*, unsigned long):
✓ Branch 0 taken 387952074 times.
✓ Branch 1 taken 1684305 times.
|
2/2✓ Decision 'true' taken 387952074 times.
✓ Decision 'false' taken 1684305 times.
|
389636388 | } else if (m_bytesUsed + count < TBufferSize) { |
| 23 | // Write that will fit in the buffer, just copy to intermediate buffer | |||
| 24 | 387952081 | memcpy(m_buffer + m_bytesUsed, buffer, count); | ||
| 25 | 387952081 | m_bytesUsed += count; | ||
| 26 | 387952081 | count = 0; | ||
| 27 | } else { | |||
| 28 | // Need to write partial, then flush buffer | |||
| 29 | 1684307 | size_t bytesToWrite = TBufferSize - m_bytesUsed; | ||
| 30 | // Copy this block in to place | |||
| 31 | 1684307 | memcpy(m_buffer + m_bytesUsed, buffer, bytesToWrite); | ||
| 32 | 1684307 | m_bytesUsed += bytesToWrite; | ||
| 33 | ||||
| 34 | // Flush to underlying | |||
| 35 | 1684307 | bytesFlushed += flush(); | ||
| 36 | // Step the read pointer ahead | |||
| 37 | 1684307 | buffer += bytesToWrite; | ||
| 38 | // Decrement remaining bytes | |||
| 39 | 1684307 | count -= bytesToWrite; | ||
| 40 | } | |||
| 41 | } | |||
| 42 | ||||
| 43 | 388625368 | return bytesFlushed; | ||
| 44 | } | |||
| 45 | ||||
| 46 | // Flush the internal buffer to the underlying interface. | |||
| 47 | 1684863 | size_t flush() override { | ||
| 48 | 1684863 | size_t bytesToWrite = m_bytesUsed; | ||
| 49 | ||||
| 50 |
1/2✓ Branch 0 taken 1684863 times.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 1684863 times.
✗ Decision 'false' not taken.
|
1684863 | if (bytesToWrite > 0) { |
| 51 | 1684863 | m_bytesUsed = 0; | ||
| 52 | 1684863 | return writeInternal(m_buffer, bytesToWrite); | ||
| 53 | } else { | |||
| 54 | ✗ | return 0; | ||
| 55 | } | |||
| 56 | } | |||
| 57 | ||||
| 58 | protected: | |||
| 59 | virtual size_t writeInternal(const char* buffer, size_t count) = 0; | |||
| 60 | ||||
| 61 | private: | |||
| 62 | char m_buffer[TBufferSize]; | |||
| 63 | size_t m_bytesUsed = 0; | |||
| 64 | }; | |||
| 65 |