Simulation Core
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

281 linhas
9.0 KiB

  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. typedef long long ssize_t;
  37. #define SSIZE_MAX (LLONG_MAX >> 1)
  38. #ifndef __clang__
  39. #define __attribute__(x)
  40. #endif
  41. #endif
  42. #include <sys/types.h>
  43. #include <stdarg.h>
  44. #include <stdint.h>
  45. typedef char *sds;
  46. /* Note: sdshdr5 is never used, we just access the flags byte directly.
  47. * However is here to document the layout of type 5 SDS strings. */
  48. struct __attribute__ ((__packed__)) sdshdr5 {
  49. unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
  50. char buf[];
  51. };
  52. struct __attribute__ ((__packed__)) sdshdr8 {
  53. uint8_t len; /* used */
  54. uint8_t alloc; /* excluding the header and null terminator */
  55. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  56. char buf[];
  57. };
  58. struct __attribute__ ((__packed__)) sdshdr16 {
  59. uint16_t len; /* used */
  60. uint16_t alloc; /* excluding the header and null terminator */
  61. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  62. char buf[];
  63. };
  64. struct __attribute__ ((__packed__)) sdshdr32 {
  65. uint32_t len; /* used */
  66. uint32_t alloc; /* excluding the header and null terminator */
  67. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  68. char buf[];
  69. };
  70. struct __attribute__ ((__packed__)) sdshdr64 {
  71. uint64_t len; /* used */
  72. uint64_t alloc; /* excluding the header and null terminator */
  73. unsigned char flags; /* 3 lsb of type, 5 unused bits */
  74. char buf[];
  75. };
  76. #define SDS_TYPE_5 0
  77. #define SDS_TYPE_8 1
  78. #define SDS_TYPE_16 2
  79. #define SDS_TYPE_32 3
  80. #define SDS_TYPE_64 4
  81. #define SDS_TYPE_MASK 7
  82. #define SDS_TYPE_BITS 3
  83. #define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T)));
  84. #define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))))
  85. #define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS)
  86. static inline size_t sdslen(const sds s) {
  87. unsigned char flags = s[-1];
  88. switch(flags&SDS_TYPE_MASK) {
  89. case SDS_TYPE_5:
  90. return SDS_TYPE_5_LEN(flags);
  91. case SDS_TYPE_8:
  92. return SDS_HDR(8,s)->len;
  93. case SDS_TYPE_16:
  94. return SDS_HDR(16,s)->len;
  95. case SDS_TYPE_32:
  96. return SDS_HDR(32,s)->len;
  97. case SDS_TYPE_64:
  98. return SDS_HDR(64,s)->len;
  99. }
  100. return 0;
  101. }
  102. static inline size_t sdsavail(const sds s) {
  103. unsigned char flags = s[-1];
  104. switch(flags&SDS_TYPE_MASK) {
  105. case SDS_TYPE_5: {
  106. return 0;
  107. }
  108. case SDS_TYPE_8: {
  109. SDS_HDR_VAR(8,s);
  110. return sh->alloc - sh->len;
  111. }
  112. case SDS_TYPE_16: {
  113. SDS_HDR_VAR(16,s);
  114. return sh->alloc - sh->len;
  115. }
  116. case SDS_TYPE_32: {
  117. SDS_HDR_VAR(32,s);
  118. return sh->alloc - sh->len;
  119. }
  120. case SDS_TYPE_64: {
  121. SDS_HDR_VAR(64,s);
  122. return sh->alloc - sh->len;
  123. }
  124. }
  125. return 0;
  126. }
  127. static inline void sdssetlen(sds s, size_t newlen) {
  128. unsigned char flags = s[-1];
  129. switch(flags&SDS_TYPE_MASK) {
  130. case SDS_TYPE_5:
  131. {
  132. unsigned char *fp = ((unsigned char*)s)-1;
  133. *fp = (unsigned char)(SDS_TYPE_5 | (newlen << SDS_TYPE_BITS));
  134. }
  135. break;
  136. case SDS_TYPE_8:
  137. SDS_HDR(8,s)->len = (uint8_t)newlen;
  138. break;
  139. case SDS_TYPE_16:
  140. SDS_HDR(16,s)->len = (uint16_t)newlen;
  141. break;
  142. case SDS_TYPE_32:
  143. SDS_HDR(32,s)->len = (uint32_t)newlen;
  144. break;
  145. case SDS_TYPE_64:
  146. SDS_HDR(64,s)->len = (uint64_t)newlen;
  147. break;
  148. }
  149. }
  150. static inline void sdsinclen(sds s, size_t inc) {
  151. unsigned char flags = s[-1];
  152. switch(flags&SDS_TYPE_MASK) {
  153. case SDS_TYPE_5:
  154. {
  155. unsigned char *fp = ((unsigned char*)s)-1;
  156. unsigned char newlen = SDS_TYPE_5_LEN(flags)+(unsigned char)inc;
  157. *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
  158. }
  159. break;
  160. case SDS_TYPE_8:
  161. SDS_HDR(8,s)->len += (uint8_t)inc;
  162. break;
  163. case SDS_TYPE_16:
  164. SDS_HDR(16,s)->len += (uint16_t)inc;
  165. break;
  166. case SDS_TYPE_32:
  167. SDS_HDR(32,s)->len += (uint32_t)inc;
  168. break;
  169. case SDS_TYPE_64:
  170. SDS_HDR(64,s)->len += (uint64_t)inc;
  171. break;
  172. }
  173. }
  174. /* sdsalloc() = sdsavail() + sdslen() */
  175. static inline size_t sdsalloc(const sds s) {
  176. unsigned char flags = s[-1];
  177. switch(flags&SDS_TYPE_MASK) {
  178. case SDS_TYPE_5:
  179. return SDS_TYPE_5_LEN(flags);
  180. case SDS_TYPE_8:
  181. return SDS_HDR(8,s)->alloc;
  182. case SDS_TYPE_16:
  183. return SDS_HDR(16,s)->alloc;
  184. case SDS_TYPE_32:
  185. return SDS_HDR(32,s)->alloc;
  186. case SDS_TYPE_64:
  187. return SDS_HDR(64,s)->alloc;
  188. }
  189. return 0;
  190. }
  191. static inline void sdssetalloc(sds s, size_t newlen) {
  192. unsigned char flags = s[-1];
  193. switch(flags&SDS_TYPE_MASK) {
  194. case SDS_TYPE_5:
  195. /* Nothing to do, this type has no total allocation info. */
  196. break;
  197. case SDS_TYPE_8:
  198. SDS_HDR(8,s)->alloc = (uint8_t)newlen;
  199. break;
  200. case SDS_TYPE_16:
  201. SDS_HDR(16,s)->alloc = (uint16_t)newlen;
  202. break;
  203. case SDS_TYPE_32:
  204. SDS_HDR(32,s)->alloc = (uint32_t)newlen;
  205. break;
  206. case SDS_TYPE_64:
  207. SDS_HDR(64,s)->alloc = (uint64_t)newlen;
  208. break;
  209. }
  210. }
  211. sds sdsnewlen(const void *init, size_t initlen);
  212. sds sdsnew(const char *init);
  213. sds sdsempty(void);
  214. sds sdsdup(const sds s);
  215. void sdsfree(sds s);
  216. sds sdsgrowzero(sds s, size_t len);
  217. sds sdscatlen(sds s, const void *t, size_t len);
  218. sds sdscat(sds s, const char *t);
  219. sds sdscatsds(sds s, const sds t);
  220. sds sdscpylen(sds s, const char *t, size_t len);
  221. sds sdscpy(sds s, const char *t);
  222. sds sdscatvprintf(sds s, const char *fmt, va_list ap);
  223. #ifdef __GNUC__
  224. sds sdscatprintf(sds s, const char *fmt, ...)
  225. __attribute__((format(printf, 2, 3)));
  226. #else
  227. sds sdscatprintf(sds s, const char *fmt, ...);
  228. #endif
  229. sds sdscatfmt(sds s, char const *fmt, ...);
  230. sds sdstrim(sds s, const char *cset);
  231. int sdsrange(sds s, ssize_t start, ssize_t end);
  232. void sdsupdatelen(sds s);
  233. void sdsclear(sds s);
  234. int sdscmp(const sds s1, const sds s2);
  235. sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
  236. void sdsfreesplitres(sds *tokens, int count);
  237. void sdstolower(sds s);
  238. void sdstoupper(sds s);
  239. sds sdsfromlonglong(long long value);
  240. sds sdscatrepr(sds s, const char *p, size_t len);
  241. sds *sdssplitargs(const char *line, int *argc);
  242. sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
  243. sds sdsjoin(char **argv, int argc, char *sep);
  244. sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);
  245. /* Low level functions exposed to the user API */
  246. sds sdsMakeRoomFor(sds s, size_t addlen);
  247. void sdsIncrLen(sds s, int incr);
  248. sds sdsRemoveFreeSpace(sds s);
  249. size_t sdsAllocSize(sds s);
  250. void *sdsAllocPtr(sds s);
  251. /* Export the allocator used by SDS to the program using SDS.
  252. * Sometimes the program SDS is linked to, may use a different set of
  253. * allocators, but may want to allocate or free things that SDS will
  254. * respectively free or allocate. */
  255. void *sds_malloc(size_t size);
  256. void *sds_realloc(void *ptr, size_t size);
  257. void sds_free(void *ptr);
  258. #ifdef REDIS_TEST
  259. int sdsTest(int argc, char *argv[]);
  260. #endif
  261. #endif