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

Function sdsMakeRoomFor

src/util/sds/sds.c:206–249  ·  view source on GitHub ↗

Enlarge the free space at the end of the sds string so that the caller * is sure that after calling this function can overwrite up to addlen * bytes after the end of the string, plus one more byte for nul term. * * Note: this does not change the *length* of the sds string as returned * by sdslen(), but only the free buffer space we have. */

Source from the content-addressed store, hash-verified

204 * Note: this does not change the *length* of the sds string as returned
205 * by sdslen(), but only the free buffer space we have. */
206sds sdsMakeRoomFor(sds s, size_t addlen) {
207 void *sh, *newsh;
208 size_t avail = sdsavail(s);
209 size_t len, newlen;
210 char type, oldtype = s[-1] & SDS_TYPE_MASK;
211 int hdrlen;
212
213 /* Return ASAP if there is enough space left. */
214 if (avail >= addlen) return s;
215
216 len = sdslen(s);
217 sh = (char*)s-sdsHdrSize(oldtype);
218 newlen = (len+addlen);
219 if (newlen < SDS_MAX_PREALLOC)
220 newlen *= 2;
221 else
222 newlen += SDS_MAX_PREALLOC;
223
224 type = sdsReqType(newlen);
225
226 /* Don't use type 5: the user is appending to the string and type 5 is
227 * not able to remember empty space, so sdsMakeRoomFor() must be called
228 * at every appending operation. */
229 if (type == SDS_TYPE_5) type = SDS_TYPE_8;
230
231 hdrlen = sdsHdrSize(type);
232 if (oldtype==type) {
233 newsh = s_realloc(sh, hdrlen+newlen+1);
234 if (newsh == NULL) return NULL;
235 s = (char*)newsh+hdrlen;
236 } else {
237 /* Since the header size changes, need to move the string forward,
238 * and can't use realloc */
239 newsh = s_malloc(hdrlen+newlen+1);
240 if (newsh == NULL) return NULL;
241 memcpy((char*)newsh+hdrlen, s, len+1);
242 s_free(sh);
243 s = (char*)newsh+hdrlen;
244 s[-1] = type;
245 sdssetlen(s, len);
246 }
247 sdssetalloc(s, newlen);
248 return s;
249}
250
251/* Reallocate the sds string so that it has no free space at the end. The
252 * contained string remains not altered, but next concatenation operations

Callers 5

sdsgrowzeroFunction · 0.85
sdscatlenFunction · 0.85
sdscpylenFunction · 0.85
sdscatfmtFunction · 0.85
sdsTestFunction · 0.85

Calls 6

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

Tested by

no test coverage detected