Hacking Strings of Redis code reading

Source: Internet
Author: User

Hacking Strings

The implementation of Redis strings is contained in SDS.C (SDS-stands for simple Dynamic strings).
The C structure SDSHDR declared in sds.h represents a Redis string:

struct SDSHDR {    long len;    Long free;    Char buf[];};

The buf character array stores the actual string.
The Len field stores the length of buf. This makes obtaining, the length of a Redis string an O (1) operation.
The free field stores the number of additional bytes available for use.
Together the len and free field can be thought of as holding the metadata of the buf Charac ter Array.

Creating Redis Strings

A new data type named is sds defined in sds.h to being a synonymn for a character pointer:

typedef char *SDS;

sdsnewlenfunction defined in SDS.C creates 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] = ' + ';    Return (char*) sh->buf;}

Remember a Redis string is a variable of type struct sdshdr . But sdsnewlen returns a character pointer!!
That ' s a trick and needs some explanation.
Suppose I create a Redis string using like sdsnewlen below:

Sdsnewlen ("Redis", 5);

This creates a new variable of type struct sdshdr allocating memory for Len and free fields as well as for the C6>buf character Array.

SH = zmalloc (sizeof (struct SDSHDR) +initlen+1); Initlen is length of init argument.

After sdsnewlen succesfully creates a Redis string The result was something like:

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

sdsnewlen  returns sh->buf to the caller.

What does if you need to free the Redis string pointed by sh ?
You want the Pointer sh  but "Python" from the Pointer .
Can you get the pointer sh  from sh->buf ?
Yes. Pointer arithmetic. Notice from the above ASCII art so if you subtract the size of the Longs from sh->buf you get the Pointer sh .  
The sizeof, longs happens to be the size OF&NB Sp struct sdshdr .
Look at sdslen  function 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 your could easily go through the rest of the functions in sds.c.
The Redis string implementation is hidden behind a interface that accepts only character pointers. The users of Redis strings need not care about how it implemented and treat Redis strings as a character pointer.

C Struct array member without specific length

Hacking Strings of Redis code reading

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.