MDEV-22441 SCOPE_VALUE macro for temporary values

- Needless engaged_ removed;
  - SCOPE_VALUE, SCOPE_SET, SCOPE_CLEAR macros for neater declaration;
  - IF_CLASS / IF_NOT_CLASS SFINAE checkers to pass arg by value or
    reference;
  - inline keyword;
  - couple of refactorings of temporary free_list.
This commit is contained in:
Aleksey Midenkov 2025-01-13 15:40:58 +03:00
parent 52dd489515
commit d8adc52863
4 changed files with 76 additions and 34 deletions

View file

@ -32,6 +32,11 @@ public:
{
}
template <typename F>
scope_exit(F &&f, bool engaged) : function_(std::forward<F>(f)), engaged_(engaged)
{
}
scope_exit(scope_exit &&rhs)
: function_(std::move(rhs.function_)), engaged_(rhs.engaged_)
{
@ -43,6 +48,7 @@ public:
scope_exit &operator=(const scope_exit &)= delete;
void release() { engaged_= false; }
void engage() { DBUG_ASSERT(!engaged_); engaged_= true; }
~scope_exit()
{
@ -58,38 +64,51 @@ private:
} // end namespace detail
template <typename Callable>
detail::scope_exit<typename std::decay<Callable>::type>
make_scope_exit(Callable &&f)
inline
::detail::scope_exit<typename std::decay<Callable>::type>
make_scope_exit(Callable &&f, bool engaged= true)
{
return detail::scope_exit<typename std::decay<Callable>::type>(
std::forward<Callable>(f));
return ::detail::scope_exit<typename std::decay<Callable>::type>(
std::forward<Callable>(f), engaged);
}
#define CONCAT_IMPL(x, y) x##y
#define CONCAT(x, y) CONCAT_IMPL(x, y)
#define ANONYMOUS_VARIABLE CONCAT(_anonymous_variable, __LINE__)
#define SCOPE_EXIT auto ANONYMOUS_VARIABLE= make_scope_exit
#define IF_CLASS(C) typename std::enable_if<std::is_class<C>::value>::type
#define IF_NOT_CLASS(C) typename std::enable_if<!std::is_class<C>::value>::type
namespace detail
{
template <typename T> class Scope_value
template <typename T>
class Scope_value
{
public:
// Use SFINAE for passing structs by reference and plain types by value.
// This ctor is defined only if T is a class or struct:
template <typename U = T, typename = IF_CLASS(U)>
Scope_value(T &variable, const T &scope_value)
: variable_(variable), saved_value_(variable)
: variable_(&variable), saved_value_(variable)
{
variable= scope_value;
}
// This ctor is defined only if T is NOT a class or struct:
template <typename U = T, typename = IF_NOT_CLASS(U)>
Scope_value(T &variable, const T scope_value)
: variable_(&variable), saved_value_(variable)
{
variable= scope_value;
}
Scope_value(Scope_value &&rhs)
: variable_(rhs.variable_), saved_value_(rhs.saved_value_),
engaged_(rhs.engaged_)
: variable_(rhs.variable_), saved_value_(rhs.saved_value_)
{
rhs.engaged_= false;
rhs.variable_= NULL;
}
Scope_value(const Scope_value &)= delete;
@ -98,22 +117,50 @@ public:
~Scope_value()
{
if (engaged_)
variable_= saved_value_;
if (variable_)
*variable_= saved_value_;
}
private:
T &variable_;
T *variable_;
T saved_value_;
bool engaged_= true;
};
} // namespace detail
// Use like this:
// auto _= make_scope_value(var, tmp_value);
template <typename T>
detail::Scope_value<T> make_scope_value(T &variable, const T &scope_value)
template <typename T, typename = IF_CLASS(T)>
inline
::detail::Scope_value<T> make_scope_value(T &variable, const T &scope_value)
{
return detail::Scope_value<T>(variable, scope_value);
return ::detail::Scope_value<T>(variable, scope_value);
}
template <typename T, typename = IF_NOT_CLASS(T)>
inline
::detail::Scope_value<T> make_scope_value(T &variable, T scope_value)
{
return ::detail::Scope_value<T>(variable, scope_value);
}
/*
Note: perfect forwarding version can not pass const:
template <typename T, typename U>
inline
detail::Scope_value<T> make_scope_value(T &variable, U &&scope_value)
{
return detail::Scope_value<T>(variable, std::forward<U>(scope_value));
}
as `const U &&` fails with error `expects an rvalue for 2nd argument`. That
happens because const U && is treated as rvalue only (this is the exact syntax
for declaring rvalues).
*/
#define SCOPE_VALUE auto ANONYMOUS_VARIABLE= make_scope_value
#define SCOPE_SET(VAR, MASK) auto ANONYMOUS_VARIABLE= make_scope_value(VAR, VAR | MASK)
#define SCOPE_CLEAR(VAR, MASK) auto ANONYMOUS_VARIABLE= make_scope_value(VAR, VAR & ~MASK)

View file

@ -2555,9 +2555,7 @@ static bool acl_load(THD *thd, const Grant_tables& tables)
char tmp_name[SAFE_NAME_LEN+1];
DBUG_ENTER("acl_load");
auto _= make_scope_value(thd->variables.sql_mode,
thd->variables.sql_mode &
~MODE_PAD_CHAR_TO_FULL_LENGTH);
SCOPE_CLEAR(thd->variables.sql_mode, MODE_PAD_CHAR_TO_FULL_LENGTH);
grant_version++; /* Privileges updated */

View file

@ -2993,8 +2993,8 @@ void mysql_sql_stmt_execute_immediate(THD *thd)
DBUG_VOID_RETURN; // out of memory
// See comments on thd->free_list in mysql_sql_stmt_execute()
Item *free_list_backup= thd->free_list;
thd->free_list= NULL;
SCOPE_VALUE(thd->free_list, (Item *) NULL);
SCOPE_EXIT([thd]() mutable { thd->free_items(); });
/*
Make sure we call Prepared_statement::execute_immediate()
with an empty THD::change_list. It can be non empty as the above
@ -3017,8 +3017,6 @@ void mysql_sql_stmt_execute_immediate(THD *thd)
Item_change_list_savepoint change_list_savepoint(thd);
(void) stmt->execute_immediate(query.str, (uint) query.length);
change_list_savepoint.rollback(thd);
thd->free_items();
thd->free_list= free_list_backup;
/*
stmt->execute_immediately() sets thd->query_string with the executed
@ -3578,8 +3576,13 @@ void mysql_sql_stmt_execute(THD *thd)
so they don't get freed in case of re-prepare.
See MDEV-10702 Crash in SET STATEMENT FOR EXECUTE
*/
Item *free_list_backup= thd->free_list;
thd->free_list= NULL; // Hide the external (e.g. "SET STATEMENT") Items
/*
Hide and restore at scope exit the "external" (e.g. "SET STATEMENT") Item list.
It will be freed normaly in THD::cleanup_after_query().
*/
SCOPE_VALUE(thd->free_list, (Item *) NULL);
// Free items created by execute_loop() at scope exit
SCOPE_EXIT([thd]() mutable { thd->free_items(); });
/*
Make sure we call Prepared_statement::execute_loop() with an empty
THD::change_list. It can be non-empty because the above
@ -3603,12 +3606,6 @@ void mysql_sql_stmt_execute(THD *thd)
(void) stmt->execute_loop(&expanded_query, FALSE, NULL, NULL);
change_list_savepoint.rollback(thd);
thd->free_items(); // Free items created by execute_loop()
/*
Now restore the "external" (e.g. "SET STATEMENT") Item list.
It will be freed normaly in THD::cleanup_after_query().
*/
thd->free_list= free_list_backup;
stmt->lex->restore_set_statement_var();
DBUG_VOID_RETURN;

View file

@ -6438,7 +6438,7 @@ bool store_schema_params(THD *thd, TABLE *table, TABLE *proc_table,
{
Field *field;
LEX_CSTRING tmp_string;
auto _= make_scope_value(thd->variables.sql_mode, sql_mode);
SCOPE_VALUE(thd->variables.sql_mode, sql_mode);
if (sph->type() == SP_TYPE_FUNCTION)
{