MCPcopy Create free account
hub / github.com/RedisGraph/RedisGraph / sdsRemoveFreeSpace

Function sdsRemoveFreeSpace

src/util/sds/sds.c:257–288  ·  view source on GitHub ↗

Reallocate the sds string so that it has no free space at the end. The * contained string remains not altered, but next concatenation operations * will require a reallocation. * * After the call, the passed sds string is no longer valid and all the * references must be substituted with the new pointer returned by the call. */

Source from the content-addressed store, hash-verified

255 * After the call, the passed sds string is no longer valid and all the
256 * references must be substituted with the new pointer returned by the call. */
257sds sdsRemoveFreeSpace(sds s) {
258 void *sh, *newsh;
259 char type, oldtype = s[-1] & SDS_TYPE_MASK;
260 int hdrlen, oldhdrlen = sdsHdrSize(oldtype);
261 size_t len = sdslen(s);
262 sh = (char*)s-oldhdrlen;
263
264 /* Check what would be the minimum SDS header that is just good enough to
265 * fit this string. */
266 type = sdsReqType(len);
267 hdrlen = sdsHdrSize(type);
268
269 /* If the type is the same, or at least a large enough type is still
270 * required, we just realloc(), letting the allocator to do the copy
271 * only if really needed. Otherwise if the change is huge, we manually
272 * reallocate the string to use the different header type. */
273 if (oldtype==type || type > SDS_TYPE_8) {
274 newsh = s_realloc(sh, oldhdrlen+len+1);
275 if (newsh == NULL) return NULL;
276 s = (char*)newsh+oldhdrlen;
277 } else {
278 newsh = s_malloc(hdrlen+len+1);
279 if (newsh == NULL) return NULL;
280 memcpy((char*)newsh+hdrlen, s, len+1);
281 s_free(sh);
282 s = (char*)newsh+hdrlen;
283 s[-1] = type;
284 sdssetlen(s, len);
285 }
286 sdssetalloc(s, len);
287 return s;
288}
289
290/* Return the total size of the allocation of the specified sds string,
291 * including:

Callers

nothing calls this directly

Calls 5

sdsHdrSizeFunction · 0.85
sdslenFunction · 0.85
sdsReqTypeFunction · 0.85
sdssetlenFunction · 0.85
sdssetallocFunction · 0.85

Tested by

no test coverage detected