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

Method reserve

Sming/Wiring/WString.cpp:157–198  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

155}
156
157bool String::reserve(size_t size)
158{
159 // Can we use SSO here to avoid allocation?
160 if(size <= SSO_CAPACITY) {
161 // If already using SSO then no further action required
162 if(sso.set) {
163 return true;
164 }
165
166 // If heap hasn't been used yet then switch to SSO mode
167 if(ptr.buffer == nullptr) {
168 sso.set = true;
169 } else {
170 // Otherwise continue with existing heap allocation
171 assert(ptr.capacity >= size);
172 }
173
174 return true;
175 }
176
177 // Reallocation required?
178 if(!sso.set && ptr.buffer != nullptr && ptr.capacity >= size) {
179 return true; // Nope :-)
180 }
181
182 // Need to handle resizing an existing heap buffer and moving from SSO to heap
183 char* newbuffer = (char*)realloc(sso.set ? nullptr : ptr.buffer, size + 1);
184 if(newbuffer == nullptr) {
185 // allocation failed - leave existing buffer arrangement unchanged
186 return false;
187 }
188
189 if(sso.set) {
190 // Move content out of SSO
191 memcpy(newbuffer, sso.buffer, sso.len);
192 ptr.len = sso.len;
193 sso.set = false;
194 }
195 ptr.buffer = newbuffer;
196 ptr.capacity = size;
197 return true;
198}
199
200String::Buffer String::getBuffer()
201{

Callers 8

onDataMethod · 0.80
calcValueMethod · 0.80
executeMethod · 0.80
toStringMethod · 0.80
CommandMethod · 0.80
joinMethod · 0.80
evaluateMethod · 0.80

Calls 1

reallocFunction · 0.85

Tested by

no test coverage detected