~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty
  • Date: 2008-11-07 05:51:15 UTC
  • mto: This revision was merged to the branch mainline in revision 579.
  • Revision ID: mordred@palanthas.inaugust.com-20081107055115-0275gvq62buzls77
Fixed a decimal sign thing.

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.ic
21
 
Memory primitives
22
 
 
23
 
Created 5/30/1994 Heikki Tuuri
24
 
************************************************************************/
25
 
 
26
 
#include "ut0byte.h"
27
 
#include "mach0data.h"
28
 
 
29
 
/** Wrapper for memcpy(3).  Copy memory area when the source and
30
 
target are not overlapping.
31
 
* @param dest   in: copy to
32
 
* @param sour   in: copy from
33
 
* @param n      in: number of bytes to copy
34
 
* @return       dest */
35
 
UNIV_INLINE
36
 
void*
37
 
ut_memcpy(void* dest, const void* sour, ulint n)
38
 
{
39
 
        return(memcpy(dest, sour, n));
40
 
}
41
 
 
42
 
/** Wrapper for memmove(3).  Copy memory area when the source and
43
 
target are overlapping.
44
 
* @param dest   in: copy to
45
 
* @param sour   in: copy from
46
 
* @param n      in: number of bytes to copy
47
 
* @return       dest */
48
 
UNIV_INLINE
49
 
void*
50
 
ut_memmove(void* dest, const void* sour, ulint n)
51
 
{
52
 
        return(memmove(dest, sour, n));
53
 
}
54
 
 
55
 
/** Wrapper for memcmp(3).  Compare memory areas.
56
 
* @param str1   in: first memory block to compare
57
 
* @param str2   in: second memory block to compare
58
 
* @param n      in: number of bytes to compare
59
 
* @return       negative, 0, or positive if str1 is smaller, equal,
60
 
                or greater than str2, respectively. */
61
 
UNIV_INLINE
62
 
int
63
 
ut_memcmp(const void* str1, const void* str2, ulint n)
64
 
{
65
 
        return(memcmp(str1, str2, n));
66
 
}
67
 
 
68
 
/** Wrapper for strcpy(3).  Copy a NUL-terminated string.
69
 
* @param dest   in: copy to
70
 
* @param sour   in: copy from
71
 
* @return       dest */
72
 
UNIV_INLINE
73
 
char*
74
 
ut_strcpy(char* dest, const char* sour)
75
 
{
76
 
        return(strcpy(dest, sour));
77
 
}
78
 
 
79
 
/** Wrapper for strlen(3).  Determine the length of a NUL-terminated string.
80
 
* @param str    in: string
81
 
* @return       length of the string in bytes, excluding the terminating NUL */
82
 
UNIV_INLINE
83
 
ulint
84
 
ut_strlen(const char* str)
85
 
{
86
 
        return(strlen(str));
87
 
}
88
 
 
89
 
/** Wrapper for strcmp(3).  Compare NUL-terminated strings.
90
 
* @param str1   in: first string to compare
91
 
* @param str2   in: second string to compare
92
 
* @return       negative, 0, or positive if str1 is smaller, equal,
93
 
                or greater than str2, respectively. */
94
 
UNIV_INLINE
95
 
int
96
 
ut_strcmp(const char* str1, const char* str2)
97
 
{
98
 
        return(strcmp(str1, str2));
99
 
}
100
 
 
101
 
/**********************************************************************//**
102
 
Compute strlen(ut_strcpyq(str, q)).
103
 
@return length of the string when quoted */
104
 
UNIV_INLINE
105
 
ulint
106
 
ut_strlenq(
107
 
/*=======*/
108
 
        const char*     str,    /*!< in: null-terminated string */
109
 
        char            q)      /*!< in: the quote character */
110
 
{
111
 
        ulint len;
112
 
 
113
 
        for (len = 0; *str; len++, str++) {
114
 
                if (*str == q) {
115
 
                        len++;
116
 
                }
117
 
        }
118
 
 
119
 
        return(len);
120
 
}
121
 
 
122
 
/**********************************************************************//**
123
 
Converts a raw binary data to a NUL-terminated hex string. The output is
124
 
truncated if there is not enough space in "hex", make sure "hex_size" is at
125
 
least (2 * raw_size + 1) if you do not want this to happen. Returns the
126
 
actual number of characters written to "hex" (including the NUL).
127
 
@return number of chars written */
128
 
