MySQL 8.0.40
Source Code Documentation
pfs_std_allocator.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef PFS_STD_ALLOCATOR_H
25#define PFS_STD_ALLOCATOR_H
26
27#include "my_config.h"
28
29#include <memory>
30
31#include "my_compiler.h"
32#include "my_inttypes.h"
34
36
37template <class T>
39 public:
40 typedef T value_type;
41
43
44 template <class U>
45 constexpr PFS_std_allocator(const PFS_std_allocator<U> &u) noexcept
46 : m_klass(u.get_class()) {}
47
48 [[nodiscard]] T *allocate(std::size_t n) {
49 if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
50 throw std::bad_array_new_length();
51
52 const size_t size = n * sizeof(T);
53 void *mem = pfs_malloc(m_klass, size, MYF(0));
54 if (mem == nullptr) {
55 throw std::bad_alloc();
56 }
57 return static_cast<T *>(mem);
58 }
59
60 void deallocate(T *p, std::size_t n) noexcept {
61 const size_t size = n * sizeof(T);
62 pfs_free(m_klass, size, p);
63 }
64
66
67 private:
69};
70
71template <class T, class U>
73 return (t.m_klass == u.m_klass);
74}
75
76template <class T, class U>
78 return (t.m_klass != u.m_klass);
79}
80
81#endif
const char * p
Definition: ctype-mb.cc:1237
Header for compiler-dependent features.
Some integer typedefs for easier portability.
#define MYF(v)
Definition: my_inttypes.h:97
void * pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf flags)
Memory allocation for the performance schema.
Definition: pfs_global.cc:60
void pfs_free(PFS_builtin_memory_class *klass, size_t size, void *ptr)
Free memory allocated with.
Definition: pfs_global.cc:107
Miscellaneous global dependencies (declarations).
bool operator==(const PFS_std_allocator< T > &t, const PFS_std_allocator< U > &u)
Definition: pfs_std_allocator.h:72
bool operator!=(const PFS_std_allocator< T > &t, const PFS_std_allocator< U > &u)
Definition: pfs_std_allocator.h:77
static MEM_ROOT mem
Definition: sql_servers.cc:99
Definition: pfs_builtin_memory.h:39
Definition: pfs_std_allocator.h:38
PFS_builtin_memory_class * m_klass
Definition: pfs_std_allocator.h:68
PFS_std_allocator(PFS_builtin_memory_class *klass)
Definition: pfs_std_allocator.h:42
void deallocate(T *p, std::size_t n) noexcept
Definition: pfs_std_allocator.h:60
T value_type
Definition: pfs_std_allocator.h:40
PFS_builtin_memory_class * get_class() const
Definition: pfs_std_allocator.h:65
T * allocate(std::size_t n)
Definition: pfs_std_allocator.h:48
constexpr PFS_std_allocator(const PFS_std_allocator< U > &u) noexcept
Definition: pfs_std_allocator.h:45
int n
Definition: xcom_base.cc:509