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