2
0

sds.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #ifdef _MSC_VER
  36. #define __attribute__(x)
  37. #endif
  38. #include <sys/types.h>
  39. #include <stdarg.h>
  40. #include <stdint.h>
  41. typedef char *sds;
  42. /* Note: sdshdr5 is never used, we just access the flags byte directly.
  43. * However is here to document the layout of type 5 SDS strings. */
  44. struct __attribute__ ((__packed__)) sdshdr5 {
  45. unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
  46. char buf[];
  47. };
  48. struct __attribute__ ((__packed__)) sdshdr8 {
  49. uint8_t len; /* used */
  50. uint8_t alloc; /* excluding the header and null terminator */
  51. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  52. char buf[];
  53. };
  54. struct __attribute__ ((__packed__)) sdshdr16 {
  55. uint16_t len; /* used */
  56. uint16_t alloc; /* excluding the header and null terminator */
  57. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  58. char buf[];
  59. };
  60. struct __attribute__ ((__packed__)) sdshdr32 {
  61. uint32_t len; /* used */
  62. uint32_t alloc; /* excluding the header and null terminator */
  63. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  64. char buf[];
  65. };
  66. struct __attribute__ ((__packed__)) sdshdr64 {
  67. uint64_t len; /* used */
  68. uint64_t alloc; /* excluding the header and null terminator */
  69. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  70. char buf[];
  71. };
  72. #define SDS_TYPE_5 0
  73. #define SDS_TYPE_8 1
  74. #define SDS_TYPE_16 2
  75. #define SDS_TYPE_32 3
  76. #define SDS_TYPE_64 4
  77. #define SDS_TYPE_MASK 7
  78. #define SDS_TYPE_BITS 3
  79. #define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T)));
  80. #define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))))
  81. #define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS)
  82. static inline size_t sdslen(const sds s) {
  83. unsigned char flags = s[-1];
  84. switch(flags&SDS_TYPE_MASK) {
  85. case SDS_TYPE_5:
  86. return SDS_TYPE_5_LEN(flags);
  87. case SDS_TYPE_8:
  88. return SDS_HDR(8,s)->len;
  89. case SDS_TYPE_16:
  90. return SDS_HDR(16,s)->len;
  91. case SDS_TYPE_32:
  92. return SDS_HDR(32,s)->len;
  93. case SDS_TYPE_64:
  94. return SDS_HDR(64,s)->len;
  95. }
  96. return 0;
  97. }
  98. static inline size_t sdsavail(const sds s) {
  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. unsigned char flags = s[-1];
  125. switch(flags&SDS_TYPE_MASK) {
  126. case SDS_TYPE_5:
  127. {
  128. unsigned char *fp = ((unsigned char*)s)-1;
  129. *fp = (unsigned char)(SDS_TYPE_5 | (newlen << SDS_TYPE_BITS));
  130. }
  131. break;
  132. case SDS_TYPE_8:
  133. SDS_HDR(8,s)->len = (uint8_t)newlen;
  134. break;
  135. case SDS_TYPE_16:
  136. SDS_HDR(16,s)->len = (uint16_t)newlen;
  137. break;
  138. case SDS_TYPE_32:
  139. SDS_HDR(32,s)->len = (uint32_t)newlen;
  140. break;
  141. case SDS_TYPE_64:
  142. SDS_HDR(64,s)->len = (uint64_t)newlen;
  143. break;
  144. }
  145. }
  146. static inline void sdsinclen(sds s, size_t inc) {
  147. unsigned char flags = s[-1];
  148. switch(flags&SDS_TYPE_MASK) {
  149. case SDS_TYPE_5:
  150. {
  151. unsigned char *fp = ((unsigned char*)s)-1;
  152. unsigned char newlen = SDS_TYPE_5_LEN(flags)+(unsigned char)inc;
  153. *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
  154. }
  155. break;
  156. case SDS_TYPE_8:
  157. SDS_HDR(8,s)->len += (uint8_t)inc;
  158. break;
  159. case SDS_TYPE_16:
  160. SDS_HDR(16,s)->len += (uint16_t)inc;
  161. break;
  162. case SDS_TYPE_32:
  163. SDS_HDR(32,s)->len += (uint32_t)inc;
  164. break;
  165. case SDS_TYPE_64:
  166. SDS_HDR(64,s)->len += (uint64_t)inc;
  167. break;
  168. }
  169. }
  170. /* sdsalloc() = sdsavail() + sdslen() */
  171. static inline size_t sdsalloc(const sds s) {
  172. unsigned char flags = s[-1];
  173. switch(flags&SDS_TYPE_MASK) {
  174. case SDS_TYPE_5:
  175. return SDS_TYPE_5_LEN(flags);
  176. case SDS_TYPE_8:
  177. return SDS_HDR(8,s)->alloc;
  178. case SDS_TYPE_16:
  179. return SDS_HDR(16,s)->alloc;
  180. case SDS_TYPE_32:
  181. return SDS_HDR(32,s)->alloc;
  182. case SDS_TYPE_64:
  183. return SDS_HDR(64,s)->alloc;
  184. }
  185. return 0;
  186. }
  187. static inline void sdssetalloc(sds s, size_t newlen) {
  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 = (uint8_t)newlen;
  195. break;
  196. case SDS_TYPE_16:
  197. SDS_HDR(16,s)->alloc = (uint16_t)newlen;
  198. break;
  199. case SDS_TYPE_32:
  200. SDS_HDR(32,s)->alloc = (uint32_t)newlen;
  201. break;
  202. case SDS_TYPE_64:
  203. SDS_HDR(64,s)->alloc = (uint64_t)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(const char *s, int len, const char *sep, int seplen, int *count);
  232. void sdsfreesplitres(sds *tokens, int count);
  233. void sdstolower(sds s);
  234. void sdstoupper(sds s);
  235. sds sdsfromlonglong(long long value);
  236. sds sdscatrepr(sds s, const char *p, size_t len);
  237. sds *sdssplitargs(const char *line, int *argc);
  238. sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
  239. sds sdsjoin(char **argv, int argc, char *sep);
  240. sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);
  241. /* Low level functions exposed to the user API */
  242. sds sdsMakeRoomFor(sds s, size_t addlen);
  243. void sdsIncrLen(sds s, int incr);
  244. sds sdsRemoveFreeSpace(sds s);
  245. size_t sdsAllocSize(sds s);
  246. void *sdsAllocPtr(sds s);
  247. /* Export the allocator used by SDS to the program using SDS.
  248. * Sometimes the program SDS is linked to, may use a different set of
  249. * allocators, but may want to allocate or free things that SDS will
  250. * respectively free or allocate. */
  251. void *sds_malloc(size_t size);
  252. void *sds_realloc(void *ptr, size_t size);
  253. void sds_free(void *ptr);
  254. #ifdef REDIS_TEST
  255. int sdsTest(int argc, char *argv[]);
  256. #endif
  257. #endif