MySQL 8.0.40
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 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"
45
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*/
122inline 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*/
149inline 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*/
168inline 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 */
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*/
192inline 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*/
212inline 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
#define MY_WME
Definition: my_sys.h:123
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.
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:181
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:149
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:192
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:168
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:212
int n
Definition: xcom_base.cc:509