MySQL 9.1.0
Source Code Documentation
array_view.h
Go to the documentation of this file.
1// Copyright (c) 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 MYSQL_ABI_HELPERS_ARRAY_VIEW_H
25#define MYSQL_ABI_HELPERS_ARRAY_VIEW_H
26
27/// @file
28/// Experimental API header
29
30#include <cstdlib> // std::size_t
31#include <type_traits> // std::is_trivial_v
32#include "mysql/abi_helpers/detail/array_base.h" // Array_base
33#ifdef MYSQL_SERVER
34#include "my_sys.h" // MY_WME
35#include "mysql/psi/psi_memory.h" // PSI_memory_key
36#include "mysql/service_mysql_alloc.h" // my_malloc
37#endif
38
39/// @addtogroup GroupLibsMysqlAbiHelpers
40/// @{
41
43
44/// Ownership-agnostic array class, which is both _trivial_ and
45/// _standard-layout_.
46///
47/// This holds a length and a raw pointer to an array. The user has to manage
48/// ownership of the memory as needed.
49///
50/// @tparam Element_tp The type of elements in the array.
51template <class Element_tp>
52 requires requires { std::is_trivial_v<Element_tp>; }
53class Array_view : public detail::Array_base<Element_tp, Element_tp *> {
54 private:
57
58 public:
59 using Element_t = Element_tp;
60 Array_view() = default;
61
62 /// Construct a view over the given array
63 Array_view(Element_t *array, std::size_t size)
64 : Base_t::m_size(size), Base_t::m_data(array) {}
65
66 void assign(Element_t *array, std::size_t size) {
67 this->m_data = array;
68 this->m_size = (int32_t)size;
69 }
70
71#ifdef MYSQL_SERVER
72 /// Create a new array of the given type, replacing the existing one without
73 /// deallocating it.
74 ///
75 /// @note This, and @c free below, are *only* enabled in the MySQL server.
76 /// This ensures that a component does not try to free memory allocated by the
77 /// server or vice versa, which is disallowed on some platforms.
78 ///
79 /// @param size The number of elements.
80 ///
81 /// @param key The instrumentation key to track the allocation.
82 void allocate(std::size_t size, PSI_memory_key key) {
84 size);
85 }
86
87 /// Free the array, assuming it was previously allocated using @c allocate (or
88 /// `my_malloc`).
89 ///
90 /// This, and @c allocate above, are *only* enabled in the MySQL server. See
91 /// above for justification.
92 void free() {
93 my_free(this->data());
94 clear();
95 }
96#endif
97
98 void clear() {
99 this->m_data = nullptr;
100 this->m_size = 0;
101 }
102};
103
104static_assert(std::is_trivial_v<Array_view<int>>);
105static_assert(std::is_trivially_default_constructible_v<Array_view<int>>);
106static_assert(std::is_trivially_destructible_v<Array_view<int>>);
107static_assert(std::is_trivially_copy_constructible_v<Array_view<int>>);
108static_assert(std::is_trivially_move_constructible_v<Array_view<int>>);
109static_assert(std::is_trivially_copy_assignable_v<Array_view<int>>);
110static_assert(std::is_trivially_move_assignable_v<Array_view<int>>);
111static_assert(std::is_standard_layout_v<Array_view<int>>);
112
113} // namespace mysql::abi_helpers
114
115// addtogroup GroupLibsMysqlAbiHelpers
116/// @}
117
118#endif // ifndef MYSQL_ABI_HELPERS_ARRAY_VIEW_H
Experimental API header.
Ownership-agnostic array class, which is both trivial and standard-layout.
Definition: array_view.h:53
void clear()
Definition: array_view.h:98
void assign(Element_t *array, std::size_t size)
Definition: array_view.h:66
void free()
Free the array, assuming it was previously allocated using allocate (or my_malloc).
Definition: array_view.h:92
void allocate(std::size_t size, PSI_memory_key key)
Create a new array of the given type, replacing the existing one without deallocating it.
Definition: array_view.h:82
Array_view(Element_t *array, std::size_t size)
Construct a view over the given array.
Definition: array_view.h:63
Element_tp Element_t
Definition: array_view.h:59
Base class for specific implementations of standard-layout classes for arrays.
Definition: array_base.h:51
int32_t m_size
Number of elements in the array.
Definition: array_base.h:115
std::size_t size() const
Definition: array_base.h:54
Element_tp * m_data
Array data.
Definition: array_base.h:118
#define MY_WME
Definition: my_sys.h:130
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
#define MYF(v)
Definition: my_inttypes.h:97
void * my_malloc(PSI_memory_key key, size_t size, int flags)
Allocates size bytes of memory.
Definition: my_memory.cc:57
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:81
Common header for many mysys elements.
Definition: array_view.h:42
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:60