~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/ut/ut0mem.c

Merged in InnoDB Plugin tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
typedef struct ut_mem_block_struct ut_mem_block_t;
21
21
 
22
22
/* The total amount of memory currently allocated from the OS with malloc */
23
 
ulint   ut_total_allocated_memory       = 0;
 
23
UNIV_INTERN ulint       ut_total_allocated_memory       = 0;
24
24
 
25
25
struct ut_mem_block_struct{
26
26
        UT_LIST_NODE_T(ut_mem_block_t) mem_block_list;
33
33
 
34
34
/* List of all memory blocks allocated from the operating system
35
35
with malloc */
36
 
UT_LIST_BASE_NODE_T(ut_mem_block_t)   ut_mem_block_list;
37
 
 
38
 
os_fast_mutex_t ut_list_mutex;  /* this protects the list */
39
 
 
40
 
ibool  ut_mem_block_list_inited = FALSE;
41
 
 
42
 
ulint*  ut_mem_null_ptr = NULL;
 
36
static UT_LIST_BASE_NODE_T(ut_mem_block_t)   ut_mem_block_list;
 
37
 
 
38
static os_fast_mutex_t ut_list_mutex;   /* this protects the list */
 
39
 
 
40
static ibool  ut_mem_block_list_inited = FALSE;
 
41
 
 
42
static ulint*   ut_mem_null_ptr = NULL;
43
43
 
44
44
/**************************************************************************
45
45
Initializes the mem block list at database startup. */
56
56
/**************************************************************************
57
57
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
58
58
defined and set_to_zero is TRUE. */
59
 
 
 
59
UNIV_INTERN
60
60
void*
61
61
ut_malloc_low(
62
62
/*==========*/
138
138
                trace */
139
139
                /* Intentional segfault on NetWare causes an abend. Avoid this
140
140
                by graceful exit handling in ut_a(). */
 
141
#if (!defined __NETWARE__)
141
142
                if (assert_on_error) {
142
143
                        ut_print_timestamp(stderr);
143
144
 
150
151
                } else {
151
152
                        return(NULL);
152
153
                }
 
154
#else
 
155
                ut_a(0);
 
156
#endif
153
157
        }
154
158
 
155
159
        if (set_to_zero) {
175
179
/**************************************************************************
176
180
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
177
181
defined. */
178
 
 
 
182
UNIV_INTERN
179
183
void*
180
184
ut_malloc(
181
185
/*======*/
189
193
Tests if malloc of n bytes would succeed. ut_malloc() asserts if memory runs
190
194
out. It cannot be used if we want to return an error message. Prints to
191
195
stderr a message if fails. */
192
 
 
 
196
UNIV_INTERN
193
197
ibool
194
198
ut_test_malloc(
195
199
/*===========*/
227
231
 
228
232
/**************************************************************************
229
233
Frees a memory block allocated with ut_malloc. */
230
 
 
 
234
UNIV_INTERN
231
235
void
232
236
ut_free(
233
237
/*====*/
274
278
       be passed to free() is returned.  If realloc()  fails  the
275
279
       original  block  is  left  untouched  - it is not freed or
276
280
       moved. */
277
 
 
 
281
UNIV_INTERN
278
282
void*
279
283
ut_realloc(
280
284
/*=======*/
327
331
 
328
332
/**************************************************************************
329
333
Frees in shutdown all allocated memory not freed yet. */
330
 
 
 
334
UNIV_INTERN
331
335
void
332
336
ut_free_all_mem(void)
333
337
/*=================*/
359
363
Copies up to size - 1 characters from the NUL-terminated string src to
360
364
dst, NUL-terminating the result. Returns strlen(src), so truncation
361
365
occurred if the return value >= size. */
362
 
 
 
366
UNIV_INTERN
363
367
ulint
364
368
ut_strlcpy(
365
369
/*=======*/
383
387
/**************************************************************************
384
388
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
385
389
(size - 1) bytes of src, not the first. */
386
 
 
 
390
UNIV_INTERN
387
391
ulint
388
392
ut_strlcpy_rev(
389
393
/*===========*/
407
411
Make a quoted copy of a NUL-terminated string.  Leading and trailing
408
412
quotes will not be included; only embedded quotes will be escaped.
409
413
See also ut_strlenq() and ut_memcpyq(). */
410
 
 
 
414
UNIV_INTERN
411
415
char*
412
416
ut_strcpyq(
413
417
/*=======*/
429
433
Make a quoted copy of a fixed-length string.  Leading and trailing
430
434
quotes will not be included; only embedded quotes will be escaped.
431
435
See also ut_strlenq() and ut_strcpyq(). */
432
 
 
 
436
UNIV_INTERN
433
437
char*
434
438
ut_memcpyq(
435
439
/*=======*/
453
457
/**************************************************************************
454
458
Return the number of times s2 occurs in s1. Overlapping instances of s2
455
459
are only counted once. */
456
 
 
 
460
UNIV_INTERN
457
461
ulint
458
462
ut_strcount(
459
463
/*========*/
487
491
/**************************************************************************
488
492
Replace every occurrence of s1 in str with s2. Overlapping instances of s1
489
493
are only replaced once. */
490
 
 
491
 
char *
 
494
UNIV_INTERN
 
495
char*
492
496
ut_strreplace(
493
497
/*==========*/
494
498
                                /* out, own: modified string, must be
542
546
 
543
547
        return(new_str);
544
548
}
 
549
 
 
550
#ifdef UNIV_COMPILE_TEST_FUNCS
 
551
 
 
552
void
 
553
test_ut_str_sql_format()
 
554
{
 
555
        char    buf[128];
 
556
        ulint   ret;
 
557
 
 
558
#define CALL_AND_TEST(str, str_len, buf, buf_size, ret_expected, buf_expected)\
 
559
        do {\
 
560
                ibool   ok = TRUE;\
 
561
                memset(buf, 'x', 10);\
 
562
                buf[10] = '\0';\
 
563
                fprintf(stderr, "TESTING \"%s\", %lu, %lu\n",\
 
564
                        str, (ulint) str_len, (ulint) buf_size);\
 
565
                ret = ut_str_sql_format(str, str_len, buf, buf_size);\
 
566
                if (ret != ret_expected) {\
 
567
                        fprintf(stderr, "expected ret %lu, got %lu\n",\
 
568
                                (ulint) ret_expected, ret);\
 
569
                        ok = FALSE;\
 
570
                }\
 
571
                if (strcmp((char*) buf, buf_expected) != 0) {\
 
572
                        fprintf(stderr, "expected buf \"%s\", got \"%s\"\n",\
 
573
                                buf_expected, buf);\
 
574
                        ok = FALSE;\
 
575
                }\
 
576
                if (ok) {\
 
577
                        fprintf(stderr, "OK: %lu, \"%s\"\n\n",\
 
578
                                (ulint) ret, buf);\
 
579
                } else {\
 
580
                        return;\
 
581
                }\
 
582
        } while (0)
 
583
 
 
584
        CALL_AND_TEST("abcd", 4, buf, 0, 0, "xxxxxxxxxx");
 
585
 
 
586
        CALL_AND_TEST("abcd", 4, buf, 1, 1, "");
 
587
 
 
588
        CALL_AND_TEST("abcd", 4, buf, 2, 1, "");
 
589
 
 
590
        CALL_AND_TEST("abcd", 0, buf, 3, 3, "''");
 
591
        CALL_AND_TEST("abcd", 1, buf, 3, 1, "");
 
592
        CALL_AND_TEST("abcd", 2, buf, 3, 1, "");
 
593
        CALL_AND_TEST("abcd", 3, buf, 3, 1, "");
 
594
        CALL_AND_TEST("abcd", 4, buf, 3, 1, "");
 
595
 
 
596
        CALL_AND_TEST("abcd", 0, buf, 4, 3, "''");
 
597
        CALL_AND_TEST("abcd", 1, buf, 4, 4, "'a'");
 
598
        CALL_AND_TEST("abcd", 2, buf, 4, 4, "'a'");
 
599
        CALL_AND_TEST("abcd", 3, buf, 4, 4, "'a'");
 
600
        CALL_AND_TEST("abcd", 4, buf, 4, 4, "'a'");
 
601
        CALL_AND_TEST("abcde", 5, buf, 4, 4, "'a'");
 
602
        CALL_AND_TEST("'", 1, buf, 4, 3, "''");
 
603
        CALL_AND_TEST("''", 2, buf, 4, 3, "''");
 
604
        CALL_AND_TEST("a'", 2, buf, 4, 4, "'a'");
 
605
        CALL_AND_TEST("'a", 2, buf, 4, 3, "''");
 
606
        CALL_AND_TEST("ab", 2, buf, 4, 4, "'a'");
 
607
 
 
608
        CALL_AND_TEST("abcdef", 0, buf, 5, 3, "''");
 
609
        CALL_AND_TEST("abcdef", 1, buf, 5, 4, "'a'");
 
610
        CALL_AND_TEST("abcdef", 2, buf, 5, 5, "'ab'");
 
611
        CALL_AND_TEST("abcdef", 3, buf, 5, 5, "'ab'");
 
612
        CALL_AND_TEST("abcdef", 4, buf, 5, 5, "'ab'");
 
613
        CALL_AND_TEST("abcdef", 5, buf, 5, 5, "'ab'");
 
614
        CALL_AND_TEST("abcdef", 6, buf, 5, 5, "'ab'");
 
615
        CALL_AND_TEST("'", 1, buf, 5, 5, "''''");
 
616
        CALL_AND_TEST("''", 2, buf, 5, 5, "''''");
 
617
        CALL_AND_TEST("a'", 2, buf, 5, 4, "'a'");
 
618
        CALL_AND_TEST("'a", 2, buf, 5, 5, "''''");
 
619
        CALL_AND_TEST("ab", 2, buf, 5, 5, "'ab'");
 
620
        CALL_AND_TEST("abc", 3, buf, 5, 5, "'ab'");
 
621
 
 
622
        CALL_AND_TEST("ab", 2, buf, 6, 5, "'ab'");
 
623
 
 
624
        CALL_AND_TEST("a'b'c", 5, buf, 32, 10, "'a''b''c'");
 
625
        CALL_AND_TEST("a'b'c'", 6, buf, 32, 12, "'a''b''c'''");
 
626
}
 
627
 
 
628
#endif /* UNIV_COMPILE_TEST_FUNCS */