~drizzle-trunk/drizzle/development

641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
1
/*****************************************************************************
2
3
Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
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
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
16
17
*****************************************************************************/
18
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
19
/***********************************************************************
20
Memory primitives
21
22
Created 5/30/1994 Heikki Tuuri
23
************************************************************************/
24
25
#ifndef ut0mem_h
26
#define ut0mem_h
27
28
#include "univ.i"
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
29
#include "os0sync.h"
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
30
#include <string.h>
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
31
32
/* The total amount of memory currently allocated from the operating
33
system with os_mem_alloc_large() or malloc().  Does not count malloc()
34
if srv_use_sys_malloc is set.  Protected by ut_list_mutex. */
35
extern ulint		ut_total_allocated_memory;
36
37
/* Mutex protecting ut_total_allocated_memory and ut_mem_block_list */
38
extern os_fast_mutex_t	ut_list_mutex;
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
39
40
UNIV_INLINE
41
void*
42
ut_memcpy(void* dest, const void* sour, ulint n);
43
44
UNIV_INLINE
45
void*
46
ut_memmove(void* dest, const void* sour, ulint n);
47
48
UNIV_INLINE
49
int
50
ut_memcmp(const void* str1, const void* str2, ulint n);
51
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
52
/**************************************************************************
53
Initializes the mem block list at database startup. */
54
UNIV_INTERN
55
void
56
ut_mem_init(void);
57
/*=============*/
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
58
59
/**************************************************************************
60
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
61
defined and set_to_zero is TRUE. */
62
UNIV_INTERN
63
void*
64
ut_malloc_low(
65
/*==========*/
66
					/* out, own: allocated memory */
67
	ulint	n,			/* in: number of bytes to allocate */
68
	ibool	set_to_zero,		/* in: TRUE if allocated memory
69
					should be set to zero if
70
					UNIV_SET_MEM_TO_ZERO is defined */
71
	ibool	assert_on_error);	/* in: if TRUE, we crash mysqld if
72
					the memory cannot be allocated */
73
/**************************************************************************
74
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
75
defined. */
76
UNIV_INTERN
77
void*
78
ut_malloc(
79
/*======*/
80
			/* out, own: allocated memory */
81
	ulint	n);	/* in: number of bytes to allocate */
82
/**************************************************************************
83
Tests if malloc of n bytes would succeed. ut_malloc() asserts if memory runs
84
out. It cannot be used if we want to return an error message. Prints to
85
stderr a message if fails. */
86
UNIV_INTERN
87
ibool
88
ut_test_malloc(
89
/*===========*/
90
			/* out: TRUE if succeeded */
91
	ulint	n);	/* in: try to allocate this many bytes */
92
/**************************************************************************
93
Frees a memory block allocated with ut_malloc. */
94
UNIV_INTERN
95
void
96
ut_free(
97
/*====*/
98
	void* ptr);  /* in, own: memory block */
99
/**************************************************************************
100
Implements realloc. This is needed by /pars/lexyy.c. Otherwise, you should not
101
use this function because the allocation functions in mem0mem.h are the
102
recommended ones in InnoDB.
103
104
man realloc in Linux, 2004:
105
106
       realloc()  changes the size of the memory block pointed to
107
       by ptr to size bytes.  The contents will be  unchanged  to
108
       the minimum of the old and new sizes; newly allocated memĀ­
109
       ory will be uninitialized.  If ptr is NULL,  the	 call  is
110
       equivalent  to malloc(size); if size is equal to zero, the
111
       call is equivalent to free(ptr).	 Unless ptr is	NULL,  it
112
       must  have  been	 returned by an earlier call to malloc(),
113
       calloc() or realloc().
114
115
RETURN VALUE
116
       realloc() returns a pointer to the newly allocated memory,
117
       which is suitably aligned for any kind of variable and may
118
       be different from ptr, or NULL if the  request  fails.  If
119
       size  was equal to 0, either NULL or a pointer suitable to
120
       be passed to free() is returned.	 If realloc()  fails  the
121
       original	 block	is  left  untouched  - it is not freed or
122
       moved. */
123
UNIV_INTERN
124
void*
125
ut_realloc(
126
/*=======*/
127
			/* out, own: pointer to new mem block or NULL */
128
	void*	ptr,	/* in: pointer to old block or NULL */
129
	ulint	size);	/* in: desired size */
