Back to listC
Redis - Simple Dynamic String
Lv.5740@mukitaro11 playsDec 31, 2025
Redis Simple Dynamic String (SDS) implementation. A binary-safe string library with O(1) length access.
preview.c
1struct sdshdr {2 unsigned int len;3 unsigned int free;4 char buf[];5};6 7static inline size_t sdslen(const sds s)8{9 struct sdshdr *sh = (void *)(s - sizeof(struct sdshdr));10 return sh->len;11}12 13static inline size_t sdsavail(const sds s)14{15 struct sdshdr *sh = (void *)(s - sizeof(struct sdshdr));16 return sh->free;17}18 19sds sdsnewlen(const void *init, size_t initlen)20{21 struct sdshdr *sh;22 sh = malloc(sizeof(struct sdshdr) + initlen + 1);23 if (sh == NULL) return NULL;24 25 sh->len = initlen;26 sh->free = 0;27 if (initlen && init)28 memcpy(sh->buf, init, initlen);29 sh->buf[initlen] = '\0';30 return (char *)sh->buf;31}32 33sds sdscat(sds s, const char *t)34{35 return sdscatlen(s, t, strlen(t));36}Custom problems are not included in rankings