~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2008-09-15 17:24:04 UTC
  • Revision ID: monty@inaugust.com-20080915172404-ygh6hiyu0q7qpa9x
Removed strndup calls.

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
 
UNIV_INTERN ulint       ut_total_allocated_memory       = 0;
 
23
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
 
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;
 
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;
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
 
UNIV_INTERN
 
59
 
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__)
142
141
                if (assert_on_error) {
143
142
                        ut_print_timestamp(stderr);
144
143
 
151
150
                } else {
152
151
                        return(NULL);
153
152
                }
154
 
#else
155
 
                ut_a(0);
156
 
#endif
157
153
        }
158
154
 
159
155
        if (set_to_zero) {
179
175
/**************************************************************************
180
176
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
181
177
defined. */
182
 
UNIV_INTERN
 
178
 
183
179
void*
184
180
ut_malloc(
185
181
/*======*/
193
189
Tests if malloc of n bytes would succeed. ut_malloc() asserts if memory runs
194
190
out. It cannot be used if we want to return an error message. Prints to
195
191
stderr a message if fails. */
196
 
UNIV_INTERN
 
192
 
197
193
ibool
198
194
ut_test_malloc(
199
195
/*===========*/
231
227
 
232
228
/**************************************************************************
233
229
Frees a memory block allocated with ut_malloc. */
234
 
UNIV_INTERN
 
230
 
235
231
void
236
232
ut_free(
237
233
/*====*/
278
274
       be passed to free() is returned.  If realloc()  fails  the
279
275
       original  block  is  left  untouched  - it is not freed or
280
276
       moved. */
281
 
UNIV_INTERN
 
277
 
282
278
void*
283
279
ut_realloc(
284
280
/*=======*/
331
327
 
332
328
/**************************************************************************
333
329
Frees in shutdown all allocated memory not freed yet. */
334
 
UNIV_INTERN
 
330
 
335
331
void
336
332
ut_free_all_mem(void)
337
333
/*=================*/
363
359
Copies up to size - 1 characters from the NUL-terminated string src to
364
360
dst, NUL-terminating the result. Returns strlen(src), so truncation
365
361
occurred if the return value >= size. */
366
 
UNIV_INTERN
 
362
 
367
363
ulint
368
364
ut_strlcpy(
369
365
/*=======*/
387
383
/**************************************************************************
388
384
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
389
385
(size - 1) bytes of src, not the first. */
390
 
UNIV_INTERN
 
386
 
391
387
ulint
392
388
ut_strlcpy_rev(
393
389
/*===========*/
411
407
Make a quoted copy of a NUL-terminated string.  Leading and trailing
412
408
quotes will not be included; only embedded quotes will be escaped.
413
409
See also ut_strlenq() and ut_memcpyq(). */
414
 
UNIV_INTERN
 
410
 
415
411
char*
416
412
ut_strcpyq(
417
413
/*=======*/
433
429
Make a quoted copy of a fixed-length string.  Leading and trailing
434
430
quotes will not be included; only embedded quotes will be escaped.
435
431
See also ut_strlenq() and ut_strcpyq(). */
436
 
UNIV_INTERN
 
432
 
437
433
char*
438
434
ut_memcpyq(
439
435
/*=======*/
457
453
/**************************************************************************
458
454
Return the number of times s2 occurs in s1. Overlapping instances of s2
459
455
are only counted once. */
460
 
UNIV_INTERN
 
456
 
461
457
ulint
462
458
ut_strcount(
463
459
/*========*/
491
487
/**************************************************************************
492
488
Replace every occurrence of s1 in str with s2. Overlapping instances of s1
493
489
are only replaced once. */
494
 
UNIV_INTERN
495
 
char*
 
490
 
 
491
char *
496
492
ut_strreplace(
497
493
/*==========*/
498
494
                                /* out, own: modified string, must be
546
542
 
547
543
        return(new_str);
548
544
}
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 */