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

Code before:
    char ele_sds_type = ele ? sdsReqType(ele_sds_len) : 0;
    size_t ele_sds_size = ele ? sdsReqSize(ele_sds_len, ele_sds_type) : 0;
    /* Allocate enough space for the node, levels, and the element SDS string.
     * The +1 is for the header size of the SDS string. */
    zskiplistNode *zn = zmalloc(sizeof(*zn) + height * sizeof(struct zskiplistLevel) + 1 + ele_sds_size);
    zn->score = score;
    zslSetNodeHeight(zn, height);
    if (ele) {
        char *data = ((char *)(zn + 1)) + height * sizeof(struct zskiplistLevel);
        *data++ = sdsHdrSize(ele_sds_type);
        sdswrite(data, ele_sds_size, ele_sds_type, ele, ele_sds_len);
    }
    return zn;
}

sds zslGetNodeElement(const zskiplistNode *x) {
    unsigned char *data = (void *)(x + 1);
    data += zslGetNodeHeight(x) * sizeof(struct zskiplistLevel);
    uint8_t hdr_size = *(uint8_t *)data;
    data += 1 + hdr_size;
    return (sds)data;
}

/* Create a new skiplist. */
zskiplist *zslCreate(void) {
    int j;
    zskiplist *zsl;

    zsl = zmalloc(sizeof(*zsl));
    zsl->level = 1;
    zsl->length = 0;
    zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL, 0, NULL);
    for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) {
        zsl->header->level[j].forward = NULL;
        zsl->header->level[j].span = 0;
    }
    zsl->header->backward = NULL;
    zsl->tail = NULL;
    return zsl;
}

/* Free the specified skiplist node. */
static void zslFreeNode(zskiplistNode *node) {
    zfree(node);
}

/* Free a whole skiplist. */
void zslFree(zskiplist *zsl) {
    zskiplistNode *node = zsl->header->level[0].forward, *next;

    zfree(zsl->header);

Review comment (suggestion):
Add a line or two documenting the function, like we have for the existing functions. "Returns the embedded element" or something better.

Bts, it takes a `const` node but it returns a non-const sds. Shouldn't it return a `const_sds` instead? (We added the `const_sds` type some year ago.) Ideally. But if changing these constness types causes a lot of trouble, we can leave it as is.

Code after:
static zskiplistNode *zslCreateNode(int height, double score, const_sds ele) {
    size_t ele_sds_len = sdslen(ele);
    char ele_sds_type = sdsReqType(ele_sds_len);
    size_t ele_sds_size = sdsReqSize(ele_sds_len, ele_sds_type);
    /* Allocate enough space for the node, levels, and the element sds.
     * We include one extra byte representing the sds header size,
     * which is the offset into the embedded sds data where the
     * string content starts. */
    zskiplistNode *zn = zmalloc(sizeof(*zn) + height * sizeof(struct zskiplistLevel) + 1 + ele_sds_size);
    zn->score = score;
    zslSetNodeHeight(zn, height);
    char *data = ((char *)(zn + 1)) + height * sizeof(struct zskiplistLevel);
    *data++ = sdsHdrSize(ele_sds_type);
    sdswrite(data, ele_sds_size, ele_sds_type, ele, ele_sds_len);
    return zn;
}

static zskiplistNode *zslCreateHeaderNode(void) {
    /* Allocate enough space for the node and levels. */
    zskiplistNode *zn = zmalloc(sizeof(*zn) + ZSKIPLIST_MAXLEVEL * sizeof(struct zskiplistLevel));
    zslSetNodeHeight(zn, ZSKIPLIST_MAXLEVEL);
    return zn;
}

/* Helper function to return the element string from a skip list node. */
sds zslGetNodeElement(const zskiplistNode *x) {
    unsigned char *data = (void *)(x + 1);
    data += zslGetNodeHeight(x) * sizeof(struct zskiplistLevel);
    uint8_t hdr_size = *(uint8_t *)data;
    data += 1 + hdr_size;
    return (sds)data;
}

/* Create a new skiplist. */
zskiplist *zslCreate(void) {
    int j;
    zskiplist *zsl;

    zsl = zmalloc(sizeof(*zsl));
    zsl->level = 1;
    zsl->length = 0;
    zsl->header = zslCreateHeaderNode();
    for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) {
        zsl->header->level[j].forward = NULL;
        zsl->header->level[j].span = 0;
    }
    zsl->header->backward = NULL;
    zsl->tail = NULL;
    return zsl;
}