130
/**************************************************************************
131
Frees in shutdown all allocated memory not freed yet. */
132
UNIV_INTERN
133
void
134
ut_free_all_mem(void);
135
/*=================*/
136
137
UNIV_INLINE
138
char*
139
ut_strcpy(char* dest, const char* sour);
140
141
UNIV_INLINE
142
ulint
143
ut_strlen(const char* str);
144
145
UNIV_INLINE
146
int
147
ut_strcmp(const char* str1, const char* str2);
148
149
/**************************************************************************
150
Copies up to size - 1 characters from the NUL-terminated string src to
151
dst, NUL-terminating the result. Returns strlen(src), so truncation
152
occurred if the return value >= size. */
153
UNIV_INTERN
154
ulint
155
ut_strlcpy(
156
/*=======*/
157
				/* out: strlen(src) */
158
	char*		dst,	/* in: destination buffer */
159
	const char*	src,	/* in: source buffer */
160
	ulint		size);	/* in: size of destination buffer */
161
162
/**************************************************************************
163
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
164
(size - 1) bytes of src, not the first. */
165
UNIV_INTERN
166
ulint
167
ut_strlcpy_rev(
168
/*===========*/
169
				/* out: strlen(src) */
170
	char*		dst,	/* in: destination buffer */
171
	const char*	src,	/* in: source buffer */
172
	ulint		size);	/* in: size of destination buffer */
173
174
/**************************************************************************
175
Compute strlen(ut_strcpyq(str, q)). */
176
UNIV_INLINE
177
ulint
178
ut_strlenq(
179
/*=======*/
180
				/* out: length of the string when quoted */
181
	const char*	str,	/* in: null-terminated string */
182
	char		q);	/* in: the quote character */
183
184
/**************************************************************************
185
Make a quoted copy of a NUL-terminated string.	Leading and trailing
186
quotes will not be included; only embedded quotes will be escaped.
187
See also ut_strlenq() and ut_memcpyq(). */
188
UNIV_INTERN
189
char*
190
ut_strcpyq(
191
/*=======*/
192
				/* out: pointer to end of dest */
193
	char*		dest,	/* in: output buffer */
194
	char		q,	/* in: the quote character */
195
	const char*	src);	/* in: null-terminated string */
196
197
/**************************************************************************
198
Make a quoted copy of a fixed-length string.  Leading and trailing
199
quotes will not be included; only embedded quotes will be escaped.
200
See also ut_strlenq() and ut_strcpyq(). */
201
UNIV_INTERN
202
char*
203
ut_memcpyq(
204
/*=======*/
205
				/* out: pointer to end of dest */
206
	char*		dest,	/* in: output buffer */
207
	char		q,	/* in: the quote character */
208
	const char*	src,	/* in: string to be quoted */
209
	ulint		len);	/* in: length of src */
210
211
/**************************************************************************
212
Return the number of times s2 occurs in s1. Overlapping instances of s2
213
are only counted once. */
214
UNIV_INTERN
215
ulint
216
ut_strcount(
217
/*========*/
218
				/* out: the number of times s2 occurs in s1 */
219
	const char*	s1,	/* in: string to search in */
220
	const char*	s2);	/* in: string to search for */
221
222
/**************************************************************************
223
Replace every occurrence of s1 in str with s2. Overlapping instances of s1
224
are only replaced once. */
225
UNIV_INTERN
226
char*
227
ut_strreplace(
228
/*==========*/
229
				/* out, own: modified string, must be
230
				freed with mem_free() */
231
	const char*	str,	/* in: string to operate on */
232
	const char*	s1,	/* in: string to replace */
233
	const char*	s2);	/* in: string to replace s1 with */
234
235
/**************************************************************************
236
Converts a raw binary data to a '\0'-terminated hex string. The output is
237
truncated if there is not enough space in "hex", make sure "hex_size" is at
238
least (2 * raw_size + 1) if you do not want this to happen. Returns the
239
actual number of characters written to "hex" (including the '\0'). */
240
UNIV_INLINE
241
ulint
242
ut_raw_to_hex(
243
/*==========*/
244
					/* out: number of chars written */
245
	const void*	raw,		/* in: raw data */
246
	ulint		raw_size,	/* in: "raw" length in bytes */
247
	char*		hex,		/* out: hex string */
248
	ulint		hex_size);	/* in: "hex" size in bytes */
249
250
/***********************************************************************
251
Adds single quotes to the start and end of string and escapes any quotes
252
by doubling them. Returns the number of bytes that were written to "buf"
253
(including the terminating '\0'). If buf_size is too small then the
254
trailing bytes from "str" are discarded. */
255
UNIV_INLINE
256
ulint
257
ut_str_sql_format(
258
/*==============*/
259
					/* out: number of bytes
260
					that were written */
261
	const char*	str,		/* in: string */
262
	ulint		str_len,	/* in: string length in bytes */
263
	char*		buf,		/* out: output buffer */
264
	ulint		buf_size);	/* in: output buffer size
265
					in bytes */
266
267
#ifndef UNIV_NONINL
268
#include "ut0mem.ic"
269
#endif
270
271
#endif