Hacking Strings and redishacking for Redis code reading

Source: Internet
Author: User

Hacking Strings and redishacking for Redis code reading
Hacking Strings

The implementation of Redis strings is contained in sds. c (sds stands for Simple Dynamic Strings ).
The C structureSdshdrDeclared inSds. hRepresents a Redis string:

struct sdshdr {    long len;    long free;    char buf[];};

TheBufCharacter array stores the actual string.
TheLenField stores the lengthBuf. This makes obtaining the length of a Redis string an O (1) operation.
TheFreeField stores the number of additional bytes available for use.
TogetherLenAndFreeField can be thought of as holding the metadata ofBufCharacter array.

Creating Redis Strings

A new data type namedsdsIs defined inSds. hTo be a synonymn for a character pointer:

typedef char *sds;

sdsnewlenFunction defined inSds. cCreates a new Redis String:

sds sdsnewlen(const void *init, size_t initlen) {    struct sdshdr *sh;    sh = zmalloc(sizeof(struct sdshdr)+initlen+1);#ifdef SDS_ABORT_ON_OOM    if (sh == NULL) sdsOomAbort();#else    if (sh == NULL) return NULL;#endif    sh->len = initlen;    sh->free = 0;    if (initlen) {        if (init) memcpy(sh->buf, init, initlen);        else memset(sh->buf,0,initlen);    }    sh->buf[initlen] = '\0';    return (char*)sh->buf;}

Remember a Redis string is a variable of typestruct sdshdr.sdsnewlenReturns a character pointer !!
That's a trick and needs some explanation.
Suppose I create a Redis string usingsdsnewlenLike below:

sdsnewlen("redis", 5);

This creates a new variable of typestruct sdshdrAllocating memoryLenAndFreeFields as well as forBufCharacter array.

sh = zmalloc(sizeof(struct sdshdr)+initlen+1); // initlen is length of init argument.

AftersdsnewlenSuccesfully creates a Redis string the result is something like:

-----------|5|0|redis|-----------^   ^sh  sh->buf 

sdsnewlenReturns sh-> buf to the caller.

What do you do if you need to free the Redis string pointedsh?
You want the pointershBut you only have the pointersh->buf.
Can you get the pointershFromsh->buf?
Yes. Pointer arithmetic. Notice from the above ASCII art that if you subtract the size of two longs fromsh->bufYou get the pointersh.
The sizeof two longs happens to be the sizestruct sdshdr.
LooksdslenFunction and see this trick at work:

size_t sdslen(const sds s) {    struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));    return sh->len;}

Knowing this trick you cocould easily go through the rest of the functions inSds. c.
The Redis string implementation is hidden behind an interface that accepts only character pointers. The users of Redis strings need not care about how its implemented and treat Redis strings as a character pointer.

C Struct array member without specific length

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.