MySQL 9.1.0
Source Code Documentation
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 also distributed 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 included with MySQL.
13//
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License, version 2.0, for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22
23#ifndef MYSQL_ABI_HELPERS_PACKET_H
24#define MYSQL_ABI_HELPERS_PACKET_H
25
26/// @file
27/// Experimental API header
28
29#include <cstddef> // std::size_t
30#include <cstring> // std::memcpy
31#include "array_view.h"
32#include "field.h"
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
42namespace mysql::abi_helpers {
43
44/// @brief Class to store a number of fields of heterogeneous types, in a way
45/// that provides ABI compatibility.
46///
47/// This provides ABI compatibility in two ways:
48///
49/// - Packets can be passed as function parameters in calls between two shared
50/// objects that are linked dynamically, even if compiled with different
51/// options/compilers. This is works because the class satisfies
52/// std::is_standard_layout, which enforces a defined order of struct members.
53/// This holds only if memory alignment is the same, which is not guaranteed by
54/// the C++ standard, but typicall is the same across compilers on the same
55/// system. If stricter guarantees on memory alignment is ever needed, this
56/// needs to be replaced by something else.
57///
58/// - Packet definitions can evolve over time, by adding new type codes and
59/// abandoning (but not removing) old ones, and then packets can be shared
60/// between shared objects that expect different packet definitions.
61///
62/// @tparam Type_enum_t Enumeration type to use for the type code in the Field
63/// objects.
64template <class Type_enum_t>
66
67#ifdef MYSQL_SERVER
68/// @brief Class to help constructing a @c Packet, by pushing values one by one.
69///
70/// @tparam Type_enum_t Enumeration for the type codes.
71template <class Type_enum_t>
74
75 public:
76 /// Construct a new Packet_builder that can be used to store data in the given
77 /// @c Packet.
78 ///
79 /// @param packet Target @c Packet.
80 Packet_builder(Packet_t &packet) : m_packet(packet), m_position(0) {}
81
82 /// @brief Append an int field.
83 ///
84 /// @param type the field type
85 /// @param value the field value
86 void push_int(Type_enum_t type, long long value) {
87 m_packet[m_position].m_type = type;
88 m_packet[m_position].m_data.m_int = value;
89 m_position++;
90 }
91
92 /// @brief Append a string field, taking a copy of the parameter (raw pointer
93 /// and length).
94 ///
95 /// @param type The field type
96 /// @param value The string to copy (not necessarily null-terminated).
97 /// @param length The number of bytes to copy
98 /// @param key PSI_memory_key used to track the allocation.
99 void push_string_copy(Type_enum_t type, const char *value, std::size_t length,
101 char *copy = (char *)my_malloc(length + 1, key, MYF(MY_WME));
102 std::memcpy(copy, value, length);
103 copy[length] = '\0';
105 }
106
107 /// @brief Append a string field, taking a copy of the parameter (raw pointer
108 /// to null-terminated string).
109 ///
110 /// @param type The field type
111 /// @param value The string to copy (null-terminated).
112 /// @param key PSI_memory_key used to track the allocation.
113 void push_string_copy(Type_enum_t type, const char *value,
115 push_string_copy(type, value, std::strlen(value), key);
116 }
117
118 /// @brief Append a string field, taking a copy of the parameter
119 /// (std::string).
120 ///
121 /// @param type The field type.
122 /// @param value The string to copy.
123 /// @param key PSI_memory_key used to track the allocation.
124 void push_string_copy(Type_enum_t type, const std::string &value,
126 push_string_copy(type, value.data(), value.size(), key);
127 }
128
129 /// @brief Append a string field, sharing memory with the caller (raw pointer
130 /// to null-terminated string).
131 ///
132 /// @param type The field type.
133 /// @param value The string to push (null-terminated).
134 void push_string_view(Type_enum_t type, char *value) {
135 m_packet[m_position].m_type = type;
136 m_packet[m_position].m_data.m_string = value;
137 m_position++;
138 }
139
140 /// @brief Append a string field, sharing memory with the caller
141 /// (std::string).
142 ///
143 /// @param type The field type.
144 /// @param value String object. The pointer `value.c_str()` will be pushed.
145 void push_string_view(Type_enum_t type, const std::string &value) {
146 push_string_view(type, value.c_str());
147 }
148
149 /// @brief Append a boolean field.
150 /// @param type The field type.
151 /// @param value The boolean value.
152 void push_bool(Type_enum_t type, bool value) {
153 m_packet[m_position].m_type = type;
154 m_packet[m_position].m_data.m_bool = value;
155 m_position++;
156 }
157
158 /// @return The current position.
159 std::size_t get_position() { return m_position; }
160
161 private:
163 std::size_t m_position;
164};
165#endif // ifdef MYSQL_SERVER
166
167} // namespace mysql::abi_helpers
168
169// addtogroup GroupLibsMysqlAbiHelpers
170/// @}
171
172#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:72
Packet_t & m_packet
Definition: packet.h:162
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:145
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:134
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:124
void push_bool(Type_enum_t type, bool value)
Append a boolean field.
Definition: packet.h:152
std::size_t get_position()
Definition: packet.h:159
void push_int(Type_enum_t type, long long value)
Append an int field.
Definition: packet.h:86
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:99
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:113
std::size_t m_position
Definition: packet.h:163
Packet_builder(Packet_t &packet)
Construct a new Packet_builder that can be used to store data in the given Packet.
Definition: packet.h:80
Array_t 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
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