| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
#ifndef buf0block_hint_h |
| 28 |
#define buf0block_hint_h |
| 29 |
#include "buf0types.h" |
| 30 |
|
| 31 |
namespace buf { |
| 32 |
class Block_hint { |
| 33 |
public: |
| 34 |
/** Stores the pointer to the block, which is currently buffer-fixed. |
| 35 |
@param[in] block a pointer to a buffer-fixed block to be stored */ |
| 36 |
void store(buf_block_t *block); |
| 37 |
|
| 38 |
/** Clears currently stored pointer. */ |
| 39 |
void clear(); |
| 40 |
|
| 41 |
/** Executes given function with the block pointer which was previously stored |
| 42 |
or with nullptr if the pointer is no longer valid, was cleared or not stored. |
| 43 |
@param[in] f The function to be executed. It will be passed the pointer. |
| 44 |
If you wish to use the block pointer subsequently, you need to |
| 45 |
ensure you buffer-fix it before returning from f. |
| 46 |
@return the return value of f |
| 47 |
*/ |
| 48 |
template <typename F> |
| 49 |
auto run_with_hint(F &&f) { |
| 50 |
buffer_fix_block_if_still_valid(); |
| 51 |
/* m_block could be changed during f() call, so we use local variable to |
| 52 |
remember which block we need to unfix */ |
| 53 |
buf_block_t *buffer_fixed_block = m_block; |
| 54 |
auto res = f(buffer_fixed_block); |
| 55 |
buffer_unfix_block_if_needed(buffer_fixed_block); |
| 56 |
return res; |
| 57 |
} |
| 58 |
|
| 59 |
private: |
| 60 |
/** The block pointer stored by store(). */ |
| 61 |
buf_block_t *m_block{nullptr}; |
| 62 |
/** If m_block is non-null, the m_block->page.id at time it was stored. */ |
| 63 |
page_id_t m_page_id{0, 0}; |
| 64 |
|
| 65 |
/** A helper function which checks if m_block is not a dangling pointer and |
| 66 |
still points to block with page with m_page_id and if so, buffer-fixes it, |
| 67 |
otherwise clear()s it */ |
| 68 |
void buffer_fix_block_if_still_valid(); |
| 69 |
|
| 70 |
/** A helper function which decrements block->buf_fix_count if it's non-null |
| 71 |
@param[in] block A pointer to a block or nullptr */ |
| 72 |
static void buffer_unfix_block_if_needed(buf_block_t *block); |
| 73 |
}; |
| 74 |
|
| 75 |
} // namespace buf |
| 76 |
#endif /* buf0hint_h*/ |