~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2008-12-12 20:40:37 UTC
  • mto: This revision was merged to the branch mainline in revision 676.
  • Revision ID: monty@inaugust.com-20081212204037-udss8n7zagthzc19
Renamed functions to function... everything else is singular.

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