~drizzle-trunk/drizzle/development

641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
1
/*****************************************************************************
2
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
3
Copyright (C) 1994, 2009, Innobase Oy. All Rights Reserved.
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
4
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
8
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13
You should have received a copy of the GNU General Public License along with
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
16
17
*****************************************************************************/
18
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
19
/*******************************************************************//**
20
@file include/ut0mem.h
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
21
Memory primitives
22
23
Created 5/30/1994 Heikki Tuuri
24
************************************************************************/
25
26
#ifndef ut0mem_h
27
#define ut0mem_h
28
29
#include "univ.i"
30
#include <string.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
31
#include <sys/types.h>
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
32
#ifndef UNIV_HOTBACKUP
33
# include "os0sync.h"
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
34
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
35
/** The total amount of memory currently allocated from the operating
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
36
system with os_mem_alloc_large() or malloc().  Does not count malloc()
37
if srv_use_sys_malloc is set.  Protected by ut_list_mutex. */
38
extern ulint		ut_total_allocated_memory;
39
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
40
/** Mutex protecting ut_total_allocated_memory and ut_mem_block_list */
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
41
extern os_fast_mutex_t	ut_list_mutex;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
42
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
43
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
44
/** Wrapper for memcpy(3).  Copy memory area when the source and
45
target are not overlapping.
46
* @param dest	in: copy to
47
* @param sour	in: copy from
48
* @param n	in: number of bytes to copy
49
* @return	dest */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
50
UNIV_INLINE
51
void*
52
ut_memcpy(void* dest, const void* sour, ulint n);
53
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
54
/** Wrapper for memmove(3).  Copy memory area when the source and
55
target are overlapping.
56
* @param dest	in: copy to
57
* @param sour	in: copy from
58
* @param n	in: number of bytes to copy
59
* @return	dest */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
60
UNIV_INLINE
61
void*
62
ut_memmove(void* dest, const void* sour, ulint n);
63
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
64
/** Wrapper for memcmp(3).  Compare memory areas.
65
* @param str1	in: first memory block to compare
66
* @param str2	in: second memory block to compare
67
* @param n	in: number of bytes to compare
68
* @return	negative, 0, or positive if str1 is smaller, equal,
69
		or greater than str2, respectively. */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
70
UNIV_INLINE
71
int
72
ut_memcmp(const void* str1, const void* str2, ulint n);
73
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
74
/**********************************************************************//**
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
75
Initializes the mem block list at database startup. */
76
UNIV_INTERN
77
void
78
ut_mem_init(void);
79
/*=============*/
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
80
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
81
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
82
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
83
defined and set_to_zero is TRUE.
84
@return	own: allocated memory */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
85
UNIV_INTERN
86
void*
87
ut_malloc_low(
88
/*==========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
89
	ulint	n,			/*!< in: number of bytes to allocate */
90
	ibool	set_to_zero,		/*!< in: TRUE if allocated memory
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
91
					should be set to zero if
92
					UNIV_SET_MEM_TO_ZERO is defined */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
93
	ibool	assert_on_error);	/*!< in: if TRUE, we crash mysqld if
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
94
					the memory cannot be allocated */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
