Во-первых, мне очень жаль название, но у меня нет другого представления о том, как я могу сказать в противном случае.Блок if-else в источнике Redis (простые динамические строки), который я не мог понять
Я пытаюсь понять Simple Dynamic Strings и между строками 138-141 в sds.c есть блок if-else, который я не мог понять. Я даже не знаю, почему это так, и я не знаю, что он делает.
Соответствующая функция:
/* 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. */
sds sdsMakeRoomFor(sds s, size_t addlen) {
struct sdshdr *sh, *newsh;
size_t free = sdsavail(s);
size_t len, newlen;
if (free >= addlen) return s;
len = sdslen(s);
sh = (void*) (s-(sizeof(struct sdshdr)));
newlen = (len+addlen);
if (newlen < SDS_MAX_PREALLOC) /* unwind: line 138 */
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC;
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
if (newsh == NULL) return NULL;
newsh->free = newlen - len;
return newsh->buf;
}
Извините за такой нуб вопрос, но любая помощь будет оценена.
Большое вам спасибо! –