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