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