MCPcopy Create free account
hub / github.com/SmingHub/Sming / ensureCapacity

Method ensureCapacity

Sming/Core/Data/Stream/MemoryDataStream.cpp:22–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20}
21
22bool 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
49size_t MemoryDataStream::write(const uint8_t* data, size_t len)
50{

Callers 2

sendMethod · 0.45
getNextStreamMethod · 0.45

Calls 1

reallocFunction · 0.85

Tested by

no test coverage detected