UNIV_INLINE
129
 
ulint
130
 
ut_raw_to_hex(
131
 
/*==========*/
132
 
        const void*     raw,            /*!< in: raw data */
133
 
        ulint           raw_size,       /*!< in: "raw" length in bytes */
134
 
        char*           hex,            /*!< out: hex string */
135
 
        ulint           hex_size)       /*!< in: "hex" size in bytes */
136
 
{
137
 
 
138
 
#ifdef WORDS_BIGENDIAN
139
 
 
140
 
#define MK_UINT16(a, b) (((uint16) (a)) << 8 | (uint16) (b))
141
 
 
142
 
#define UINT16_GET_A(u) ((unsigned char) ((u) >> 8))
143
 
#define UINT16_GET_B(u) ((unsigned char) ((u) & 0xFF))
144
 
 
145
 
#else /* WORDS_BIGENDIAN */
146
 
 
147
 
#define MK_UINT16(a, b) (((uint16) (b)) << 8 | (uint16) (a))
148
 
 
149
 
#define UINT16_GET_A(u) ((unsigned char) ((u) & 0xFF))
150
 
#define UINT16_GET_B(u) ((unsigned char) ((u) >> 8))
151
 
 
152
 
#endif /* WORDS_BIGENDIAN */
153
 
 
154
 
#define MK_ALL_UINT16_WITH_A(a) \
155
 
        MK_UINT16(a, '0'),      \
156
 
        MK_UINT16(a, '1'),      \
157
 
        MK_UINT16(a, '2'),      \
158
 
        MK_UINT16(a, '3'),      \
159
 
        MK_UINT16(a, '4'),      \
160
 
        MK_UINT16(a, '5'),      \
161
 
        MK_UINT16(a, '6'),      \
162
 
        MK_UINT16(a, '7'),      \
163
 
        MK_UINT16(a, '8'),      \
164
 
        MK_UINT16(a, '9'),      \
165
 
        MK_UINT16(a, 'A'),      \
166
 
        MK_UINT16(a, 'B'),      \
167
 
        MK_UINT16(a, 'C'),      \
168
 
        MK_UINT16(a, 'D'),      \
169
 
        MK_UINT16(a, 'E'),      \
170
 
        MK_UINT16(a, 'F')
171
 
 
172
 
        static const uint16     hex_map[256] = {
173
 
                MK_ALL_UINT16_WITH_A('0'),
174
 
                MK_ALL_UINT16_WITH_A('1'),
175
 
                MK_ALL_UINT16_WITH_A('2'),
176
 
                MK_ALL_UINT16_WITH_A('3'),
177
 
                MK_ALL_UINT16_WITH_A('4'),
178
 
                MK_ALL_UINT16_WITH_A('5'),
179
 
                MK_ALL_UINT16_WITH_A('6'),
180
 
                MK_ALL_UINT16_WITH_A('7'),
181
 
                MK_ALL_UINT16_WITH_A('8'),
182
 
                MK_ALL_UINT16_WITH_A('9'),
183
 
                MK_ALL_UINT16_WITH_A('A'),
184
 
                MK_ALL_UINT16_WITH_A('B'),
185
 
                MK_ALL_UINT16_WITH_A('C'),
186
 
                MK_ALL_UINT16_WITH_A('D'),
187
 
                MK_ALL_UINT16_WITH_A('E'),
188
 
                MK_ALL_UINT16_WITH_A('F')
189
 
        };
190
 
        const unsigned char*    rawc;
191
 
        ulint                   read_bytes;
192
 
        ulint                   write_bytes;
193
 
        ulint                   i;
194
 
 
195
 
        rawc = (const unsigned char*) raw;
196
 
 
197
 
        if (hex_size == 0) {
198
 
 
199
 
                return(0);
200
 
        }
201
 
 
202
 
        if (hex_size <= 2 * raw_size) {
203
 
 
204
 
                read_bytes = hex_size / 2;
205
 
                write_bytes = hex_size;
206
 
        } else {
207
 
 
208
 
                read_bytes = raw_size;
209
 
                write_bytes = 2 * raw_size + 1;
210
 
        }
211
 
 
212
 
#define LOOP_READ_BYTES(ASSIGN)                 \
213
 
        for (i = 0; i < read_bytes; i++) {      \
214
 
                ASSIGN;                         \
215
 
                hex += 2;                       \
216
 
                rawc++;                         \
217
 
        }
218
 
 
219
 
        if (ut_align_offset(hex, 2) == 0) {
220
 
 
221
 
                LOOP_READ_BYTES(
222
 
                        *(uint16*) hex = hex_map[*rawc]
223
 
                );
224
 
        } else {
225
 
 
226
 
                LOOP_READ_BYTES(
227
 
                        *hex       = UINT16_GET_A(hex_map[*rawc]);
228
 
                        *(hex + 1) = UINT16_GET_B(hex_map[*rawc])
229
 
                );
230
 
        }
231
 
 
232
 
        if (hex_size <= 2 * raw_size && hex_size % 2 == 0) {
233
 
 
234
 
                hex--;
235
 
        }
236
 
 
237
 
        *hex = '\0';
238
 
 
239
 
        return(write_bytes);
240
 
}
241
 
 
242
 
