MCPcopy Create free account
hub / github.com/F-Stack/f-stack / sdsRemoveFreeSpace

Function sdsRemoveFreeSpace

app/redis-6.2.6/src/sds.c:290–325  ·  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

288 * After the call, the passed sds string is no longer valid and all the
289 * references must be substituted with the new pointer returned by the call. */
290sds sdsRemoveFreeSpace(sds s) {
291 void *sh, *newsh;
292 char type, oldtype = s[-1] & SDS_TYPE_MASK;
293 int hdrlen, oldhdrlen = sdsHdrSize(oldtype);
294 size_t len = sdslen(s);
295 size_t avail = sdsavail(s);
296 sh = (char*)s-oldhdrlen;
297
298 /* Return ASAP if there is no space left. */
299 if (avail == 0) return s;
300
301 /* Check what would be the minimum SDS header that is just good enough to
302 * fit this string. */
303 type = sdsReqType(len);
304 hdrlen = sdsHdrSize(type);
305
306 /* If the type is the same, or at least a large enough type is still
307 * required, we just realloc(), letting the allocator to do the copy
308 * only if really needed. Otherwise if the change is huge, we manually
309 * reallocate the string to use the different header type. */
310 if (oldtype==type || type > SDS_TYPE_8) {
311 newsh = s_realloc(sh, oldhdrlen+len+1);
312 if (newsh == NULL) return NULL;
313 s = (char*)newsh+oldhdrlen;
314 } else {
315 newsh = s_malloc(hdrlen+len+1);
316 if (newsh == NULL) return NULL;
317 memcpy((char*)newsh+hdrlen, s, len+1);
318 s_free(sh);
319 s = (char*)newsh+hdrlen;
320 s[-1] = type;
321 sdssetlen(s, len);
322 }
323 sdssetalloc(s, len);
324 return s;
325}
326
327/* Return the total size of the allocation of the specified sds string,
328 * including:

Callers 3

trimStringObjectIfNeededFunction · 0.85
RM_StringTruncateFunction · 0.85

Calls 7

sdsHdrSizeFunction · 0.85
sdslenFunction · 0.85
sdsavailFunction · 0.85
sdsReqTypeFunction · 0.85
sdssetlenFunction · 0.85
sdssetallocFunction · 0.85
memcpyFunction · 0.50

Tested by

no test coverage detected