~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/innobase/include/ut0mem.h

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
Import upstream version 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
/*******************************************************************//**
 
20
@file include/ut0mem.h
 
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>
 
31
#include <sys/types.h>
 
32
#ifndef UNIV_HOTBACKUP
 
33
# include "os0sync.h"
 
34
 
 
35
/** The total amount of memory currently allocated from the operating
 
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
 
 
40
/** Mutex protecting ut_total_allocated_memory and ut_mem_block_list */
 
41
extern os_fast_mutex_t  ut_list_mutex;
 
42
#endif /* !UNIV_HOTBACKUP */
 
43
 
 
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 */
 
50
UNIV_INLINE
 
51
void*
 
52
ut_memcpy(void* dest, const void* sour, ulint n);
 
53
 
 
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 */
 
60
UNIV_INLINE
 
61
void*
 
62
ut_memmove(void* dest, const void* sour, ulint n);
 
63
 
 
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. */
 
70
UNIV_INLINE
 
71
int
 
72
ut_memcmp(const void* str1, const void* str2, ulint n);
 
73
 
 
74
/**********************************************************************//**
 
75
Initializes the mem block list at database startup. */
 
76
UNIV_INTERN
 
77
void
 
78
ut_mem_init(void);
 
79
/*=============*/
 
80
 
 
81
/**********************************************************************//**
 
82
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
 
83
defined and set_to_zero is TRUE.
 
84
@return own: allocated memory */
 
85
UNIV_INTERN
 
86
void*
 
87
ut_malloc_low(
 
88
/*==========*/
 
89
        ulint   n,                      /*!< in: number of bytes to allocate */
 
90
        ibool   set_to_zero,            /*!< in: TRUE if allocated memory
 
91
                                        should be set to zero if
 
92
                                        UNIV_SET_MEM_TO_ZERO is defined */
 
93
        ibool   assert_on_error);       /*!< in: if TRUE, we crash mysqld if
 
94
                                        the memory cannot be allocated */
 
95
/**********************************************************************//**
 
96
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
 
97
defined.
 
98
@return own: allocated memory */
 
99
UNIV_INTERN
 
100
void*
 
101
ut_malloc(
 
102
/*======*/
 
103
        ulint   n);     /*!< in: number of bytes to allocate */
 
104
#ifndef UNIV_HOTBACKUP
 
105
/**********************************************************************//**
 
106
Tests if malloc of n bytes would succeed. ut_malloc() asserts if memory runs
 
107
out. It cannot be used if we want to return an error message. Prints to
 
108
stderr a message if fails.
 
109
@return TRUE if succeeded */
 
110
UNIV_INTERN
 
111
ibool
 
112
ut_test_malloc(
 
113
/*===========*/
 
114
        ulint   n);     /*!< in: try to allocate this many bytes */
 
115
#endif /* !UNIV_HOTBACKUP */
 
116
/**********************************************************************//**
 
117
Frees a memory block allocated with ut_malloc. */
 
118
UNIV_INTERN
 
119
void
 
120
ut_free(
 
121
/*====*/
 
122
        void* ptr);  /*!< in, own: memory block */
 
123
#ifndef UNIV_HOTBACKUP
 
124
/**********************************************************************//**
 
125
Implements realloc. This is needed by /pars/lexyy.c. Otherwise, you should not
 
126
use this function because the allocation functions in mem0mem.h are the
 
127
recommended ones in InnoDB.
 
128
 
 
129
man realloc in Linux, 2004:
 
130
 
 
131
       realloc()  changes the size of the memory block pointed to
 
132
       by ptr to size bytes.  The contents will be  unchanged  to
 
133
       the minimum of the old and new sizes; newly allocated mem�
 
134
       ory will be uninitialized.  If ptr is NULL,  the  call  is
 
135
       equivalent  to malloc(size); if size is equal to zero, the
 
136
       call is equivalent to free(ptr).  Unless ptr is  NULL,  it
 
137
       must  have  been  returned by an earlier call to malloc(),
 
138
       calloc() or realloc().
 
139
 
 
140
RETURN VALUE
 
141
       realloc() returns a pointer to the newly allocated memory,
 
142
       which is suitably aligned for any kind of variable and may
 
143
       be different from ptr, or NULL if the  request  fails.  If
 
144
       size  was equal to 0, either NULL or a pointer suitable to
 
145
       be passed to free() is returned.  If realloc()  fails  the
 
146
       original  block  is  left  untouched  - it is not freed or
 
147
       moved.
 
148
@return own: pointer to new mem block or NULL */
 
149
UNIV_INTERN
 
150
void*
 
151
ut_realloc(
 
152
/*=======*/
 
153
        void*   ptr,    /*!< in: pointer to old block or NULL */
 
154
        ulint   size);  /*!< in: desired size */
 
155
/**********************************************************************//**
 
156
Frees in shutdown all allocated memory not freed yet. */
 
157
UNIV_INTERN
 
158
void
 
159
ut_free_all_mem(void);
 
160
/*=================*/
 
161
#endif /* !UNIV_HOTBACKUP */
 
162
 
 
163
/** Wrapper for strcpy(3).  Copy a NUL-terminated string.
 
164
* @param dest   in: copy to
 
165
* @param sour   in: copy from
 
166
* @return       dest */
 
167
UNIV_INLINE
 
168
char*
 
169
ut_strcpy(char* dest, const char* sour);
 
170
 
 
171
/** Wrapper for strlen(3).  Determine the length of a NUL-terminated string.
 
172
* @param str    in: string
 
173
* @return       length of the string in bytes, excluding the terminating NUL */
 
174
UNIV_INLINE
 
175
ulint
 
176
ut_strlen(const char* str);
 
