Repository: valkey-io/valkey
File: src/dict.c (C)

Code before:
static inline dictEntry *createEmbeddedEntry(void *key, dictEntry *next, dictType *dt) {
    size_t key_len = dt->embedKey(NULL, 0, key, NULL);
    embeddedDictEntry *entry = zmalloc(compactSizeEmbeddedDictEntry() + key_len);
    dt->embedKey(entry->key_buf, key_len, key, &entry->key_header_size);
    entry->next = next;
    return encodeMaskedPtr(entry, ENTRY_PTR_EMBEDDED);
}

static inline void *getEmbeddedKey(const dictEntry *de) {
    embeddedDictEntry *entry = (embeddedDictEntry *)decodeMaskedPtr(de);
    return &entry->key_buf[entry->key_header_size];
}

/* Decodes the pointer to an entry without value, when you know it is an entry
 * without value. Hint: Use entryIsNoValue to check. */
static inline dictEntryNoValue *decodeEntryNoValue(const dictEntry *de) {
    return decodeMaskedPtr(de);
}

static inline embeddedDictEntry *decodeEmbeddedEntry(const dictEntry *de) {
    return decodeMaskedPtr(de);
}

/* Returns 1 if the entry has a value field and 0 otherwise. */
static inline int entryHasValue(const dictEntry *de) {
    return !entryIsNoValue(de);
}

/* ----------------------------- API implementation ------------------------- */

/* Reset hash table parameters already initialized with _dictInit()*/
static void _dictReset(dict *d, int htidx) {
    d->ht_table[htidx] = NULL;
    d->ht_size_exp[htidx] = -1;
    d->ht_used[htidx] = 0;
}

/* Create a new hash table */
dict *dictCreate(dictType *type) {
    size_t metasize = type->dictMetadataBytes ? type->dictMetadataBytes(NULL) : 0;
    dict *d = zmalloc(sizeof(*d) + metasize);
    if (metasize > 0) {
        memset(dictMetadata(d), 0, metasize);
    }
    _dictInit(d, type);
    return d;
}

/* Initialize the hash table */
int _dictInit(dict *d, dictType *type) {
    _dictReset(d, 0);

Review comment (suggestion):
I believe we need to exclude entryIsKey too.

```suggestion
    return !entryIsNoValue(de) && !entryIsKey(de);
```

Code after:
static inline dictEntry *encodeMaskedPtr(const void *ptr, unsigned int bits) {
    assert(((uintptr_t)ptr & ENTRY_PTR_MASK) == 0);
    return (dictEntry *)(void *)((uintptr_t)ptr | bits);
}

static inline void *decodeMaskedPtr(const dictEntry *de) {
    assert(!entryIsKey(de));
    return (void *)((uintptr_t)(void *)de & ~ENTRY_PTR_MASK);
}

/* Creates an entry without a value field. */
static inline dictEntry *createEntryNoValue(void *key, dictEntry *next) {
    dictEntryNoValue *entry = zmalloc(sizeof(*entry));
    entry->key = key;
    entry->next = next;
    return encodeMaskedPtr(entry, ENTRY_PTR_NO_VALUE);
}

static inline dictEntry *createEmbeddedEntry(void *key, dictEntry *next, dictType *dt) {
    size_t key_len = dt->embedKey(NULL, 0, key, NULL);
    embeddedDictEntry *entry = zmalloc(compactSizeEmbeddedDictEntry() + key_len);
    dt->embedKey(entry->key_buf, key_len, key, &entry->key_header_size);
    entry->next = next;
    return encodeMaskedPtr(entry, ENTRY_PTR_EMBEDDED);
}

static inline void *getEmbeddedKey(const dictEntry *de) {
    embeddedDictEntry *entry = (embeddedDictEntry *)decodeMaskedPtr(de);
    return &entry->key_buf[entry->key_header_size];
}

/* Decodes the pointer to an entry without value, when you know it is an entry
 * without value. Hint: Use entryIsNoValue to check. */
static inline dictEntryNoValue *decodeEntryNoValue(const dictEntry *de) {
    return decodeMaskedPtr(de);
}

static inline embeddedDictEntry *decodeEmbeddedEntry(const dictEntry *de) {
    return decodeMaskedPtr(de);
}

/* Returns 1 if the entry has a value field and 0 otherwise. */
static inline int entryHasValue(const dictEntry *de) {
    return entryIsNormal(de) || entryIsEmbedded(de);
}

/* ----------------------------- API implementation ------------------------- */

/* Reset hash table parameters already initialized with _dictInit()*/
static void _dictReset(dict *d, int htidx) {
    d->ht_table[htidx] = NULL;