关键词搜索

源码搜索 ×
×

漫话Redis源码之六十四

发布2022-01-23浏览554次

详情内容

这里主要是内存相关的一些操作,C语言爱好者肯定很熟悉。其实也不难。

  1. #include <stdint.h>
  2. /* Toggle the 16 bit unsigned integer pointed by *p from little endian to
  3. * big endian */
  4. void memrev16(void *p) {
  5. unsigned char *x = p, t;
  6. t = x[0];
  7. x[0] = x[1];
  8. x[1] = t;
  9. }
  10. /* Toggle the 32 bit unsigned integer pointed by *p from little endian to
  11. * big endian */
  12. void memrev32(void *p) {
  13. unsigned char *x = p, t;
  14. t = x[0];
  15. x[0] = x[3];
  16. x[3] = t;
  17. t = x[1];
  18. x[1] = x[2];
  19. x[2] = t;
  20. }
  21. /* Toggle the 64 bit unsigned integer pointed by *p from little endian to
  22. * big endian */
  23. void memrev64(void *p) {
  24. unsigned char *x = p, t;
  25. t = x[0];
  26. x[0] = x[7];
  27. x[7] = t;
  28. t = x[1];
  29. x[1] = x[6];
  30. x[6] = t;
  31. t = x[2];
  32. x[2] = x[5];
  33. x[5] = t;
  34. t = x[3];
  35. x[3] = x[4];
  36. x[4] = t;
  37. }
  38. uint16_t intrev16(uint16_t v) {
  39. memrev16(&v);
  40. return v;
  41. }
  42. uint32_t intrev32(uint32_t v) {
  43. memrev32(&v);
  44. return v;
  45. }
  46. uint64_t intrev64(uint64_t v) {
  47. memrev64(&v);
  48. return v;
  49. }
  50. #ifdef REDIS_TEST
  51. #include <stdio.h>
  52. #define UNUSED(x) (void)(x)
  53. int endianconvTest(int argc, char *argv[], int accurate) {
  54. char buf[32];
  55. UNUSED(argc);
  56. UNUSED(argv);
  57. UNUSED(accurate);
  58. sprintf(buf,"ciaoroma");
  59. memrev16(buf);
  60. printf("%s\n", buf);
  61. sprintf(buf,"ciaoroma");
  62. memrev32(buf);
  63. printf("%s\n", buf);
  64. sprintf(buf,"ciaoroma");
  65. memrev64(buf);
  66. printf("%s\n", buf);
  67. return 0;
  68. }
  69. #endif

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载