2
0

sds.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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) struct sdshdr##T *sh = (struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T)));
  77. #define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))))
  78. #define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS)
  79. static inline size_t sdslen(const sds s) {
  80. unsigned char flags = s[-1];
  81. switch(flags&SDS_TYPE_MASK) {
  82. case SDS_TYPE_5:
  83. return SDS_TYPE_5_LEN(flags);
  84. case SDS_TYPE_8:
  85. return SDS_HDR(8,s)->len;
  86. case SDS_TYPE_16:
  87. return SDS_HDR(16,s)->len;
  88. case SDS_TYPE_32:
  89. return SDS_HDR(32,s)->len;
  90. case SDS_TYPE_64:
  91. return SDS_HDR(64,s)->len;
  92. }
  93. return 0;
  94. }
  95. static inline size_t sdsavail(const sds s) {
  96. unsigned char flags = s[-1];
  97. switch(flags&SDS_TYPE_MASK) {
  98. case SDS_TYPE_5: {
  99. return 0;
  100. }
  101. case SDS_TYPE_8: {
  102. SDS_HDR_VAR(8,s);
  103. return sh->alloc - sh->len;
  104. }
  105. case SDS_TYPE_16: {
  106. SDS_HDR_VAR(16,s);
  107. return sh->alloc - sh->len;
  108. }
  109. case SDS_TYPE_32: {
  110. SDS_HDR_VAR(32,s);
  111. return sh->alloc - sh->len;
  112. }
  113. case SDS_TYPE_64: {
  114. SDS_HDR_VAR(64,s);
  115. return sh->alloc - sh->len;
  116. }
  117. }
  118. return 0;
  119. }
  120. static inline void sdssetlen(sds s, size_t newlen) {
  121. unsigned char flags = s[-1];
  122. switch(flags&SDS_TYPE_MASK) {
  123. case SDS_TYPE_5:
  124. {
  125. unsigned char *fp = ((unsigned char*)s)-1;
  126. *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
  127. }
  128. break;
  129. case SDS_TYPE_8:
  130. SDS_HDR(8,s)->len = newlen;
  131. break;
  132. case SDS_TYPE_16:
  133. SDS_HDR(16,s)->len = newlen;
  134. break;
  135. case SDS_TYPE_32:
  136. SDS_HDR(32,s)->len = newlen;
  137. break;
  138. case SDS_TYPE_64:
  139. SDS_HDR(64,s)->len = newlen;
  140. break;
  141. }
  142. }
  143. static inline void sdsinclen(sds s, size_t inc) {
  144. unsigned char flags = s[-1];
  145. switch(flags&SDS_TYPE_MASK) {
  146. case SDS_TYPE_5:
  147. {
  148. unsigned char *fp = ((unsigned char*)s)-1;
  149. unsigned char newlen = SDS_TYPE_5_LEN(flags)+inc;
  150. *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
  151. }
  152. break;
  153. case SDS_TYPE_8:
  154. SDS_HDR(8,s)->len += inc;
  155. break;
  156. case SDS_TYPE_16:
  157. SDS_HDR(16,s)->len += inc;
  158. break;
  159. case SDS_TYPE_32:
  160. SDS_HDR(32,s)->len += inc;
  161. break;
  162. case SDS_TYPE_64:
  163. SDS_HDR(64,s)->len += inc;
  164. break;
  165. }
  166. }
  167. /* sdsalloc() = sdsavail() + sdslen() */
  168. static inline size_t sdsalloc(const sds s) {
  169. unsigned char flags = s[-1];
  170. switch(flags&SDS_TYPE_MASK) {
  171. case SDS_TYPE_5:
  172. return SDS_TYPE_5_LEN(flags);
  173. case SDS_TYPE_8:
  174. return SDS_HDR(8,s)->alloc;
  175. case SDS_TYPE_16:
  176. return SDS_HDR(16,s)->alloc;
  177. case SDS_TYPE_32:
  178. return SDS_HDR(32,s)->alloc;
  179. case SDS_TYPE_64:
  180. return SDS_HDR(64,s)->alloc;
  181. }
  182. return 0;
  183. }
  184. static inline void sdssetalloc(sds s, size_t newlen) {
  185. unsigned char flags = s[-1];
  186. switch(flags&SDS_TYPE_MASK) {
  187. case SDS_TYPE_5:
  188. /* Nothing to do, this type has no total allocation info. */
  189. break;
  190. case SDS_TYPE_8:
  191. SDS_HDR(8,s)->alloc = newlen;
  192. break;
  193. case SDS_TYPE_16:
  194. SDS_HDR(16,s)->alloc = newlen;
  195. break;
  196. case SDS_TYPE_32:
  197. SDS_HDR(32,s)->alloc = newlen;
  198. break;
  199. case SDS_TYPE_64:
  200. SDS_HDR(64,s)->alloc = newlen;
  201. break;
  202. }
  203. }
  204. sds sdsnewlen(const void *init, size_t initlen);
  205. sds sdsnew(const char *init);
  206. sds sdsempty(void);
  207. sds sdsdup(const sds s);
  208. void sdsfree(sds s);
  209. sds sdsgrowzero(sds s, size_t len);
  210. sds sdscatlen(sds s, const void *t, size_t len);
  211. sds sdscat(sds s, const char *t);
  212. sds sdscatsds(sds s, const sds t);
  213. sds sdscpylen(sds s, const char *t, size_t len);
  214. sds sdscpy(sds s, const char *t);
  215. sds sdscatvprintf(sds s, const char *fmt, va_list ap);
  216. #ifdef __GNUC__
  217. sds sdscatprintf(sds s, const char *fmt, ...)
  218. __attribute__((format(printf, 2, 3)));
  219. #else
  220. sds sdscatprintf(sds s, const char *fmt, ...);
  221. #endif
  222. sds sdscatfmt(sds s, char const *fmt, ...);
  223. sds sdstrim(sds s, const char *cset);
  224. void sdsrange(sds s, int start, int end);
  225. void sdsupdatelen(sds s);
  226. void sdsclear(sds s);
  227. int sdscmp(const sds s1, const sds s2);
  228. sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
  229. void sdsfreesplitres(sds *tokens, int count);
  230. void sdstolower(sds s);
  231. void sdstoupper(sds s);
  232. sds sdsfromlonglong(long long value);
  233. sds sdscatrepr(sds s, const char *p, size_t len);
  234. sds *sdssplitargs(const char *line, int *argc);
  235. sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
  236. sds sdsjoin(char **argv, int argc, char *sep);
  237. sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);
  238. /* Low level functions exposed to the user API */
  239. sds sdsMakeRoomFor(sds s, size_t addlen);
  240. void sdsIncrLen(sds s, int incr);
  241. sds sdsRemoveFreeSpace(sds s);
  242. size_t sdsAllocSize(sds s);
  243. void *sdsAllocPtr(sds s);
  244. /* Export the allocator used by SDS to the program using SDS.
  245. * Sometimes the program SDS is linked to, may use a different set of
  246. * allocators, but may want to allocate or free things that SDS will
  247. * respectively free or allocate. */
  248. void *sds_malloc(size_t size);
  249. void *sds_realloc(void *ptr, size_t size);
  250. void sds_free(void *ptr);
  251. #ifdef REDIS_TEST
  252. int sdsTest(int argc, char *argv[]);
  253. #endif
  254. #endif