Create a string object with encoding OBJ_ENCODING_EMBSTR, that is * an object where the sds string is actually an unmodifiable string * allocated in the same chunk as the object itself. */
| 82 | * an object where the sds string is actually an unmodifiable string |
| 83 | * allocated in the same chunk as the object itself. */ |
| 84 | robj *createEmbeddedStringObject(const char *ptr, size_t len) { |
| 85 | robj *o = zmalloc(sizeof(robj)+sizeof(struct sdshdr8)+len+1); |
| 86 | struct sdshdr8 *sh = (void*)(o+1); |
| 87 | |
| 88 | o->type = OBJ_STRING; |
| 89 | o->encoding = OBJ_ENCODING_EMBSTR; |
| 90 | o->ptr = sh+1; |
| 91 | o->refcount = 1; |
| 92 | if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) { |
| 93 | o->lru = (LFUGetTimeInMinutes()<<8) | LFU_INIT_VAL; |
| 94 | } else { |
| 95 | o->lru = LRU_CLOCK(); |
| 96 | } |
| 97 | |
| 98 | sh->len = len; |
| 99 | sh->alloc = len; |
| 100 | sh->flags = SDS_TYPE_8; |
| 101 | if (ptr == SDS_NOINIT) |
| 102 | sh->buf[len] = '\0'; |
| 103 | else if (ptr) { |
| 104 | memcpy(sh->buf,ptr,len); |
| 105 | sh->buf[len] = '\0'; |
| 106 | } else { |
| 107 | memset(sh->buf,0,len+1); |
| 108 | } |
| 109 | return o; |
| 110 | } |
| 111 | |
| 112 | /* Create a string object with EMBSTR encoding if it is smaller than |
| 113 | * OBJ_ENCODING_EMBSTR_SIZE_LIMIT, otherwise the RAW encoding is |
no test coverage detected