示例#1
0
/* Decode the encoded length pointed by 'p' */
static unsigned int zipmapDecodeLength(unsigned char *p) {
    unsigned int len = *p;

    if (len < ZIPMAP_BIGLEN) return len;
    memcpy(&len,p+1,sizeof(unsigned int));
    memrev32ifbe(&len);
    return len;
}
/* 获取len字段的数值(即随后字符串的长度),其原理很简单:查看第一个字节的数值,如果该数值小于254,则直接返回,
    否则读取接下来的4个字节内容表示的数值。 */
static unsigned int zipmapDecodeLength(unsigned char *p) {
    unsigned int len = *p;

    if (len < ZIPMAP_BIGLEN) return len;
    // 读取随后4个字节的内容
    memcpy(&len,p+1,sizeof(unsigned int));
    // 统一转化为小端模式表示
    memrev32ifbe(&len);
    return len;
}
示例#3
0
/* Decode the encoded length pointed by 'p'
 * 解码的长度*/
static unsigned int zipmapDecodeLength(unsigned char *p) {
    unsigned int len = *p;

    if (len < ZIPMAP_BIGLEN) return len;
    /**
     * memcpy 函数用于 把资源内存(src所指向的内存区域) 拷贝到目标内存(dest所指向的内存区域);拷贝多少个?有一个size变量控制
     */
    memcpy(&len,p+1,sizeof(unsigned int));
    memrev32ifbe(&len);
    return len;
}
示例#4
0
/* Set the value at pos, using the configured encoding. */
static void _intsetSet(intset *is, int pos, int64_t value) {
    if (is->encoding == INTSET_ENC_INT64) {
        ((int64_t*)is->contents)[pos] = value;
        memrev64ifbe(((int64_t*)is->contents)+pos);
    } else if (is->encoding == INTSET_ENC_INT32) {
        ((int32_t*)is->contents)[pos] = (int32_t)value;
        memrev32ifbe(((int32_t*)is->contents)+pos);
    } else {
        ((int16_t*)is->contents)[pos] = (int16_t)value;
        memrev16ifbe(((int16_t*)is->contents)+pos);
    }
}
示例#5
0
文件: intset.c 项目: Coder-chen/redis
/*
*
* 设置以 pos 为起点的特定的 enc (编码) 的值
*
*/
static void _intsetSet(intset *is, int pos, int64_t value) {
    uint32_t encoding = intrev32ifbe(is->encoding);

    if (encoding == INTSET_ENC_INT64) {
        ((int64_t*)is->contents)[pos] = value;        // value 顺序存放在数组 contents 中 
        memrev64ifbe(((int64_t*)is->contents)+pos);  // 统一大小端
    } else if (encoding == INTSET_ENC_INT32) {
        ((int32_t*)is->contents)[pos] = value;
        memrev32ifbe(((int32_t*)is->contents)+pos);
    } else {
        ((int16_t*)is->contents)[pos] = value;
        memrev16ifbe(((int16_t*)is->contents)+pos);
    }
}
示例#6
0
/* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
 * the amount of bytes required to encode such a length. */
static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) {
    if (p == NULL) {
        return ZIPMAP_LEN_BYTES(len);
    } else {
        if (len < ZIPMAP_BIGLEN) {
            p[0] = len;
            return 1;
        } else {
            p[0] = ZIPMAP_BIGLEN;
            memcpy(p+1,&len,sizeof(len));
            memrev32ifbe(p+1);
            return 1+sizeof(len);
        }
    }
}
示例#7
0
//将is中pos位置上的值设为value
static void _intsetSet(intset *is, int pos, int64_t value) {
    uint32_t encoding = intrev32ifbe(is->encoding);

    //根据编码,将contents转换为对应得数组类型,然后对该位置的元素赋值
    if (encoding == INTSET_ENC_INT64) {
        ((int64_t*)is->contents)[pos] = value;
        memrev64ifbe(((int64_t*)is->contents)+pos);
    } else if (encoding == INTSET_ENC_INT32) {
        ((int32_t*)is->contents)[pos] = value;
        memrev32ifbe(((int32_t*)is->contents)+pos);
    } else {
        ((int16_t*)is->contents)[pos] = value;
        memrev16ifbe(((int16_t*)is->contents)+pos);
    }
}
/* 将长度len编码到p指针指向的内存空间 */
static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) {
    if (p == NULL) {
        return ZIPMAP_LEN_BYTES(len);
    } else {
        if (len < ZIPMAP_BIGLEN) {
            // 长度小于254,只需要1个字节表示
            p[0] = len;
            return 1;
        } else {
            // 长度大于等于254,第一个字节赋值为254,接下来的4歌字节才是真正的长度值
            p[0] = ZIPMAP_BIGLEN;
            memcpy(p+1,&len,sizeof(len));
            memrev32ifbe(p+1);
            return 1+sizeof(len);
        }
    }
}
示例#9
0
文件: intset.c 项目: LC2010/note
/*
 * 根据给定的编码方式,返回给定位置上的值
 *
 * T = theta(1)
 */
