| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 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 |
| 44 |
target 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 */ |
| 49 |
static inline void *ut_memcpy(void *dest, const void *src, ulint n); |
| 50 |
|
| 51 |
/** Wrapper for memmove(3). Copy memory area when the source and |
| 52 |
target 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 */ |
| 57 |
static 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. */ |
| 65 |
static 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 */ |
| 71 |
static 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 */ |
| 76 |
static 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. */ |
| 83 |
static 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) */ |
| 89 |
ulint ut_strlcpy(char *dst, /*!< in: destination buffer */ |
| 90 |
const char *src, /*!< in: source buffer */ |
| 91 |
ulint size); /*!< in: size of destination buffer */ |
| 92 |
|
| 93 |
/******************************************************************** |
| 94 |
Concatenate 3 strings.*/ |
| 95 |
char *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 |
| 103 |
truncated if there is not enough space in "hex", make sure "hex_size" is at |
| 104 |
least (2 * raw_size + 1) if you do not want this to happen. Returns the actual |
| 105 |
number 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 */ |
| 111 |
static 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 |
| 115 |
doubling 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 |
| 117 |
bytes 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 */ |
| 123 |
static inline ulint ut_str_sql_format(const char *str, ulint str_len, char *buf, |
| 124 |
ulint buf_size); |
| 125 |
|
| 126 |
namespace 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) */ |
| 131 |
template <typename T> |
| 132 |
bool 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 |
| 142 |
zeros. */ |
| 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 |