Coverage Report

Created: 2026-03-31 07:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mysql-server/sql/malloc_allocator.h
Line
Count
Source
1
/* Copyright (c) 2014, 2025, 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 MALLOC_ALLOCATOR_INCLUDED
25
#define MALLOC_ALLOCATOR_INCLUDED
26
27
#include <assert.h>
28
#include <limits>
29
#include <new>
30
#include <utility>  // std::forward
31
32
#include "my_sys.h"
33
#include "mysql/service_mysql_alloc.h"
34
#include "sql/psi_memory_key.h"
35
36
/**
37
  Malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
38
39
  This allows for P_S instrumentation of memory allocation done by
40
  internally by STL container classes.
41
42
  Example usage:
43
  vector<int, Malloc_allocator<int>>
44
    v((Malloc_allocator<int>(PSI_NOT_INSTRUMENTED)));
45
46
  If the type is complicated, you can just write Malloc_allocator<>(psi_key)
47
  as a shorthand for Malloc_allocator<My_complicated_type>(psi_key), as all
48
  Malloc_allocator instances are implicitly convertible to each other
49
  and there is a default template parameter.
50
51
  @note allocate() throws std::bad_alloc() similarly to the default
52
  STL memory allocator. This is necessary - STL functions which allocates
53
  memory expects it. Otherwise these functions will try to use the memory,
54
  leading to segfaults if memory allocation was not successful.
55
56
  @note This allocator cannot be used for std::basic_string with RHEL 6/7
57
  because of this bug:
58
  https://bugzilla.redhat.com/show_bug.cgi?id=1546704
59
  "Define _GLIBCXX_USE_CXX11_ABI gets ignored by gcc in devtoolset-7"
60
*/
61
62
template <class T = void *>
63
class Malloc_allocator {
64
  // This cannot be const if we want to be able to swap.
65
  PSI_memory_key m_key;
66
67
 public:
68
  typedef T value_type;
69
  typedef size_t size_type;
70
  typedef ptrdiff_t difference_type;
71
72
  typedef T *pointer;
73
  typedef const T *const_pointer;
74
75
  typedef T &reference;
76
  typedef const T &const_reference;
77
78
  pointer address(reference r) const { return &r; }
79
  const_pointer address(const_reference r) const { return &r; }
80
81
2
  explicit Malloc_allocator(PSI_memory_key key) : m_key(key) {}
82
83
  template <class U>
84
  Malloc_allocator(const Malloc_allocator<U> &other [[maybe_unused]])
85
      : m_key(other.psi_key()) {}
86
87
  template <class U>
88
  Malloc_allocator &operator=(const Malloc_allocator<U> &other
89
                              [[maybe_unused]]) {
90
    assert(m_key == other.psi_key());  // Don't swap key.
91
  }
92
93
0
  pointer allocate(size_type n, const_pointer hint [[maybe_unused]] = nullptr) {
94
0
    if (n == 0) return nullptr;
95
0
    if (n > max_size()) throw std::bad_alloc();
96
97
0
    pointer p = static_cast<pointer>(
98
0
        my_malloc(m_key, n * sizeof(T), MYF(MY_WME | ME_FATALERROR)));
99
0
    if (p == nullptr) throw std::bad_alloc();
100
0
    return p;
101
0
  }
102
103
0
  void deallocate(pointer p, size_type) { my_free(p); }
104
105
  template <class U, class... Args>
106
0
  void construct(U *p, Args &&...args) {
107
0
    assert(p != nullptr);
108
0
    try {
109
0
      ::new ((void *)p) U(std::forward<Args>(args)...);
110
0
    } catch (...) {
111
0
      assert(false);  // Constructor should not throw an exception.
112
0
    }
113
0
  }
Unexecuted instantiation: my_file.cc:void Malloc_allocator<(anonymous namespace)::FileInfo>::construct<(anonymous namespace)::FileInfo>((anonymous namespace)::FileInfo*)
Unexecuted instantiation: my_file.cc:void Malloc_allocator<(anonymous namespace)::FileInfo>::construct<(anonymous namespace)::FileInfo, (anonymous namespace)::FileInfo>((anonymous namespace)::FileInfo*, (anonymous namespace)::FileInfo&&)
114
115
0
  void destroy(pointer p) {
116
0
    assert(p != nullptr);
117
0
    try {
118
0
      p->~T();
119
0
    } catch (...) {
120
0
      assert(false);  // Destructor should not throw an exception
121
0
    }
122
0
  }
123
124
0
  size_type max_size() const {
125
0
    return std::numeric_limits<size_t>::max() / sizeof(T);
126
0
  }
127
128
  template <class U>
129
  struct rebind {
130
    typedef Malloc_allocator<U> other;
131
  };
132
133
  PSI_memory_key psi_key() const { return m_key; }
134
};
135
136
template <class T>
137
bool operator==(const Malloc_allocator<T> &a1, const Malloc_allocator<T> &a2) {
138
  return a1.psi_key() == a2.psi_key();
139
}
140
141
template <class T>
142
bool operator!=(const Malloc_allocator<T> &a1, const Malloc_allocator<T> &a2) {
143
  return a1.psi_key() != a2.psi_key();
144
}
145
146
#endif  // MALLOC_ALLOCATOR_INCLUDED