Debian Codegraph @

storage/innobase/include/detail/ut/large_page_alloc-win.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2021, 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
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"
41 #include "storage/innobase/include/detail/ut/helper.h"
42 #include "storage/innobase/include/ut0log.h"
43
44 extern const size_t large_page_default_size;
45
46 namespace ut {
47 namespace 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 */
54 inline 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),
57 large_page_default_size);
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 */
75 inline 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 */
93 inline size_t large_page_size() { return GetLargePageMinimum(); }
94
95 } // namespace detail
96 } // namespace ut
97
98 #endif