1 /*****************************************************************************
2 
3 Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed 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 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
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 #include <string.h>
39 #ifndef UNIV_HOTBACKUP
40 # include "os0sync.h"
41 
42 /** The total amount of memory currently allocated from the operating
43 system with os_mem_alloc_large() or malloc().  Does not count malloc()
44 if srv_use_sys_malloc is set.  Protected by ut_list_mutex. */
45 extern ulint		ut_total_allocated_memory;
46 
47 /** Mutex protecting ut_total_allocated_memory and ut_mem_block_list */
48 extern os_fast_mutex_t	ut_list_mutex;
49 #endif /* !UNIV_HOTBACKUP */
50 
51 /** Wrapper for memcpy(3).  Copy memory area when the source and
52 target are not overlapping.
53 * @param dest	in: copy to
54 * @param sour	in: copy from
55 * @param n	in: number of bytes to copy
56 * @return	dest */
57 UNIV_INLINE
58 void*
59 ut_memcpy(void* dest, const void* sour, ulint n);
60 
61 /** Wrapper for memmove(3).  Copy memory area when the source and
62 target are overlapping.
63 * @param dest	in: copy to
64 * @param sour	in: copy from
65 * @param n	in: number of bytes to copy
66 * @return	dest */
67 UNIV_INLINE
68 void*
69 ut_memmove(void* dest, const void* sour, ulint n);
70 
71 /** Wrapper for memcmp(3).  Compare memory areas.
72 * @param str1	in: first memory block to compare
73 * @param str2	in: second memory block to compare
74 * @param n	in: number of bytes to compare
75 * @return	negative, 0, or positive if str1 is smaller, equal,
76 		or greater than str2, respectively. */
77 UNIV_INLINE
78 int
79 ut_memcmp(const void* str1, const void* str2, ulint n);
80 
81 /**********************************************************************//**
82 Initializes the mem block list at database startup. */
83 UNIV_INTERN
84 void
85 ut_mem_init(void);
86 /*=============*/
87 
88 /**********************************************************************//**
89 Allocates memory.
90 @return	own: allocated memory */
91 UNIV_INTERN
92 void*
93 ut_malloc_low(
94 /*==========*/
95 	ulint	n,			/*!< in: number of bytes to allocate */
96 	ibool	assert_on_error)	/*!< in: if TRUE, we crash mysqld if
97 					the memory cannot be allocated */
98 	MY_ATTRIBUTE((malloc));
99 /**********************************************************************//**
100 Allocates memory. */
101 #define ut_malloc(n) ut_malloc_low(n, TRUE)
102 /**********************************************************************//**
103 Frees a memory block allocated with ut_malloc. Freeing a NULL pointer is
104 a nop. */
105 UNIV_INTERN
106 void
107 ut_free(
108 /*====*/
109 	void* ptr);  /*!< in, own: memory block, can be NULL */
110 #ifndef UNIV_HOTBACKUP
111 /**********************************************************************//**
112 Implements realloc. This is needed by /pars/lexyy.cc. Otherwise, you should not
113 use this function because the allocation functions in mem0mem.h are the
114 recommended ones in InnoDB.
115 
116 man realloc in Linux, 2004:
117 
118        realloc()  changes the size of the memory block pointed to
119        by ptr to size bytes.  The contents will be  unchanged  to
120        the minimum of the old and new sizes; newly allocated mem�
121        ory will be uninitialized.  If ptr is NULL,  the	 call  is
122        equivalent  to malloc(size); if size is equal to zero, the
123        call is equivalent to free(ptr).	 Unless ptr is	NULL,  it
124        must  have  been	 returned by an earlier call to malloc(),
125        calloc() or realloc().
126 
127 RETURN VALUE
128        realloc() returns a pointer to the newly allocated memory,
129        which is suitably aligned for any kind of variable and may
130        be different from ptr, or NULL if the  request  fails.  If
131        size  was equal to 0, either NULL or a pointer suitable to
132        be passed to free() is returned.	 If realloc()  fails  the
133        original	 block	is  left  untouched  - it is not freed or
134        moved.
135 @return	own: pointer to new mem block or NULL */
136 UNIV_INTERN
137 void*
138 ut_realloc(
139 /*=======*/
140 	void*	ptr,	/*!< in: pointer to old block or NULL */
141 	ulint	size);	/*!< in: desired size */
142 /**********************************************************************//**
143 Frees in shutdown all allocated memory not freed yet. */
144 UNIV_INTERN
145 void
146 ut_free_all_mem(void);
147 /*=================*/
148 #endif /* !UNIV_HOTBACKUP */
149 
150 /** Wrapper for strcpy(3).  Copy a NUL-terminated string.
151 * @param dest	in: copy to
152 * @param sour	in: copy from
153 * @return	dest */
154 UNIV_INLINE
155 char*
156 ut_strcpy(char* dest, const char* sour);
157 
158 /** Wrapper for strlen(3).  Determine the length of a NUL-terminated string.
159 * @param str	in: string
160 * @return	length of the string in bytes, excluding the terminating NUL */
161 UNIV_INLINE
162 ulint
163 ut_strlen(const char* str);
164 
165 /** Wrapper for strcmp(3).  Compare NUL-terminated strings.
166 * @param str1	in: first string to compare
167 * @param str2	in: second string to compare
168 * @return	negative, 0, or positive if str1 is smaller, equal,
169 		or greater than str2, respectively. */
170 UNIV_INLINE
171 int
172 ut_strcmp(const char* str1, const char* str2);
173 
174 /**********************************************************************//**
175 Copies up to size - 1 characters from the NUL-terminated string src to
176 dst, NUL-terminating the result. Returns strlen(src), so truncation
177 occurred if the return value >= size.
178 @return	strlen(src) */
179 UNIV_INTERN
180 ulint
181 ut_strlcpy(
182 /*=======*/
183 	char*		dst,	/*!< in: destination buffer */
184 	const char*	src,	/*!< in: source buffer */
185 	ulint		size);	/*!< in: size of destination buffer */
186 
187 /**********************************************************************//**
188 Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
189 (size - 1) bytes of src, not the first.
190 @return	strlen(src) */
191 UNIV_INTERN
192 ulint
193 ut_strlcpy_rev(
194 /*===========*/
195 	char*		dst,	/*!< in: destination buffer */
196 	const char*	src,	/*!< in: source buffer */
197 	ulint		size);	/*!< in: size of destination buffer */
198 
199 /**********************************************************************//**
200 Return the number of times s2 occurs in s1. Overlapping instances of s2
201 are only counted once.
202 @return	the number of times s2 occurs in s1 */
203 UNIV_INTERN
204 ulint
205 ut_strcount(
206 /*========*/
207 	const char*	s1,	/*!< in: string to search in */
208 	const char*	s2);	/*!< in: string to search for */
209 
210 /**********************************************************************//**
211 Replace every occurrence of s1 in str with s2. Overlapping instances of s1
212 are only replaced once.
213 @return	own: modified string, must be freed with mem_free() */
214 UNIV_INTERN
215 char*
216 ut_strreplace(
217 /*==========*/
218 	const char*	str,	/*!< in: string to operate on */
219 	const char*	s1,	/*!< in: string to replace */
220 	const char*	s2);	/*!< in: string to replace s1 with */
221 
222 /********************************************************************
223 Concatenate 3 strings.*/
224 
225 char*
226 ut_str3cat(
227 /*=======*/
228 				/* out, own: concatenated string, must be
229 				freed with mem_free() */
230 	const char*	s1,	/* in: string 1 */
231 	const char*	s2,	/* in: string 2 */
232 	const char*	s3);	/* in: string 3 */
233 
234 /**********************************************************************//**
235 Converts a raw binary data to a NUL-terminated hex string. The output is
236 truncated if there is not enough space in "hex", make sure "hex_size" is at
237 least (2 * raw_size + 1) if you do not want this to happen. Returns the
238 actual number of characters written to "hex" (including the NUL).
239 @return	number of chars written */
240 UNIV_INLINE
241 ulint
242 ut_raw_to_hex(
243 /*==========*/
244 	const void*	raw,		/*!< in: raw data */
245 	ulint		raw_size,	/*!< in: "raw" length in bytes */
246 	char*		hex,		/*!< out: hex string */
247 	ulint		hex_size);	/*!< in: "hex" size in bytes */
248 
249 /*******************************************************************//**
250 Adds single quotes to the start and end of string and escapes any quotes
251 by doubling them. Returns the number of bytes that were written to "buf"
252 (including the terminating NUL). If buf_size is too small then the
253 trailing bytes from "str" are discarded.
254 @return	number of bytes that were written */
255 UNIV_INLINE
256 ulint
257 ut_str_sql_format(
258 /*==============*/
259 	const char*	str,		/*!< in: string */
260 	ulint		str_len,	/*!< in: string length in bytes */
261 	char*		buf,		/*!< out: output buffer */
262 	ulint		buf_size);	/*!< in: output buffer size
263 					in bytes */
264 
265 #ifndef UNIV_NONINL
266 #include "ut0mem.ic"
267 #endif
268 
269 #endif
270