From 4dd6131711287a1910f2b3394195aff3854e9141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= Date: Fri, 10 Jan 2025 16:40:22 +0200 Subject: [PATCH] MDEV-35049: Privatize ut_fold_ulint_pair() Now that ut_fold_ulint_pair() and ut_fold_binary() are no longer needed for anything else than compatibility with old InnoDB data files that may use innodb_checksum_algorithm=innodb, let us move the code to a single compilation unit. Reviewed by: Vladislav Lesin --- extra/innochecksum.cc | 6 -- storage/innobase/CMakeLists.txt | 1 - storage/innobase/buf/buf0checksum.cc | 72 +++++++++++++------ storage/innobase/include/buf0checksum.h | 18 +++-- storage/innobase/include/ut0rnd.h | 31 +------- storage/innobase/include/ut0rnd.inl | 94 ------------------------- storage/innobase/log/log0recv.cc | 1 + 7 files changed, 62 insertions(+), 161 deletions(-) delete mode 100644 storage/innobase/include/ut0rnd.inl diff --git a/extra/innochecksum.cc b/extra/innochecksum.cc index d4d15df2d72..8d33ae3d63e 100644 --- a/extra/innochecksum.cc +++ b/extra/innochecksum.cc @@ -54,12 +54,6 @@ The parts not included are excluded by #ifndef UNIV_INNOCHECKSUM. */ #include -#ifdef UNIV_NONINL -# include "fsp0fsp.inl" -# include "mach0data.inl" -# include "ut0rnd.inl" -#endif - #ifndef PRIuMAX #define PRIuMAX "llu" #endif diff --git a/storage/innobase/CMakeLists.txt b/storage/innobase/CMakeLists.txt index 711d88616eb..113bad3d703 100644 --- a/storage/innobase/CMakeLists.txt +++ b/storage/innobase/CMakeLists.txt @@ -358,7 +358,6 @@ SET(INNOBASE_SOURCES include/ut0pool.h include/ut0rbt.h include/ut0rnd.h - include/ut0rnd.inl include/ut0sort.h include/ut0stage.h include/ut0ut.h diff --git a/storage/innobase/buf/buf0checksum.cc b/storage/innobase/buf/buf0checksum.cc index 662343ae60d..185aedbb344 100644 --- a/storage/innobase/buf/buf0checksum.cc +++ b/storage/innobase/buf/buf0checksum.cc @@ -26,10 +26,10 @@ Created Aug 11, 2011 Vasil Dimov #include "buf0checksum.h" #include "fil0fil.h" -#include "ut0rnd.h" +#include "my_sys.h" #ifndef UNIV_INNOCHECKSUM -#include "srv0srv.h" +# include "srv0srv.h" #endif /* !UNIV_INNOCHECKSUM */ /** Calculate the CRC32 checksum of a page. The value is stored to the page @@ -38,7 +38,7 @@ the file. Note that we must be careful to calculate the same value on all architectures. @param[in] page buffer page (srv_page_size bytes) @return CRC-32C */ -uint32_t buf_calc_page_crc32(const byte* page) +uint32_t buf_calc_page_crc32(const byte *page) noexcept { /* Note: innodb_checksum_algorithm=crc32 could and should have included the entire page in the checksum, and CRC-32 values @@ -54,13 +54,60 @@ uint32_t buf_calc_page_crc32(const byte* page) } #ifndef UNIV_INNOCHECKSUM +static inline ulint ut_fold_ulint_pair(ulint n1, ulint n2) +{ + return ((((n1 ^ n2 ^ 1653893711) << 8) + n1) ^ 1463735687) + n2; +} + +/** Fold a binary string, similar to innodb_checksum_algorithm=innodb. +@return folded value */ +ulint ut_fold_binary(const byte *str, size_t len) noexcept +{ + ulint fold= 0; + for (const byte *const str_end= str + (len & 0xFFFFFFF8); str < str_end; + str+= 8) + { + fold= ut_fold_ulint_pair(fold, str[0]); + fold= ut_fold_ulint_pair(fold, str[1]); + fold= ut_fold_ulint_pair(fold, str[2]); + fold= ut_fold_ulint_pair(fold, str[3]); + fold= ut_fold_ulint_pair(fold, str[4]); + fold= ut_fold_ulint_pair(fold, str[5]); + fold= ut_fold_ulint_pair(fold, str[6]); + fold= ut_fold_ulint_pair(fold, str[7]); + } + + switch (len & 0x7) { + case 7: + fold= ut_fold_ulint_pair(fold, *str++); + /* fall through */ + case 6: + fold= ut_fold_ulint_pair(fold, *str++); + /* fall through */ + case 5: + fold= ut_fold_ulint_pair(fold, *str++); + /* fall through */ + case 4: + fold= ut_fold_ulint_pair(fold, *str++); + /* fall through */ + case 3: + fold= ut_fold_ulint_pair(fold, *str++); + /* fall through */ + case 2: + fold= ut_fold_ulint_pair(fold, *str++); + /* fall through */ + case 1: + fold= ut_fold_ulint_pair(fold, *str++); + } + return fold; +} + /** Calculate a checksum which is stored to the page when it is written to a file. Note that we must be careful to calculate the same value on 32-bit and 64-bit architectures. @param[in] page file page (srv_page_size bytes) @return checksum */ -uint32_t -buf_calc_page_new_checksum(const byte* page) +uint32_t buf_calc_page_new_checksum(const byte *page) noexcept { ulint checksum; @@ -80,19 +127,4 @@ buf_calc_page_new_checksum(const byte* page) - FIL_PAGE_END_LSN_OLD_CHKSUM); return(static_cast(checksum)); } - -/** In MySQL before 4.0.14 or 4.1.1 there was an InnoDB bug that -the checksum only looked at the first few bytes of the page. -This calculates that old checksum. -NOTE: we must first store the new formula checksum to -FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum -because this takes that field as an input! -@param[in] page file page (srv_page_size bytes) -@return checksum */ -uint32_t -buf_calc_page_old_checksum(const byte* page) -{ - return(static_cast - (ut_fold_binary(page, FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION))); -} #endif /* !UNIV_INNOCHECKSUM */ diff --git a/storage/innobase/include/buf0checksum.h b/storage/innobase/include/buf0checksum.h index d9f03177812..a6455304a12 100644 --- a/storage/innobase/include/buf0checksum.h +++ b/storage/innobase/include/buf0checksum.h @@ -33,25 +33,23 @@ the file. Note that we must be careful to calculate the same value on all architectures. @param[in] page buffer page (srv_page_size bytes) @return CRC-32C */ -uint32_t buf_calc_page_crc32(const byte* page); +uint32_t buf_calc_page_crc32(const byte *page) noexcept; #ifndef UNIV_INNOCHECKSUM +/** Fold a binary string, similar to innodb_checksum_algorithm=innodb. +@return folded value */ +ulint ut_fold_binary(const byte *str, size_t len) noexcept; + /** Calculate a checksum which is stored to the page when it is written to a file. Note that we must be careful to calculate the same value on 32-bit and 64-bit architectures. @param[in] page file page (srv_page_size bytes) @return checksum */ uint32_t -buf_calc_page_new_checksum(const byte* page); +buf_calc_page_new_checksum(const byte *page) noexcept; /** In MySQL before 4.0.14 or 4.1.1 there was an InnoDB bug that the checksum only looked at the first few bytes of the page. -This calculates that old checksum. -NOTE: we must first store the new formula checksum to -FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum -because this takes that field as an input! -@param[in] page file page (srv_page_size bytes) -@return checksum */ -uint32_t -buf_calc_page_old_checksum(const byte* page); +This calculates that old checksum. */ +# define buf_calc_page_old_checksum(page) uint32_t(ut_fold_binary(page, 26)) #endif /* !UNIV_INNOCHECKSUM */ diff --git a/storage/innobase/include/ut0rnd.h b/storage/innobase/include/ut0rnd.h index 5cba6ee534f..90c69a0464d 100644 --- a/storage/innobase/include/ut0rnd.h +++ b/storage/innobase/include/ut0rnd.h @@ -24,13 +24,11 @@ Random numbers and hashing Created 1/20/1994 Heikki Tuuri ***********************************************************************/ -#ifndef ut0rnd_h -#define ut0rnd_h +#pragma once #include "ut0byte.h" #include -#ifndef UNIV_INNOCHECKSUM /** Seed value of ut_rnd_gen() */ extern std::atomic ut_rnd_current; @@ -87,30 +85,3 @@ ut_find_prime( /*==========*/ ulint n) /*!< in: positive number > 100 */ MY_ATTRIBUTE((const)); - -#endif /* !UNIV_INNOCHECKSUM */ - -/*************************************************************//** -Folds a pair of ulints. -@return folded value */ -UNIV_INLINE -ulint -ut_fold_ulint_pair( -/*===============*/ - ulint n1, /*!< in: ulint */ - ulint n2) /*!< in: ulint */ - MY_ATTRIBUTE((const)); -/*************************************************************//** -Folds a binary string. -@return folded value */ -UNIV_INLINE -ulint -ut_fold_binary( -/*===========*/ - const byte* str, /*!< in: string of bytes */ - ulint len) /*!< in: length */ - MY_ATTRIBUTE((pure)); - -#include "ut0rnd.inl" - -#endif diff --git a/storage/innobase/include/ut0rnd.inl b/storage/innobase/include/ut0rnd.inl deleted file mode 100644 index c9f7373dd1b..00000000000 --- a/storage/innobase/include/ut0rnd.inl +++ /dev/null @@ -1,94 +0,0 @@ -/***************************************************************************** - -Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved. -Copyright (c) 2017, 2021, MariaDB Corporation. - -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 -this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA - -*****************************************************************************/ - -/**************************************************************//** -@file include/ut0rnd.ic -Random numbers and hashing - -Created 5/30/1994 Heikki Tuuri -*******************************************************************/ - -#define UT_HASH_RANDOM_MASK 1463735687 -#define UT_HASH_RANDOM_MASK2 1653893711 - -/*************************************************************//** -Folds a pair of ulints. -@return folded value */ -UNIV_INLINE -ulint -ut_fold_ulint_pair( -/*===============*/ - ulint n1, /*!< in: ulint */ - ulint n2) /*!< in: ulint */ -{ - return(((((n1 ^ n2 ^ UT_HASH_RANDOM_MASK2) << 8) + n1) - ^ UT_HASH_RANDOM_MASK) + n2); -} - -/*************************************************************//** -Folds a binary string. -@return folded value */ -UNIV_INLINE -ulint -ut_fold_binary( -/*===========*/ - const byte* str, /*!< in: string of bytes */ - ulint len) /*!< in: length */ -{ - ulint fold = 0; - const byte* str_end = str + (len & 0xFFFFFFF8); - - ut_ad(str || !len); - - while (str < str_end) { - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - } - - switch (len & 0x7) { - case 7: - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - /* fall through */ - case 6: - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - /* fall through */ - case 5: - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - /* fall through */ - case 4: - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - /* fall through */ - case 3: - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - /* fall through */ - case 2: - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - /* fall through */ - case 1: - fold = ut_fold_ulint_pair(fold, (ulint)(*str++)); - } - - return(fold); -} diff --git a/storage/innobase/log/log0recv.cc b/storage/innobase/log/log0recv.cc index e748eb35e9c..6317c760e0f 100644 --- a/storage/innobase/log/log0recv.cc +++ b/storage/innobase/log/log0recv.cc @@ -41,6 +41,7 @@ Created 9/20/1997 Heikki Tuuri #include "buf0buf.h" #include "buf0dblwr.h" #include "buf0flu.h" +#include "buf0checksum.h" #include "mtr0mtr.h" #include "mtr0log.h" #include "page0page.h"