MySQL 9.0.0
Source Code Documentation
allocator_traits.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/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
40namespace ut {
41namespace detail {
42
43#if defined(_WIN32) && defined(MYSQL_SERVER)
44/** Wrapper functions for using jemalloc on Windows.
45If jemalloc.dll is available and its use is enabled, the
46init_malloc_pointers function will set up the pfn_malloc, pfn_calloc,
47pfn_realloc and pfn_free function pointers to point to the corresponding
48jemalloc functions. Otherwise they will point to the standard MSVC library
49implementations of the malloc, calloc etc. */
50
51inline 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}
56inline 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}
61inline 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}
66inline 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
72inline void *malloc(size_t nbytes) { return std::malloc(nbytes); }
73inline void *calloc(size_t nbytes) { return std::calloc(1, nbytes); }
74inline void *realloc(void *ptr, size_t nbytes) {
75 return std::realloc(ptr, nbytes);
76}
77inline void free(void *ptr) { std::free(ptr); }
78#endif /* _WIN32 && MYSQL_SERVER */
79
80/** Simple allocator traits. */
81template <bool Pfs_instrumented>
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.*/
88struct 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
Details for dynamically loading and using jemalloc.dll on Windows.
#define malloc(A)
Definition: lexyy.cc:914
#define realloc(P, A)
Definition: lexyy.cc:916
#define free(A)
Definition: lexyy.cc:915
Definition: ut0tuple.h:57
size_t size(const char *const c)
Definition: base64.h:46
void * realloc(void *ptr, size_t nbytes)
Definition: allocator_traits.h:74
void * malloc(size_t nbytes)
Definition: allocator_traits.h:72
void free(void *ptr)
Definition: allocator_traits.h:77
void * calloc(size_t nbytes)
Definition: allocator_traits.h:73
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
Simple wrapping type around malloc, calloc and friends.
Definition: allocator_traits.h:88
static void * malloc(size_t nbytes)
Definition: allocator_traits.h:89
static void * alloc(size_t size)
Definition: allocator_traits.h:94
static void * calloc(size_t nbytes)
Definition: allocator_traits.h:91
static void free(void *ptr)
Definition: allocator_traits.h:105
static void * realloc(void *ptr, size_t nbytes)
Definition: allocator_traits.h:101
Simple allocator traits.
Definition: allocator_traits.h:82
static constexpr auto is_pfs_instrumented_v
Definition: allocator_traits.h:84