MySQL 9.0.0
Source Code Documentation
sql_array.h
Go to the documentation of this file.
1#ifndef SQL_ARRAY_INCLUDED
2#define SQL_ARRAY_INCLUDED
3
4/* Copyright (c) 2005, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <assert.h>
28#include <array>
29
30#include "my_alloc.h"
31
32/**
33 A wrapper class which provides array bounds checking.
34 We do *not* own the array, we simply have a pointer to the first element,
35 and a length.
36
37 @remark
38 We want the compiler-generated versions of:
39 - the copy CTOR (memberwise initialization)
40 - the assignment operator (memberwise assignment)
41
42 This is roughly analogous to C++20's std::span.
43
44 @tparam Element_type The type of the elements of the container.
45 */
46template <typename Element_type>
48 public:
49 // Convenience typedef, same typedef name as std::vector
50 typedef Element_type value_type;
51
53
54 Bounds_checked_array(Element_type *el, size_t size_arg)
55 : m_array(el), m_size(size_arg) {}
56
57 // NOTE: non-const reference intentional; mirrors std::span's constructor.
58 template <class T, size_t N>
59 explicit Bounds_checked_array(std::array<T, N> &arr)
60 : m_array(arr.data()), m_size(arr.size()) {}
61
62 // Not a constructor because it does something else from the other
63 // constructors (allocates new memory instead of wrapping existing memory),
64 // and also because nullptr for the first argument be ambiguous. The latter
65 // could be solved with an explicit nullptr_t overload, though.
66 //
67 // The elements of the array are initialized with value initialization. For
68 // primitive types, like int and pointers, this means the elements will be set
69 // to the equivalent of 0 (or false or nullptr).
71 return {mem_root->ArrayAlloc<Element_type>(size), size};
72 }
73
74 /// Make a copy of '*this'. Allocate memory for m_array on 'mem_root'.
76 if (data() == nullptr) {
77 return {};
78 } else {
80 if (duplicate.m_array != nullptr) {
81 std::copy(cbegin(), cend(), duplicate.begin());
82 }
83 return duplicate;
84 }
85 }
86
87 void reset() {
88 m_array = nullptr;
89 m_size = 0;
90 }
91
92 void reset(Element_type *array, size_t size) {
93 m_array = array;
94 m_size = size;
95 }
96
97 /**
98 Set a new bound on the array. Does not resize the underlying
99 array, so the new size must be smaller than or equal to the
100 current size.
101 */
102 void resize(size_t new_size) {
103 assert(new_size <= m_size);
104 m_size = new_size;
105 }
106
107 /**
108 Like resize(), but returns a new view of the array without modifying
109 this one.
110 */
111 Bounds_checked_array prefix(size_t new_size) {
112 assert(new_size <= m_size);
113 return Bounds_checked_array(m_array, new_size);
114 }
115
116 Element_type *data() { return m_array; }
117
118 const Element_type *data() const { return m_array; }
119
120 Element_type &operator[](size_t n) {
121 assert(n < m_size);
122 return m_array[n];
123 }
124
125 const Element_type &operator[](size_t n) const {
126 assert(n < m_size);
127 return m_array[n];
128 }
129
130 typedef Element_type *iterator;
131 typedef const Element_type *const_iterator;
132
133 /// begin : Returns a pointer to the first element in the array.
134 iterator begin() { return m_array; }
135 /// end : Returns a pointer to the past-the-end element in the array.
136 iterator end() { return m_array + size(); }
137
138 /// begin : Returns a pointer to the first element in the array.
139 const_iterator begin() const { return m_array; }
140 /// end : Returns a pointer to the past-the-end element in the array.
141 const_iterator end() const { return m_array + size(); }
142
143 /// Returns a pointer to the first element in the array.
144 const_iterator cbegin() const { return m_array; }
145 /// Returns a pointer to the past-the-end element in the array.
146 const_iterator cend() const { return m_array + size(); }
147
149 assert(m_size > 0);
151 }
152
153 size_t element_size() const { return sizeof(Element_type); }
154 size_t size() const { return m_size; }
155 bool empty() const { return m_size == 0; }
156
157 bool is_null() const { return m_array == nullptr; }
158
159 void pop_front() {
160 assert(m_size > 0);
161 m_array += 1;
162 m_size -= 1;
163 }
164
165 Element_type *array() const { return m_array; }
166
168 return m_array == rhs.m_array && m_size == rhs.m_size;
169 }
171 return m_array != rhs.m_array || m_size != rhs.m_size;
172 }
173
174 private:
175 Element_type *m_array;
176 size_t m_size;
177};
178
179template <typename Element_type>
182}
183
184#endif /* SQL_ARRAY_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
A wrapper class which provides array bounds checking.
Definition: sql_array.h:47
Element_type * data()
Definition: sql_array.h:116
const Element_type * data() const
Definition: sql_array.h:118
Bounds_checked_array(std::array< T, N > &arr)
Definition: sql_array.h:59
Element_type & operator[](size_t n)
Definition: sql_array.h:120
Element_type value_type
Definition: sql_array.h:50
Element_type * m_array
Definition: sql_array.h:175
bool operator!=(const Bounds_checked_array< Element_type > &rhs) const
Definition: sql_array.h:170
static Bounds_checked_array Alloc(MEM_ROOT *mem_root, size_t size)
Definition: sql_array.h:70
bool is_null() const
Definition: sql_array.h:157
Element_type * array() const
Definition: sql_array.h:165
void resize(size_t new_size)
Set a new bound on the array.
Definition: sql_array.h:102
iterator begin()
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:134
iterator end()
end : Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:136
Bounds_checked_array(Element_type *el, size_t size_arg)
Definition: sql_array.h:54
const_iterator end() const
end : Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:141
const_iterator cbegin() const
Returns a pointer to the first element in the array.
Definition: sql_array.h:144
size_t size() const
Definition: sql_array.h:154
size_t element_size() const
Definition: sql_array.h:153
const Element_type & operator[](size_t n) const
Definition: sql_array.h:125
void pop_front()
Definition: sql_array.h:159
const_iterator begin() const
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:139
Bounds_checked_array Clone(MEM_ROOT *mem_root) const
Make a copy of '*this'. Allocate memory for m_array on 'mem_root'.
Definition: sql_array.h:75
Bounds_checked_array()
Definition: sql_array.h:52
Bounds_checked_array without_back() const
Definition: sql_array.h:148
void reset()
Definition: sql_array.h:87
bool empty() const
Definition: sql_array.h:155
const Element_type * const_iterator
Definition: sql_array.h:131
Element_type * iterator
Definition: sql_array.h:130
size_t m_size
Definition: sql_array.h:176
void reset(Element_type *array, size_t size)
Definition: sql_array.h:92
bool operator==(const Bounds_checked_array< Element_type > &rhs) const
Definition: sql_array.h:167
Bounds_checked_array prefix(size_t new_size)
Like resize(), but returns a new view of the array without modifying this one.
Definition: sql_array.h:111
const_iterator cend() const
Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:146
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
const char * p
Definition: ctype-mb.cc:1225
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
void copy(Shards< COUNT > &dst, const Shards< COUNT > &src) noexcept
Copy the counters, overwrite destination.
Definition: ut0counter.h:354
Bounds_checked_array< Element_type > make_array(Element_type *p, size_t n)
Definition: sql_array.h:180
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
T * ArrayAlloc(size_t num, Args... args)
Allocate 'num' objects of type T, and initialize them to a default value that is created by passing t...
Definition: my_alloc.h:180
int n
Definition: xcom_base.cc:509