mach0data.h 14.4 KB
Newer Older
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1 2
/*****************************************************************************

3
Copyright (c) 1995, 2009, Oracle and/or its affiliates. All Rights Reserved.
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4 5 6 7 8 9 10 11 12 13

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
14 15
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
Vadim Tkachenko's avatar
Vadim Tkachenko committed
16 17 18

*****************************************************************************/

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
19 20
/******************************************************************//**
@file include/mach0data.h
21 22 23 24 25 26 27 28 29
Utilities for converting data from the database file
to the machine format.

Created 11/28/1995 Heikki Tuuri
***********************************************************************/

#ifndef mach0data_h
#define mach0data_h

30 31
#ifndef UNIV_INNOCHECKSUM

32 33 34 35 36 37 38 39
#include "univ.i"
#include "ut0byte.h"

/* The data and all fields are always stored in a database file
in the same format: ascii, big-endian, ... .
All data in the files MUST be accessed using the functions in this
module. */

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
40
/*******************************************************//**
41 42 43 44 45
The following function is used to store data in one byte. */
UNIV_INLINE
void
mach_write_to_1(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
46 47 48 49 50
	byte*	b,	/*!< in: pointer to byte where to store */
	ulint	n);	 /*!< in: ulint integer to be stored, >= 0, < 256 */
/********************************************************//**
The following function is used to fetch data from one byte.
@return	ulint integer, >= 0, < 256 */
51 52 53 54
UNIV_INLINE
ulint
mach_read_from_1(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
55
	const byte*	b)	/*!< in: pointer to byte */
56
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
57
/*******************************************************//**
58 59 60 61 62 63
The following function is used to store data in two consecutive
bytes. We store the most significant byte to the lower address. */
UNIV_INLINE
void
mach_write_to_2(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
64 65 66
	byte*	b,	/*!< in: pointer to two bytes where to store */
	ulint	n);	 /*!< in: ulint integer to be stored, >= 0, < 64k */
/********************************************************//**
67
The following function is used to fetch data from two consecutive
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
68 69
bytes. The most significant byte is at the lowest address.
@return	ulint integer, >= 0, < 64k */
70 71 72 73
UNIV_INLINE
ulint
mach_read_from_2(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
74
	const byte*	b)	/*!< in: pointer to two bytes */
