Debian Codegraph @

storage/innobase/include/detail/ut/allocator_traits.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/allocator_traits.h
29 Simple allocator traits.
30 */
31
32 #ifndef detail_ut_allocator_traits_h
33 #define detail_ut_allocator_traits_h
34
35 #if defined(_WIN32) && defined(MYSQL_SERVER)
36 #include <mutex>
37 #include "jemalloc_win.h"
38 #endif /* _WIN32 && MYSQL_SERVER */
39
40 namespace ut {
41 namespace detail {
42
43 #if defined(_WIN32) && defined(MYSQL_SERVER)
44 /** Wrapper functions for using jemalloc on Windows.
45 If jemalloc.dll is available and its use is enabled, the
46 init_malloc_pointers function will set up the pfn_malloc, pfn_calloc,
47 pfn_realloc and pfn_free function pointers to point to the corresponding
48 jemalloc functions. Otherwise they will point to the standard MSVC library
49 implementations of the malloc, calloc etc. */
50
51 inline void *malloc(size_t nbytes) {
52 std::call_once(mysys::detail::init_malloc_pointers_flag,
53 mysys::detail::init_malloc_pointers);
54 return mysys::detail::pfn_malloc(nbytes);
55 }
56 inline void *calloc(size_t nbytes) {
57 std::call_once(mysys::detail::init_malloc_pointers_flag,
58 mysys::detail::init_malloc_pointers);
59 return mysys::detail::pfn_calloc(1, nbytes);
60 }
61 inline void *realloc(void *ptr, size_t nbytes) {
62 std::call_once(mysys::detail::init_malloc_pointers_flag,
63 mysys::detail::init_malloc_pointers);
64 return mysys::detail::pfn_realloc(ptr, nbytes);
65 }
66 inline void free(void *ptr) {
67 std::call_once(mysys::detail::init_malloc_pointers_flag,
68 mysys::detail::init_malloc_pointers);
69 mysys::detail::pfn_free(ptr);
70 }
71 #else
72 inline void *malloc(size_t nbytes) { return std::malloc(nbytes); }
73 inline void *calloc(size_t nbytes) { return std::calloc(1, nbytes); }
74 inline void *realloc(void *ptr, size_t nbytes) {
75 return std::realloc(ptr, nbytes);
76 }
77 inline void free(void *ptr) { std::free(ptr); }
78 #endif /* _WIN32 && MYSQL_SERVER */
79
80 /** Simple allocator traits. */
81 template <bool Pfs_instrumented>
82 struct allocator_traits {
83 // Is allocator PFS instrumented or not
84 static constexpr auto is_pfs_instrumented_v = Pfs_instrumented;
85 };
86
87 /** Simple wrapping type around malloc, calloc and friends.*/
88 struct Alloc_fn {
89 static void *malloc(size_t nbytes) { return ut::detail::malloc(nbytes); }
90
91 static void *calloc(size_t nbytes) { return ut::detail::calloc(nbytes); }
92
93 template <bool Zero_initialized>
94 static void *alloc(size_t size) {
95 if constexpr (Zero_initialized)
96 return Alloc_fn::calloc(size);
97 else
98 return Alloc_fn::malloc(size);
99 }
100
101 static void *realloc(void *ptr, size_t nbytes) {
102 return ut::detail::realloc(ptr, nbytes);
103 }
104
105 static void free(void *ptr) { ut::detail::free(ptr); }
106 };
107
108 } // namespace detail
109 } // namespace ut
110
111 #endif