95
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
96
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
97
defined.
98
@return	own: allocated memory */
2023.3.2 by Monty Taylor
Fixed lexer symbols.
99
#ifdef __cplusplus
100
extern "C"
101
#endif
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
102
UNIV_INTERN
103
void*
104
ut_malloc(
105
/*======*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
106
	ulint	n);	/*!< in: number of bytes to allocate */
107
#ifndef UNIV_HOTBACKUP
108
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
109
Tests if malloc of n bytes would succeed. ut_malloc() asserts if memory runs
110
out. It cannot be used if we want to return an error message. Prints to
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
111
stderr a message if fails.
112
@return	TRUE if succeeded */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
113
UNIV_INTERN
114
ibool
115
ut_test_malloc(
116
/*===========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
117
	ulint	n);	/*!< in: try to allocate this many bytes */
118
#endif /* !UNIV_HOTBACKUP */
119
/**********************************************************************//**
1819.7.68 by Stewart Smith
Merge initial InnoDB+ import.
120
Frees a memory block allocated with ut_malloc. Freeing a NULL pointer is
121
a nop. */
2023.3.2 by Monty Taylor
Fixed lexer symbols.
122
#ifdef __cplusplus
123
extern "C"
124
#endif
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
125
UNIV_INTERN
126
void
127
ut_free(
128
/*====*/
1819.7.68 by Stewart Smith
Merge initial InnoDB+ import.
129
	void* ptr);  /*!< in, own: memory block, can be NULL */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
130
#ifndef UNIV_HOTBACKUP
131
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
132
Implements realloc. This is needed by /pars/lexyy.c. Otherwise, you should not
133
use this function because the allocation functions in mem0mem.h are the
134
recommended ones in InnoDB.
135
136
man realloc in Linux, 2004:
137
138
       realloc()  changes the size of the memory block pointed to
139
       by ptr to size bytes.  The contents will be  unchanged  to
140
       the minimum of the old and new sizes; newly allocated memĀ­
141
       ory will be uninitialized.  If ptr is NULL,  the	 call  is
142
       equivalent  to malloc(size); if size is equal to zero, the
143
       call is equivalent to free(ptr).	 Unless ptr is	NULL,  it
144
       must  have  been	 returned by an earlier call to malloc(),
145
       calloc() or realloc().
146
147
RETURN VALUE
148
       realloc() returns a pointer to the newly allocated memory,
149
       which is suitably aligned for any kind of variable and may
150
       be different from ptr, or NULL if the  request  fails.  If
151
       size  was equal to 0, either NULL or a pointer suitable to
152
       be passed to free() is returned.	 If realloc()  fails  the
153
       original	 block	is  left  untouched  - it is not freed or
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
154
       moved.
155
@return	own: pointer to new mem block or NULL */
2023.3.2 by Monty Taylor
Fixed lexer symbols.
156
#ifdef __cplusplus
157
extern "C"
158
#endif
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
159
UNIV_INTERN
160
void*
161
ut_realloc(
162
/*=======*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
163
	void*	ptr,	/*!< in: pointer to old block or NULL */
164
	ulint	size);	/*!< in: desired size */
165
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
166
Frees in shutdown all allocated memory not freed yet. */
167
UNIV_INTERN
168
void
169
ut_free_all_mem(void);
170
/*=================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
171
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
172
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
173
/** Wrapper for strcpy(3).  Copy a NUL-terminated string.
174
* @param dest	in: copy to
175
* @param sour	in: copy from
176
* @return	dest */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
177
UNIV_INLINE
178
char*
179
ut_strcpy(char* dest, const char* sour);
180
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
181
/** Wrapper for strlen(3).  Determine the length of a NUL-terminated string.
182
* @param str	in: string
183
* @return	length of the string in bytes, excluding the terminating NUL */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
184
UNIV_INLINE
185
ulint
186
ut_strlen(const char* str);
187
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
188
/** Wrapper for strcmp(3).  Compare NUL-terminated strings.
189
* @param str1	in: first string to compare
190
* @param str2	in: second string to compare
191
* @return	negative, 0, or positive if str1 is smaller, equal,
192
		or greater than str2, respectively. */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
193
UNIV_INLINE
194
int
195
ut_strcmp(const char* str1, const char* str2);
196
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
197
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
198
Copies up to size - 1 characters from the NUL-terminated string src to
199
dst, NUL-terminating the result. Returns strlen(src), so truncation
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
200
occurred if the return value >= size.
201
@return	strlen(src) */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
202
UNIV_INTERN
203
ulint
204
ut_strlcpy(
205
/*=======*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
206
	char*		dst,	/*!< in: destination buffer */
207
	const char*	src,	/*!< in: source buffer */
208
	ulint		size);	/*!< in: size of destination buffer */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
209
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
210
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
211
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
212
(size - 1) bytes of src, not the first.
213
@return	strlen(src) */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
214
UNIV_INTERN
215
ulint
216
ut_strlcpy_rev(
217
/*===========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
218
	char*		dst,	/*!< in: destination buffer */
219
	const char*	src,	/*!< in: source buffer */
220
	ulint		size);	/*!< in: size of destination buffer */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
221
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
222
/**********************************************************************//**
223
Compute strlen(ut_strcpyq(str, q)).
224
@return	length of the string when quoted */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
225
UNIV_INLINE
226
ulint
227
ut_strlenq(
228
/*=======*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
229
	const char*	str,	/*!< in: null-terminated string */
230
	char		q);	/*!< in: the quote character */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
231
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
232
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
233
Make a quoted copy of a NUL-terminated string.	Leading and trailing
234
quotes will not be included; only embedded quotes will be escaped.
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
235
See also ut_strlenq() and ut_memcpyq().
236
@return	pointer to end of dest */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
237
UNIV_INTERN
238
char*
239
ut_strcpyq(
240
/*=======*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
241
	char*		dest,	/*!< in: output buffer */
242
	char		q,	/*!< in: the quote character */
243
	const char*	src);	/*!< in: null-terminated string */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
244
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
245
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
246
Make a quoted copy of a fixed-length string.  Leading and trailing
247
quotes will not be included; only embedded quotes will be escaped.
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
248
See also ut_strlenq() and ut_strcpyq().
249
@return	pointer to end of dest */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
250
UNIV_INTERN
251
char*
252
ut_memcpyq(
253
/*=======*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
254
	char*		dest,	/*!< in: output buffer */
255
	char		q,	/*!< in: the quote character */
256
	const char*	src,	/*!< in: string to be quoted */
257
	ulint		len);	/*!< in: length of src */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
258
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
259
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
260
Return the number of times s2 occurs in s1. Overlapping instances of s2
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
261
are only counted once.
262
@return	the number of times s2 occurs in s1 */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
263
UNIV_INTERN
264
ulint
265
ut_strcount(
266
/*========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
267
	const char*	s1,	/*!< in: string to search in */
268
	const char*	s2);	/*!< in: string to search for */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
269
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
270
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
271
Replace every occurrence of s1 in str with s2. Overlapping instances of s1
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
272
are only replaced once.
273
@return	own: modified string, must be freed with mem_free() */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
274
UNIV_INTERN
275
char*
276
ut_strreplace(
277
/*==========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
278
	const char*	str,	/*!< in: string to operate on */
279
	const char*	s1,	/*!< in: string to replace */
280
	const char*	s2);	/*!< in: string to replace s1 with */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
281
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
282
/**********************************************************************//**
283
Converts a raw binary data to a NUL-terminated hex string. The output is
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
284
truncated if there is not enough space in "hex", make sure "hex_size" is at
285
least (2 * raw_size + 1) if you do not want this to happen. Returns the
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
286
actual number of characters written to "hex" (including the NUL).
287
@return	number of chars written */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
288
UNIV_INLINE
289
ulint
290
ut_raw_to_hex(
291
/*==========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
292
	const void*	raw,		/*!< in: raw data */
293
	ulint		raw_size,	/*!< in: "raw" length in bytes */
294
	char*		hex,		/*!< out: hex string */
295
	ulint		hex_size);	/*!< in: "hex" size in bytes */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
296
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
297
/*******************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
298
Adds single quotes to the start and end of string and escapes any quotes
299
by doubling them. Returns the number of bytes that were written to "buf"
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
300
(including the terminating NUL). If buf_size is too small then the
301
trailing bytes from "str" are discarded.
302
@return	number of bytes that were written */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
303
UNIV_INLINE
304
ulint
305
ut_str_sql_format(
306
/*==============*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
307
	const char*	str,		/*!< in: string */
308
	ulint		str_len,	/*!< in: string length in bytes */
309
	char*		buf,		/*!< out: output buffer */
310
	ulint		buf_size);	/*!< in: output buffer size
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
311
					in bytes */
312
313
#ifndef UNIV_NONINL
314
#include "ut0mem.ic"
315
#endif
316
317
#endif