You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

400 lines
14 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /******************************************************************//**
  14. @file include/mach0data.h
  15. Utilities for converting data from the database file
  16. to the machine format.
  17. Created 11/28/1995 Heikki Tuuri
  18. ***********************************************************************/
  19. #ifndef mach0data_h
  20. #define mach0data_h
  21. #include "univ.i"
  22. #include "ut0byte.h"
  23. /* The data and all fields are always stored in a database file
  24. in the same format: ascii, big-endian, ... .
  25. All data in the files MUST be accessed using the functions in this
  26. module. */
  27. /*******************************************************//**
  28. The following function is used to store data in one byte. */
  29. UNIV_INLINE
  30. void
  31. mach_write_to_1(
  32. /*============*/
  33. byte* b, /*!< in: pointer to byte where to store */
  34. ulint n); /*!< in: ulint integer to be stored, >= 0, < 256 */
  35. /********************************************************//**
  36. The following function is used to fetch data from one byte.
  37. @return ulint integer, >= 0, < 256 */
  38. UNIV_INLINE
  39. ulint
  40. mach_read_from_1(
  41. /*=============*/
  42. const byte* b) /*!< in: pointer to byte */
  43. __attribute__((nonnull, pure));
  44. /*******************************************************//**
  45. The following function is used to store data in two consecutive
  46. bytes. We store the most significant byte to the lower address. */
  47. UNIV_INLINE
  48. void
  49. mach_write_to_2(
  50. /*============*/
  51. byte* b, /*!< in: pointer to two bytes where to store */
  52. ulint n); /*!< in: ulint integer to be stored, >= 0, < 64k */
  53. /********************************************************//**
  54. The following function is used to fetch data from two consecutive
  55. bytes. The most significant byte is at the lowest address.
  56. @return ulint integer, >= 0, < 64k */
  57. UNIV_INLINE
  58. ulint
  59. mach_read_from_2(
  60. /*=============*/
  61. const byte* b) /*!< in: pointer to two bytes */
  62. __attribute__((nonnull, pure));
  63. /********************************************************//**
  64. The following function is used to convert a 16-bit data item
  65. to the canonical format, for fast bytewise equality test
  66. against memory.
  67. @return 16-bit integer in canonical format */
  68. UNIV_INLINE
  69. uint16
  70. mach_encode_2(
  71. /*==========*/
  72. ulint n) /*!< in: integer in machine-dependent format */
  73. __attribute__((const));
  74. /********************************************************//**
  75. The following function is used to convert a 16-bit data item
  76. from the canonical format, for fast bytewise equality test
  77. against memory.
  78. @return integer in machine-dependent format */
  79. UNIV_INLINE
  80. ulint
  81. mach_decode_2(
  82. /*==========*/
  83. uint16 n) /*!< in: 16-bit integer in canonical format */
  84. __attribute__((const));
  85. /*******************************************************//**
  86. The following function is used to store data in 3 consecutive
  87. bytes. We store the most significant byte to the lowest address. */
  88. UNIV_INLINE
  89. void
  90. mach_write_to_3(
  91. /*============*/
  92. byte* b, /*!< in: pointer to 3 bytes where to store */
  93. ulint n); /*!< in: ulint integer to be stored */
  94. /********************************************************//**
  95. The following function is used to fetch data from 3 consecutive
  96. bytes. The most significant byte is at the lowest address.
  97. @return ulint integer */
  98. UNIV_INLINE
  99. ulint
  100. mach_read_from_3(
  101. /*=============*/
  102. const byte* b) /*!< in: pointer to 3 bytes */
  103. __attribute__((nonnull, pure));
  104. /*******************************************************//**
  105. The following function is used to store data in four consecutive
  106. bytes. We store the most significant byte to the lowest address. */
  107. UNIV_INLINE
  108. void
  109. mach_write_to_4(
  110. /*============*/
  111. byte* b, /*!< in: pointer to four bytes where to store */
  112. ulint n); /*!< in: ulint integer to be stored */
  113. /********************************************************//**
  114. The following function is used to fetch data from 4 consecutive
  115. bytes. The most significant byte is at the lowest address.
  116. @return ulint integer */
  117. UNIV_INLINE
  118. ulint
  119. mach_read_from_4(
  120. /*=============*/
  121. const byte* b) /*!< in: pointer to four bytes */
  122. __attribute__((nonnull, pure));
  123. /*********************************************************//**
  124. Writes a ulint in a compressed form (1..5 bytes).
  125. @return stored size in bytes */
  126. UNIV_INLINE
  127. ulint
  128. mach_write_compressed(
  129. /*==================*/
  130. byte* b, /*!< in: pointer to memory where to store */
  131. ulint n); /*!< in: ulint integer to be stored */
  132. /*********************************************************//**
  133. Returns the size of an ulint when written in the compressed form.
  134. @return compressed size in bytes */
  135. UNIV_INLINE
  136. ulint
  137. mach_get_compressed_size(
  138. /*=====================*/
  139. ulint n) /*!< in: ulint integer to be stored */
  140. __attribute__((const));
  141. /*********************************************************//**
  142. Reads a ulint in a compressed form.
  143. @return read integer */
  144. UNIV_INLINE
  145. ulint
  146. mach_read_compressed(
  147. /*=================*/
  148. const byte* b) /*!< in: pointer to memory from where to read */
  149. __attribute__((nonnull, pure));
  150. /*******************************************************//**
  151. The following function is used to store data in 6 consecutive
  152. bytes. We store the most significant byte to the lowest address. */
  153. UNIV_INLINE
  154. void
  155. mach_write_to_6(
  156. /*============*/
  157. byte* b, /*!< in: pointer to 6 bytes where to store */
  158. dulint n); /*!< in: dulint integer to be stored */
  159. /********************************************************//**
  160. The following function is used to fetch data from 6 consecutive
  161. bytes. The most significant byte is at the lowest address.
  162. @return dulint integer */
  163. UNIV_INLINE
  164. dulint
  165. mach_read_from_6(
  166. /*=============*/
  167. const byte* b) /*!< in: pointer to 6 bytes */
  168. __attribute__((nonnull, pure));
  169. /*******************************************************//**
  170. The following function is used to store data in 7 consecutive
  171. bytes. We store the most significant byte to the lowest address. */
  172. UNIV_INLINE
  173. void
  174. mach_write_to_7(
  175. /*============*/
  176. byte* b, /*!< in: pointer to 7 bytes where to store */
  177. dulint n); /*!< in: dulint integer to be stored */
  178. /********************************************************//**
  179. The following function is used to fetch data from 7 consecutive
  180. bytes. The most significant byte is at the lowest address.
  181. @return dulint integer */
  182. UNIV_INLINE
  183. dulint
  184. mach_read_from_7(
  185. /*=============*/
  186. const byte* b) /*!< in: pointer to 7 bytes */
  187. __attribute__((nonnull, pure));
  188. /*******************************************************//**
  189. The following function is used to store data in 8 consecutive
  190. bytes. We store the most significant byte to the lowest address. */
  191. UNIV_INLINE
  192. void
  193. mach_write_to_8(
  194. /*============*/
  195. byte* b, /*!< in: pointer to 8 bytes where to store */
  196. dulint n); /*!< in: dulint integer to be stored */
  197. /*******************************************************//**
  198. The following function is used to store data in 8 consecutive
  199. bytes. We store the most significant byte to the lowest address. */
  200. UNIV_INLINE
  201. void
  202. mach_write_ull(
  203. /*===========*/
  204. byte* b, /*!< in: pointer to 8 bytes where to store */
  205. ib_uint64_t n); /*!< in: 64-bit integer to be stored */
  206. /********************************************************//**
  207. The following function is used to fetch data from 8 consecutive
  208. bytes. The most significant byte is at the lowest address.
  209. @return dulint integer */
  210. UNIV_INLINE
  211. dulint
  212. mach_read_from_8(
  213. /*=============*/
  214. const byte* b) /*!< in: pointer to 8 bytes */
  215. __attribute__((nonnull, pure));
  216. /********************************************************//**
  217. The following function is used to fetch data from 8 consecutive
  218. bytes. The most significant byte is at the lowest address.
  219. @return 64-bit integer */
  220. UNIV_INLINE
  221. ib_uint64_t
  222. mach_read_ull(
  223. /*==========*/
  224. const byte* b) /*!< in: pointer to 8 bytes */
  225. __attribute__((nonnull, pure));
  226. /*********************************************************//**
  227. Writes a dulint in a compressed form (5..9 bytes).
  228. @return size in bytes */
  229. UNIV_INLINE
  230. ulint
  231. mach_dulint_write_compressed(
  232. /*=========================*/
  233. byte* b, /*!< in: pointer to memory where to store */
  234. dulint n); /*!< in: dulint integer to be stored */
  235. /*********************************************************//**
  236. Returns the size of a dulint when written in the compressed form.
  237. @return compressed size in bytes */
  238. UNIV_INLINE
  239. ulint
  240. mach_dulint_get_compressed_size(
  241. /*============================*/
  242. dulint n); /*!< in: dulint integer to be stored */
  243. /*********************************************************//**
  244. Reads a dulint in a compressed form.
  245. @return read dulint */
  246. UNIV_INLINE
  247. dulint
  248. mach_dulint_read_compressed(
  249. /*========================*/
  250. const byte* b) /*!< in: pointer to memory from where to read */
  251. __attribute__((nonnull, pure));
  252. /*********************************************************//**
  253. Writes a dulint in a compressed form (1..11 bytes).
  254. @return size in bytes */
  255. UNIV_INLINE
  256. ulint
  257. mach_dulint_write_much_compressed(
  258. /*==============================*/
  259. byte* b, /*!< in: pointer to memory where to store */
  260. dulint n); /*!< in: dulint integer to be stored */
  261. /*********************************************************//**
  262. Returns the size of a dulint when written in the compressed form.
  263. @return compressed size in bytes */
  264. UNIV_INLINE
  265. ulint
  266. mach_dulint_get_much_compressed_size(
  267. /*=================================*/
  268. dulint n) /*!< in: dulint integer to be stored */
  269. __attribute__((const));
  270. /*********************************************************//**
  271. Reads a dulint in a compressed form.
  272. @return read dulint */
  273. UNIV_INLINE
  274. dulint
  275. mach_dulint_read_much_compressed(
  276. /*=============================*/
  277. const byte* b) /*!< in: pointer to memory from where to read */
  278. __attribute__((nonnull, pure));
  279. /*********************************************************//**
  280. Reads a ulint in a compressed form if the log record fully contains it.
  281. @return pointer to end of the stored field, NULL if not complete */
  282. UNIV_INTERN
  283. byte*
  284. mach_parse_compressed(
  285. /*==================*/
  286. byte* ptr, /*!< in: pointer to buffer from where to read */
  287. byte* end_ptr,/*!< in: pointer to end of the buffer */
  288. ulint* val); /*!< out: read value */
  289. /*********************************************************//**
  290. Reads a dulint in a compressed form if the log record fully contains it.
  291. @return pointer to end of the stored field, NULL if not complete */
  292. UNIV_INTERN
  293. byte*
  294. mach_dulint_parse_compressed(
  295. /*=========================*/
  296. byte* ptr, /*!< in: pointer to buffer from where to read */
  297. byte* end_ptr,/*!< in: pointer to end of the buffer */
  298. dulint* val); /*!< out: read value */
  299. #ifndef UNIV_HOTBACKUP
  300. /*********************************************************//**
  301. Reads a double. It is stored in a little-endian format.
  302. @return double read */
  303. UNIV_INLINE
  304. double
  305. mach_double_read(
  306. /*=============*/
  307. const byte* b) /*!< in: pointer to memory from where to read */
  308. __attribute__((nonnull, pure));
  309. /*********************************************************//**
  310. Writes a double. It is stored in a little-endian format. */
  311. UNIV_INLINE
  312. void
  313. mach_double_write(
  314. /*==============*/
  315. byte* b, /*!< in: pointer to memory where to write */
  316. double d); /*!< in: double */
  317. /*********************************************************//**
  318. Reads a float. It is stored in a little-endian format.
  319. @return float read */
  320. UNIV_INLINE
  321. float
  322. mach_float_read(
  323. /*============*/
  324. const byte* b) /*!< in: pointer to memory from where to read */
  325. __attribute__((nonnull, pure));
  326. /*********************************************************//**
  327. Writes a float. It is stored in a little-endian format. */
  328. UNIV_INLINE
  329. void
  330. mach_float_write(
  331. /*=============*/
  332. byte* b, /*!< in: pointer to memory where to write */
  333. float d); /*!< in: float */
  334. /*********************************************************//**
  335. Reads a ulint stored in the little-endian format.
  336. @return unsigned long int */
  337. UNIV_INLINE
  338. ulint
  339. mach_read_from_n_little_endian(
  340. /*===========================*/
  341. const byte* buf, /*!< in: from where to read */
  342. ulint buf_size) /*!< in: from how many bytes to read */
  343. __attribute__((nonnull, pure));
  344. /*********************************************************//**
  345. Writes a ulint in the little-endian format. */
  346. UNIV_INLINE
  347. void
  348. mach_write_to_n_little_endian(
  349. /*==========================*/
  350. byte* dest, /*!< in: where to write */
  351. ulint dest_size, /*!< in: into how many bytes to write */
  352. ulint n); /*!< in: unsigned long int to write */
  353. /*********************************************************//**
  354. Reads a ulint stored in the little-endian format.
  355. @return unsigned long int */
  356. UNIV_INLINE
  357. ulint
  358. mach_read_from_2_little_endian(
  359. /*===========================*/
  360. const byte* buf) /*!< in: from where to read */
  361. __attribute__((nonnull, pure));
  362. /*********************************************************//**
  363. Writes a ulint in the little-endian format. */
  364. UNIV_INLINE
  365. void
  366. mach_write_to_2_little_endian(
  367. /*==========================*/
  368. byte* dest, /*!< in: where to write */
  369. ulint n); /*!< in: unsigned long int to write */
  370. /*********************************************************//**
  371. Convert integral type from storage byte order (big endian) to
  372. host byte order.
  373. @return integer value */
  374. UNIV_INLINE
  375. ullint
  376. mach_read_int_type(
  377. /*===============*/
  378. const byte* src, /*!< in: where to read from */
  379. ulint len, /*!< in: length of src */
  380. ibool unsigned_type); /*!< in: signed or unsigned flag */
  381. #endif /* !UNIV_HOTBACKUP */
  382. #ifndef UNIV_NONINL
  383. #include "mach0data.ic"
  384. #endif
  385. #endif