| 124 | |
| 125 | |
| 126 | void* Writer::writeToNewChunk(const void* dataOrNull, size_t length) { |
| 127 | // If we got here, a call to `write` or `reserveSpace` would not fit in the current chunk |
| 128 | if (_outputFile) { |
| 129 | flush(); |
| 130 | if (length > _chunkSize) { |
| 131 | freeChunk(_chunks.back()); |
| 132 | _chunks.clear(); |
| 133 | addChunk(length); |
| 134 | } |
| 135 | _length -= _available.size; |
| 136 | _available = _chunks[0]; |
| 137 | _length += _available.size; |
| 138 | } else { |
| 139 | if (_usuallyTrue(_chunkSize <= 64*1024)) |
| 140 | _chunkSize *= 2; |
| 141 | addChunk(std::max(length, _chunkSize)); |
| 142 | } |
| 143 | |
| 144 | // Now that we have room, write: |
| 145 | auto result = (void*)_available.buf; |
| 146 | if (dataOrNull) |
| 147 | memcpy((void*)_available.buf, dataOrNull, length); |
| 148 | _available.moveStart(length); |
| 149 | return result; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | void Writer::flush() { |