| 1 |
/* Copyright (c) 2014, 2026, 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 |
/** |
| 25 |
@file |
| 26 |
|
| 27 |
@brief Contains wrapper functions for memory allocation and deallocation. |
| 28 |
This includes generic functions to be called from the binlogevent library, |
| 29 |
which call the appropriate corresponding function, depending on whether |
| 30 |
the library is compiled independently, or with the MySQL server. |
| 31 |
*/ |
| 32 |
|
| 33 |
#ifndef WRAPPER_FUNCTIONS_INCLUDED |
| 34 |
#define WRAPPER_FUNCTIONS_INCLUDED |
| 35 |
|
| 36 |
#include "binlog_config.h" |
| 37 |
#ifndef STANDALONE_BINLOG |
| 38 |
#define HAVE_MYSYS 1 |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifdef HAVE_MYSYS |
| 42 |
#include "my_config.h" |
| 43 |
#include "my_sys.h" |
| 44 |
#include "mysql/service_mysql_alloc.h" |
| 45 |
|
| 46 |
extern PSI_memory_key key_memory_log_event; |
| 47 |
#else |
| 48 |
#include <cassert> |
| 49 |
#ifndef _GNU_SOURCE |
| 50 |
#define _GNU_SOURCE |
| 51 |
#endif |
| 52 |
#include <stdlib.h> |
| 53 |
#include <string.h> |
| 54 |
|
| 55 |
#endif |
| 56 |
|
| 57 |
#if !defined(NDEBUG) |
| 58 |
|
| 59 |
#include "my_dbug.h" |
| 60 |
|
| 61 |
#ifdef HAVE_MYSYS |
| 62 |
#define BAPI_ASSERT(x) assert(x) |
| 63 |
#define BAPI_PRINT(name, params) DBUG_PRINT(name, params) |
| 64 |
#define BAPI_ENTER(x) DBUG_ENTER(x) |
| 65 |
#define BAPI_RETURN(x) DBUG_RETURN(x) |
| 66 |
#define BAPI_TRACE DBUG_TRACE |
| 67 |
#define BAPI_VOID_RETURN DBUG_VOID_RETURN |
| 68 |
#define BAPI_LOG(x, y) DBUG_LOG(x, y) |
| 69 |
#define BAPI_VAR(v) DBUG_VAR(v) |
| 70 |
#else |
| 71 |
#define BAPI_ASSERT(x) assert(x) |
| 72 |
#define BAPI_PRINT(name, params) \ |
| 73 |
do { \ |
| 74 |
} while (0) |
| 75 |
#define BAPI_ENTER(x) \ |
| 76 |
do { \ |
| 77 |
} while (0) |
| 78 |
#define BAPI_RETURN(x) return (x) |
| 79 |
#define BAPI_TRACE \ |
| 80 |
do { \ |
| 81 |
} while (0) |
| 82 |
#define BAPI_VOID_RETURN return |
| 83 |
#define BAPI_LOG(x, y) \ |
| 84 |
do { \ |
| 85 |
} while (0) |
| 86 |
#define BAPI_VAR(v) "" |
| 87 |
#endif |
| 88 |
#else |
| 89 |
#define BAPI_ASSERT(x) \ |
| 90 |
do { \ |
| 91 |
} while (0) |
| 92 |
#define BAPI_PRINT(name, params) \ |
| 93 |
do { \ |
| 94 |
} while (0) |
| 95 |
#define BAPI_ENTER(x) \ |
| 96 |
do { \ |
| 97 |
} while (0) |
| 98 |
#define BAPI_RETURN(x) return (x) |
| 99 |
#define BAPI_TRACE \ |
| 100 |
do { \ |
| 101 |
} while (0) |
| 102 |
#define BAPI_VOID_RETURN return |
| 103 |
#define BAPI_LOG(x, y) \ |
| 104 |
do { \ |
| 105 |
} while (0) |
| 106 |
#define BAPI_VAR(v) "" |
| 107 |
#endif |
| 108 |
|
| 109 |
#ifndef HAVE_STRNDUP |
| 110 |
/** |
| 111 |
The strndup() function returns a pointer to a new string which is a duplicate |
| 112 |
of the string s, but it only copies at most n bytes. If s is longer than n, |
| 113 |
only n bytes are copied, and a terminating null byte ('\0') is added. |
| 114 |
Memory for the new string is obtained with malloc, |
| 115 |
and can be freed with free. |
| 116 |
@param s The string whose copy we want to create |
| 117 |
@param n Number of bytes to be copied |
| 118 |
|
| 119 |
@return The duplicated string, or NULL if insufficient memory was |
| 120 |
available. |
| 121 |
*/ |
| 122 |
inline char *strndup(const char *s, size_t n) { |
| 123 |
char *result; |
| 124 |
size_t len = strlen(s); |
| 125 |
|
| 126 |
if (n < len) len = n; |
| 127 |
|
| 128 |
result = (char *)malloc(len + 1); |
| 129 |
if (!result) return 0; |
| 130 |
|
| 131 |
result[len] = '\0'; |
| 132 |
return (char *)memcpy(result, s, len); |
| 133 |
} |
| 134 |
#endif |
| 135 |
|
| 136 |
/** |
| 137 |
This is a wrapper function, and returns a pointer to a new string which is |
| 138 |
a duplicate of the input string. The terminating Null character is added. |
| 139 |
|
| 140 |
If compiled with MySQL server,the strndup function from the mysys library is |
| 141 |
called, which allow instrumenting memory allocated. Else, the standard |
| 142 |
string function is called. |
| 143 |
|
| 144 |
@param destination The string to be duplicated |
| 145 |
@param n The number of bytes to be copied |
| 146 |
|
| 147 |
@return The duplicated string, or NULL if insufficient memory was available. |
| 148 |
*/ |
| 149 |
inline const char *bapi_strndup(const char *destination, size_t n) { |
| 150 |
#ifdef HAVE_MYSYS |
| 151 |
/* Call the function in mysys library, required for memory instrumentation */ |
| 152 |
return my_strndup(key_memory_log_event, destination, n, MYF(MY_WME)); |
| 153 |
#else |
| 154 |
return strndup(destination, n); |
| 155 |
#endif |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
This is a wrapper function, and returns a pointer to a new memory with the |
| 160 |
contents copied from the input memory pointer, up to a given length |
| 161 |
|
| 162 |
@param source Pointer to the buffer from which data is to be copied |
| 163 |
@param len Length up to which the source should be copied |
| 164 |
|
| 165 |
@return dest pointer to a new memory if allocation was successful |
| 166 |
NULL otherwise |
| 167 |
*/ |
| 168 |
inline void *bapi_memdup(const void *source, size_t len) { |
| 169 |
void *dest; |
| 170 |
#ifdef HAVE_MYSYS |
| 171 |
/* Call the function in mysys library, required for memory instrumentation */ |
| 172 |
dest = my_memdup(key_memory_log_event, source, len, MYF(MY_WME)); |
| 173 |
#else |
| 174 |
dest = malloc(len); |
| 175 |
if (dest) memcpy(dest, source, len); |
| 176 |
#endif |
| 177 |
return dest; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
This is a wrapper function in order to allocate memory from the heap |
| 182 |
in the binlogevent library. |
| 183 |
|
| 184 |
If compiled with the MySQL server, and memory is allocated using memory |
| 185 |
allocating methods from the mysys library, my_malloc is called. Otherwise, |
| 186 |
the standard malloc() is called from the function. |
| 187 |
|
| 188 |
@param size Size of the memory to be allocated. |
| 189 |
@param flags flags to pass to MySQL server my_malloc functions |
| 190 |
@return Void pointer to the allocated chunk of memory |
| 191 |
*/ |
| 192 |
inline void *bapi_malloc(size_t size, int flags [[maybe_unused]]) { |
| 193 |
void *dest = nullptr; |
| 194 |
#ifdef HAVE_MYSYS |
| 195 |
dest = my_malloc(key_memory_log_event, size, MYF(flags)); |
| 196 |
#else |
| 197 |
dest = malloc(size); |
| 198 |
#endif |
| 199 |
return dest; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
This is a wrapper function in order to free the memory allocated from the heap |
| 204 |
in the binlogevent library. |
| 205 |
|
| 206 |
If compiled with the MySQL server, and memory is allocated using memory |
| 207 |
allocating methods from the mysys library, my_free is called. Otherwise, |
| 208 |
the standard free() is called from the function. |
| 209 |
|
| 210 |
@param ptr Pointer to the memory which is to be freed. |
| 211 |
*/ |
| 212 |
inline void bapi_free(void *ptr) { |
| 213 |
#ifdef HAVE_MYSYS |
| 214 |
return my_free(ptr); |
| 215 |
#else |
| 216 |
return free(ptr); |
| 217 |
#endif |
| 218 |
} |
| 219 |
#endif |