无法编译hiredis sds文件

eqoofvh9  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(367)

将指针延迟到不完整的类型structerror无效sizof应用到不完整的类型struct我正在尝试编译此sds文件以在qt的帮助下运行redis。我使用了中描述的pack宏https://stackoverflow.com/a/3312896 并对分号的定义和分号问题进行了讨论。我正在使用qt creater运行rdm,并在mingw的帮助下构建它(\rdm\build-rdm-desktop\uqt\u5\u9\umingw\u32bit-release)。当我构建它的时候,我得到了一个错误“无效的sizeof应用到一个结构不完整的类型”。我还将附加相同的快照。需要帮助来解决这个问题。

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

q43xntqr1#

它的意思是 sizeof(struct sdshdr##T) 不起作用,因为编译时 struct sdshdr##T 尚未定义,因此其大小尚未确定。
通常需要在这之前定义结构,例如通过包含定义它的头文件。

相关问题