MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
packet.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_PACKET_H
25#define MYSQL_ABI_HELPERS_PACKET_H
26
27/// @file
28/// Experimental API header
29
30#include <cstddef> // std::size_t
31#include <cstring> // std::memcpy
34#ifdef MYSQL_SERVER
35#include "my_sys.h" // MY_WME
36#include "mysql/psi/psi_memory.h" // PSI_memory_key
37#include "mysql/service_mysql_alloc.h" // my_malloc
38#endif
39
40/// @addtogroup GroupLibsMysqlAbiHelpers
41/// @{
42
43namespace mysql::abi_helpers {
44
45/// @brief Class to store a number of fields of heterogeneous types, in a way
46/// that provides ABI compatibility.
47///
48/// This provides ABI compatibility in two ways:
49///
50/// - Packets can be passed as function parameters in calls between two shared
51/// objects that are linked dynamically, even if compiled with different
52/// options/compilers. This is works because the class satisfies
53/// std::is_standard_layout, which enforces a defined order of struct members.
54/// This holds only if memory alignment is the same, which is not guaranteed by
55/// the C++ standard, but typicall is the same across compilers on the same
56/// system. If stricter guarantees on memory alignment is ever needed, this
57/// needs to be replaced by something else.
58///
59/// - Packet definitions can evolve over time, by adding new type codes and
60/// abandoning (but not removing) old ones, and then packets can be shared
61/// between shared objects that expect different packet definitions.
62///
63/// @tparam Type_enum_t Enumeration type to use for the type code in the Field
64/// objects.
65template <class Type_enum_t>
67
68/// @brief Class to store an array of @c Packet objects.
69///
70/// @tparam Type_enum_t Enumeration type used for the type code in the Field
71/// objects.
72template <class Type_enum_t>
74
75#ifdef MYSQL_SERVER
76/// @brief Class to help constructing a @c Packet, by pushing values one by one.
77///
78/// @tparam Type_enum_t Enumeration for the type codes.
79template <class Type_enum_t>
82
83 public:
84 /// Construct a new Packet_builder that can be used to store data in the given
85 /// @c Packet.
86 ///
87 /// @param packet Target @c Packet.
88 Packet_builder(Packet_t &packet) : m_packet(packet), m_position(0) {}
89
90 /// @brief Append an int field.
91 ///
92 /// @param type the field type
93 /// @param value the field value
94 void push_int(Type_enum_t type, long long value) {
95 m_packet[m_position].m_type = type;
96 m_packet[m_position].m_data.m_int = value;
97 m_position++;
98 }
99
100 /// @brief Append a string field, taking a copy of the parameter (raw pointer
101 /// and length).
102 ///
103 /// @param type The field type
104 /// @param value The string to copy (not necessarily null-terminated).
105 /// @param length The number of bytes to copy
106 /// @param key PSI_memory_key used to track the allocation.
107 void push_string_copy(Type_enum_t type, const char *value, std::size_t length,
109 char *copy = (char *)my_malloc(length + 1, key, MYF(MY_WME));
110 std::memcpy(copy, value, length);
111 copy[length] = '\0';
113 }
114
115 /// @brief Append a string field, taking a copy of the parameter (raw pointer
116 /// to null-terminated string).
117 ///
118 /// @param type The field type
119 /// @param value The string to copy (null-terminated).
120 /// @param key PSI_memory_key used to track the allocation.
121 void push_string_copy(Type_enum_t type, const char *value,
123 push_string_copy(type, value, std::strlen(value), key);
124 }
125
126 /// @brief Append a string field, taking a copy of the parameter
127 /// (std::string).
128 ///
129 /// @param type The field type.
130 /// @param value The string to copy.
131 /// @param key PSI_memory_key used to track the allocation.
132 void push_string_copy(Type_enum_t type, const std::string &value,
134 push_string_copy(type, value.data(), value.size(), key);
135 }
136
137 /// @brief Append a string field, sharing memory with the caller (raw pointer
138 /// to null-terminated string).
139 ///
140 /// @param type The field type.
141 /// @param value The string to push (null-terminated).
142 void push_string_view(Type_enum_t type, char *value) {
143 m_packet[m_position].m_type = type;
144 m_packet[m_position].m_data.m_string = value;
145 m_position++;
146 }
147
148 /// @brief Append a string field, sharing memory with the caller
149 /// (std::string).
150 ///
151 /// @param type The field type.
152 /// @param value String object. The pointer `value.c_str()` will be pushed.
153 void push_string_view(Type_enum_t type, const std::string &value) {
154 push_string_view(type, value.c_str());
155 }
156
157 /// @brief Append a boolean field.
158 /// @param type The field type.
159 /// @param value The boolean value.
160 void push_bool(Type_enum_t type, bool value) {
161 m_packet[m_position].m_type = type;
162 m_packet[m_position].m_data.m_bool = value;
163 m_position++;
164 }
165
166 /// @return The current position.
167 std::size_t get_position() { return m_position; }
168
169 private:
171 std::size_t m_position;
172};
173#endif // ifdef MYSQL_SERVER
174
175} // namespace mysql::abi_helpers
176
177// addtogroup GroupLibsMysqlAbiHelpers
178/// @}
179
180#endif // ifndef MYSQL_ABI_HELPERS_PACKET_H
Experimental API header.
Ownership-agnostic array class, which is both trivial and standard-layout.
Definition: array_view.h:53
Class to help constructing a Packet, by pushing values one by one.
Definition: packet.h:80
Packet_t & m_packet
Definition: packet.h:170
void push_string_view(Type_enum_t type, const std::string &value)
Append a string field, sharing memory with the caller (std::string).
Definition: packet.h:153
void push_string_view(Type_enum_t type, char *value)
Append a string field, sharing memory with the caller (raw pointer to null-terminated string).
Definition: packet.h:142
void push_string_copy(Type_enum_t type, const std::string &value, PSI_memory_key key)
Append a string field, taking a copy of the parameter (std::string).
Definition: packet.h:132
void push_bool(Type_enum_t type, bool value)
Append a boolean field.
Definition: packet.h:160
std::size_t get_position()
Definition: packet.h:167
void push_int(Type_enum_t type, long long value)
Append an int field.
Definition: packet.h:94
void push_string_copy(Type_enum_t type, const char *value, std::size_t length, PSI_memory_key key)
Append a string field, taking a copy of the parameter (raw pointer and length).
Definition: packet.h:107
void push_string_copy(Type_enum_t type, const char *value, PSI_memory_key key)
Append a string field, taking a copy of the parameter (raw pointer to null-terminated string).
Definition: packet.h:121
std::size_t m_position
Definition: packet.h:171
Packet_builder(Packet_t &packet)
Construct a new Packet_builder that can be used to store data in the given Packet.
Definition: packet.h:88
Array_t m_data
Array data.
Definition: array_base.h:119
#define MY_WME
Definition: my_sys.h:131
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
Experimental API header.
#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
Common header for many mysys elements.
void copy(Shards< COUNT > &dst, const Shards< COUNT > &src) noexcept
Copy the counters, overwrite destination.
Definition: ut0counter.h:354
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Definition: array_view.h:42
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
required string type
Definition: replication_group_member_actions.proto:34