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