sds.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* SDSLib 2.0 -- A C dynamic strings library
  2. *
  3. * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>
  4. * Copyright (c) 2015, Oran Agra
  5. * Copyright (c) 2015, Redis Labs, Inc
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of Redis nor the names of its contributors may be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef __SDS_H
  33. #define __SDS_H
  34. #define SDS_MAX_PREALLOC (1024 * 1024)
  35. #include <sys/types.h>
  36. #include <stdarg.h>
  37. #include <stdint.h>
  38. typedef char* sds;
  39. /* Note: sdshdr5 is never used, we just access the flags byte directly.
  40. * However is here to document the layout of type 5 SDS strings. */
  41. struct __attribute__((__packed__)) sdshdr5 {
  42. unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
  43. char buf[];
  44. };
  45. struct __attribute__((__packed__)) sdshdr8 {
  46. uint8_t len; /* used */
  47. uint8_t alloc; /* excluding the header and null terminator */
  48. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  49. char buf[];
  50. };
  51. struct __attribute__((__packed__)) sdshdr16 {
  52. uint16_t len; /* used */
  53. uint16_t alloc; /* excluding the header and null terminator */
  54. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  55. char buf[];
  56. };
  57. struct __attribute__((__packed__)) sdshdr32 {
  58. uint32_t len; /* used */
  59. uint32_t alloc; /* excluding the header and null terminator */
  60. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  61. char buf[];
  62. };
  63. struct __attribute__((__packed__)) sdshdr64 {
  64. uint64_t len; /* used */
  65. uint64_t alloc; /* excluding the header and null terminator */
  66. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  67. char buf[];
  68. };
  69. #define SDS_TYPE_5 0
  70. #define SDS_TYPE_8 1
  71. #define SDS_TYPE_16 2
  72. #define SDS_TYPE_32 3
  73. #define SDS_TYPE_64 4
  74. #define SDS_TYPE_MASK 7
  75. #define SDS_TYPE_BITS 3
  76. #define SDS_HDR_VAR(T, s) \
  77. struct sdshdr ## T* sh = (void*) ((s) - (sizeof(struct sdshdr ## T)));
  78. #define SDS_HDR(T, s) ((struct sdshdr ## T*)((s) - (sizeof(struct sdshdr ## T))))
  79. #define SDS_TYPE_5_LEN(f) ((f) >> SDS_TYPE_BITS)
  80. static inline size_t sdslen(const sds s)
  81. {
  82. unsigned char flags = s[-1];
  83. switch (flags & SDS_TYPE_MASK) {
  84. case SDS_TYPE_5:
  85. return SDS_TYPE_5_LEN(flags);
  86. case SDS_TYPE_8:
  87. return SDS_HDR(8, s)->len;
  88. case SDS_TYPE_16:
  89. return SDS_HDR(16, s)->len;
  90. case SDS_TYPE_32:
  91. return SDS_HDR(32, s)->len;
  92. case SDS_TYPE_64:
  93. return SDS_HDR(64, s)->len;
  94. }
  95. return 0;
  96. }
  97. static inline size_t sdsavail(const sds s)
  98. {
  99. unsigned char flags = s[-1];
  100. switch (flags & SDS_TYPE_MASK) {
  101. case SDS_TYPE_5: {
  102. return 0;
  103. }
  104. case SDS_TYPE_8: {
  105. SDS_HDR_VAR(8, s);
  106. return sh->alloc - sh->len;
  107. }
  108. case SDS_TYPE_16: {
  109. SDS_HDR_VAR(16, s);
  110. return sh->alloc - sh->len;
  111. }
  112. case SDS_TYPE_32: {
  113. SDS_HDR_VAR(32, s);
  114. return sh->alloc - sh->len;
  115. }
  116. case SDS_TYPE_64: {
  117. SDS_HDR_VAR(64, s);
  118. return sh->alloc - sh->len;
  119. }
  120. }
  121. return 0;
  122. }
  123. static inline void sdssetlen(sds s, size_t newlen)
  124. {
  125. unsigned char flags = s[-1];
  126. switch (flags & SDS_TYPE_MASK) {
  127. case SDS_TYPE_5: {
  128. unsigned char* fp = ((unsigned char*) s) - 1;
  129. *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
  130. } break;
  131. case SDS_TYPE_8:
  132. SDS_HDR(8, s)->len = newlen;
  133. break;
  134. case SDS_TYPE_16:
  135. SDS_HDR(16, s)->len = newlen;
  136. break;
  137. case SDS_TYPE_32:
  138. SDS_HDR(32, s)->len = newlen;
  139. break;
  140. case SDS_TYPE_64:
  141. SDS_HDR(64, s)->len = newlen;
  142. break;
  143. }
  144. }
  145. static inline void sdsinclen(sds s, size_t inc)
  146. {
  147. unsigned char flags = s[-1];
  148. switch (flags & SDS_TYPE_MASK) {
  149. case SDS_TYPE_5: {
  150. unsigned char* fp = ((unsigned char*) s) - 1;
  151. unsigned char newlen = SDS_TYPE_5_LEN(flags) + inc;
  152. *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
  153. } break;
  154. case SDS_TYPE_8:
  155. SDS_HDR(8, s)->len += inc;
  156. break;
  157. case SDS_TYPE_16:
  158. SDS_HDR(16, s)->len += inc;
  159. break;
  160. case SDS_TYPE_32:
  161. SDS_HDR(32, s)->len += inc;
  162. break;
  163. case SDS_TYPE_64:
  164. SDS_HDR(64, s)->len += inc;
  165. break;
  166. }
  167. }
  168. /* sdsalloc() = sdsavail() + sdslen() */
  169. static inline size_t sdsalloc(const sds s)
  170. {
  171. unsigned char flags = s[-1];
  172. switch (flags & SDS_TYPE_MASK) {
  173. case SDS_TYPE_5:
  174. return SDS_TYPE_5_LEN(flags);
  175. case SDS_TYPE_8:
  176. return SDS_HDR(8, s)->alloc;
  177. case SDS_TYPE_16:
  178. return SDS_HDR(16, s)->alloc;
  179. case SDS_TYPE_32:
  180. return SDS_HDR(32, s)->alloc;
  181. case SDS_TYPE_64:
  182. return SDS_HDR(64, s)->alloc;
  183. }
  184. return 0;
  185. }
  186. static inline void sdssetalloc(sds s, size_t newlen)
  187. {
  188. unsigned char flags = s[-1];
  189. switch (flags & SDS_TYPE_MASK) {
  190. case SDS_TYPE_5:
  191. /* Nothing to do, this type has no total allocation info. */
  192. break;
  193. case SDS_TYPE_8:
  194. SDS_HDR(8, s)->alloc = newlen;
  195. break;
  196. case SDS_TYPE_16:
  197. SDS_HDR(16, s)->alloc = newlen;
  198. break;
  199. case SDS_TYPE_32:
  200. SDS_HDR(32, s)->alloc = newlen;
  201. break;
  202. case SDS_TYPE_64:
  203. SDS_HDR(64, s)->alloc = newlen;
  204. break;
  205. }
  206. }
  207. sds sdsnewlen(const void* init, size_t initlen);
  208. sds sdsnew(const char* init);
  209. sds sdsempty(void);
  210. sds sdsdup(const sds s);
  211. void sdsfree(sds s);
  212. sds sdsgrowzero(sds s, size_t len);
  213. sds sdscatlen(sds s, const void* t, size_t len);
  214. sds sdscat(sds s, const char* t);
  215. sds sdscatsds(sds s, const sds t);
  216. sds sdscpylen(sds s, const char* t, size_t len);
  217. sds sdscpy(sds s, const char* t);
  218. sds sdscatvprintf(sds s, const char* fmt, va_list ap);
  219. #ifdef __GNUC__
  220. sds sdscatprintf(sds s, const char* fmt, ...)
  221. __attribute__((format(printf, 2, 3)));
  222. #else
  223. sds sdscatprintf(sds s, const char* fmt, ...);
  224. #endif
  225. sds sdscatfmt(sds s, char const* fmt, ...);
  226. sds sdstrim(sds s, const char* cset);
  227. void sdsrange(sds s, int start, int end);
  228. void sdsupdatelen(sds s);
  229. void sdsclear(sds s);
  230. int sdscmp(const sds s1, const sds s2);
  231. sds* sdssplitlen(
  232. const char* s, int len, const char* sep, int seplen, int* count);
  233. void sdsfreesplitres(sds* tokens, int count);
  234. void sdstolower(sds s);
  235. void sdstoupper(sds s);
  236. sds sdsfromlonglong(long long value);
  237. sds sdscatrepr(sds s, const char* p, size_t len);
  238. sds* sdssplitargs(const char* line, int* argc);
  239. sds sdsmapchars(sds s, const char* from, const char* to, size_t setlen);
  240. sds sdsjoin(char** argv, int argc, char* sep);
  241. sds sdsjoinsds(sds* argv, int argc, const char* sep, size_t seplen);
  242. /* Low level functions exposed to the user API */
  243. sds sdsMakeRoomFor(sds s, size_t addlen);
  244. void sdsIncrLen(sds s, int incr);
  245. sds sdsRemoveFreeSpace(sds s);
  246. size_t sdsAllocSize(sds s);
  247. void* sdsAllocPtr(sds s);
  248. #ifdef REDIS_TEST
  249. int sdsTest(int argc, char* argv[]);
  250. #endif
  251. #endif