在阅读redis源码/src/endianconv.c时遇到了一些不理解的语句
#include <stdint.h> /* Toggle the 16 bit unsigned integer pointed by *p from little endian to * big endian */ void memrev16(void *p) { unsigned char *x = p, t; t = x[0]; x[0] = x[1]; x[1] = t; } /* Toggle the 32 bit unsigned integer pointed by *p from little endian to * big endian */ void memrev32(void *p) { unsigned char *x = p, t; t = x[0]; x[0] = x[3]; x[3] = t; t = x[1]; x[1] = x[2]; x[2] = t; } /* Toggle the 64 bit unsigned integer pointed by *p from little endian to * big endian */ void memrev64(void *p) { unsigned char *x = p, t; t = x[0]; x[0] = x[7]; x[7] = t; t = x[1]; x[1] = x[6]; x[6] = t; t = x[2]; x[2] = x[5]; x[5] = t; t = x[3]; x[3] = x[4]; x[4] = t; } uint16_t intrev16(uint16_t v) { memrev16(&v); return v; } uint32_t intrev32(uint32_t v) { memrev32(&v); return v; } uint64_t intrev64(uint64_t v) { memrev64(&v); return v; } #ifdef TESTMAIN #include <stdio.h> int main(void) { char buf[32]; sprintf(buf,"ciaoroma"); memrev16(buf); printf("%s\n", buf); sprintf(buf,"ciaoroma"); memrev32(buf); printf("%s\n", buf); sprintf(buf,"ciaoroma"); memrev64(buf); printf("%s\n", buf); return 0; } #endif
从代码可知方法是用来低位与高位交换,但为什么要交换?
这里就要了解相关的Big_Endian&Little_Endian的知识。
Big_Endian是指低地址存放最高有效字节(MSB),而Little_Endian则是低地址存放最低有效字节(LSB),不同的CPU采用不同的方式存储数据。因此考虑到兼容性的问题,必须先判断主机CPU的存储方式,再进行字节序的转换工作。
redis中/src/config.h
/* Byte ordering detection */ #include <sys/types.h> /* This will likely define BYTE_ORDER */ #ifndef BYTE_ORDER #if (BSD >= 199103) # include <machine/endian.h> #else #if defined(linux) || defined(__linux__) # include <endian.h> #else #define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */ #define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */ #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp)*/ #if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \ defined(vax) || defined(ns32000) || defined(sun386) || \ defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \ defined(__alpha__) || defined(__alpha) #define BYTE_ORDER LITTLE_ENDIAN #endif #if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \ defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \ defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\ defined(apollo) || defined(__convex__) || defined(_CRAY) || \ defined(__hppa) || defined(__hp9000) || \ defined(__hp9000s300) || defined(__hp9000s700) || \ defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc) #define BYTE_ORDER BIG_ENDIAN #endif #endif /* linux */ #endif /* BSD */ #endif /* BYTE_ORDER */ /* Sometimes after including an OS-specific header that defines the * endianess we end with __BYTE_ORDER but not with BYTE_ORDER that is what * the Redis code uses. In this case let's define everything without the * underscores. */ #ifndef BYTE_ORDER #ifdef __BYTE_ORDER #if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN) #ifndef LITTLE_ENDIAN #define LITTLE_ENDIAN __LITTLE_ENDIAN #endif #ifndef BIG_ENDIAN #define BIG_ENDIAN __BIG_ENDIAN #endif #if (__BYTE_ORDER == __LITTLE_ENDIAN) #define BYTE_ORDER LITTLE_ENDIAN #else #define BYTE_ORDER BIG_ENDIAN #endif #endif #endif #endif #if !defined(BYTE_ORDER) || \ (BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN) /* you must determine what the correct bit order is for * your compiler - the next line is an intentional error * which will force your compiles to bomb until you fix * the above macros. */ #error "Undefined or invalid BYTE_ORDER" #endif
做了详细的兼容
【用函数判断系统是Big Endian还是Little Endian】 bool IsBig_Endian() //如果字节序为big-endian,返回true; //反之为 little-endian,返回false { unsigned short test = 0x1122; if(*( (unsigned char*) &test ) == 0x11) return TRUE; else return FALSE; }
相关推荐
redis-stack-server-7.2.0-v9.arm64.snap redis-stack-server-7.2.0-v9.bionic.arm64.tar.gz redis-stack-server-7.2.0-v9.bionic.x86_64.tar.gz redis-stack-server-7.2.0-v9.bullseye.x86_64.tar.gz redis-stack-...
RedisPlus是为Redis可视化管理开发的一款开源免费的桌面客户端软件,支持Windows 、Linux、Mac三大系统平台,RedisPlus提供更加高效、方便、快捷的使用体验,有着更加现代化的用户界面风格。该软件支持单机、集群...
RedisPlus是一款针对Redis数据库的可视化管理工具,专为提升Redis数据操作和监控的便捷性而设计。这个压缩包“redisplus-3.0.1-exe-x86_64.rar”是适用于64位Windows操作系统的版本。通过提供的文件名“redisplus-...
redis-4.0.14-1.x86_64.rpm
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
**Redis2-NGINX-Module 0.15 知识详解** Redis2-NGINX-Module 是一个用于 NGINX Web 服务器的扩展模块,它允许开发者将 NGINX 与 Redis 数据库集成,实现高效的数据缓存和交互。这个版本是 0.15,它提供了一系列...
php_redis-2.2.7-5.5-nts-vc11-x86.dll php_igbinary-1.2.1-5.5-ts-vc11-x86.pdb 解压缩后,将php_redis.dll和php_redis.pdb拷贝至php的ext目录下, 修改php.ini,在该文件中加入: ; php_redis extension=...
RedisInsight v2.0(RedisInsight-v2-linux-x86_64.AppImage)适用于Linux系统
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
Redis-x64-3.2.100&Redis-3.2.100-Windows-32下载整理的windows 64位&32位包。 欢迎下载。Redis-x64-3.2.100&Redis-3.2.100-Windows-32下载整理的windows 64位&32位包。 欢迎下载。Redis-x64-3.2.100&Redis-3.2.100-...
官方离线安装包,亲测可用
redis 5.0.10 rpm包
标题中的"php_redis-2.2.7-5.6-nts-vc11-x86"和"php_redis-2.2.5-5.6-ts-vc11-x86"指的是PHP的Redis扩展的不同版本,适用于PHP 5.6。这里的"2.2.7"和"2.2.5"是Redis扩展的版本号,"5.6"对应的是PHP的版本号。"nts"和...
官方离线安装包,亲测可用
windows系统redis安装文件,Redis-x64-3.0.504(稳定版);Redis-x64-3.2.100(预发行版);redis-desktop-manager-0.9.3.817(redis界面工具)。具体安装方法详见:...
php_redis-2.2.7-5.3-nts-vc9-x86 php_redis-2.2.7-5.3-ts-vc9-x86 php_redis-2.2.7-5.4-nts-vc9-x86 php_redis-2.2.7-5.4-ts-vc9-x86 php_redis-2.2.7-5.5-nts-vc11-x64 php_redis-2.2.7-5.5-nts-vc11-x86 ...
自己在工作中用到的各类工具安装包,种类比较杂,方便自己方便别人
"tomcat9+tomcat-cluster-redis-session-manager_4.0.zip"这个文件组合涉及到的是在Tomcat 9上实现负载均衡以及使用Redis作为Session管理器的高级配置。 首先,Tomcat 9是Apache Tomcat服务器的一个版本,它是Java ...
赠送jar包:flink-connector-redis_2.10-1.1.5.jar; 赠送原API文档:flink-connector-redis_2.10-1.1.5-javadoc.jar; 赠送源代码:flink-connector-redis_2.10-1.1.5-sources.jar; 赠送Maven依赖信息文件:flink-...
redis-desktop-manager_0.9.2.19_amd64.deb for linux Redis管理工具 0.9.2