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