#include <stdio.h> #include <string.h> #include "zmalloc.h" #include "testhelp.h" typedef char *sds; struct sdshdr { unsigned int len; unsigned int free; char buf[]; }; static inline size_t sdslen(const sds s) { printf("sizeof(struct sdshdr)=%d ",sizeof(struct sdshdr)); struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); printf("sh->buf=%s ",sh->buf); printf("sh->len=%d ",sh->len); return sh->len; } /* Create a new sds string with the content specified by the 'init' pointer * and 'initlen'. * If NULL is used for 'init' the string is initialized with zero bytes. * * The string is always null-termined (all the sds strings are, always) so * even if you create an sds string with: * * mystring = sdsnewlen("abc",3); * * You can print the string with printf() as there is an implicit at the * end of the string. However the string is binary safe and can contain * characters in the middle, as the length is stored in the sds header. */ sds sdsnewlen(const void *init, size_t initlen) { struct sdshdr *sh; printf("***sdsnewlen init=%s len=%d*** ",init,initlen); printf("sizeof(struct sdshdr)+initlen+1=%d*** ",sizeof(struct sdshdr)+initlen+1); if (init) { sh = zmalloc(sizeof(struct sdshdr)+initlen+1); } else { sh = zcalloc(sizeof(struct sdshdr)+initlen+1); } if (sh == NULL) return NULL; sh->len = initlen; sh->free = 0; if (initlen && init) memcpy(sh->buf, init, initlen); sh->buf[initlen] = '