MySQL 8.3.0
Source Code Documentation
large_page_alloc-win.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2021, 2023, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/detail/ut/large_page_alloc-win.h
28 Windows-specific implementation bits and pieces for large (huge) page
29 allocations. */
30
31#ifndef detail_ut_large_page_alloc_win_h
32#define detail_ut_large_page_alloc_win_h
33
34#include <windows.h>
35// _must_ go after windows.h, this comment makes clang-format to preserve
36// the include order
37#include <memoryapi.h>
38
39#include "mysqld_error.h"
42
43extern const size_t large_page_default_size;
44
45namespace ut {
46namespace detail {
47
48/** Allocates memory backed by large (huge) pages.
49
50 @param[in] n_bytes Size of storage (in bytes) requested to be allocated.
51 @return Pointer to the allocated storage. nullptr if allocation failed.
52*/
53inline void *large_page_aligned_alloc(size_t n_bytes) {
54 // VirtualAlloc requires for n_bytes to be a multiple of large-page size
55 size_t n_bytes_rounded = pow2_round(n_bytes + (large_page_default_size - 1),
57 void *ptr =
58 VirtualAlloc(nullptr, n_bytes_rounded,
59 MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE);
60 if (unlikely(!ptr)) {
61 ib::log_warn(ER_IB_MSG_856)
62 << "large_page_aligned_alloc VirtualAlloc(" << n_bytes_rounded
63 << " bytes) failed; Windows error " << GetLastError();
64 }
65 return ptr;
66}
67
68/** Releases memory backed by large (huge) pages.
69
70 @param[in] ptr Pointer to large (huge) page aligned storage.
71 @param[in] n_bytes Size of the storage.
72 @return Returns true if releasing the large (huge) page succeeded.
73 */
74inline bool large_page_aligned_free(void *ptr, size_t n_bytes) {
75 if (unlikely(!ptr)) return false;
76 auto ret = VirtualFree(ptr, 0, MEM_RELEASE);
77 (void)n_bytes;
78 if (unlikely(ret == 0)) {
79 ib::log_error(ER_IB_MSG_858)
80 << "large_page_aligned_free VirtualFree(" << ptr
81 << ") failed;"
82 " Windows error "
83 << GetLastError();
84 }
85 return ret != 0;
86}
87
88/** Queries the current size of large (huge) pages on running system.
89
90 @return Large (huge) page size in bytes.
91*/
92inline size_t large_page_size() { return GetLargePageMinimum(); }
93
94} // namespace detail
95} // namespace ut
96
97#endif
Small helper functions.
const size_t large_page_default_size
System-default huge (large) page setting.
Definition: ut0new.cc:39
constexpr bool unlikely(bool expr)
Definition: my_compiler.h:57
Definition: ut0tuple.h:56
static auto log_warn()
Definition: ut0log.h:451
static auto log_error()
Definition: ut0log.h:452
constexpr size_t pow2_round(size_t n, size_t m)
Calculates the biggest multiple of m that is not bigger than n when m is a power of two.
Definition: helper.h:56
void * large_page_aligned_alloc(size_t n_bytes)
Allocates memory backed by large (huge) pages.
Definition: large_page_alloc-linux.h:51
bool large_page_aligned_free(void *ptr, size_t n_bytes)
Releases memory backed by large (huge) pages.
Definition: large_page_alloc-linux.h:74
size_t large_page_size()
Queries the current size of large (huge) pages on running system.
Definition: large_page_alloc-linux.h:94
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:47
Base of InnoDB utilities.