177
 
 
178
/** Wrapper for strcmp(3).  Compare NUL-terminated strings.
 
179
* @param str1   in: first string to compare
 
180
* @param str2   in: second string to compare
 
181
* @return       negative, 0, or positive if str1 is smaller, equal,
 
182
                or greater than str2, respectively. */
 
183
UNIV_INLINE
 
184
int
 
185
ut_strcmp(const char* str1, const char* str2);
 
186
 
 
187
/**********************************************************************//**
 
188
Copies up to size - 1 characters from the NUL-terminated string src to
 
189
dst, NUL-terminating the result. Returns strlen(src), so truncation
 
190
occurred if the return value >= size.
 
191
@return strlen(src) */
 
192
UNIV_INTERN
 
193
ulint
 
194
ut_strlcpy(
 
195
/*=======*/
 
196
        char*           dst,    /*!< in: destination buffer */
 
197
        const char*     src,    /*!< in: source buffer */
 
198
        ulint           size);  /*!< in: size of destination buffer */
 
199
 
 
200
/**********************************************************************//**
 
201
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
 
202
(size - 1) bytes of src, not the first.
 
203
@return strlen(src) */
 
204
UNIV_INTERN
 
205
ulint
 
206
ut_strlcpy_rev(
 
207
/*===========*/
 
208
        char*           dst,    /*!< in: destination buffer */
 
209
        const char*     src,    /*!< in: source buffer */
 
210
        ulint           size);  /*!< in: size of destination buffer */
 
211
 
 
212
/**********************************************************************//**
 
213
Compute strlen(ut_strcpyq(str, q)).
 
214
@return length of the string when quoted */
 
215
UNIV_INLINE
 
216
ulint
 
217
ut_strlenq(
 
218
/*=======*/
 
219
        const char*     str,    /*!< in: null-terminated string */
 
220
        char            q);     /*!< in: the quote character */
 
221
 
 
222
/**********************************************************************//**
 
223
Make a quoted copy of a NUL-terminated string.  Leading and trailing
 
224
quotes will not be included; only embedded quotes will be escaped.
 
225
See also ut_strlenq() and ut_memcpyq().
 
226
@return pointer to end of dest */
 
227
UNIV_INTERN
 
228
char*
 
229
ut_strcpyq(
 
230
/*=======*/
 
231
        char*           dest,   /*!< in: output buffer */
 
232
        char            q,      /*!< in: the quote character */
 
233
        const char*     src);   /*!< in: null-terminated string */
 
234
 
 
235
/**********************************************************************//**
 
236
Make a quoted copy of a fixed-length string.  Leading and trailing
 
237
quotes will not be included; only embedded quotes will be escaped.
 
238
See also ut_strlenq() and ut_strcpyq().
 
239
@return pointer to end of dest */
 
240
UNIV_INTERN
 
241
char*
 
242
ut_memcpyq(
 
243
/*=======*/
 
244
        char*           dest,   /*!< in: output buffer */
 
245
        char            q,      /*!< in: the quote character */
 
246
        const char*     src,    /*!< in: string to be quoted */
 
247
        ulint           len);   /*!< in: length of src */
 
248
 
 
249
/**********************************************************************//**
 
250
Return the number of times s2 occurs in s1. Overlapping instances of s2
 
251
are only counted once.
 
252
@return the number of times s2 occurs in s1 */
 
253
UNIV_INTERN
 
254
ulint
 
255
ut_strcount(
 
256
/*========*/
 
257
        const char*     s1,     /*!< in: string to search in */
 
258
        const char*     s2);    /*!< in: string to search for */
 
259
 
 
260
/**********************************************************************//**
 
261
Replace every occurrence of s1 in str with s2. Overlapping instances of s1
 
262
are only replaced once.
 
263
@return own: modified string, must be freed with mem_free() */
 
264
UNIV_INTERN
 
265
char*
 
266
ut_strreplace(
 
267
/*==========*/
 
268
        const char*     str,    /*!< in: string to operate on */
 
269
        const char*     s1,     /*!< in: string to replace */
 
270
        const char*     s2);    /*!< in: string to replace s1 with */
 
271
 
 
272
/**********************************************************************//**
 
273
Converts a raw binary data to a NUL-terminated hex string. The output is
 
274
truncated if there is not enough space in "hex", make sure "hex_size" is at
 
275
least (2 * raw_size + 1) if you do not want this to happen. Returns the
 
276
actual number of characters written to "hex" (including the NUL).
 
277
@return number of chars written */
 
278
UNIV_INLINE
 
279
ulint
 
280
ut_raw_to_hex(
 
281
/*==========*/
 
282
        const void*     raw,            /*!< in: raw data */
 
283
        ulint           raw_size,       /*!< in: "raw" length in bytes */
 
284
        char*           hex,            /*!< out: hex string */
 
285
        ulint           hex_size);      /*!< in: "hex" size in bytes */
 
286
 
 
287
/*******************************************************************//**
 
288
Adds single quotes to the start and end of string and escapes any quotes
 
289
by doubling them. Returns the number of bytes that were written to "buf"
 
290
(including the terminating NUL). If buf_size is too small then the
 
291
trailing bytes from "str" are discarded.
 
292
@return number of bytes that were written */
 
293
UNIV_INLINE
 
294
ulint
 
295
ut_str_sql_format(
 
296
/*==============*/
 
297
        const char*     str,            /*!< in: string */
 
298
        ulint           str_len,        /*!< in: string length in bytes */
 
299
        char*           buf,            /*!< out: output buffer */
 
300
        ulint           buf_size);      /*!< in: output buffer size
 
301
                                        in bytes */
 
302
 
 
303
#ifndef UNIV_NONINL
 
304
#include "ut0mem.ic"
 
305
#endif
 
306
 
 
307
#endif