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 | 381638972 | size_t write(const char* buffer, size_t count) override { | ||
15 | 381638972 | 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 382547725 times.
✓ Branch 1 taken 381638962 times.
|
2/2✓ Decision 'true' taken 382547725 times.
✓ Decision 'false' taken 381638962 times.
|
764186709 | 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 1654918 times.
✓ Branch 1 taken 380892807 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1654918 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 382547725 times.
|
382547737 | 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 380893364 times.
✓ Branch 1 taken 1654361 times.
|
2/2✓ Decision 'true' taken 380893364 times.
✓ Decision 'false' taken 1654361 times.
|
382547734 | } else if (m_bytesUsed + count < TBufferSize) { |
23 | // Write that will fit in the buffer, just copy to intermediate buffer | |||
24 | 380893371 | memcpy(m_buffer + m_bytesUsed, buffer, count); | ||
25 | 380893371 | m_bytesUsed += count; | ||
26 | 380893371 | count = 0; | ||
27 | } else { | |||
28 | // Need to write partial, then flush buffer | |||
29 | 1654363 | size_t bytesToWrite = TBufferSize - m_bytesUsed; | ||
30 | // Copy this block in to place | |||
31 | 1654363 | memcpy(m_buffer + m_bytesUsed, buffer, bytesToWrite); | ||
32 | 1654363 | m_bytesUsed += bytesToWrite; | ||
33 | ||||
34 | // Flush to underlying | |||
35 | 1654363 | bytesFlushed += flush(); | ||
36 | // Step the read pointer ahead | |||
37 | 1654363 | buffer += bytesToWrite; | ||
38 | // Decrement remaining bytes | |||
39 | 1654363 | count -= bytesToWrite; | ||
40 | } | |||
41 | } | |||
42 | ||||
43 | 381638972 | return bytesFlushed; | ||
44 | } | |||
45 | ||||
46 | // Flush the internal buffer to the underlying interface. | |||
47 | 1654918 | size_t flush() override { | ||
48 | 1654918 | size_t bytesToWrite = m_bytesUsed; | ||
49 | ||||
50 |
1/2✓ Branch 0 taken 1654918 times.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 1654918 times.
✗ Decision 'false' not taken.
|
1654918 | if (bytesToWrite > 0) { |
51 | 1654918 | m_bytesUsed = 0; | ||
52 | 1654918 | 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 |