/*******************************************************************//**
243
 
Adds single quotes to the start and end of string and escapes any quotes
244
 
by doubling them. Returns the number of bytes that were written to "buf"
245
 
(including the terminating NUL). If buf_size is too small then the
246
 
trailing bytes from "str" are discarded.
247
 
@return number of bytes that were written */
248
 
UNIV_INLINE
249
 
ulint
250
 
ut_str_sql_format(
251
 
/*==============*/
252
 
        const char*     str,            /*!< in: string */
253
 
        ulint           str_len,        /*!< in: string length in bytes */
254
 
        char*           buf,            /*!< out: output buffer */
255
 
        ulint           buf_size)       /*!< in: output buffer size
256
 
                                        in bytes */
257
 
{
258
 
        ulint   str_i;
259
 
        ulint   buf_i;
260
 
 
261
 
        buf_i = 0;
262
 
 
263
 
        switch (buf_size) {
264
 
        case 3:
265
 
 
266
 
                if (str_len == 0) {
267
 
 
268
 
                        buf[buf_i] = '\'';
269
 
                        buf_i++;
270
 
                        buf[buf_i] = '\'';
271
 
                        buf_i++;
272
 
                }
273
 
                /* FALLTHROUGH */
274
 
        case 2:
275
 
        case 1:
276
 
 
277
 
                buf[buf_i] = '\0';
278
 
                buf_i++;
279
 
                /* FALLTHROUGH */
280
 
        case 0:
281
 
 
282
 
                return(buf_i);
283
 
        }
284
 
 
285
 
        /* buf_size >= 4 */
286
 
 
287
 
        buf[0] = '\'';
288
 
        buf_i = 1;
289
 
 
290
 
        for (str_i = 0; str_i < str_len; str_i++) {
291
 
 
292
 
                char    ch;
293
 
 
294
 
                if (buf_size - buf_i == 2) {
295
 
 
296
 
                        break;
297
 
                }
298
 
 
299
 
                ch = str[str_i];
300
 
 
301
 
                switch (ch) {
302
 
                case '\0':
303
 
 
304
 
                        if (UNIV_UNLIKELY(buf_size - buf_i < 4)) {
305
 
 
306
 
                                goto func_exit;
307
 
                        }
308
 
                        buf[buf_i] = '\\';
309
 
                        buf_i++;
310
 
                        buf[buf_i] = '0';
311
 
                        buf_i++;
312
 
                        break;
313
 
                case '\'':
314
 
                case '\\':
315
 
 
316
 
                        if (UNIV_UNLIKELY(buf_size - buf_i < 4)) {
317
 
 
318
 
                                goto func_exit;
319
 
                        }
320
 
                        buf[buf_i] = ch;
321
 
                        buf_i++;
322
 
                        /* FALLTHROUGH */
323
 
                default:
324
 
 
325
 
                        buf[buf_i] = ch;
326
 
                        buf_i++;
327
 
                }
328
 
        }
329
 
 
330
 
func_exit:
331
 
 
332
 
        buf[buf_i] = '\'';
333
 
        buf_i++;
334
 
        buf[buf_i] = '\0';
335
 
        buf_i++;
336
 
 
337
 
        return(buf_i);
338
 
}