| 20 | } |
| 21 | |
| 22 | bool MemoryDataStream::ensureCapacity(size_t minCapacity) |
| 23 | { |
| 24 | if(capacity < minCapacity) { |
| 25 | if(minCapacity > maxCapacity) { |
| 26 | debug_e("MemoryDataStream too large, requested %u limit is %u", minCapacity, maxCapacity); |
| 27 | return false; |
| 28 | } |
| 29 | size_t newCapacity = minCapacity; |
| 30 | if(capacity != 0) { |
| 31 | // If expanding stream, increase buffer capacity in anticipation of further writes |
| 32 | newCapacity += (minCapacity < 256) ? 128 : 64; |
| 33 | } |
| 34 | debug_d("MemoryDataStream::realloc %u -> %u", capacity, newCapacity); |
| 35 | // realloc can fail, store the result in temporary pointer |
| 36 | auto newBuffer = (char*)realloc(buffer, newCapacity); |
| 37 | if(newBuffer == nullptr) { |
| 38 | debug_e("MemoryDataStream realloc(%u) failed", newCapacity); |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | buffer = newBuffer; |
| 43 | capacity = newCapacity; |
| 44 | } |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | size_t MemoryDataStream::write(const uint8_t* data, size_t len) |
| 50 | { |
no test coverage detected