home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / src / proto / endecode-funcs.h < prev    next >
C/C++ Source or Header  |  2010-04-30  |  30KB  |  838 lines

  1. /*
  2.  * (C) 2003-6 Pete Wyckoff, Ohio Supercomputer Center <pw@osc.edu>
  3.  *
  4.  * See COPYING in top-level directory.
  5.  *
  6.  * Defines for macros related to wire encoding and decoding.  Only included
  7.  * by include/pvfs2-encode-stubs.h by core encoding users.
  8.  */
  9. /* NOTE: if you make any changes to the code contained in this file, please
  10.  * update the PVFS2_PROTO_VERSION in pvfs2-req-proto.h accordingly
  11.  */
  12. #ifndef __SRC_PROTO_ENDECODE_FUNCS_H
  13. #define __SRC_PROTO_ENDECODE_FUNCS_H
  14.  
  15. #include "src/io/bmi/bmi-byteswap.h"
  16. #include <stdint.h>
  17. #include <assert.h>
  18.  
  19. /*
  20.  * NOTE - Every macro defined here needs to have a stub defined in
  21.  * include/pvfs2-encode-stubs.h
  22.  */
  23.  
  24. /*
  25.  * Generic macros to define encoding near target structure declarations.
  26.  */
  27.  
  28. /* basic types */
  29. #define encode_uint64_t(pptr,x) do { \
  30.     *(u_int64_t*) *(pptr) = htobmi64(*(x)); \
  31.     *(pptr) += 8; \
  32. } while (0)
  33. #define decode_uint64_t(pptr,x) do { \
  34.     *(x) = bmitoh64(*(u_int64_t*) *(pptr)); \
  35.     *(pptr) += 8; \
  36. } while (0)
  37.  
  38. #define encode_int64_t(pptr,x) do { \
  39.     *(int64_t*) *(pptr) = htobmi64(*(x)); \
  40.     *(pptr) += 8; \
  41. } while (0)
  42. #define decode_int64_t(pptr,x) do { \
  43.     *(x) = bmitoh64(*(int64_t*) *(pptr)); \
  44.     *(pptr) += 8; \
  45. } while (0)
  46.  
  47. #define encode_uint32_t(pptr,x) do { \
  48.     *(u_int32_t*) *(pptr) = htobmi32(*(x)); \
  49.     *(pptr) += 4; \
  50. } while (0)
  51. #define decode_uint32_t(pptr,x) do { \
  52.     *(x) = bmitoh32(*(u_int32_t*) *(pptr)); \
  53.     *(pptr) += 4; \
  54. } while (0)
  55.  
  56. #define encode_int32_t(pptr,x) do { \
  57.     *(int32_t*) *(pptr) = htobmi32(*(x)); \
  58.     *(pptr) += 4; \
  59. } while (0)
  60. #define decode_int32_t(pptr,x) do { \
  61.     *(x) = bmitoh32(*(int32_t*) *(pptr)); \
  62.     *(pptr) += 4; \
  63. } while (0)
  64.  
  65. /* skip 4 bytes, maybe zeroing them to avoid valgrind getting annoyed */
  66. #ifdef HAVE_VALGRIND_H
  67. #define encode_skip4(pptr,x) do { \
  68.     *(int32_t*) *(pptr) = 0; \
  69.     *(pptr) += 4; \
  70. } while (0)
  71. #else
  72. #define encode_skip4(pptr,x) do { \
  73.     *(pptr) += 4; \
  74. } while (0)
  75. #endif
  76.  
  77. #define decode_skip4(pptr,x) do { \
  78.     *(pptr) += 4; \
  79. } while (0)
  80.  
  81. /*
  82.  * Strings. Decoding just points into existing character data.  This handles
  83.  * NULL strings too, just encoding the length and a single zero byte.  The
  84.  * valgrind version zeroes out any padding.
  85.  */
  86. #ifdef HAVE_VALGRIND_H
  87. #define encode_string(pptr,pbuf) do { \
  88.     u_int32_t len = 0; \
  89.     if (*pbuf) \
  90.     len = strlen(*pbuf); \
  91.     *(u_int32_t *) *(pptr) = htobmi32(len); \
  92.     if (len) { \
  93.     memcpy(*(pptr)+4, *pbuf, len+1); \
  94.     int pad = roundup8(4 + len + 1) - (4 + len + 1); \
  95.     *(pptr) += roundup8(4 + len + 1); \
  96.     memset(*(pptr)-pad, 0, pad); \
  97.     } else { \
  98.     *(u_int32_t *) (*(pptr)+4) = 0; \
  99.     *(pptr) += 8; \
  100.     } \
  101. } while (0)
  102. #else
  103. #define encode_string(pptr,pbuf) do { \
  104.     u_int32_t len = 0; \
  105.     if (*pbuf) \
  106.     len = strlen(*pbuf); \
  107.     *(u_int32_t *) *(pptr) = htobmi32(len); \
  108.     if (len) { \
  109.     memcpy(*(pptr)+4, *pbuf, len+1); \
  110.     *(pptr) += roundup8(4 + len + 1); \
  111.     } else { \
  112.     *(u_int32_t *) *(pptr) = 0; \
  113.     *(pptr) += 8; \
  114.     } \
  115. } while (0)
  116. #endif
  117.  
  118. /* determines how much protocol space a string encoding will consume */
  119. #define encode_string_size_check(pbuf) (strlen(*pbuf) + 5)
  120.  
  121. #define decode_string(pptr,pbuf) do { \
  122.     u_int32_t len = bmitoh32(*(u_int32_t *) *(pptr)); \
  123.     *pbuf = *(pptr) + 4; \
  124.     *(pptr) += roundup8(4 + len + 1); \
  125. } while (0)
  126.  
  127. /* odd variation, space exists in some structure, must copy-in string */
  128. #define encode_here_string(pptr,pbuf) encode_string(pptr,pbuf)
  129. #define decode_here_string(pptr,pbuf) do { \
  130.     u_int32_t len = bmitoh32(*(u_int32_t *) *(pptr)); \
  131.     memcpy(pbuf, *(pptr) + 4, len + 1); \
  132.     *(pptr) += roundup8(4 + len + 1); \
  133. } while (0)
  134.  
  135.  
  136. /* keyvals; a lot like strings; decoding points existing character data */
  137. /* BTW we are skipping the read_sz field - keep that in mind */
  138. #define encode_PVFS_ds_keyval(pptr,pbuf) do { \
  139.     u_int32_t len = ((PVFS_ds_keyval *)pbuf)->buffer_sz; \
  140.     *(u_int32_t *) *(pptr) = htobmi32(len); \
  141.     memcpy(*(pptr)+4, ((PVFS_ds_keyval *)pbuf)->buffer, len); \
  142.     *(pptr) += roundup8(4 + len); \
  143. } while (0)
  144. #define decode_PVFS_ds_keyval(pptr,pbuf) do { \
  145.     u_int32_t len = bmitoh32(*(u_int32_t *) *(pptr)); \
  146.     ((PVFS_ds_keyval *)pbuf)->buffer_sz = len; \
  147.     ((PVFS_ds_keyval *)pbuf)->buffer = *(pptr) + 4; \
  148.     *(pptr) += roundup8(4 + len); \
  149. } while (0)
  150.  
  151. /*
  152.  * Type maps are put near the type definitions, except for this special one.
  153.  *
  154.  * Please remember when changing a fundamental type, e.g. PVFS_size, to update
  155.  * also the set of #defines that tell the encoder what its type really is.
  156.  */
  157. #define encode_enum encode_int32_t
  158. #define decode_enum decode_int32_t
  159.  
  160. /* memory alloc and free, just for decoding */
  161. #if 0
  162. /* this is for debugging, if you want to see what is malloc'd */
  163. static inline void *decode_malloc (int n) {
  164.     void *p;
  165.     if (n>0)
  166.         p = malloc(n);
  167.     else
  168.         p = (void *)0;
  169.     printf("decode malloc %d bytes: %p\n",n,p);
  170.     return p;
  171. }
  172. /* this is for debugging, if you want to see what is free'd */
  173. static inline void decode_free (void *p) {
  174.     printf("decode free: %p\n",p);
  175.     free(p);
  176. }
  177. #else
  178. #define decode_malloc(n) ((n) ? malloc(n) : 0)
  179. #define decode_free(n) free(n)
  180. #endif
  181.  
  182. /*
  183.  * These wrappers define functions to do the encoding of the types or
  184.  * structures they describe.  Please remember to update the empty stub versions
  185.  * of these routines in include/pvfs2-encode-stubs.h, although the compiler
  186.  * will certainly complain too.
  187.  *
  188.  * Note that decode can not take a const since we point into the
  189.  * undecoded buffer for strings.
  190.  */
  191. #define endecode_fields_1_generic(name, sname, t1, x1) \
  192. static inline void encode_##name(char **pptr, const sname *x) { \
  193.     encode_##t1(pptr, &x->x1); \
  194. } \
  195. static inline void decode_##name(char **pptr, sname *x) { \
  196.     decode_##t1(pptr, &x->x1); \
  197. }
  198.  
  199. #define endecode_fields_1(name, t1, x1) \
  200.     endecode_fields_1_generic(name, name, t1, x1)
  201. #define endecode_fields_1_struct(name, t1, x1) \
  202.     endecode_fields_1_generic(name, struct name, t1, x1)
  203.  
  204. #define endecode_fields_2_generic(name, sname, t1, x1, t2, x2) \
  205. static inline void encode_##name(char **pptr, const sname *x) { \
  206.     encode_##t1(pptr, &x->x1); \
  207.     encode_##t2(pptr, &x->x2); \
  208. } \
  209. static inline void decode_##name(char **pptr, sname *x) { \
  210.     decode_##t1(pptr, &x->x1); \
  211.     decode_##t2(pptr, &x->x2); \
  212. }
  213.  
  214. #define endecode_fields_2(name, t1, x1, t2, x2) \
  215.     endecode_fields_2_generic(name, name, t1, x1, t2, x2)
  216. #define endecode_fields_2_struct(name, t1, x1, t2, x2) \
  217.     endecode_fields_2_generic(name, struct name, t1, x1, t2, x2)
  218.  
  219. #define endecode_fields_3_generic(name, sname, t1, x1, t2, x2, t3, x3) \
  220. static inline void encode_##name(char **pptr, const sname *x) { \
  221.     encode_##t1(pptr, &x->x1); \
  222.     encode_##t2(pptr, &x->x2); \
  223.     encode_##t3(pptr, &x->x3); \
  224. } \
  225. static inline void decode_##name(char **pptr, sname *x) { \
  226.     decode_##t1(pptr, &x->x1); \
  227.     decode_##t2(pptr, &x->x2); \
  228.     decode_##t3(pptr, &x->x3); \
  229. }
  230.  
  231. #define endecode_fields_3(name, t1, x1, t2, x2, t3, x3) \
  232.     endecode_fields_3_generic(name, name, t1, x1, t2, x2, t3, x3)
  233. #define endecode_fields_3_struct(name, t1, x1, t2, x2, t3, x3) \
  234.     endecode_fields_3_generic(name, struct name, t1, x1, t2, x2, t3, x3)
  235.  
  236. #define endecode_fields_4_generic(name, sname, t1, x1, t2, x2, t3, x3, t4, x4) \
  237. static inline void encode_##name(char **pptr, const sname *x) { \
  238.     encode_##t1(pptr, &x->x1); \
  239.     encode_##t2(pptr, &x->x2); \
  240.     encode_##t3(pptr, &x->x3); \
  241.     encode_##t4(pptr, &x->x4); \
  242. } \
  243. static inline void decode_##name(char **pptr, sname *x) { \
  244.     decode_##t1(pptr, &x->x1); \
  245.     decode_##t2(pptr, &x->x2); \
  246.     decode_##t3(pptr, &x->x3); \
  247.     decode_##t4(pptr, &x->x4); \
  248. }
  249.  
  250. #define endecode_fields_4(name, t1, x1, t2, x2, t3, x3, t4, x4) \
  251.     endecode_fields_4_generic(name, name, t1, x1, t2, x2, t3, x3, t4, x4)
  252. #define endecode_fields_4_struct(name, t1, x1, t2, x2, t3, x3, t4, x4) \
  253.     endecode_fields_4_generic(name, struct name, t1, x1, t2, x2, t3, x3, t4, x4)
  254.  
  255. #define endecode_fields_5_generic(name, sname, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5) \
  256. static inline void encode_##name(char **pptr, const sname *x) { \
  257.     encode_##t1(pptr, &x->x1); \
  258.     encode_##t2(pptr, &x->x2); \
  259.     encode_##t3(pptr, &x->x3); \
  260.     encode_##t4(pptr, &x->x4); \
  261.     encode_##t5(pptr, &x->x5); \
  262. } \
  263. static inline void decode_##name(char **pptr, sname *x) { \
  264.     decode_##t1(pptr, &x->x1); \
  265.     decode_##t2(pptr, &x->x2); \
  266.     decode_##t3(pptr, &x->x3); \
  267.     decode_##t4(pptr, &x->x4); \
  268.     decode_##t5(pptr, &x->x5); \
  269. }
  270.  
  271. #define endecode_fields_5(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5) \
  272.     endecode_fields_5_generic(name, name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5)
  273. #define endecode_fields_5_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5) \
  274.     endecode_fields_5_generic(name, struct name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5)
  275.  
  276. #define endecode_fields_6_generic(name, sname, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6) \
  277. static inline void encode_##name(char **pptr, const sname *x) { \
  278.     encode_##t1(pptr, &x->x1); \
  279.     encode_##t2(pptr, &x->x2); \
  280.     encode_##t3(pptr, &x->x3); \
  281.     encode_##t4(pptr, &x->x4); \
  282.     encode_##t5(pptr, &x->x5); \
  283.     encode_##t6(pptr, &x->x6); \
  284. } \
  285. static inline void decode_##name(char **pptr, sname *x) { \
  286.     decode_##t1(pptr, &x->x1); \
  287.     decode_##t2(pptr, &x->x2); \
  288.     decode_##t3(pptr, &x->x3); \
  289.     decode_##t4(pptr, &x->x4); \
  290.     decode_##t5(pptr, &x->x5); \
  291.     decode_##t6(pptr, &x->x6); \
  292. }
  293.  
  294. #define endecode_fields_6(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6) \
  295.     endecode_fields_6_generic(name, name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6)
  296. #define endecode_fields_6_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6) \
  297.     endecode_fields_6_generic(name, struct name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, t6, x6)
  298.  
  299. #define endecode_fields_7(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7) \
  300. static inline void encode_##name(char **pptr, const name *x) { \
  301.     encode_##t1(pptr, &x->x1); \
  302.     encode_##t2(pptr, &x->x2); \
  303.     encode_##t3(pptr, &x->x3); \
  304.     encode_##t4(pptr, &x->x4); \
  305.     encode_##t5(pptr, &x->x5); \
  306.     encode_##t6(pptr, &x->x6); \
  307.     encode_##t7(pptr, &x->x7); \
  308. } \
  309. static inline void decode_##name(char **pptr, name *x) { \
  310.     decode_##t1(pptr, &x->x1); \
  311.     decode_##t2(pptr, &x->x2); \
  312.     decode_##t3(pptr, &x->x3); \
  313.     decode_##t4(pptr, &x->x4); \
  314.     decode_##t5(pptr, &x->x5); \
  315.     decode_##t6(pptr, &x->x6); \
  316.     decode_##t7(pptr, &x->x7); \
  317. }
  318.  
  319. #define endecode_fields_7_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7) \
  320. static inline void encode_##name(char **pptr, const struct name *x) { \
  321.     encode_##t1(pptr, &x->x1); \
  322.     encode_##t2(pptr, &x->x2); \
  323.     encode_##t3(pptr, &x->x3); \
  324.     encode_##t4(pptr, &x->x4); \
  325.     encode_##t5(pptr, &x->x5); \
  326.     encode_##t6(pptr, &x->x6); \
  327.     encode_##t7(pptr, &x->x7); \
  328. } \
  329. static inline void decode_##name(char **pptr, struct name *x) { \
  330.     decode_##t1(pptr, &x->x1); \
  331.     decode_##t2(pptr, &x->x2); \
  332.     decode_##t3(pptr, &x->x3); \
  333.     decode_##t4(pptr, &x->x4); \
  334.     decode_##t5(pptr, &x->x5); \
  335.     decode_##t6(pptr, &x->x6); \
  336.     decode_##t7(pptr, &x->x7); \
  337. }
  338.  
  339. #define endecode_fields_8_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8) \
  340. static inline void encode_##name(char **pptr, const struct name *x) { \
  341.     encode_##t1(pptr, &x->x1); \
  342.     encode_##t2(pptr, &x->x2); \
  343.     encode_##t3(pptr, &x->x3); \
  344.     encode_##t4(pptr, &x->x4); \
  345.     encode_##t5(pptr, &x->x5); \
  346.     encode_##t6(pptr, &x->x6); \
  347.     encode_##t7(pptr, &x->x7); \
  348.     encode_##t8(pptr, &x->x8); \
  349. } \
  350. static inline void decode_##name(char **pptr, struct name *x) { \
  351.     decode_##t1(pptr, &x->x1); \
  352.     decode_##t2(pptr, &x->x2); \
  353.     decode_##t3(pptr, &x->x3); \
  354.     decode_##t4(pptr, &x->x4); \
  355.     decode_##t5(pptr, &x->x5); \
  356.     decode_##t6(pptr, &x->x6); \
  357.     decode_##t7(pptr, &x->x7); \
  358.     decode_##t8(pptr, &x->x8); \
  359. }
  360.  
  361. #define endecode_fields_9_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8,t9,x9) \
  362. static inline void encode_##name(char **pptr, const struct name *x) { \
  363.     encode_##t1(pptr, &x->x1); \
  364.     encode_##t2(pptr, &x->x2); \
  365.     encode_##t3(pptr, &x->x3); \
  366.     encode_##t4(pptr, &x->x4); \
  367.     encode_##t5(pptr, &x->x5); \
  368.     encode_##t6(pptr, &x->x6); \
  369.     encode_##t7(pptr, &x->x7); \
  370.     encode_##t8(pptr, &x->x8); \
  371.     encode_##t9(pptr, &x->x9); \
  372. } \
  373. static inline void decode_##name(char **pptr, struct name *x) { \
  374.     decode_##t1(pptr, &x->x1); \
  375.     decode_##t2(pptr, &x->x2); \
  376.     decode_##t3(pptr, &x->x3); \
  377.     decode_##t4(pptr, &x->x4); \
  378.     decode_##t5(pptr, &x->x5); \
  379.     decode_##t6(pptr, &x->x6); \
  380.     decode_##t7(pptr, &x->x7); \
  381.     decode_##t8(pptr, &x->x8); \
  382.     decode_##t9(pptr, &x->x9); \
  383. }
  384.  
  385. #define endecode_fields_10_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8,t9,x9,t10,x10) \
  386. static inline void encode_##name(char **pptr, const struct name *x) { \
  387.     encode_##t1(pptr, &x->x1); \
  388.     encode_##t2(pptr, &x->x2); \
  389.     encode_##t3(pptr, &x->x3); \
  390.     encode_##t4(pptr, &x->x4); \
  391.     encode_##t5(pptr, &x->x5); \
  392.     encode_##t6(pptr, &x->x6); \
  393.     encode_##t7(pptr, &x->x7); \
  394.     encode_##t8(pptr, &x->x8); \
  395.     encode_##t9(pptr, &x->x9); \
  396.     encode_##t10(pptr, &x->x10); \
  397. } \
  398. static inline void decode_##name(char **pptr, struct name *x) { \
  399.     decode_##t1(pptr, &x->x1); \
  400.     decode_##t2(pptr, &x->x2); \
  401.     decode_##t3(pptr, &x->x3); \
  402.     decode_##t4(pptr, &x->x4); \
  403.     decode_##t5(pptr, &x->x5); \
  404.     decode_##t6(pptr, &x->x6); \
  405.     decode_##t7(pptr, &x->x7); \
  406.     decode_##t8(pptr, &x->x8); \
  407.     decode_##t9(pptr, &x->x9); \
  408.     decode_##t10(pptr, &x->x10); \
  409. }
  410.  
  411. #define endecode_fields_11_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8,t9,x9,t10,x10,t11,x11) \
  412. static inline void encode_##name(char **pptr, const struct name *x) { \
  413.     encode_##t1(pptr, &x->x1); \
  414.     encode_##t2(pptr, &x->x2); \
  415.     encode_##t3(pptr, &x->x3); \
  416.     encode_##t4(pptr, &x->x4); \
  417.     encode_##t5(pptr, &x->x5); \
  418.     encode_##t6(pptr, &x->x6); \
  419.     encode_##t7(pptr, &x->x7); \
  420.     encode_##t8(pptr, &x->x8); \
  421.     encode_##t9(pptr, &x->x9); \
  422.     encode_##t10(pptr, &x->x10); \
  423.     encode_##t11(pptr, &x->x11); \
  424. } \
  425. static inline void decode_##name(char **pptr, struct name *x) { \
  426.     decode_##t1(pptr, &x->x1); \
  427.     decode_##t2(pptr, &x->x2); \
  428.     decode_##t3(pptr, &x->x3); \
  429.     decode_##t4(pptr, &x->x4); \
  430.     decode_##t5(pptr, &x->x5); \
  431.     decode_##t6(pptr, &x->x6); \
  432.     decode_##t7(pptr, &x->x7); \
  433.     decode_##t8(pptr, &x->x8); \
  434.     decode_##t9(pptr, &x->x9); \
  435.     decode_##t10(pptr, &x->x10); \
  436.     decode_##t11(pptr, &x->x11); \
  437. }
  438.  
  439. #define endecode_fields_12(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7,t8,x8,t9,x9,t10,x10,t11,x11,t12,x12) \
  440. static inline void encode_##name(char **pptr, const name *x) { \
  441.     encode_##t1(pptr, &x->x1); \
  442.     encode_##t2(pptr, &x->x2); \
  443.     encode_##t3(pptr, &x->x3); \
  444.     encode_##t4(pptr, &x->x4); \
  445.     encode_##t5(pptr, &x->x5); \
  446.     encode_##t6(pptr, &x->x6); \
  447.     encode_##t7(pptr, &x->x7); \
  448.     encode_##t8(pptr, &x->x8); \
  449.     encode_##t9(pptr, &x->x9); \
  450.     encode_##t10(pptr, &x->x10); \
  451.     encode_##t11(pptr, &x->x11); \
  452.     encode_##t12(pptr, &x->x12); \
  453. } \
  454. static inline void decode_##name(char **pptr, name *x) { \
  455.     decode_##t1(pptr, &x->x1); \
  456.     decode_##t2(pptr, &x->x2); \
  457.     decode_##t3(pptr, &x->x3); \
  458.     decode_##t4(pptr, &x->x4); \
  459.     decode_##t5(pptr, &x->x5); \
  460.     decode_##t6(pptr, &x->x6); \
  461.     decode_##t7(pptr, &x->x7); \
  462.     decode_##t8(pptr, &x->x8); \
  463.     decode_##t9(pptr, &x->x9); \
  464.     decode_##t10(pptr, &x->x10); \
  465.     decode_##t11(pptr, &x->x11); \
  466.     decode_##t12(pptr, &x->x12); \
  467. }
  468.  
  469. #define endecode_fields_15_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7, \
  470.     t8,x8,t9,x9,t10,x10,t11,x11,t12,x12,t13,x13,t14,x14,t15,x15) \
  471. static inline void encode_##name(char **pptr, const struct name *x) { \
  472.     encode_##t1(pptr, &x->x1); \
  473.     encode_##t2(pptr, &x->x2); \
  474.     encode_##t3(pptr, &x->x3); \
  475.     encode_##t4(pptr, &x->x4); \
  476.     encode_##t5(pptr, &x->x5); \
  477.     encode_##t6(pptr, &x->x6); \
  478.     encode_##t7(pptr, &x->x7); \
  479.     encode_##t8(pptr, &x->x8); \
  480.     encode_##t9(pptr, &x->x9); \
  481.     encode_##t10(pptr, &x->x10); \
  482.     encode_##t11(pptr, &x->x11); \
  483.     encode_##t12(pptr, &x->x12); \
  484.     encode_##t13(pptr, &x->x13); \
  485.     encode_##t14(pptr, &x->x14); \
  486.     encode_##t15(pptr, &x->x15); \
  487. } \
  488. static inline void decode_##name(char **pptr, struct name *x) { \
  489.     decode_##t1(pptr, &x->x1); \
  490.     decode_##t2(pptr, &x->x2); \
  491.     decode_##t3(pptr, &x->x3); \
  492.     decode_##t4(pptr, &x->x4); \
  493.     decode_##t5(pptr, &x->x5); \
  494.     decode_##t6(pptr, &x->x6); \
  495.     decode_##t7(pptr, &x->x7); \
  496.     decode_##t8(pptr, &x->x8); \
  497.     decode_##t9(pptr, &x->x9); \
  498.     decode_##t10(pptr, &x->x10); \
  499.     decode_##t11(pptr, &x->x11); \
  500.     decode_##t12(pptr, &x->x12); \
  501.     decode_##t13(pptr, &x->x13); \
  502.     decode_##t14(pptr, &x->x14); \
  503.     decode_##t15(pptr, &x->x15); \
  504. }
  505.  
  506. #define endecode_fields_16_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7, \
  507.     t8,x8,t9,x9,t10,x10,t11,x11,t12,x12,t13,x13,t14,x14,t15,x15,t16,x16) \
  508. static inline void encode_##name(char **pptr, const struct name *x) { \
  509.     encode_##t1(pptr, &x->x1); \
  510.     encode_##t2(pptr, &x->x2); \
  511.     encode_##t3(pptr, &x->x3); \
  512.     encode_##t4(pptr, &x->x4); \
  513.     encode_##t5(pptr, &x->x5); \
  514.     encode_##t6(pptr, &x->x6); \
  515.     encode_##t7(pptr, &x->x7); \
  516.     encode_##t8(pptr, &x->x8); \
  517.     encode_##t9(pptr, &x->x9); \
  518.     encode_##t10(pptr, &x->x10); \
  519.     encode_##t11(pptr, &x->x11); \
  520.     encode_##t12(pptr, &x->x12); \
  521.     encode_##t13(pptr, &x->x13); \
  522.     encode_##t14(pptr, &x->x14); \
  523.     encode_##t15(pptr, &x->x15); \
  524.     encode_##t16(pptr, &x->x16); \
  525. } \
  526. static inline void decode_##name(char **pptr, struct name *x) { \
  527.     decode_##t1(pptr, &x->x1); \
  528.     decode_##t2(pptr, &x->x2); \
  529.     decode_##t3(pptr, &x->x3); \
  530.     decode_##t4(pptr, &x->x4); \
  531.     decode_##t5(pptr, &x->x5); \
  532.     decode_##t6(pptr, &x->x6); \
  533.     decode_##t7(pptr, &x->x7); \
  534.     decode_##t8(pptr, &x->x8); \
  535.     decode_##t9(pptr, &x->x9); \
  536.     decode_##t10(pptr, &x->x10); \
  537.     decode_##t11(pptr, &x->x11); \
  538.     decode_##t12(pptr, &x->x12); \
  539.     decode_##t13(pptr, &x->x13); \
  540.     decode_##t14(pptr, &x->x14); \
  541.     decode_##t15(pptr, &x->x15); \
  542.     decode_##t16(pptr, &x->x16); \
  543. }
  544.  
  545.  
  546. #define endecode_fields_17_struct(name,t1,x1,t2,x2,t3,x3,t4,x4,t5,x5,t6,x6,t7,x7, \
  547.     t8,x8,t9,x9,t10,x10,t11,x11,t12,x12,t13,x13,t14,x14,t15,x15,t16,x16,t17,x17) \
  548. static inline void encode_##name(char **pptr, const struct name *x) { \
  549.     encode_##t1(pptr, &x->x1); \
  550.     encode_##t2(pptr, &x->x2); \
  551.     encode_##t3(pptr, &x->x3); \
  552.     encode_##t4(pptr, &x->x4); \
  553.     encode_##t5(pptr, &x->x5); \
  554.     encode_##t6(pptr, &x->x6); \
  555.     encode_##t7(pptr, &x->x7); \
  556.     encode_##t8(pptr, &x->x8); \
  557.     encode_##t9(pptr, &x->x9); \
  558.     encode_##t10(pptr, &x->x10); \
  559.     encode_##t11(pptr, &x->x11); \
  560.     encode_##t12(pptr, &x->x12); \
  561.     encode_##t13(pptr, &x->x13); \
  562.     encode_##t14(pptr, &x->x14); \
  563.     encode_##t15(pptr, &x->x15); \
  564.     encode_##t16(pptr, &x->x16); \
  565.     encode_##t17(pptr, &x->x17); \
  566. } \
  567. static inline void decode_##name(char **pptr, struct name *x) { \
  568.     decode_##t1(pptr, &x->x1); \
  569.     decode_##t2(pptr, &x->x2); \
  570.     decode_##t3(pptr, &x->x3); \
  571.     decode_##t4(pptr, &x->x4); \
  572.     decode_##t5(pptr, &x->x5); \
  573.     decode_##t6(pptr, &x->x6); \
  574.     decode_##t7(pptr, &x->x7); \
  575.     decode_##t8(pptr, &x->x8); \
  576.     decode_##t9(pptr, &x->x9); \
  577.     decode_##t10(pptr, &x->x10); \
  578.     decode_##t11(pptr, &x->x11); \
  579.     decode_##t12(pptr, &x->x12); \
  580.     decode_##t13(pptr, &x->x13); \
  581.     decode_##t14(pptr, &x->x14); \
  582.     decode_##t15(pptr, &x->x15); \
  583.     decode_##t16(pptr, &x->x16); \
  584.     decode_##t17(pptr, &x->x17); \
  585. }
  586.  
  587. /* ones with arrays that are allocated in the decode */
  588.  
  589. /* one field then one array */
  590. #define endecode_fields_1a_generic(name, sname, t1, x1, tn1, n1, ta1, a1) \
  591. static inline void encode_##name(char **pptr, const sname *x) { typeof(tn1) i; \
  592.     encode_##t1(pptr, &x->x1); \
  593.     encode_##tn1(pptr, &x->n1); \
  594.     for (i=0; i<x->n1; i++) \
  595.     encode_##ta1(pptr, &(x)->a1[i]); \
  596. } \
  597. static inline void decode_##name(char **pptr, sname *x) { typeof(tn1) i; \
  598.     decode_##t1(pptr, &x->x1); \
  599.     decode_##tn1(pptr, &x->n1); \
  600.     x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
  601.     for (i=0; i<x->n1; i++) \
  602.     decode_##ta1(pptr, &(x)->a1[i]); \
  603. }
  604.  
  605. /* one field then two arrays */
  606. #define endecode_fields_1aa_generic(name, sname, t1, x1, tn1, n1, ta1, a1, ta2, a2) \
  607. static inline void encode_##name(char **pptr, const sname *x) { typeof(tn1) i; \
  608.     encode_##t1(pptr, &x->x1); \
  609.     encode_##tn1(pptr, &x->n1); \
  610.     for (i=0; i<x->n1; i++) \
  611.     encode_##ta1(pptr, &(x)->a1[i]); \
  612.     for (i=0; i<x->n1; i++) \
  613.     encode_##ta2(pptr, &(x)->a2[i]); \
  614. } \
  615. static inline void decode_##name(char **pptr, sname *x) { typeof(tn1) i; \
  616.     decode_##t1(pptr, &x->x1); \
  617.     decode_##tn1(pptr, &x->n1); \
  618.     x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
  619.     for (i=0; i<x->n1; i++) \
  620.     decode_##ta1(pptr, &(x)->a1[i]); \
  621.     x->a2 = decode_malloc(x->n1 * sizeof(*x->a2)); \
  622.     for (i=0; i<x->n1; i++) \
  623.     decode_##ta2(pptr, &(x)->a2[i]); \
  624. }
  625.  
  626.  
  627. #define endecode_fields_1a(name, t1, x1, tn1, n1, ta1, a1) \
  628.     endecode_fields_1a_generic(name, name, t1, x1, tn1, n1, ta1, a1)
  629. #define endecode_fields_1a_struct(name, t1, x1, tn1, n1, ta1, a1) \
  630.     endecode_fields_1a_generic(name, struct name, t1, x1, tn1, n1, ta1, a1)
  631.  
  632. #define endecode_fields_1aa(name, t1, x1, tn1, n1, ta1, a1, ta2, a2) \
  633.     endecode_fields_1aa_generic(name, name, t1, x1, tn1, n1, ta1, a1, ta2, a2)
  634. #define endecode_fields_1aa_struct(name, t1, x1, tn1, n1, ta1, a1, ta2, a2) \
  635.     endecode_fields_1aa_generic(name, struct name, t1, x1, tn1, n1, ta1, a1, ta2, a2)
  636.  
  637. /* one field, and array, another field, another array - a special case */
  638. #define endecode_fields_1a_1a_struct(name, t1,x1, tn1, n1, ta1, a1, t2,x2, tn2, n2, ta2, a2) \
  639. static inline void encode_##name(char **pptr, const struct name *x) { int i; \
  640.     encode_##t1(pptr, &x->x1); \
  641.     encode_##tn1(pptr, &x->n1); \
  642.     for (i=0; i<x->n1; i++) { int n; n = i; \
  643.     encode_##ta1(pptr, &(x)->a1[n]); } \
  644.     encode_##t2(pptr, &x->x2); \
  645.     encode_##tn2(pptr, &x->n2); \
  646.     for (i=0; i<x->n2; i++) { int n; n = i; \
  647.     encode_##ta2(pptr, &(x)->a2[n]); } \
  648. } \
  649. static inline void decode_##name(char **pptr, struct name *x) { int i; \
  650.     decode_##t1(pptr, &x->x1); \
  651.     decode_##tn1(pptr, &x->n1); \
  652.     x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
  653.     for (i=0; i<x->n1; i++) \
  654.     decode_##ta1(pptr, &(x)->a1[i]); \
  655.     decode_##t1(pptr, &x->x1); \
  656.     decode_##tn2(pptr, &x->n2); \
  657.     x->a2 = decode_malloc(x->n2 * sizeof(*x->a2)); \
  658.     for (i=0; i<x->n2; i++) \
  659.     decode_##ta2(pptr, &(x)->a2[i]); \
  660. }
  661.  
  662. /* 2 fields, then an array */
  663. #define endecode_fields_2a_generic(name, sname, t1, x1, t2, x2, tn1, n1, ta1, a1) \
  664. static inline void encode_##name(char **pptr, const sname *x) { int i; \
  665.     encode_##t1(pptr, &x->x1); \
  666.     encode_##t2(pptr, &x->x2); \
  667.     encode_##tn1(pptr, &x->n1); \
  668.     for (i=0; i<x->n1; i++) \
  669.     encode_##ta1(pptr, &(x)->a1[i]); \
  670. } \
  671. static inline void decode_##name(char **pptr, sname *x) { int i; \
  672.     decode_##t1(pptr, &x->x1); \
  673.     decode_##t2(pptr, &x->x2); \
  674.     decode_##tn1(pptr, &x->n1); \
  675.     x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
  676.     for (i=0; i<x->n1; i++) \
  677.     decode_##ta1(pptr, &(x)->a1[i]); \
  678. }
  679.  
  680. #define endecode_fields_2a(name, t1, x1, t2, x2, tn1, n1, ta1, a1) \
  681.     endecode_fields_2a_generic(name, name, t1, x1, t2, x2, tn1, n1, ta1, a1)
  682. #define endecode_fields_2a_struct(name, t1, x1, t2, x2, tn1, n1, ta1, a1) \
  683.     endecode_fields_2a_generic(name, struct name, t1, x1, t2, x2, tn1, n1, ta1, a1)
  684.  
  685. /* special case where we have two arrays of the same size after 2 fields */
  686. #define endecode_fields_2aa_struct(name, t1, x1, t2, x2, tn1, n1, ta1, a1, ta2, a2) \
  687. static inline void encode_##name(char **pptr, const struct name *x) { int i; \
  688.     encode_##t1(pptr, &x->x1); \
  689.     encode_##t2(pptr, &x->x2); \
  690.     encode_##tn1(pptr, &x->n1); \
  691.     for (i=0; i<x->n1; i++) \
  692.     encode_##ta1(pptr, &(x)->a1[i]); \
  693.     for (i=0; i<x->n1; i++) \
  694.     encode_##ta2(pptr, &(x)->a2[i]); \
  695. } \
  696. static inline void decode_##name(char **pptr, struct name *x) { int i; \
  697.     decode_##t1(pptr, &x->x1); \
  698.     decode_##t2(pptr, &x->x2); \
  699.     decode_##tn1(pptr, &x->n1); \
  700.     x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
  701.     for (i=0; i<x->n1; i++) \
  702.     decode_##ta1(pptr, &(x)->a1[i]); \
  703.     x->a2 = decode_malloc(x->n1 * sizeof(*x->a2)); \
  704.     for (i=0; i<x->n1; i++) \
  705.     decode_##ta2(pptr, &(x)->a2[i]); \
  706. }
  707.  
  708. /* 3 fields, then an array */
  709. #define endecode_fields_3a_struct(name, t1, x1, t2, x2, t3, x3, tn1, n1, ta1, a1) \
  710. static inline void encode_##name(char **pptr, const struct name *x) { int i; \
  711.     encode_##t1(pptr, &x->x1); \
  712.     encode_##t2(pptr, &x->x2); \
  713.     encode_##t3(pptr, &x->x3); \
  714.     encode_##tn1(pptr, &x->n1); \
  715.     for (i=0; i<x->n1; i++) \
  716.     encode_##ta1(pptr, &(x)->a1[i]); \
  717. } \
  718. static inline void decode_##name(char **pptr, struct name *x) { int i; \
  719.     decode_##t1(pptr, &x->x1); \
  720.     decode_##t2(pptr, &x->x2); \
  721.     decode_##t3(pptr, &x->x3); \
  722.     decode_##tn1(pptr, &x->n1); \
  723.     x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
  724.     for (i=0; i<x->n1; i++) \
  725.     decode_##ta1(pptr, &(x)->a1[i]); \
  726. }
  727.  
  728. /* special case where we have two arrays of the same size after 4 fields */
  729. #define endecode_fields_4aa_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, tn1, n1, ta1, a1, ta2, a2) \
  730. static inline void encode_##name(char **pptr, const struct name *x) { int i; \
  731.     encode_##t1(pptr, &x->x1); \
  732.     encode_##t2(pptr, &x->x2); \
  733.     encode_##t3(pptr, &x->x3); \
  734.     encode_##t4(pptr, &x->x4); \
  735.     encode_##tn1(pptr, &x->n1); \
  736.     for (i=0; i<x->n1; i++) \
  737.     encode_##ta1(pptr, &(x)->a1[i]); \
  738.     for (i=0; i<x->n1; i++) \
  739.     encode_##ta2(pptr, &(x)->a2[i]); \
  740. } \
  741. static inline void decode_##name(char **pptr, struct name *x) { int i; \
  742.     decode_##t1(pptr, &x->x1); \
  743.     decode_##t2(pptr, &x->x2); \
  744.     decode_##t3(pptr, &x->x3); \
  745.     decode_##t4(pptr, &x->x4); \
  746.     decode_##tn1(pptr, &x->n1); \
  747.     x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
  748.     for (i=0; i<x->n1; i++) \
  749.     decode_##ta1(pptr, &(x)->a1[i]); \
  750.     x->a2 = decode_malloc(x->n1 * sizeof(*x->a2)); \
  751.     for (i=0; i<x->n1; i++) \
  752.     decode_##ta2(pptr, &(x)->a2[i]); \
  753. }
  754.  
  755.  
  756. /* 4 fields, then an array */
  757. #define endecode_fields_4a_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, tn1,n1,ta1,a1) \
  758. static inline void encode_##name(char **pptr, const struct name *x) { int i; \
  759.     encode_##t1(pptr, &x->x1); \
  760.     encode_##t2(pptr, &x->x2); \
  761.     encode_##t3(pptr, &x->x3); \
  762.     encode_##t4(pptr, &x->x4); \
  763.     encode_##tn1(pptr, &x->n1); \
  764.     for (i=0; i<x->n1; i++) \
  765.     encode_##ta1(pptr, &(x)->a1[i]); \
  766. } \
  767. static inline void decode_##name(char **pptr, struct name *x) { int i; \
  768.     decode_##t1(pptr, &x->x1); \
  769.     decode_##t2(pptr, &x->x2); \
  770.     decode_##t3(pptr, &x->x3); \
  771.     decode_##t4(pptr, &x->x4); \
  772.     decode_##tn1(pptr, &x->n1); \
  773.     x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
  774.     for (i=0; i<x->n1; i++) \
  775.     decode_##ta1(pptr, &(x)->a1[i]); \
  776. }
  777.  
  778. /* 5 fields, then an array */
  779. #define endecode_fields_5a_struct(name, t1, x1, t2, x2, t3, x3, t4, x4, t5, x5, tn1,n1,ta1,a1) \
  780. static inline void encode_##name(char **pptr, const struct name *x) { int i; \
  781.     encode_##t1(pptr, &x->x1); \
  782.     encode_##t2(pptr, &x->x2); \
  783.     encode_##t3(pptr, &x->x3); \
  784.     encode_##t4(pptr, &x->x4); \
  785.     encode_##t5(pptr, &x->x5); \
  786.     encode_##tn1(pptr, &x->n1); \
  787.     for (i=0; i<x->n1; i++) \
  788.     encode_##ta1(pptr, &(x)->a1[i]); \
  789. } \
  790. static inline void decode_##name(char **pptr, struct name *x) { int i; \
  791.     decode_##t1(pptr, &x->x1); \
  792.     decode_##t2(pptr, &x->x2); \
  793.     decode_##t3(pptr, &x->x3); \
  794.     decode_##t4(pptr, &x->x4); \
  795.     decode_##t5(pptr, &x->x5); \
  796.     decode_##tn1(pptr, &x->n1); \
  797.     x->a1 = decode_malloc(x->n1 * sizeof(*x->a1)); \
  798.     for (i=0; i<x->n1; i++) \
  799.     decode_##ta1(pptr, &(x)->a1[i]); \
  800. }
  801.  
  802. #define DEFINE_STATIC_ENDECODE_FUNCS(__name__, __type__) \
  803. __attribute__((unused)) \
  804. static void encode_func_##__name__(char **pptr, void *x) \
  805. { \
  806.     encode_##__name__(pptr, (__type__ *)x); \
  807. }; \
  808. __attribute__((unused)) \
  809. static void decode_func_##__name__(char **pptr, void *x) \
  810. { \
  811.     decode_##__name__(pptr, (__type__ *)x); \
  812. }
  813.  
  814. #define encode_enum_union_2_struct(name, ename, uname, ut1, un1, en1, ut2, un2, en2)                         \
  815. static inline void encode_##name(char **pptr, const struct name *x)           \
  816. {                                                                             \
  817.     encode_enum(pptr, &x->ename);                                             \
  818.     switch(x->ename)                                                          \
  819.     {                                                                         \
  820.         case en1: encode_##ut1(pptr, &x->uname.un1); break;                   \
  821.         case en2: encode_##ut2(pptr, &x->uname.un2); break;                   \
  822.         default: assert(0);                                                   \
  823.     }                                                                         \
  824. };                                                                            \
  825. static inline void decode_##name(char **pptr, struct name *x)                 \
  826. {                                                                             \
  827.     decode_enum(pptr, &x->ename);                                             \
  828.     switch(x->ename)                                                          \
  829.     {                                                                         \
  830.         case en1: decode_##ut1(pptr, &x->uname.un1); break;                   \
  831.         case en2: decode_##ut2(pptr, &x->uname.un2); break;                   \
  832.         default: assert(0);                                                   \
  833.     }                                                                         \
  834. };
  835.  
  836. #endif  /* __SRC_PROTO_ENDECODE_FUNCS_H */
  837.  
  838.