static int64_t _intsetGetEncoded(intset *is, int pos, uint8_t enc) {
    int64_t v64;
    int32_t v32;
    int16_t v16;

    if (enc == INTSET_ENC_INT64) {
        memcpy(&v64,((int64_t*)is->contents)+pos,sizeof(v64));
        memrev64ifbe(&v64);
        return v64;
    } else if (enc == INTSET_ENC_INT32) {
        memcpy(&v32,((int32_t*)is->contents)+pos,sizeof(v32));
        memrev32ifbe(&v32);
        return v32;
    } else {
        memcpy(&v16,((int16_t*)is->contents)+pos,sizeof(v16));
        memrev16ifbe(&v16);
        return v16;
    }
}
/* Set the value at pos, using the configured encoding.
 *
 * 根据集合的编码方式,将底层数组在pos位置上的值设为value。
 *
 * T = O(1)
 */
static void _intsetSet(intset *is, int pos, int64_t value) {
	// 取出集合的编码方式
    uint32_t encoding = intrev32ifbe(is->encoding);

    // 根据编码((Enc_t*)is->contents)将数组转换回正确的类型
    // 然后((Enc_t*)is->contents)[pos]定位到数组索引上
    // 接着((Enc_t*)is->contents)[pos] = value将值赋给数组
    // 最后,((Enc_t*)is->contents)+pos定位到刚刚设置的新值上
    // 如果有需要的话,memrevEncifbe将对值进行大小端转换
    if (encoding == INTSET_ENC_INT64) {
        ((int64_t*)is->contents)[pos] = value;
        memrev64ifbe(((int64_t*)is->contents)+pos);
    } else if (encoding == INTSET_ENC_INT32) {
        ((int32_t*)is->contents)[pos] = value;
        memrev32ifbe(((int32_t*)is->contents)+pos);
    } else {
        ((int16_t*)is->contents)[pos] = value;
        memrev16ifbe(((int16_t*)is->contents)+pos);
    }
}
示例#11
0
//对于给定的编码enc以及位置pos,返回is中的值
static int64_t _intsetGetEncoded(intset *is, int pos, uint8_t enc) {
    int64_t v64;
    int32_t v32;
    int16_t v16;

    if (enc == INTSET_ENC_INT64) {
    	//如果是64的编码,将contents转换为指向int64_t的指针,移动到第pos个元素的位置,
    	//将这里起得64位内存复制到v64中。
        memcpy(&v64,((int64_t*)is->contents)+pos,sizeof(v64));
        memrev64ifbe(&v64);
        return v64;
    } else if (enc == INTSET_ENC_INT32) {
    	//同上,只是这时contents相当与是存放int32_t的数组
        memcpy(&v32,((int32_t*)is->contents)+pos,sizeof(v32));
        memrev32ifbe(&v32);
        return v32;
    } else {
    	//同上,只是这时contents相当与是存放int16_t的数组
        memcpy(&v16,((int16_t*)is->contents)+pos,sizeof(v16));
        memrev16ifbe(&v16);
        return v16;
    }
}
/* Return the value at pos, given an encoding.
 *
 * 根据给定的编码方式enc,返回集合的底层数组在pos索引上的元素。
 *
 * T = O(1)
 */
static int64_t _intsetGetEncoded(intset *is, int pos, uint8_t enc) {
    int64_t v64;
    int32_t v32;
    int16_t v16;

    // ((ENCODING*)is->contents) 首先将数组转换回被编码的类型
    // 然后((ENCODING*)is->contents)+pos计算出元素在数组中的正确位置
    // 之后member(&vEnc, ..., sizeof(vEnc))再从数组中拷贝出正确数量的字节
    // 如果有需要的话,memrevEncifbe(&vEnc)会对拷贝出的字节进行大小端转换
    // 最后将值返回
    if (enc == INTSET_ENC_INT64) {
        memcpy(&v64,((int64_t*)is->contents)+pos,sizeof(v64));
        memrev64ifbe(&v64);
        return v64;
    } else if (enc == INTSET_ENC_INT32) {
        memcpy(&v32,((int32_t*)is->contents)+pos,sizeof(v32));
        memrev32ifbe(&v32);
        return v32;
    } else {
        memcpy(&v16,((int16_t*)is->contents)+pos,sizeof(v16));
        memrev16ifbe(&v16);
        return v16;
    }
}