summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarko Mäkelä <marko.makela@mariadb.com>2019-04-08 15:55:09 +0300
committerMarko Mäkelä <marko.makela@mariadb.com>2019-04-08 21:33:49 +0300
commite7f426d2c9be3ebc7c373e80873996ce8857ab48 (patch)
tree9a7503bd3687941c64207e95ffccf872dd31afe9
parentf120a15b93dcb88adc31ad4fc7caf63b813ca63e (diff)
downloadmariadb-git-e7f426d2c9be3ebc7c373e80873996ce8857ab48.tar.gz
MDEV-19212: Replace macros with type-safe inline functions
The regression that was reported in MDEV-19212 occurred due to use of macros that did not ensure that the arguments have compatible types. ut_2pow_remainder(), ut_2pow_round(), ut_calc_align(): Define as inline function templates. UT_CALC_ALIGN(): Define as a macro, because this is used in compile_time_assert(). Only starting with C++11 (MariaDB 10.4) we could define the inline functions as constexpr.
-rw-r--r--storage/innobase/fsp/fsp0fsp.cc2
-rw-r--r--storage/innobase/include/mem0mem.h8
-rw-r--r--storage/innobase/include/ut0ut.h14
-rw-r--r--storage/innobase/log/log0log.cc20
-rw-r--r--storage/innobase/os/os0proc.cc6
5 files changed, 22 insertions, 28 deletions
diff --git a/storage/innobase/fsp/fsp0fsp.cc b/storage/innobase/fsp/fsp0fsp.cc
index 7ef7788e4ec..ac48747513b 100644
--- a/storage/innobase/fsp/fsp0fsp.cc
+++ b/storage/innobase/fsp/fsp0fsp.cc
@@ -955,7 +955,7 @@ fsp_try_extend_data_file(fil_space_t* space, fsp_header_t* header, mtr_t* mtr)
/* We ignore any fragments of a full megabyte when storing the size
to the space header */
- space->size_in_header = ut_calc_align_down(
+ space->size_in_header = ut_2pow_round(
space->size, (1024 * 1024) / page_size.physical());
mlog_write_ulint(
diff --git a/storage/innobase/include/mem0mem.h b/storage/innobase/include/mem0mem.h
index 17a388ab68c..658919f27c7 100644
--- a/storage/innobase/include/mem0mem.h
+++ b/storage/innobase/include/mem0mem.h
@@ -1,7 +1,7 @@
/*****************************************************************************
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
-Copyright (c) 2017, 2018, MariaDB Corporation.
+Copyright (c) 2017, 2019, 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
@@ -77,7 +77,7 @@ is the maximum size for a single allocated buffer: */
/** Space needed when allocating for a user a field of length N.
The space is allocated only in multiples of UNIV_MEM_ALIGNMENT. */
-#define MEM_SPACE_NEEDED(N) ut_calc_align((N), UNIV_MEM_ALIGNMENT)
+#define MEM_SPACE_NEEDED(N) UT_CALC_ALIGN((N), UNIV_MEM_ALIGNMENT)
#ifdef UNIV_DEBUG
/** Macro for memory heap creation.
@@ -342,8 +342,8 @@ struct mem_block_info_t {
#define MEM_FREED_BLOCK_MAGIC_N 547711122
/* Header size for a memory heap block */
-#define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\
- UNIV_MEM_ALIGNMENT)
+#define MEM_BLOCK_HEADER_SIZE UT_CALC_ALIGN(sizeof(mem_block_info_t),\
+ UNIV_MEM_ALIGNMENT)
#include "mem0mem.ic"
#endif
diff --git a/storage/innobase/include/ut0ut.h b/storage/innobase/include/ut0ut.h
index 4e9c2599933..81f4a9b8b23 100644
--- a/storage/innobase/include/ut0ut.h
+++ b/storage/innobase/include/ut0ut.h
@@ -1,6 +1,7 @@
/*****************************************************************************
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
+Copyright (c) 2019, 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
@@ -165,26 +166,23 @@ Calculates fast the remainder of n/m when m is a power of two.
@param n in: numerator
@param m in: denominator, must be a power of two
@return the remainder of n/m */
-#define ut_2pow_remainder(n, m) ((n) & ((m) - 1))
+template <typename T> inline T ut_2pow_remainder(T n, T m){return n & (m - 1);}
/*************************************************************//**
Calculates the biggest multiple of m that is not bigger than n
when m is a power of two. In other words, rounds n down to m * k.
@param n in: number to round down
@param m in: alignment, must be a power of two
@return n rounded down to the biggest possible integer multiple of m */
-#define ut_2pow_round(n, m) ((n) & ~((m) - 1))
-/** Align a number down to a multiple of a power of two.
-@param n in: number to round down
-@param m in: alignment, must be a power of two
-@return n rounded down to the biggest possible integer multiple of m */
-#define ut_calc_align_down(n, m) ut_2pow_round(n, m)
+template <typename T> inline T ut_2pow_round(T n, T m) { return n & ~(m - 1); }
/********************************************************//**
Calculates the smallest multiple of m that is not smaller than n
when m is a power of two. In other words, rounds n up to m * k.
@param n in: number to round up
@param m in: alignment, must be a power of two
@return n rounded up to the smallest possible integer multiple of m */
-#define ut_calc_align(n, m) (((n) + ((m) - 1)) & ~((m) - 1))
+#define UT_CALC_ALIGN(n, m) ((n + m - 1) & ~(m - 1))
+template <typename T> inline T ut_calc_align(T n, T m)
+{ return UT_CALC_ALIGN(n, m); }
/*************************************************************//**
Calculates fast the 2-logarithm of a number, rounded upward to an
diff --git a/storage/innobase/log/log0log.cc b/storage/innobase/log/log0log.cc
index 516d92164c4..3a54af8e3c2 100644
--- a/storage/innobase/log/log0log.cc
+++ b/storage/innobase/log/log0log.cc
@@ -203,10 +203,8 @@ log_buffer_extend(
log_sys->is_extending = true;
- while (ut_calc_align_down(log_sys->buf_free,
- OS_FILE_LOG_BLOCK_SIZE)
- != ut_calc_align_down(log_sys->buf_next_to_write,
- OS_FILE_LOG_BLOCK_SIZE)) {
+ while ((log_sys->buf_free ^ log_sys->buf_next_to_write)
+ & (OS_FILE_LOG_BLOCK_SIZE - 1)) {
/* Buffer might have >1 blocks to write still. */
log_mutex_exit_all();
@@ -215,9 +213,8 @@ log_buffer_extend(
log_mutex_enter_all();
}
- move_start = ut_calc_align_down(
- log_sys->buf_free,
- OS_FILE_LOG_BLOCK_SIZE);
+ move_start = ut_2pow_round(log_sys->buf_free,
+ ulint(OS_FILE_LOG_BLOCK_SIZE));
move_end = log_sys->buf_free;
/* store the last log block in buffer */
@@ -1079,8 +1076,8 @@ log_buffer_switch()
ut_ad(log_write_mutex_own());
const byte* old_buf = log_sys->buf;
- ulint area_end = ut_calc_align(log_sys->buf_free,
- OS_FILE_LOG_BLOCK_SIZE);
+ ulint area_end = ut_calc_align(
+ log_sys->buf_free, ulint(OS_FILE_LOG_BLOCK_SIZE));
if (log_sys->first_in_use) {
log_sys->first_in_use = false;
@@ -1215,8 +1212,9 @@ loop:
start_offset = log_sys->buf_next_to_write;
end_offset = log_sys->buf_free;
- area_start = ut_calc_align_down(start_offset, OS_FILE_LOG_BLOCK_SIZE);
- area_end = ut_calc_align(end_offset, OS_FILE_LOG_BLOCK_SIZE);
+ area_start = ut_2pow_round(start_offset,
+ ulint(OS_FILE_LOG_BLOCK_SIZE));
+ area_end = ut_calc_align(end_offset, ulint(OS_FILE_LOG_BLOCK_SIZE));
ut_ad(area_end - area_start > 0);
diff --git a/storage/innobase/os/os0proc.cc b/storage/innobase/os/os0proc.cc
index 685bcef25db..b3202992af0 100644
--- a/storage/innobase/os/os0proc.cc
+++ b/storage/innobase/os/os0proc.cc
@@ -115,10 +115,8 @@ skip:
/* Align block size to system page size */
ut_ad(ut_is_2pow(system_info.dwPageSize));
- /* system_info.dwPageSize is only 32-bit. Casting to ulint is required
- on 64-bit Windows. */
- size = *n = ut_2pow_round(*n + (system_info.dwPageSize - 1),
- (ulint) system_info.dwPageSize);
+ size = *n = ut_2pow_round<ulint>(*n + (system_info.dwPageSize - 1),
+ system_info.dwPageSize);
ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
if (!ptr) {