75 76
	__attribute__((nonnull, pure));

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
77
/********************************************************//**
78 79
The following function is used to convert a 16-bit data item
to the canonical format, for fast bytewise equality test
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
80 81
against memory.
@return	16-bit integer in canonical format */
82 83 84 85
UNIV_INLINE
uint16
mach_encode_2(
/*==========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
86
	ulint	n)	/*!< in: integer in machine-dependent format */
87
	__attribute__((const));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
88
/********************************************************//**
89 90
The following function is used to convert a 16-bit data item
from the canonical format, for fast bytewise equality test
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
91 92
against memory.
@return	integer in machine-dependent format */
93 94 95 96
UNIV_INLINE
ulint
mach_decode_2(
/*==========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
97
	uint16	n)	/*!< in: 16-bit integer in canonical format */
98
	__attribute__((const));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
99
/*******************************************************//**
100 101 102 103 104 105
The following function is used to store data in 3 consecutive
bytes. We store the most significant byte to the lowest address. */
UNIV_INLINE
void
mach_write_to_3(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
106 107 108
	byte*	b,	/*!< in: pointer to 3 bytes where to store */
	ulint	n);	 /*!< in: ulint integer to be stored */
/********************************************************//**
109
The following function is used to fetch data from 3 consecutive
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
110 111
bytes. The most significant byte is at the lowest address.
@return	ulint integer */
112 113 114 115
UNIV_INLINE
ulint
mach_read_from_3(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
116
	const byte*	b)	/*!< in: pointer to 3 bytes */
117
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
118
/*******************************************************//**
119 120 121 122 123 124
The following function is used to store data in four consecutive
bytes. We store the most significant byte to the lowest address. */
UNIV_INLINE
void
mach_write_to_4(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
125 126 127
	byte*	b,	/*!< in: pointer to four bytes where to store */
	ulint	n);	 /*!< in: ulint integer to be stored */
/********************************************************//**
128
The following function is used to fetch data from 4 consecutive
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
129 130
bytes. The most significant byte is at the lowest address.
@return	ulint integer */
131 132 133 134
UNIV_INLINE
ulint
mach_read_from_4(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
135
	const byte*	b)	/*!< in: pointer to four bytes */
136
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
137 138 139
/*********************************************************//**
Writes a ulint in a compressed form (1..5 bytes).
@return	stored size in bytes */
140 141 142 143
UNIV_INLINE
ulint
mach_write_compressed(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
144 145 146 147 148
	byte*	b,	/*!< in: pointer to memory where to store */
	ulint	n);	/*!< in: ulint integer to be stored */
/*********************************************************//**
Returns the size of an ulint when written in the compressed form.
@return	compressed size in bytes */
149 150 151 152
UNIV_INLINE
ulint
mach_get_compressed_size(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
153
	ulint	n)	/*!< in: ulint integer to be stored */
154
	__attribute__((const));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
155 156 157
/*********************************************************//**
Reads a ulint in a compressed form.
@return	read integer */
158 159 160 161
UNIV_INLINE
ulint
mach_read_compressed(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
162
	const byte*	b)	/*!< in: pointer to memory from where to read */
163
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
164
/*******************************************************//**
165 166 167 168 169 170
The following function is used to store data in 6 consecutive
bytes. We store the most significant byte to the lowest address. */
UNIV_INLINE
void
mach_write_to_6(
/*============*/
Sergei Golubchik's avatar
Sergei Golubchik committed
171 172
	byte*		b,	/*!< in: pointer to 6 bytes where to store */
	ib_uint64_t	id);	/*!< in: 48-bit integer */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
173
/********************************************************//**
174
The following function is used to fetch data from 6 consecutive
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
175
bytes. The most significant byte is at the lowest address.
Sergei Golubchik's avatar
Sergei Golubchik committed
176
@return	48-bit integer */
177
UNIV_INLINE
Sergei Golubchik's avatar
Sergei Golubchik committed
178
ib_uint64_t
179 180
mach_read_from_6(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
181
	const byte*	b)	/*!< in: pointer to 6 bytes */
182
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
183
/*******************************************************//**
184 185 186 187 188 189
The following function is used to store data in 7 consecutive
bytes. We store the most significant byte to the lowest address. */
UNIV_INLINE
void
mach_write_to_7(
/*============*/
Sergei Golubchik's avatar
Sergei Golubchik committed
190 191
	byte*		b,	/*!< in: pointer to 7 bytes where to store */
	ib_uint64_t	n);	/*!< in: 56-bit integer */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
192
/********************************************************//**
193
The following function is used to fetch data from 7 consecutive
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
194
bytes. The most significant byte is at the lowest address.
Sergei Golubchik's avatar
Sergei Golubchik committed
195
@return	56-bit integer */
196
UNIV_INLINE
Sergei Golubchik's avatar
Sergei Golubchik committed
197
ib_uint64_t
198 199
mach_read_from_7(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
200
	const byte*	b)	/*!< in: pointer to 7 bytes */
201
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
202
/*******************************************************//**
203 204 205 206 207 208
The following function is used to store data in 8 consecutive
bytes. We store the most significant byte to the lowest address. */
UNIV_INLINE
void
mach_write_to_8(
/*============*/
209
	void*		b,	/*!< in: pointer to 8 bytes where to store */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
210 211
	ib_uint64_t	n);	/*!< in: 64-bit integer to be stored */
/********************************************************//**
212
The following function is used to fetch data from 8 consecutive
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
213 214
bytes. The most significant byte is at the lowest address.
@return	64-bit integer */
215 216
UNIV_INLINE
ib_uint64_t
Sergei Golubchik's avatar
Sergei Golubchik committed
217 218
mach_read_from_8(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
219
	const byte*	b)	/*!< in: pointer to 8 bytes */
220
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
221
/*********************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
222
Writes a 64-bit integer in a compressed form (5..9 bytes).
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
223
@return	size in bytes */
224 225
UNIV_INLINE
ulint
Sergei Golubchik's avatar
Sergei Golubchik committed
226 227 228 229
mach_ull_write_compressed(
/*======================*/
	byte*		b,	/*!< in: pointer to memory where to store */
	ib_uint64_t	n);	/*!< in: 64-bit integer to be stored */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
230
/*********************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
231
Returns the size of a 64-bit integer when written in the compressed form.
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
232
@return	compressed size in bytes */
233 234
UNIV_INLINE
ulint
Sergei Golubchik's avatar
Sergei Golubchik committed
235 236 237
mach_ull_get_compressed_size(
/*=========================*/
	ib_uint64_t	n);	/*!< in: 64-bit integer to be stored */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
238
/*********************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
239 240
Reads a 64-bit integer in a compressed form.
@return	the value read */
241
UNIV_INLINE
Sergei Golubchik's avatar
Sergei Golubchik committed
242 243 244
ib_uint64_t
mach_ull_read_compressed(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
245
	const byte*	b)	/*!< in: pointer to memory from where to read */
246
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
247
/*********************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
248
Writes a 64-bit integer in a compressed form (1..11 bytes).
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
249
@return	size in bytes */
250 251
UNIV_INLINE
ulint
Sergei Golubchik's avatar
Sergei Golubchik committed
252 253 254 255
mach_ull_write_much_compressed(
/*===========================*/
	byte*		b,	/*!< in: pointer to memory where to store */
	ib_uint64_t	n);	/*!< in: 64-bit integer to be stored */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
256
/*********************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
257
Returns the size of a 64-bit integer when written in the compressed form.
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
258
@return	compressed size in bytes */
259 260
UNIV_INLINE
ulint
Sergei Golubchik's avatar
Sergei Golubchik committed
261 262 263
mach_ull_get_much_compressed_size(
/*==============================*/
	ib_uint64_t	n)	/*!< in: 64-bit integer to be stored */
264
	__attribute__((const));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
265
/*********************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
266 267
Reads a 64-bit integer in a compressed form.
@return	the value read */
268
UNIV_INLINE
Sergei Golubchik's avatar
Sergei Golubchik committed
269 270 271
ib_uint64_t
mach_ull_read_much_compressed(
/*==========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
272
	const byte*	b)	/*!< in: pointer to memory from where to read */
273
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
274 275 276
/*********************************************************//**
Reads a ulint in a compressed form if the log record fully contains it.
@return	pointer to end of the stored field, NULL if not complete */
277 278 279 280
UNIV_INTERN
byte*
mach_parse_compressed(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
281 282 283 284
	byte*	ptr,	/*!< in: pointer to buffer from where to read */
	byte*	end_ptr,/*!< in: pointer to end of the buffer */
	ulint*	val);	/*!< out: read value */
/*********************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
285 286 287 288
Reads a 64-bit integer in a compressed form
if the log record fully contains it.
@return pointer to end of the stored field, NULL if not complete */
UNIV_INLINE
289
byte*
Sergei Golubchik's avatar
Sergei Golubchik committed
290 291 292 293 294
mach_ull_parse_compressed(
/*======================*/
	byte*		ptr,	/*!< in: pointer to buffer from where to read */
	byte*		end_ptr,/*!< in: pointer to end of the buffer */
	ib_uint64_t*	val);	/*!< out: read value */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
295 296 297 298
#ifndef UNIV_HOTBACKUP
/*********************************************************//**
Reads a double. It is stored in a little-endian format.
@return	double read */
299 300 301 302
UNIV_INLINE
double
mach_double_read(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
303
	const byte*	b)	/*!< in: pointer to memory from where to read */
304
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
305
/*********************************************************//**
306 307 308 309 310
Writes a double. It is stored in a little-endian format. */
UNIV_INLINE
void
mach_double_write(
/*==============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
311 312 313 314 315
	byte*	b,	/*!< in: pointer to memory where to write */
	double	d);	/*!< in: double */
/*********************************************************//**
Reads a float. It is stored in a little-endian format.
@return	float read */
316 317 318 319
UNIV_INLINE
float
mach_float_read(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
320
	const byte*	b)	/*!< in: pointer to memory from where to read */
321
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
322
/*********************************************************//**
323 324 325 326 327
Writes a float. It is stored in a little-endian format. */
UNIV_INLINE
void
mach_float_write(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
328 329 330 331 332
	byte*	b,	/*!< in: pointer to memory where to write */
	float	d);	/*!< in: float */
/*********************************************************//**
Reads a ulint stored in the little-endian format.
@return	unsigned long int */
333 334 335 336
UNIV_INLINE
ulint
mach_read_from_n_little_endian(
/*===========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
337 338
	const byte*	buf,		/*!< in: from where to read */
	ulint		buf_size)	/*!< in: from how many bytes to read */
339
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
340
/*********************************************************//**
341 342 343 344 345
Writes a ulint in the little-endian format. */
UNIV_INLINE
void
mach_write_to_n_little_endian(
/*==========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
346 347 348 349 350 351
	byte*	dest,		/*!< in: where to write */
	ulint	dest_size,	/*!< in: into how many bytes to write */
	ulint	n);		/*!< in: unsigned long int to write */
/*********************************************************//**
Reads a ulint stored in the little-endian format.
@return	unsigned long int */
352 353 354 355
UNIV_INLINE
ulint
mach_read_from_2_little_endian(
/*===========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
356
	const byte*	buf)		/*!< in: from where to read */
357
	__attribute__((nonnull, pure));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
358
/*********************************************************//**
359 360 361 362 363
Writes a ulint in the little-endian format. */
UNIV_INLINE
void
mach_write_to_2_little_endian(
/*==========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
364 365 366
	byte*	dest,		/*!< in: where to write */
	ulint	n);		/*!< in: unsigned long int to write */
/*********************************************************//**
367
Convert integral type from storage byte order (big endian) to
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
368 369
host byte order.
@return	integer value */
370
UNIV_INLINE
371
ib_uint64_t
372 373
mach_read_int_type(
/*===============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
374 375 376
	const byte*	src,		/*!< in: where to read from */
	ulint		len,		/*!< in: length of src */
	ibool		unsigned_type);	/*!< in: signed or unsigned flag */
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/***********************************************************//**
Convert integral type from host byte order to (big-endian) storage
byte order. */
UNIV_INLINE
void
mach_write_int_type(
/*================*/
	byte*		dest,		/*!< in: where to write*/
	const byte*	src,		/*!< in: where to read from */
	ulint		len,		/*!< in: length of src */
	bool		usign);		/*!< in: signed or unsigned flag */

/*************************************************************
Convert a ulonglong integer from host byte order to (big-endian)
storage byte order. */
UNIV_INLINE
void
mach_write_ulonglong(
/*=================*/
	byte*		dest,		/*!< in: where to write */
	ulonglong	src,		/*!< in: where to read from */
	ulint		len,		/*!< in: length of dest */
	bool		usign);		/*!< in: signed or unsigned flag */

/********************************************************//**
Reads 1 - 4 bytes from a file page buffered in the buffer pool.
@return	value read */
UNIV_INLINE
ulint
mach_read_ulint(
/*============*/
	const byte*	ptr,	/*!< in: pointer from where to read */
	ulint		type);	/*!< in: MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
411
#endif /* !UNIV_HOTBACKUP */
412
#endif /* !UNIV_INNOCHECKSUM */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
413

414 415 416 417 418
#ifndef UNIV_NONINL
#include "mach0data.ic"
#endif

#endif