mar_gen.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2006-2011 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef MAR_GEN_H_DEFINED
  35. #define MAR_GEN_H_DEFINED
  36. #include <stdint.h>
  37. #include <string.h>
  38. #include <corosync/corotypes.h>
  39. #include <corosync/swab.h>
  40. #define MAR_ALIGN_UP(addr,size) (((addr)+((size)-1))&(~((size)-1)))
  41. typedef int8_t mar_int8_t;
  42. typedef int16_t mar_int16_t;
  43. typedef int32_t mar_int32_t;
  44. typedef int64_t mar_int64_t;
  45. typedef uint8_t mar_uint8_t;
  46. typedef uint16_t mar_uint16_t;
  47. typedef uint32_t mar_uint32_t;
  48. typedef uint64_t mar_uint64_t;
  49. /**
  50. * @brief swab_mar_int8_t
  51. * @param to_swab
  52. */
  53. static inline void swab_mar_int8_t (mar_int8_t *to_swab)
  54. {
  55. return;
  56. }
  57. /**
  58. * @brief swab_mar_int16_t
  59. * @param to_swab
  60. */
  61. static inline void swab_mar_int16_t (mar_int16_t *to_swab)
  62. {
  63. *to_swab = swab16 (*to_swab);
  64. }
  65. /**
  66. * @brief swab_mar_int32_t
  67. * @param to_swab
  68. */
  69. static inline void swab_mar_int32_t (mar_int32_t *to_swab)
  70. {
  71. *to_swab = swab32 (*to_swab);
  72. }
  73. /**
  74. * @brief swab_mar_int64_t
  75. * @param to_swab
  76. */
  77. static inline void swab_mar_int64_t (mar_int64_t *to_swab)
  78. {
  79. *to_swab = swab64 (*to_swab);
  80. }
  81. /**
  82. * @brief swab_mar_uint8_t
  83. * @param to_swab
  84. */
  85. static inline void swab_mar_uint8_t (mar_uint8_t *to_swab)
  86. {
  87. return;
  88. }
  89. /**
  90. * @brief swab_mar_uint16_t
  91. * @param to_swab
  92. */
  93. static inline void swab_mar_uint16_t (mar_uint16_t *to_swab)
  94. {
  95. *to_swab = swab16 (*to_swab);
  96. }
  97. /**
  98. * @brief swab_mar_uint32_t
  99. * @param to_swab
  100. */
  101. static inline void swab_mar_uint32_t (mar_uint32_t *to_swab)
  102. {
  103. *to_swab = swab32 (*to_swab);
  104. }
  105. /**
  106. * @brief swab_mar_uint64_t
  107. * @param to_swab
  108. */
  109. static inline void swab_mar_uint64_t (mar_uint64_t *to_swab)
  110. {
  111. *to_swab = swab64 (*to_swab);
  112. }
  113. /**
  114. * @brief swabbin
  115. * @param data
  116. * @param len
  117. */
  118. static inline void swabbin(char *data, size_t len)
  119. {
  120. int i;
  121. char tmp;
  122. for (i = 0; i < len / 2; i++) {
  123. tmp = data[i];
  124. data[i] = data[len - i - 1];
  125. data[len - i - 1] = tmp;
  126. }
  127. }
  128. /**
  129. * @brief swabflt
  130. * @param flt
  131. */
  132. static inline void swabflt(float *flt)
  133. {
  134. swabbin((char *)flt, sizeof(*flt));
  135. }
  136. /**
  137. * @brief swabdbl
  138. * @param dbl
  139. */
  140. static inline void swabdbl(double *dbl)
  141. {
  142. swabbin((char *)dbl, sizeof(*dbl));
  143. }
  144. /**
  145. * @brief mar_name_t struct
  146. */
  147. typedef struct {
  148. mar_uint16_t length __attribute__((aligned(8)));
  149. mar_uint8_t value[CS_MAX_NAME_LENGTH] __attribute__((aligned(8)));
  150. } mar_name_t;
  151. /**
  152. * @brief get_mar_name_t
  153. * @param name
  154. * @return
  155. */
  156. static inline const char *get_mar_name_t (const mar_name_t *name) {
  157. return ((const char *)name->value);
  158. }
  159. /**
  160. * @brief mar_name_match
  161. * @param name1
  162. * @param name2
  163. * @return
  164. */
  165. static inline int mar_name_match(const mar_name_t *name1, const mar_name_t *name2)
  166. {
  167. if (name1->length == name2->length) {
  168. return ((strncmp ((const char *)name1->value,
  169. (const char *)name2->value,
  170. name1->length)) == 0);
  171. }
  172. return 0;
  173. }
  174. /**
  175. * @brief swab_mar_name_t
  176. * @param to_swab
  177. */
  178. static inline void swab_mar_name_t (mar_name_t *to_swab)
  179. {
  180. swab_mar_uint16_t (&to_swab->length);
  181. }
  182. /**
  183. * @brief marshall_from_mar_name_t
  184. * @param dest
  185. * @param src
  186. */
  187. static inline void marshall_from_mar_name_t (
  188. cs_name_t *dest,
  189. const mar_name_t *src)
  190. {
  191. dest->length = src->length;
  192. memcpy (dest->value, src->value, CS_MAX_NAME_LENGTH);
  193. }
  194. /**
  195. * @brief marshall_to_mar_name_t
  196. * @param dest
  197. * @param src
  198. */
  199. static inline void marshall_to_mar_name_t (
  200. mar_name_t *dest,
  201. const cs_name_t *src)
  202. {
  203. dest->length = src->length;
  204. memcpy (dest->value, src->value, CS_MAX_NAME_LENGTH);
  205. }
  206. /**
  207. * @brief mar_bool_t enum
  208. */
  209. typedef enum {
  210. MAR_FALSE = 0,
  211. MAR_TRUE = 1
  212. } mar_bool_t;
  213. /**
  214. * @brief mar_time_t
  215. */
  216. typedef mar_uint64_t mar_time_t;
  217. /**
  218. * @brief swab_mar_time_t
  219. * @param to_swab
  220. */
  221. static inline void swab_mar_time_t (mar_time_t *to_swab)
  222. {
  223. swab_mar_uint64_t (to_swab);
  224. }
  225. #define MAR_TIME_END ((int64_t)0x7fffffffffffffffull)
  226. #define MAR_TIME_BEGIN 0x0ULL
  227. #define MAR_TIME_UNKNOWN 0x8000000000000000ULL
  228. #define MAR_TIME_ONE_MICROSECOND 1000ULL
  229. #define MAR_TIME_ONE_MILLISECOND 1000000ULL
  230. #define MAR_TIME_ONE_SECOND 1000000000ULL
  231. #define MAR_TIME_ONE_MINUTE 60000000000ULL
  232. #define MAR_TIME_ONE_HOUR 3600000000000ULL
  233. #define MAR_TIME_ONE_DAY 86400000000000ULL
  234. #define MAR_TIME_MAX CS_TIME_END
  235. #define MAR_TRACK_CURRENT 0x01
  236. #define MAR_TRACK_CHANGES 0x02
  237. #define MAR_TRACK_CHANGES_ONLY 0x04
  238. /**
  239. * @brief mar_invocation_t
  240. */
  241. typedef mar_uint64_t mar_invocation_t;
  242. /**
  243. * @brief swab_mar_invocation_t
  244. * @param to_swab
  245. */
  246. static inline void swab_mar_invocation_t (mar_invocation_t *to_swab)
  247. {
  248. swab_mar_uint64_t (to_swab);
  249. }
  250. /**
  251. * @brief mar_size_t
  252. */
  253. typedef mar_uint64_t mar_size_t;
  254. /**
  255. * @brief swab_mar_size_t
  256. * @param to_swab
  257. */
  258. static inline void swab_mar_size_t (mar_size_t *to_swab)
  259. {
  260. swab_mar_uint64_t (to_swab);
  261. }
  262. /**
  263. * @brief swab_coroipc_request_header_t
  264. * @param to_swab
  265. */
  266. static inline void swab_coroipc_request_header_t (struct qb_ipc_request_header *to_swab)
  267. {
  268. swab_mar_int32_t (&to_swab->size);
  269. swab_mar_int32_t (&to_swab->id);
  270. }
  271. #endif /* MAR_GEN_H_DEFINED */