MySQL 9.0.0
Source Code Documentation
mach0data.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/mach0data.h
29 Utilities for converting data from the database file
30 to the machine format.
31
32 Created 11/28/1995 Heikki Tuuri
33 ***********************************************************************/
34
35#ifndef mach0data_h
36#define mach0data_h
37
38#include "mtr0types.h"
39#include "univ.i"
40
41#ifdef UNIV_HOTBACKUP
42#include "ut0byte.h"
43#endif /* UNIV_HOTBACKUP */
44
45/* The data and all fields are always stored in a database file
46in the same format: ascii, big-endian, ... .
47All data in the files MUST be accessed using the functions in this
48module. */
49
50/** The following function is used to store data in one byte.
51@param[in] b pointer to byte where to store
52@param[in] n One byte integer to be stored, >= 0, < 256 */
53static inline void mach_write_to_1(byte *b, ulint n);
54
55/** The following function is used to fetch data from one byte.
56@param[in] b pointer to a byte to read
57@return ulint integer, >= 0, < 256 */
58[[nodiscard]] static inline uint8_t mach_read_from_1(const byte *b);
59
60/** The following function is used to store data in two consecutive bytes. We
61store the most significant byte to the lower address.
62@param[in] b pointer to 2 bytes where to store
63@param[in] n 2-byte integer to be stored, >= 0, < 64k */
64static inline void mach_write_to_2(byte *b, ulint n);
65
66/** The following function is used to fetch data from 2 consecutive
67bytes. The most significant byte is at the lowest address.
68@param[in] b pointer to 2 bytes where to store
69@return 2-byte integer, >= 0, < 64k */
70[[nodiscard]] static inline uint16_t mach_read_from_2(const byte *b);
71
72/** The following function is used to convert a 16-bit data item to the
73canonical format, for fast bytewise equality test against memory.
74@param[in] n integer in machine-dependent format
75@return 16-bit integer in canonical format */
76static inline uint16_t mach_encode_2(ulint n);
77
78/** The following function is used to convert a 16-bit data item from the
79canonical format, for fast bytewise equality test against memory.
80@param[in] n 16-bit integer in canonical format
81@return integer in machine-dependent format */
82static inline ulint mach_decode_2(uint16 n);
83
84/** The following function is used to store data in 3 consecutive
85bytes. We store the most significant byte to the lowest address.
86@param[in] b pointer to 3 bytes where to store
87@param[in] n 3 byte integer to be stored */
88static inline void mach_write_to_3(byte *b, ulint n);
89
90/** The following function is used to fetch data from 3 consecutive
91bytes. The most significant byte is at the lowest address.
92@param[in] b pointer to 3 bytes to read
93@return 32 bit integer */
94[[nodiscard]] static inline uint32_t mach_read_from_3(const byte *b);
95
96/** The following function is used to store data in 4 consecutive
97bytes. We store the most significant byte to the lowest address.
98@param[in] b pointer to 4 bytes where to store
99@param[in] n 4 byte integer to be stored */
100static inline void mach_write_to_4(byte *b, ulint n);
101
102/** The following function is used to fetch data from 4 consecutive
103bytes. The most significant byte is at the lowest address.
104@param[in] b pointer to 4 bytes to read
105@return 32 bit integer */
106[[nodiscard]] static inline uint32_t mach_read_from_4(const byte *b);
107
108/** Write a ulint in a compressed form (1..5 bytes).
109@param[in] b pointer to memory where to store
110@param[in] n ulint integer to be stored
111@return stored size in bytes */
112static inline ulint mach_write_compressed(byte *b, ulint n);
113
114/** Return the size of an ulint when written in the compressed form.
115@param[in] n ulint integer to be stored
116@return compressed size in bytes */
117static inline uint32_t mach_get_compressed_size(ulint n);
118
119/** Read a 32-bit integer in a compressed form.
120@param[in,out] b pointer to memory where to read;
121advanced by the number of bytes consumed
122@return unsigned value */
123static inline uint32_t mach_read_next_compressed(const byte **b);
124
125/** The following function is used to store data in 6 consecutive
126bytes. We store the most significant byte to the lowest address.
127@param[in] b pointer to 6 bytes where to store
128@param[in] id 48-bit integer to write */
129static inline void mach_write_to_6(byte *b, uint64_t id);
130
131/** The following function is used to fetch data from 6 consecutive
132bytes. The most significant byte is at the lowest address.
133@param[in] b pointer to 6 bytes to read
134@return 48-bit integer */
135[[nodiscard]] static inline uint64_t mach_read_from_6(const byte *b);
136
137/** The following function is used to store data in 7 consecutive
138bytes. We store the most significant byte to the lowest address.
139@param[in] b pointer to 7 bytes where to store
140@param[in] n 56-bit integer */
141static inline void mach_write_to_7(byte *b, uint64_t n);
142
143/** The following function is used to fetch data from 7 consecutive
144bytes. The most significant byte is at the lowest address.
145@param[in] b pointer to 7 bytes to read
146@return 56-bit integer */
147[[nodiscard]] static inline uint64_t mach_read_from_7(const byte *b);
148
149/** The following function is used to store data in 8 consecutive
150bytes. We store the most significant byte to the lowest address.
151@param[in] b pointer to 8 bytes where to store
152@param[in] n 64-bit integer to be stored */
153static inline void mach_write_to_8(void *b, uint64_t n);
154
155/** The following function is used to fetch data from 8 consecutive
156bytes. The most significant byte is at the lowest address.
157@param[in] b pointer to 8 bytes to read
158@return 64-bit integer */
159[[nodiscard]] static inline uint64_t mach_read_from_8(const byte *b);
160
161/** Writes a 64-bit integer in a compressed form (5..9 bytes).
162@param[in] b pointer to memory where to store
163@param[in] n 64-bit integer to be stored
164@return size in bytes */
165static inline ulint mach_u64_write_compressed(byte *b, uint64_t n);
166
167/** Read a 64-bit integer in a compressed form.
168@param[in,out] b pointer to memory where to read;
169advanced by the number of bytes consumed
170@return unsigned value */
171static inline uint64_t mach_u64_read_next_compressed(const byte **b);
172
173/** Writes a 64-bit integer in a compressed form (1..11 bytes).
174@param[in] b pointer to memory where to store
175@param[in] n 64-bit integer to be stored
176@return size in bytes */
177static inline ulint mach_u64_write_much_compressed(byte *b, uint64_t n);
178
179/** Reads a 64-bit integer in a compressed form.
180@param[in] b pointer to memory from where to read
181@return the value read */
182[[nodiscard]] static inline uint64_t mach_u64_read_much_compressed(
183 const byte *b);
184
185/** Read a 64-bit integer in a much compressed form.
186@param[in,out] ptr pointer to memory from where to read,
187advanced by the number of bytes consumed, or set NULL if out of space
188@param[in] end_ptr end of the buffer
189@return unsigned 64-bit integer */
190uint64_t mach_parse_u64_much_compressed(const byte **ptr, const byte *end_ptr);
191
192/** Read a 32-bit integer in a compressed form.
193@param[in,out] ptr pointer to memory from where to read;
194advanced by the number of bytes consumed, or set NULL if out of space
195@param[in] end_ptr end of the buffer
196@return unsigned value */
197uint32_t mach_parse_compressed(const byte **ptr, const byte *end_ptr);
198
199/** Read a 64-bit integer in a compressed form.
200@param[in,out] ptr pointer to memory from where to read;
201advanced by the number of bytes consumed, or set NULL if out of space
202@param[in] end_ptr end of the buffer
203@return unsigned value */
204static inline uint64_t mach_u64_parse_compressed(const byte **ptr,
205 const byte *end_ptr);
206
207/** Reads a double. It is stored in a little-endian format.
208 @return double read */
209[[nodiscard]] static inline double mach_double_read(
210 const byte *b); /*!< in: pointer to memory from where to read */
211
212/** Writes a double. It is stored in a little-endian format.
213@param[in] b pointer to memory where to write
214@param[in] d double */
215static inline void mach_double_write(byte *b, double d);
216
217/** Reads a float. It is stored in a little-endian format.
218 @return float read */
219[[nodiscard]] static inline float mach_float_read(
220 const byte *b); /*!< in: pointer to memory from where to read */
221
222/** Writes a float. It is stored in a little-endian format.
223@param[in] b pointer to memory where to write
224@param[in] d float */
225static inline void mach_float_write(byte *b, float d);
226
227#ifndef UNIV_HOTBACKUP
228/** Reads a ulint stored in the little-endian format.
229@param[in] buf From where to read.
230@param[in] buf_size From how many bytes to read.
231@return unsigned long int */
232[[nodiscard]] static inline ulint mach_read_from_n_little_endian(
233 const byte *buf, ulint buf_size);
234
235/** Writes a ulint in the little-endian format.
236@param[in] dest where to write
237@param[in] dest_size into how many bytes to write
238@param[in] n unsigned long int to write */
239static inline void mach_write_to_n_little_endian(byte *dest, ulint dest_size,
240 ulint n);
241
242/** Reads a ulint stored in the little-endian format.
243 @return unsigned long int */
244[[nodiscard]] static inline ulint mach_read_from_2_little_endian(
245 const byte *buf); /*!< in: from where to read */
246
247/** Writes a ulint in the little-endian format.
248@param[in] dest where to write
249@param[in] n unsigned long int to write */
250static inline void mach_write_to_2_little_endian(byte *dest, ulint n);
251
252/** Convert integral type from storage byte order (big endian) to host byte
253order.
254@param[in] src where to read from
255@param[in] len length of src
256@param[in] unsigned_type signed or unsigned flag
257@return integer value */
258static inline uint64_t mach_read_int_type(const byte *src, ulint len,
259 bool unsigned_type);
260
261/** Convert integral type from host byte order to (big-endian) storage
262byte order.
263@param[in] dest where to write
264@param[in] src where to read
265@param[in] len length of src
266@param[in] usign signed or unsigned flag */
267static inline void mach_write_int_type(byte *dest, const byte *src, ulint len,
268 bool usign);
269
270/** Convert a ulonglong integer from host byte order to (big-endian) storage
271byte order.
272@param[in] dest where to write
273@param[in] src where to read from
274@param[in] len length of dest
275@param[in] usign signed or unsigned flag */
276static inline void mach_write_ulonglong(byte *dest, ulonglong src, ulint len,
277 bool usign);
278
279#endif
280
281/** Read 1 to 4 bytes from a file page buffered in the buffer pool.
282@param[in] ptr pointer where to read
283@param[in] type MLOG_1BYTE, MLOG_2BYTES, or MLOG_4BYTES
284@return value read */
285[[nodiscard]] static inline uint32_t mach_read_ulint(const byte *ptr,
287
288/** Writes 1, 2 or 4 bytes to a file page.
289@param[in] ptr pointer where to write
290@param[in] val value to write
291@param[in] type MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */
292static inline void mach_write_ulint(byte *ptr, ulint val, mlog_id_t type) {
293 switch (type) {
294 case MLOG_1BYTE:
295 mach_write_to_1(ptr, val);
296 break;
297 case MLOG_2BYTES:
298 mach_write_to_2(ptr, val);
299 break;
300 case MLOG_4BYTES:
301 mach_write_to_4(ptr, val);
302 break;
303 default:
304 ut_error;
305 }
306}
307
308#include "mach0data.ic"
309
310#endif
constexpr DWORD buf_size
Definition: create_def.cc:229
static void mach_write_int_type(byte *dest, const byte *src, ulint len, bool usign)
Convert integral type from host byte order to (big-endian) storage byte order.
static ulint mach_u64_write_much_compressed(byte *b, uint64_t n)
Writes a 64-bit integer in a compressed form (1..11 bytes).
static ulint mach_read_from_n_little_endian(const byte *buf, ulint buf_size)
Reads a ulint stored in the little-endian format.
static uint64_t mach_u64_read_much_compressed(const byte *b)
Reads a 64-bit integer in a compressed form.
static uint16_t mach_read_from_2(const byte *b)
The following function is used to fetch data from 2 consecutive bytes.
static uint64_t mach_u64_read_next_compressed(const byte **b)
Read a 64-bit integer in a compressed form.
uint64_t mach_parse_u64_much_compressed(const byte **ptr, const byte *end_ptr)
Read a 64-bit integer in a much compressed form.
Definition: mach0data.cc:44
static uint32_t mach_read_from_4(const byte *b)
The following function is used to fetch data from 4 consecutive bytes.
static void mach_write_to_n_little_endian(byte *dest, ulint dest_size, ulint n)
Writes a ulint in the little-endian format.
static void mach_write_to_7(byte *b, uint64_t n)
The following function is used to store data in 7 consecutive bytes.
static void mach_write_to_2_little_endian(byte *dest, ulint n)
Writes a ulint in the little-endian format.
static double mach_double_read(const byte *b)
Reads a double.
static void mach_write_to_2(byte *b, ulint n)
The following function is used to store data in two consecutive bytes.
static void mach_write_to_1(byte *b, ulint n)
The following function is used to store data in one byte.
static void mach_write_to_3(byte *b, ulint n)
The following function is used to store data in 3 consecutive bytes.
static uint32_t mach_read_ulint(const byte *ptr, mlog_id_t type)
Read 1 to 4 bytes from a file page buffered in the buffer pool.
static ulint mach_decode_2(uint16 n)
The following function is used to convert a 16-bit data item from the canonical format,...
static void mach_write_to_8(void *b, uint64_t n)
The following function is used to store data in 8 consecutive bytes.
static ulint mach_write_compressed(byte *b, ulint n)
Write a ulint in a compressed form (1..5 bytes).
static uint64_t mach_read_from_7(const byte *b)
The following function is used to fetch data from 7 consecutive bytes.
static uint8_t mach_read_from_1(const byte *b)
The following function is used to fetch data from one byte.
static ulint mach_u64_write_compressed(byte *b, uint64_t n)
Writes a 64-bit integer in a compressed form (5..9 bytes).
static void mach_write_ulonglong(byte *dest, ulonglong src, ulint len, bool usign)
Convert a ulonglong integer from host byte order to (big-endian) storage byte order.
static void mach_write_to_4(byte *b, ulint n)
The following function is used to store data in 4 consecutive bytes.
static float mach_float_read(const byte *b)
Reads a float.
static void mach_write_ulint(byte *ptr, ulint val, mlog_id_t type)
Writes 1, 2 or 4 bytes to a file page.
Definition: mach0data.h:292
static uint16_t mach_encode_2(ulint n)
The following function is used to convert a 16-bit data item to the canonical format,...
static void mach_write_to_6(byte *b, uint64_t id)
The following function is used to store data in 6 consecutive bytes.
uint32_t mach_parse_compressed(const byte **ptr, const byte *end_ptr)
Read a 32-bit integer in a compressed form.
Definition: mach0data.cc:80
static uint32_t mach_get_compressed_size(ulint n)
Return the size of an ulint when written in the compressed form.
static void mach_float_write(byte *b, float d)
Writes a float.
static uint64_t mach_read_from_6(const byte *b)
The following function is used to fetch data from 6 consecutive bytes.
static ulint mach_read_from_2_little_endian(const byte *buf)
Reads a ulint stored in the little-endian format.
static uint32_t mach_read_from_3(const byte *b)
The following function is used to fetch data from 3 consecutive bytes.
static uint64_t mach_read_int_type(const byte *src, ulint len, bool unsigned_type)
Convert integral type from storage byte order (big endian) to host byte order.
static uint64_t mach_read_from_8(const byte *b)
The following function is used to fetch data from 8 consecutive bytes.
static uint32_t mach_read_next_compressed(const byte **b)
Read a 32-bit integer in a compressed form.
static uint64_t mach_u64_parse_compressed(const byte **ptr, const byte *end_ptr)
Read a 64-bit integer in a compressed form.
static void mach_double_write(byte *b, double d)
Writes a double.
Utilities for converting data from the database file to the machine format.
Mini-transaction buffer global types.
mlog_id_t
Definition: mtr0types.h:63
@ MLOG_4BYTES
4 bytes ...
Definition: mtr0types.h:76
@ MLOG_1BYTE
one byte is written
Definition: mtr0types.h:70
@ MLOG_2BYTES
2 bytes ...
Definition: mtr0types.h:73
unsigned long long int ulonglong
Definition: my_inttypes.h:56
uint16_t uint16
Definition: my_inttypes.h:65
Definition: buf0block_hint.cc:30
required string type
Definition: replication_group_member_actions.proto:34
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:406
Utilities for byte operations.
#define ut_error
Abort execution.
Definition: ut0dbg.h:101
int n
Definition: xcom_base.cc:509