MySQL 8.0.30
Source Code Documentation
wrapper_functions.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2022, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/**
24 @file
25
26 @brief Contains wrapper functions for memory allocation and deallocation.
27 This includes generic functions to be called from the binlogevent library,
28 which call the appropriate corresponding function, depending on whether
29 the library is compiled independently, or with the MySQL server.
30*/
31
32#ifndef WRAPPER_FUNCTIONS_INCLUDED
33#define WRAPPER_FUNCTIONS_INCLUDED
34
35#include "binlog_config.h"
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#else
68#define BAPI_ASSERT(x) assert(x)
69#define BAPI_PRINT(name, params)
70#define BAPI_ENTER(x)
71#define BAPI_RETURN(x) return (x)
72#define BAPI_TRACE
73#define BAPI_VOID_RETURN return
74#endif
75#else
76#define BAPI_ASSERT(x) \
77 do { \
78 } while (0)
79#define BAPI_PRINT(name, params)
80#define BAPI_ENTER(x)
81#define BAPI_RETURN(x) return (x)
82#define BAPI_TRACE
83#define BAPI_VOID_RETURN return
84#endif
85
86#ifndef HAVE_STRNDUP
87/**
88 The strndup() function returns a pointer to a new string which is a duplicate
89 of the string s, but it only copies at most n bytes. If s is longer than n,
90 only n bytes are copied, and a terminating null byte ('\0') is added.
91 Memory for the new string is obtained with malloc,
92 and can be freed with free.
93 @param s The string whose copy we want to create
94 @param n Number of bytes to be copied
95
96 @return The duplicated string, or NULL if insufficient memory was
97 available.
98*/
99inline char *strndup(const char *s, size_t n) {
100 char *result;
101 size_t len = strlen(s);
102
103 if (n < len) len = n;
104
105 result = (char *)malloc(len + 1);
106 if (!result) return 0;
107
108 result[len] = '\0';
109 return (char *)memcpy(result, s, len);
110}
111#endif
112
113/**
114 This is a wrapper function, and returns a pointer to a new string which is
115 a duplicate of the input string. The terminating Null character is added.
116
117 If compiled with MySQL server,the strndup function from the mysys library is
118 called, which allow instrumenting memory allocated. Else, the standard
119 string function is called.
120
121 @param destination The string to be duplicated
122 @param n The number of bytes to be copied
123
124 @return The duplicated string, or NULL if insufficient memory was available.
125*/
126inline const char *bapi_strndup(const char *destination, size_t n) {
127#ifdef HAVE_MYSYS
128 /* Call the function in mysys library, required for memory instrumentation */
129 return my_strndup(key_memory_log_event, destination, n, MYF(MY_WME));
130#else
131 return strndup(destination, n);
132#endif
133}
134
135/**
136 This is a wrapper function, and returns a pointer to a new memory with the
137 contents copied from the input memory pointer, up to a given length
138
139 @param source Pointer to the buffer from which data is to be copied
140 @param len Length up to which the source should be copied
141
142 @return dest pointer to a new memory if allocation was successful
143 NULL otherwise
144*/
145inline void *bapi_memdup(const void *source, size_t len) {
146 void *dest;
147#ifdef HAVE_MYSYS
148 /* Call the function in mysys library, required for memory instrumentation */
150#else
151 dest = malloc(len);
152 if (dest) memcpy(dest, source, len);
153#endif
154 return dest;
155}
156
157/**
158 This is a wrapper function in order to allocate memory from the heap
159 in the binlogevent library.
160
161 If compiled with the MySQL server, and memory is allocated using memory
162 allocating methods from the mysys library, my_malloc is called. Otherwise,
163 the standard malloc() is called from the function.
164
165 @param size Size of the memory to be allocated.
166 @param flags flags to pass to MySQL server my_malloc functions
167 @return Void pointer to the allocated chunk of memory
168*/
169inline void *bapi_malloc(size_t size, int flags [[maybe_unused]]) {
170 void *dest = nullptr;
171#ifdef HAVE_MYSYS
172 dest = my_malloc(key_memory_log_event, size, MYF(flags));
173#else
174 dest = malloc(size);
175#endif
176 return dest;
177}
178
179/**
180 This is a wrapper function in order to free the memory allocated from the heap
181 in the binlogevent library.
182
183 If compiled with the MySQL server, and memory is allocated using memory
184 allocating methods from the mysys library, my_free is called. Otherwise,
185 the standard free() is called from the function.
186
187 @param ptr Pointer to the memory which is to be freed.
188*/
189inline void bapi_free(void *ptr) {
190#ifdef HAVE_MYSYS
191 return my_free(ptr);
192#else
193 return free(ptr);
194#endif
195}
196#endif
#define MY_WME
Definition: my_sys.h:122
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:48
static int flags[50]
Definition: hp_test1.cc:39
#define malloc(A)
Definition: lexyy.cc:914
#define free(A)
Definition: lexyy.cc:915
#define MYF(v)
Definition: my_inttypes.h:96
void * my_malloc(PSI_memory_key key, size_t size, int flags)
Allocates size bytes of memory.
Definition: my_memory.cc:56
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:80
Common header for many mysys elements.
struct result result
Definition: result.h:33
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:41
void * my_memdup(PSI_memory_key key, const void *from, size_t length, myf_t flags)
Definition: my_malloc.cc:475
char * my_strndup(PSI_memory_key key, const char *from, size_t length, myf_t flags)
Definition: my_malloc.cc:491
Definition: result.h:29
PSI_memory_key key_memory_log_event
Definition: log_event.cc:176
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:126
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:169
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:145
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:189
int n
Definition: xcom_base.cc:505