MySQL 9.0.0
Source Code Documentation
ut0mem.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1994, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/ut0mem.h
29 Memory primitives
30
31 Created 5/30/1994 Heikki Tuuri
32 ************************************************************************/
33
34#ifndef ut0mem_h
35#define ut0mem_h
36
37#include "univ.i"
38#ifndef UNIV_HOTBACKUP
39#include "os0event.h"
40#include "ut0mutex.h"
41#endif /* !UNIV_HOTBACKUP */
42
43/** Wrapper for memcpy(3). Copy memory area when the source and
44target are not overlapping.
45@param[in,out] dest copy to
46@param[in] src copy from
47@param[in] n number of bytes to copy
48@return dest */
49static inline void *ut_memcpy(void *dest, const void *src, ulint n);
50
51/** Wrapper for memmove(3). Copy memory area when the source and
52target are overlapping.
53@param[in,out] dest Move to
54@param[in] src Move from
55@param[in] n number of bytes to move
56@return dest */
57static inline void *ut_memmove(void *dest, const void *src, ulint n);
58
59/** Wrapper for memcmp(3). Compare memory areas.
60@param[in] str1 first memory block to compare
61@param[in] str2 second memory block to compare
62@param[in] n number of bytes to compare
63@return negative, 0, or positive if str1 is smaller, equal,
64 or greater than str2, respectively. */
65static inline int ut_memcmp(const void *str1, const void *str2, ulint n);
66
67/** Wrapper for strcpy(3). Copy a NUL-terminated string.
68@param[in,out] dest Destination to copy to
69@param[in] src Source to copy from
70@return dest */
71static inline char *ut_strcpy(char *dest, const char *src);
72
73/** Wrapper for strlen(3). Determine the length of a NUL-terminated string.
74@param[in] str string
75@return length of the string in bytes, excluding the terminating NUL */
76static inline ulint ut_strlen(const char *str);
77
78/** Wrapper for strcmp(3). Compare NUL-terminated strings.
79@param[in] str1 first string to compare
80@param[in] str2 second string to compare
81@return negative, 0, or positive if str1 is smaller, equal,
82 or greater than str2, respectively. */
83static inline int ut_strcmp(const char *str1, const char *str2);
84
85/** Copies up to size - 1 characters from the NUL-terminated string src to
86 dst, NUL-terminating the result. Returns strlen(src), so truncation
87 occurred if the return value >= size.
88 @return strlen(src) */
89ulint ut_strlcpy(char *dst, /*!< in: destination buffer */
90 const char *src, /*!< in: source buffer */
91 ulint size); /*!< in: size of destination buffer */
92
93/********************************************************************
94Concatenate 3 strings.*/
95char *ut_str3cat(
96 /* out, own: concatenated string, must be
97 freed with ut::free() */
98 const char *s1, /* in: string 1 */
99 const char *s2, /* in: string 2 */
100 const char *s3); /* in: string 3 */
101
102/** Converts a raw binary data to a NUL-terminated hex string. The output is
103truncated if there is not enough space in "hex", make sure "hex_size" is at
104least (2 * raw_size + 1) if you do not want this to happen. Returns the actual
105number of characters written to "hex" (including the NUL).
106@param[in] raw raw data
107@param[in] raw_size "raw" length in bytes
108@param[out] hex hex string
109@param[in] hex_size "hex" size in bytes
110@return number of chars written */
111static inline ulint ut_raw_to_hex(const void *raw, ulint raw_size, char *hex,
112 ulint hex_size);
113
114/** Adds single quotes to the start and end of string and escapes any quotes by
115doubling them. Returns the number of bytes that were written to "buf"
116(including the terminating NUL). If buf_size is too small then the trailing
117bytes from "str" are discarded.
118@param[in] str string
119@param[in] str_len string length in bytes
120@param[out] buf output buffer
121@param[in] buf_size output buffer size in bytes
122@return number of bytes that were written */
123static inline ulint ut_str_sql_format(const char *str, ulint str_len, char *buf,
125
126namespace ut {
127/** Checks if the pointer has address aligned properly for a given type.
128@param[in] ptr The pointer, address of which we want to check if it could be
129 pointing to object of type T.
130@return true iff ptr address is aligned w.r.t. alignof(T) */
131template <typename T>
132bool is_aligned_as(void const *const ptr) {
133 /* Implementation note: the type of the argument of this function needs to be
134 void *, not T *, because, due to C++ rules, a pointer of type T* needs to be
135 aligned. In other words, compiler could optimize out the whole function. */
136 return reinterpret_cast<uintptr_t>(ptr) % alignof(T) == 0;
137}
138/** Checks if memory range is all zeros.
139@param[in] start The pointer to first byte of a buffer
140@param[in] number_of_bytes The number of bytes in the buffer
141@return true if and only if `number_of_bytes` bytes pointed by `start` are all
142zeros. */
143[[nodiscard]] inline bool is_zeros(const void *start, size_t number_of_bytes) {
144 auto *first_byte = reinterpret_cast<const char *>(start);
145 return number_of_bytes == 0 ||
146 (*first_byte == 0 &&
147 std::memcmp(first_byte, first_byte + 1, number_of_bytes - 1) == 0);
148}
149
150} // namespace ut
151#include "ut0mem.ic"
152
153#endif
constexpr DWORD buf_size
Definition: create_def.cc:229
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1081
Definition: buf0block_hint.cc:30
size_t size(const char *const c)
Definition: base64.h:46
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
bool is_aligned_as(void const *const ptr)
Checks if the pointer has address aligned properly for a given type.
Definition: ut0mem.h:132
bool is_zeros(const void *start, size_t number_of_bytes)
Checks if memory range is all zeros.
Definition: ut0mem.h:143
The interface to the operating system condition variables.
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:406
static ulint ut_strlen(const char *str)
Wrapper for strlen(3).
char * ut_str3cat(const char *s1, const char *s2, const char *s3)
Definition: ut0mem.cc:61
static char * ut_strcpy(char *dest, const char *src)
Wrapper for strcpy(3).
static ulint ut_str_sql_format(const char *str, ulint str_len, char *buf, ulint buf_size)
Adds single quotes to the start and end of string and escapes any quotes by doubling them.
ulint ut_strlcpy(char *dst, const char *src, ulint size)
Copies up to size - 1 characters from the NUL-terminated string src to dst, NUL-terminating the resul...
Definition: ut0mem.cc:43
static int ut_memcmp(const void *str1, const void *str2, ulint n)
Wrapper for memcmp(3).
static void * ut_memcpy(void *dest, const void *src, ulint n)
Wrapper for memcpy(3).
static void * ut_memmove(void *dest, const void *src, ulint n)
Wrapper for memmove(3).
static ulint ut_raw_to_hex(const void *raw, ulint raw_size, char *hex, ulint hex_size)
Converts a raw binary data to a NUL-terminated hex string.
static int ut_strcmp(const char *str1, const char *str2)
Wrapper for strcmp(3).
Memory primitives.
Policy based mutexes.
int n
Definition: xcom_base.cc:509