I notice there is a macro uint4korr
in the MySQL/MariaDB source code.
include/byte_order_generic.h
I merely understand this macro is correlated with byte order. But I looked for the comments about this macro, found nothing. I don’t know the meaning of the suffix korr
. What does the abbreviation express?
I want to know why the code implements like this? What are the effects on different platforms?
2
Answers
"korr" is an abbreviation for "Korrekt" of the phonic and meaning equivalent of the English word "Correct".
The purpose of the code is to provide a uniform byte order of storage and communication components so the storage files are portable between different endian architectures without conversion, and the client/server communication doesn’t need to know which endian the other architecture is.
I believe that the related Swedish verb is korrigera, to correct.
uint4korr()
is kind of the opposite ofntohl()
, because it will swap the bytes on a big-endian architecture and not little-endian.Somewhat related to this, the InnoDB storage engine stores its data in big-endian byte order, so that a simple
memcmp()
can be used for comparing keys. (It also inverts the sign bit of signed integers due to this.) The InnoDB functionmach_read_from_4()
is basicallyntohl()
combined with a 32-bit load via an unaligned pointer. Recent versions of GCC and clang impress me by translating that into the IA-32 or AMD64 instructionsmov
andbswap
or simplymovbe
.