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. */
| 97 | * an object where the sds string is actually an unmodifiable string |
| 98 | * allocated in the same chunk as the object itself. */ |
| 99 | robj *createEmbeddedStringObject(const char *ptr, size_t len) { |
| 100 | serverAssert(len <= UINT8_MAX); |
| 101 | // Note: If the size changes update serializeStoredStringObject |
| 102 | size_t allocsize = sizeof(struct sdshdr8)+len+1; |
| 103 | if (allocsize < sizeof(void*)) |
| 104 | allocsize = sizeof(void*); |
| 105 | |
| 106 | size_t mvccExtraBytes = g_pserver->fActiveReplica ? sizeof(redisObjectExtended) : 0; |
| 107 | char *oB = (char*)zmalloc(sizeof(robj)+allocsize-sizeof(redisObject::m_ptr)+mvccExtraBytes, MALLOC_SHARED); |
| 108 | robj *o = reinterpret_cast<robj*>(oB + mvccExtraBytes); |
| 109 | struct sdshdr8 *sh = (sdshdr8*)(&o->m_ptr); |
| 110 | |
| 111 | new (o) redisObject; |
| 112 | o->type = OBJ_STRING; |
| 113 | o->encoding = OBJ_ENCODING_EMBSTR; |
| 114 | o->setrefcount(1); |
| 115 | setMvccTstamp(o, OBJ_MVCC_INVALID); |
| 116 | |
| 117 | if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) { |
| 118 | o->lru = (LFUGetTimeInMinutes()<<8) | LFU_INIT_VAL; |
| 119 | } else { |
| 120 | o->lru = LRU_CLOCK(); |
| 121 | } |
| 122 | |
| 123 | sh->len = len; |
| 124 | sh->alloc = len; |
| 125 | sh->flags = SDS_TYPE_8; |
| 126 | if (ptr == SDS_NOINIT) |
| 127 | sh->buf()[len] = '\0'; |
| 128 | else if (ptr) { |
| 129 | memcpy(sh->buf(),ptr,len); |
| 130 | sh->buf()[len] = '\0'; |
| 131 | } else { |
| 132 | memset(sh->buf(),0,len+1); |
| 133 | } |
| 134 | return o; |
| 135 | } |
| 136 | |
| 137 | /* Create a string object with EMBSTR encoding if it is smaller than |
| 138 | * OBJ_ENCODING_EMBSTR_SIZE_LIMIT, otherwise the RAW encoding is |
no test coverage detected