~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2009-09-30 07:01:32 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20090930070132-b1ol1xu1rpajdddy
Small namespace cleanup.

Show diffs side-by-side

added added

removed removed

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