32 #ifndef UNIV_HOTBACKUP
48 UNIV_INTERN mysql_pfs_key_t ut_list_mutex_key;
61 #define UT_MEM_MAGIC_N 1601650166
68 static ibool ut_mem_block_list_inited = FALSE;
72 static ulint* ut_mem_null_ptr = NULL;
81 ut_a(!ut_mem_block_list_inited);
84 ut_mem_block_list_inited = TRUE;
96 ibool assert_on_error)
99 #ifndef UNIV_HOTBACKUP
103 if (UNIV_LIKELY(srv_use_sys_malloc)) {
105 ut_a(ret || !assert_on_error);
111 ut_a(ut_mem_block_list_inited);
119 if (ret == NULL && retry_count < 60) {
120 if (retry_count == 0) {
124 " InnoDB: Error: cannot allocate"
126 "InnoDB: memory with malloc!"
127 " Total allocated memory\n"
128 "InnoDB: by InnoDB %lu bytes."
129 " Operating system errno: %lu\n"
130 "InnoDB: Check if you should"
131 " increase the swap file or\n"
132 "InnoDB: ulimits of your operating system.\n"
133 "InnoDB: On FreeBSD check you"
134 " have compiled the OS with\n"
135 "InnoDB: a big enough maximum process size.\n"
136 "InnoDB: Note that in most 32-bit"
137 " computers the process\n"
138 "InnoDB: memory space is limited"
139 " to 2 GB or 4 GB.\n"
140 "InnoDB: We keep retrying"
141 " the allocation for 60 seconds...\n",
144 (ulong) GetLastError()
174 if (assert_on_error) {
178 " InnoDB: We now intentionally"
179 " generate a seg fault so that\n"
180 "InnoDB: on Linux we get a stack trace.\n");
182 if (*ut_mem_null_ptr) ut_mem_null_ptr = 0;
201 void* ret = malloc(n);
202 ut_a(ret || !assert_on_error);
217 #ifndef UNIV_HOTBACKUP
222 }
else if (UNIV_LIKELY(srv_use_sys_malloc)) {
245 #ifndef UNIV_HOTBACKUP
283 if (UNIV_LIKELY(srv_use_sys_malloc)) {
284 return(realloc(ptr, size));
304 if (size < old_size) {
312 if (new_ptr == NULL) {
334 ut_a(ut_mem_block_list_inited);
335 ut_mem_block_list_inited = FALSE;
351 "InnoDB: Warning: after shutdown"
352 " total allocated memory is %lu\n",
356 ut_mem_block_list_inited = FALSE;
373 ulint src_size = strlen(src);
376 ulint
n =
ut_min(src_size, size - 1);
397 ulint src_size = strlen(src);
400 ulint
n =
ut_min(src_size, size - 1);
402 memcpy(dst, src + src_size - n, n + 1);
408 #ifndef UNIV_HOTBACKUP
421 ulint len = strlen(s2);
456 ulint s1_len = strlen(s1);
457 ulint s2_len = strlen(s2);
458 ulint s3_len = strlen(s3);
460 s =
static_cast<char*
>(mem_alloc(s1_len + s2_len + s3_len + 1));
462 memcpy(s, s1, s1_len);
463 memcpy(s + s1_len, s2, s2_len);
464 memcpy(s + s1_len + s2_len, s3, s3_len);
466 s[s1_len + s2_len + s3_len] =
'\0';
485 ulint str_len = strlen(str);
486 ulint s1_len = strlen(s1);
487 ulint s2_len = strlen(s2);
489 int len_delta = (int) s2_len - (
int) s1_len;
491 str_end = str + str_len;
493 if (len_delta <= 0) {
499 new_str =
static_cast<char*
>(
500 mem_alloc(str_len + count * len_delta + 1));
505 const char* next = strstr(str, s1);
511 memcpy(ptr, str, next - str);
514 if (next == str_end) {
519 memcpy(ptr, s2, s2_len);
530 #ifdef UNIV_COMPILE_TEST_FUNCS
533 test_ut_str_sql_format()
538 #define CALL_AND_TEST(str, str_len, buf, buf_size, ret_expected, buf_expected)\
541 memset(buf, 'x', 10);\
543 fprintf(stderr, "TESTING \"%s\", %lu, %lu\n",\
544 str, (ulint) str_len, (ulint) buf_size);\
545 ret = ut_str_sql_format(str, str_len, buf, buf_size);\
546 if (ret != ret_expected) {\
547 fprintf(stderr, "expected ret %lu, got %lu\n",\
548 (ulint) ret_expected, ret);\
551 if (strcmp((char*) buf, buf_expected) != 0) {\
552 fprintf(stderr, "expected buf \"%s\", got \"%s\"\n",\
557 fprintf(stderr, "OK: %lu, \"%s\"\n\n",\
564 CALL_AND_TEST(
"abcd", 4, buf, 0, 0,
"xxxxxxxxxx");
566 CALL_AND_TEST(
"abcd", 4, buf, 1, 1,
"");
568 CALL_AND_TEST(
"abcd", 4, buf, 2, 1,
"");
570 CALL_AND_TEST(
"abcd", 0, buf, 3, 3,
"''");
571 CALL_AND_TEST(
"abcd", 1, buf, 3, 1,
"");
572 CALL_AND_TEST(
"abcd", 2, buf, 3, 1,
"");
573 CALL_AND_TEST(
"abcd", 3, buf, 3, 1,
"");
574 CALL_AND_TEST(
"abcd", 4, buf, 3, 1,
"");
576 CALL_AND_TEST(
"abcd", 0, buf, 4, 3,
"''");
577 CALL_AND_TEST(
"abcd", 1, buf, 4, 4,
"'a'");
578 CALL_AND_TEST(
"abcd", 2, buf, 4, 4,
"'a'");
579 CALL_AND_TEST(
"abcd", 3, buf, 4, 4,
"'a'");
580 CALL_AND_TEST(
"abcd", 4, buf, 4, 4,
"'a'");
581 CALL_AND_TEST(
"abcde", 5, buf, 4, 4,
"'a'");
582 CALL_AND_TEST(
"'", 1, buf, 4, 3,
"''");
583 CALL_AND_TEST(
"''", 2, buf, 4, 3,
"''");
584 CALL_AND_TEST(
"a'", 2, buf, 4, 4,
"'a'");
585 CALL_AND_TEST(
"'a", 2, buf, 4, 3,
"''");
586 CALL_AND_TEST(
"ab", 2, buf, 4, 4,
"'a'");
588 CALL_AND_TEST(
"abcdef", 0, buf, 5, 3,
"''");
589 CALL_AND_TEST(
"abcdef", 1, buf, 5, 4,
"'a'");
590 CALL_AND_TEST(
"abcdef", 2, buf, 5, 5,
"'ab'");
591 CALL_AND_TEST(
"abcdef", 3, buf, 5, 5,
"'ab'");
592 CALL_AND_TEST(
"abcdef", 4, buf, 5, 5,
"'ab'");
593 CALL_AND_TEST(
"abcdef", 5, buf, 5, 5,
"'ab'");
594 CALL_AND_TEST(
"abcdef", 6, buf, 5, 5,
"'ab'");
595 CALL_AND_TEST(
"'", 1, buf, 5, 5,
"''''");
596 CALL_AND_TEST(
"''", 2, buf, 5, 5,
"''''");
597 CALL_AND_TEST(
"a'", 2, buf, 5, 4,
"'a'");
598 CALL_AND_TEST(
"'a", 2, buf, 5, 5,
"''''");
599 CALL_AND_TEST(
"ab", 2, buf, 5, 5,
"'ab'");
600 CALL_AND_TEST(
"abc", 3, buf, 5, 5,
"'ab'");
602 CALL_AND_TEST(
"ab", 2, buf, 6, 5,
"'ab'");
604 CALL_AND_TEST(
"a'b'c", 5, buf, 32, 10,
"'a''b''c'");
605 CALL_AND_TEST(
"a'b'c'", 6, buf, 32, 12,
"'a''b''c'''");