关键词搜索

源码搜索 ×
×

漫话Redis源码之二十三

发布2021-12-05浏览591次

详情内容

这里这要是映射,其实可以通过数组来实现:

  1. static inline char sdsReqType(size_t string_size) {
  2. if (string_size < 1<<5)
  3. return SDS_TYPE_5;
  4. if (string_size < 1<<8)
  5. return SDS_TYPE_8;
  6. if (string_size < 1<<16)
  7. return SDS_TYPE_16;
  8. #if (LONG_MAX == LLONG_MAX)
  9. if (string_size < 1ll<<32)
  10. return SDS_TYPE_32;
  11. return SDS_TYPE_64;
  12. #else
  13. return SDS_TYPE_32;
  14. #endif
  15. }
  16. static inline size_t sdsTypeMaxSize(char type) {
  17. if (type == SDS_TYPE_5)
  18. return (1<<5) - 1;
  19. if (type == SDS_TYPE_8)
  20. return (1<<8) - 1;
  21. if (type == SDS_TYPE_16)
  22. return (1<<16) - 1;
  23. #if (LONG_MAX == LLONG_MAX)
  24. if (type == SDS_TYPE_32)
  25. return (1ll<<32) - 1;
  26. #endif
  27. return -1; /* this is equivalent to the max SDS_TYPE_64 or SDS_TYPE_32 */
  28. }
  29. /* Create a new sds string with the content specified by the 'init' pointer
  30. * and 'initlen'.
  31. * If NULL is used for 'init' the string is initialized with zero bytes.
  32. * If SDS_NOINIT is used, the buffer is left uninitialized;
  33. *
  34. * The string is always null-termined (all the sds strings are, always) so
  35. * even if you create an sds string with:
  36. *
  37. * mystring = sdsnewlen("abc",3);
  38. *
  39. * You can print the string with printf() as there is an implicit \0 at the
  40. * end of the string. However the string is binary safe and can contain
  41. * \0 characters in the middle, as the length is stored in the sds header. */
  42. sds _sdsnewlen(const void *init, size_t initlen, int trymalloc) {
  43. void *sh;
  44. sds s;
  45. char type = sdsReqType(initlen);
  46. /* Empty strings are usually created in order to append. Use type 8
  47. * since type 5 is not good at this. */
  48. if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;
  49. int hdrlen = sdsHdrSize(type);
  50. unsigned char *fp; /* flags pointer. */
  51. size_t usable;
  52. assert(initlen + hdrlen + 1 > initlen); /* Catch size_t overflow */
  53. sh = trymalloc?
  54. s_trymalloc_usable(hdrlen+initlen+1, &usable) :
  55. s_malloc_usable(hdrlen+initlen+1, &usable);
  56. if (sh == NULL) return NULL;
  57. if (init==SDS_NOINIT)
  58. init = NULL;
  59. else if (!init)
  60. memset(sh, 0, hdrlen+initlen+1);
  61. s = (char*)sh+hdrlen;
  62. fp = ((unsigned char*)s)-1;
  63. usable = usable-hdrlen-1;
  64. if (usable > sdsTypeMaxSize(type))
  65. usable = sdsTypeMaxSize(type);
  66. switch(type) {
  67. case SDS_TYPE_5: {
  68. *fp = type | (initlen << SDS_TYPE_BITS);
  69. break;
  70. }
  71. case SDS_TYPE_8: {
  72. SDS_HDR_VAR(8,s);
  73. sh->len = initlen;
  74. sh->alloc = usable;
  75. *fp = type;
  76. break;
  77. }
  78. case SDS_TYPE_16: {
  79. SDS_HDR_VAR(16,s);
  80. sh->len = initlen;
  81. sh->alloc = usable;
  82. *fp = type;
  83. break;
  84. }
  85. case SDS_TYPE_32: {
  86. SDS_HDR_VAR(32,s);
  87. sh->len = initlen;
  88. sh->alloc = usable;
  89. *fp = type;
  90. break;
  91. }
  92. case SDS_TYPE_64: {
  93. SDS_HDR_VAR(64,s);
  94. sh->len = initlen;
  95. sh->alloc = usable;
  96. *fp = type;
  97. break;
  98. }
  99. }
  100. if (initlen && init)
  101. memcpy(s, init, initlen);
  102. s[initlen] = '\0';
  103. return s;
  104. }
  105. sds sdsnewlen(const void *init, size_t initlen) {
  106. return _sdsnewlen(init, initlen, 0);
  107. }
  108. sds sdstrynewlen(const void *init, size_t initlen) {
  109. return _sdsnewlen(init, initlen, 1);
  110. }
  111. /* Create an empty (zero length) sds string. Even in this case the string
  112. * always has an implicit null term. */
  113. sds sdsempty(void) {
  114. return sdsnewlen("",0);
  115. }
  116. /* Create a new sds string starting from a null terminated C string. */
  117. sds sdsnew(const char *init) {
  118. size_t initlen = (init == NULL) ? 0 : strlen(init);
  119. return sdsnewlen(init, initlen);
  120. }
  121. /* Duplicate an sds string. */
  122. sds sdsdup(const sds s) {
  123. return sdsnewlen(s, sdslen(s));
  124. }
  125. /* Free an sds string. No operation is performed if 's' is NULL. */
  126. void sdsfree(sds s) {
  127. if (s == NULL) return;
  128. s_free((char*)s-sdsHdrSize(s[-1]));
  129. }

相关技术文章

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

提示信息

×

选择支付方式

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