2
0

sds.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 HIREDIS_SDS_H
  33. #define HIREDIS_SDS_H
  34. #define HI_SDS_MAX_PREALLOC (1024*1024)
  35. #ifdef _MSC_VER
  36. #define __attribute__(x)
  37. typedef long long ssize_t;
  38. #define SSIZE_MAX (LLONG_MAX >> 1)
  39. #endif
  40. #include <sys/types.h>
  41. #include <stdarg.h>
  42. #include <stdint.h>
  43. typedef char *hisds;
  44. /* Note: sdshdr5 is never used, we just access the flags byte directly.
  45. * However is here to document the layout of type 5 SDS strings. */
  46. struct __attribute__ ((__packed__)) hisdshdr5 {
  47. unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
  48. char buf[];
  49. };
  50. struct __attribute__ ((__packed__)) hisdshdr8 {
  51. uint8_t len; /* used */
  52. uint8_t alloc; /* excluding the header and null terminator */
  53. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  54. char buf[];
  55. };
  56. struct __attribute__ ((__packed__)) hisdshdr16 {
  57. uint16_t len; /* used */
  58. uint16_t alloc; /* excluding the header and null terminator */
  59. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  60. char buf[];
  61. };
  62. struct __attribute__ ((__packed__)) hisdshdr32 {
  63. uint32_t len; /* used */
  64. uint32_t alloc; /* excluding the header and null terminator */
  65. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  66. char buf[];
  67. };
  68. struct __attribute__ ((__packed__)) hisdshdr64 {
  69. uint64_t len; /* used */
  70. uint64_t alloc; /* excluding the header and null terminator */
  71. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  72. char buf[];
  73. };
  74. #define HI_SDS_TYPE_5 0
  75. #define HI_SDS_TYPE_8 1
  76. #define HI_SDS_TYPE_16 2
  77. #define HI_SDS_TYPE_32 3
  78. #define HI_SDS_TYPE_64 4
  79. #define HI_SDS_TYPE_MASK 7
  80. #define HI_SDS_TYPE_BITS 3
  81. #define HI_SDS_HDR_VAR(T,s) struct hisdshdr##T *sh = (struct hisdshdr##T *)((s)-(sizeof(struct hisdshdr##T)));
  82. #define HI_SDS_HDR(T,s) ((struct hisdshdr##T *)((s)-(sizeof(struct hisdshdr##T))))
  83. #define HI_SDS_TYPE_5_LEN(f) ((f)>>HI_SDS_TYPE_BITS)
  84. static inline size_t hi_sdslen(const hisds s) {
  85. unsigned char flags = s[-1];
  86. switch(flags & HI_SDS_TYPE_MASK) {
  87. case HI_SDS_TYPE_5:
  88. return HI_SDS_TYPE_5_LEN(flags);
  89. case HI_SDS_TYPE_8:
  90. return HI_SDS_HDR(8,s)->len;
  91. case HI_SDS_TYPE_16:
  92. return HI_SDS_HDR(16,s)->len;
  93. case HI_SDS_TYPE_32:
  94. return HI_SDS_HDR(32,s)->len;
  95. case HI_SDS_TYPE_64:
  96. return HI_SDS_HDR(64,s)->len;
  97. }
  98. return 0;
  99. }
  100. static inline size_t hi_sdsavail(const hisds s) {
  101. unsigned char flags = s[-1];
  102. switch(flags&HI_SDS_TYPE_MASK) {
  103. case HI_SDS_TYPE_5: {
  104. return 0;
  105. }
  106. case HI_SDS_TYPE_8: {
  107. HI_SDS_HDR_VAR(8,s);
  108. return sh->alloc - sh->len;
  109. }
  110. case HI_SDS_TYPE_16: {
  111. HI_SDS_HDR_VAR(16,s);
  112. return sh->alloc - sh->len;
  113. }
  114. case HI_SDS_TYPE_32: {
  115. HI_SDS_HDR_VAR(32,s);
  116. return sh->alloc - sh->len;
  117. }
  118. case HI_SDS_TYPE_64: {
  119. HI_SDS_HDR_VAR(64,s);
  120. return sh->alloc - sh->len;
  121. }
  122. }
  123. return 0;
  124. }
  125. static inline void hi_sdssetlen(hisds s, size_t newlen) {
  126. unsigned char flags = s[-1];
  127. switch(flags&HI_SDS_TYPE_MASK) {
  128. case HI_SDS_TYPE_5:
  129. {
  130. unsigned char *fp = ((unsigned char*)s)-1;
  131. *fp = (unsigned char)(HI_SDS_TYPE_5 | (newlen << HI_SDS_TYPE_BITS));
  132. }
  133. break;
  134. case HI_SDS_TYPE_8:
  135. HI_SDS_HDR(8,s)->len = (uint8_t)newlen;
  136. break;
  137. case HI_SDS_TYPE_16:
  138. HI_SDS_HDR(16,s)->len = (uint16_t)newlen;
  139. break;
  140. case HI_SDS_TYPE_32:
  141. HI_SDS_HDR(32,s)->len = (uint32_t)newlen;
  142. break;
  143. case HI_SDS_TYPE_64:
  144. HI_SDS_HDR(64,s)->len = (uint64_t)newlen;
  145. break;
  146. }
  147. }
  148. static inline void hi_sdsinclen(hisds s, size_t inc) {
  149. unsigned char flags = s[-1];
  150. switch(flags&HI_SDS_TYPE_MASK) {
  151. case HI_SDS_TYPE_5:
  152. {
  153. unsigned char *fp = ((unsigned char*)s)-1;
  154. unsigned char newlen = HI_SDS_TYPE_5_LEN(flags)+(unsigned char)inc;
  155. *fp = HI_SDS_TYPE_5 | (newlen << HI_SDS_TYPE_BITS);
  156. }
  157. break;
  158. case HI_SDS_TYPE_8:
  159. HI_SDS_HDR(8,s)->len += (uint8_t)inc;
  160. break;
  161. case HI_SDS_TYPE_16:
  162. HI_SDS_HDR(16,s)->len += (uint16_t)inc;
  163. break;
  164. case HI_SDS_TYPE_32:
  165. HI_SDS_HDR(32,s)->len += (uint32_t)inc;
  166. break;
  167. case HI_SDS_TYPE_64:
  168. HI_SDS_HDR(64,s)->len += (uint64_t)inc;
  169. break;
  170. }
  171. }
  172. /* hi_sdsalloc() = hi_sdsavail() + hi_sdslen() */
  173. static inline size_t hi_sdsalloc(const hisds s) {
  174. unsigned char flags = s[-1];
  175. switch(flags & HI_SDS_TYPE_MASK) {
  176. case HI_SDS_TYPE_5:
  177. return HI_SDS_TYPE_5_LEN(flags);
  178. case HI_SDS_TYPE_8:
  179. return HI_SDS_HDR(8,s)->alloc;
  180. case HI_SDS_TYPE_16:
  181. return HI_SDS_HDR(16,s)->alloc;
  182. case HI_SDS_TYPE_32:
  183. return HI_SDS_HDR(32,s)->alloc;
  184. case HI_SDS_TYPE_64:
  185. return HI_SDS_HDR(64,s)->alloc;
  186. }
  187. return 0;
  188. }
  189. static inline void hi_sdssetalloc(hisds s, size_t newlen) {
  190. unsigned char flags = s[-1];
  191. switch(flags&HI_SDS_TYPE_MASK) {
  192. case HI_SDS_TYPE_5:
  193. /* Nothing to do, this type has no total allocation info. */
  194. break;
  195. case HI_SDS_TYPE_8:
  196. HI_SDS_HDR(8,s)->alloc = (uint8_t)newlen;
  197. break;
  198. case HI_SDS_TYPE_16:
  199. HI_SDS_HDR(16,s)->alloc = (uint16_t)newlen;
  200. break;
  201. case HI_SDS_TYPE_32:
  202. HI_SDS_HDR(32,s)->alloc = (uint32_t)newlen;
  203. break;
  204. case HI_SDS_TYPE_64:
  205. HI_SDS_HDR(64,s)->alloc = (uint64_t)newlen;
  206. break;
  207. }
  208. }
  209. hisds hi_sdsnewlen(const void *init, size_t initlen);
  210. hisds hi_sdsnew(const char *init);
  211. hisds hi_sdsempty(void);
  212. hisds hi_sdsdup(const hisds s);
  213. void hi_sdsfree(hisds s);
  214. hisds hi_sdsgrowzero(hisds s, size_t len);
  215. hisds hi_sdscatlen(hisds s, const void *t, size_t len);
  216. hisds hi_sdscat(hisds s, const char *t);
  217. hisds hi_sdscatsds(hisds s, const hisds t);
  218. hisds hi_sdscpylen(hisds s, const char *t, size_t len);
  219. hisds hi_sdscpy(hisds s, const char *t);
  220. hisds hi_sdscatvprintf(hisds s, const char *fmt, va_list ap);
  221. #ifdef __GNUC__
  222. hisds hi_sdscatprintf(hisds s, const char *fmt, ...)
  223. __attribute__((format(printf, 2, 3)));
  224. #else
  225. hisds hi_sdscatprintf(hisds s, const char *fmt, ...);
  226. #endif
  227. hisds hi_sdscatfmt(hisds s, char const *fmt, ...);
  228. hisds hi_sdstrim(hisds s, const char *cset);
  229. int hi_sdsrange(hisds s, ssize_t start, ssize_t end);
  230. void hi_sdsupdatelen(hisds s);
  231. void hi_sdsclear(hisds s);
  232. int hi_sdscmp(const hisds s1, const hisds s2);
  233. hisds *hi_sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
  234. void hi_sdsfreesplitres(hisds *tokens, int count);
  235. void hi_sdstolower(hisds s);
  236. void hi_sdstoupper(hisds s);
  237. hisds hi_sdsfromlonglong(long long value);
  238. hisds hi_sdscatrepr(hisds s, const char *p, size_t len);
  239. hisds *hi_sdssplitargs(const char *line, int *argc);
  240. hisds hi_sdsmapchars(hisds s, const char *from, const char *to, size_t setlen);
  241. hisds hi_sdsjoin(char **argv, int argc, char *sep);
  242. hisds hi_sdsjoinsds(hisds *argv, int argc, const char *sep, size_t seplen);
  243. /* Low level functions exposed to the user API */
  244. hisds hi_sdsMakeRoomFor(hisds s, size_t addlen);
  245. void hi_sdsIncrLen(hisds s, int incr);
  246. hisds hi_sdsRemoveFreeSpace(hisds s);
  247. size_t hi_sdsAllocSize(hisds s);
  248. void *hi_sdsAllocPtr(hisds s);
  249. /* Export the allocator used by SDS to the program using SDS.
  250. * Sometimes the program SDS is linked to, may use a different set of
  251. * allocators, but may want to allocate or free things that SDS will
  252. * respectively free or allocate. */
  253. void *hi_sds_malloc(size_t size);
  254. void *hi_sds_realloc(void *ptr, size_t size);
  255. void hi_sds_free(void *ptr);
  256. #ifdef REDIS_TEST
  257. int hi_sdsTest(int argc, char *argv[]);
  258. #endif
  259. #endif /* HIREDIS_SDS_H */