32 #ifndef UNIV_HOTBACKUP
60 #define UT_MEM_MAGIC_N 1601650166
67 static ibool ut_mem_block_list_inited = FALSE;
71 static ulint* ut_mem_null_ptr = NULL;
80 ut_a(!ut_mem_block_list_inited);
83 ut_mem_block_list_inited = TRUE;
99 ibool assert_on_error)
102 #ifndef UNIV_HOTBACKUP
106 if (UNIV_LIKELY(srv_use_sys_malloc)) {
108 ut_a(ret || !assert_on_error);
110 #ifdef UNIV_SET_MEM_TO_ZERO
112 memset(ret,
'\0', n);
113 UNIV_MEM_ALLOC(ret, n);
120 ut_a(ut_mem_block_list_inited);
128 if (ret == NULL && retry_count < 60) {
129 if (retry_count == 0) {
133 " InnoDB: Error: cannot allocate"
135 "InnoDB: memory with malloc!"
136 " Total allocated memory\n"
137 "InnoDB: by InnoDB %lu bytes."
138 " Operating system errno: %lu\n"
139 "InnoDB: Check if you should"
140 " increase the swap file or\n"
141 "InnoDB: ulimits of your operating system.\n"
142 "InnoDB: On FreeBSD check you"
143 " have compiled the OS with\n"
144 "InnoDB: a big enough maximum process size.\n"
145 "InnoDB: Note that in most 32-bit"
146 " computers the process\n"
147 "InnoDB: memory space is limited"
148 " to 2 GB or 4 GB.\n"
149 "InnoDB: We keep retrying"
150 " the allocation for 60 seconds...\n",
153 (ulong) GetLastError()
183 if (assert_on_error) {
187 " InnoDB: We now intentionally"
188 " generate a seg fault so that\n"
189 "InnoDB: on Linux we get a stack trace.\n");
191 if (*ut_mem_null_ptr) ut_mem_null_ptr = 0;
198 #ifdef UNIV_SET_MEM_TO_ZERO
216 void* ret = malloc(n);
217 ut_a(ret || !assert_on_error);
219 # ifdef UNIV_SET_MEM_TO_ZERO
221 memset(ret,
'\0', n);
239 #ifndef UNIV_HOTBACKUP
246 #ifndef UNIV_HOTBACKUP
265 " InnoDB: Error: cannot allocate"
266 " %lu bytes of memory for\n"
267 "InnoDB: a BLOB with malloc! Total allocated memory\n"
268 "InnoDB: by InnoDB %lu bytes."
269 " Operating system errno: %d\n"
270 "InnoDB: Check if you should increase"
271 " the swap file or\n"
272 "InnoDB: ulimits of your operating system.\n"
273 "InnoDB: On FreeBSD check you have"
274 " compiled the OS with\n"
275 "InnoDB: a big enough maximum process size.\n",
298 #ifndef UNIV_HOTBACKUP
303 }
else if (UNIV_LIKELY(srv_use_sys_malloc)) {
326 #ifndef UNIV_HOTBACKUP
365 if (UNIV_LIKELY(srv_use_sys_malloc)) {
366 return(realloc(ptr, size));
386 if (size < old_size) {
394 if (new_ptr == NULL) {
416 ut_a(ut_mem_block_list_inited);
417 ut_mem_block_list_inited = FALSE;
433 "InnoDB: Warning: after shutdown"
434 " total allocated memory is %lu\n",
438 ut_mem_block_list_inited = FALSE;
455 ulint src_size = strlen(src);
458 ulint n =
ut_min(src_size, size - 1);
479 ulint src_size = strlen(src);
482 ulint n =
ut_min(src_size, size - 1);
484 memcpy(dst, src + src_size - n, n + 1);
504 if ((*dest++ = *src++) == q) {
526 const char* srcend = src + len;
528 while (src < srcend) {
529 if ((*dest++ = *src++) == q) {
537 #ifndef UNIV_HOTBACKUP
550 ulint len = strlen(s2);
587 ulint str_len = strlen(str);
588 ulint s1_len = strlen(s1);
589 ulint s2_len = strlen(s2);
591 int len_delta = (int)s2_len - (
int)s1_len;
593 str_end = str + str_len;
595 if (len_delta <= 0) {
601 new_str =
static_cast<char *
>(mem_alloc(str_len + count * len_delta + 1));
605 const char* next = strstr(str, s1);
611 memcpy(ptr, str, next - str);
614 if (next == str_end) {
619 memcpy(ptr, s2, s2_len);
630 #ifdef UNIV_COMPILE_TEST_FUNCS
633 test_ut_str_sql_format()
638 #define CALL_AND_TEST(str, str_len, buf, buf_size, ret_expected, buf_expected)\
641 memset(buf, 'x', 10);\
643 fprintf(stderr, "TESTING \"%s\", %lu, %lu\n",\
644 str, (ulint) str_len, (ulint) buf_size);\
645 ret = ut_str_sql_format(str, str_len, buf, buf_size);\
646 if (ret != ret_expected) {\
647 fprintf(stderr, "expected ret %lu, got %lu\n",\
648 (ulint) ret_expected, ret);\
651 if (strcmp((char*) buf, buf_expected) != 0) {\
652 fprintf(stderr, "expected buf \"%s\", got \"%s\"\n",\
657 fprintf(stderr, "OK: %lu, \"%s\"\n\n",\
664 CALL_AND_TEST(
"abcd", 4, buf, 0, 0,
"xxxxxxxxxx");
666 CALL_AND_TEST(
"abcd", 4, buf, 1, 1,
"");
668 CALL_AND_TEST(
"abcd", 4, buf, 2, 1,
"");
670 CALL_AND_TEST(
"abcd", 0, buf, 3, 3,
"''");
671 CALL_AND_TEST(
"abcd", 1, buf, 3, 1,
"");
672 CALL_AND_TEST(
"abcd", 2, buf, 3, 1,
"");
673 CALL_AND_TEST(
"abcd", 3, buf, 3, 1,
"");
674 CALL_AND_TEST(
"abcd", 4, buf, 3, 1,
"");
676 CALL_AND_TEST(
"abcd", 0, buf, 4, 3,
"''");
677 CALL_AND_TEST(
"abcd", 1, buf, 4, 4,
"'a'");
678 CALL_AND_TEST(
"abcd", 2, buf, 4, 4,
"'a'");
679 CALL_AND_TEST(
"abcd", 3, buf, 4, 4,
"'a'");
680 CALL_AND_TEST(
"abcd", 4, buf, 4, 4,
"'a'");
681 CALL_AND_TEST(
"abcde", 5, buf, 4, 4,
"'a'");
682 CALL_AND_TEST(
"'", 1, buf, 4, 3,
"''");
683 CALL_AND_TEST(
"''", 2, buf, 4, 3,
"''");
684 CALL_AND_TEST(
"a'", 2, buf, 4, 4,
"'a'");
685 CALL_AND_TEST(
"'a", 2, buf, 4, 3,
"''");
686 CALL_AND_TEST(
"ab", 2, buf, 4, 4,
"'a'");
688 CALL_AND_TEST(
"abcdef", 0, buf, 5, 3,
"''");
689 CALL_AND_TEST(
"abcdef", 1, buf, 5, 4,
"'a'");
690 CALL_AND_TEST(
"abcdef", 2, buf, 5, 5,
"'ab'");
691 CALL_AND_TEST(
"abcdef", 3, buf, 5, 5,
"'ab'");
692 CALL_AND_TEST(
"abcdef", 4, buf, 5, 5,
"'ab'");
693 CALL_AND_TEST(
"abcdef", 5, buf, 5, 5,
"'ab'");
694 CALL_AND_TEST(
"abcdef", 6, buf, 5, 5,
"'ab'");
695 CALL_AND_TEST(
"'", 1, buf, 5, 5,
"''''");
696 CALL_AND_TEST(
"''", 2, buf, 5, 5,
"''''");
697 CALL_AND_TEST(
"a'", 2, buf, 5, 4,
"'a'");
698 CALL_AND_TEST(
"'a", 2, buf, 5, 5,
"''''");
699 CALL_AND_TEST(
"ab", 2, buf, 5, 5,
"'ab'");
700 CALL_AND_TEST(
"abc", 3, buf, 5, 5,
"'ab'");
702 CALL_AND_TEST(
"ab", 2, buf, 6, 5,
"'ab'");
704 CALL_AND_TEST(
"a'b'c", 5, buf, 32, 10,
"'a''b''c'");
705 CALL_AND_TEST(
"a'b'c'", 6, buf, 32, 12,
"'a''b''c'''");
UT_LIST_NODE_T(ut_mem_block_t) mem_block_list
UNIV_INTERN void os_fast_mutex_unlock(os_fast_mutex_t *fast_mutex)
UNIV_INTERN void * ut_realloc(void *ptr, ulint size)
pthread_mutex_t os_fast_mutex_t
UNIV_INTERN char * ut_strcpyq(char *dest, char q, const char *src)
UNIV_INTERN void os_fast_mutex_free(os_fast_mutex_t *fast_mutex)
UNIV_INLINE void * ut_memcpy(void *dest, const void *sour, ulint n)
ulint ut_total_allocated_memory
UNIV_INTERN void * ut_malloc(ulint n)
UNIV_INTERN void os_fast_mutex_init(os_fast_mutex_t *fast_mutex)
UNIV_INTERN void ut_free_all_mem(void)
os_fast_mutex_t ut_list_mutex
UNIV_INTERN void * ut_malloc_low(ulint n, ibool set_to_zero, ibool assert_on_error)
UNIV_INTERN char * ut_strreplace(const char *str, const char *s1, const char *s2)
UNIV_INTERN ulint ut_strcount(const char *s1, const char *s2)
UNIV_INLINE ulint ut_min(ulint n1, ulint n2)
typedef UT_LIST_BASE_NODE_T(mutex_t) ut_list_base_node_t
#define UT_LIST_REMOVE(NAME, BASE, N)
UNIV_INTERN void os_thread_sleep(ulint tm)
UNIV_INTERN ulint ut_strlcpy(char *dst, const char *src, ulint size)
UNIV_INTERN ibool ut_test_malloc(ulint n)
#define UT_LIST_GET_FIRST(BASE)
UNIV_INTERN void ut_mem_init(void)
UNIV_INTERN void ut_free(void *ptr)
#define UT_LIST_INIT(BASE)
#define UT_LIST_ADD_FIRST(NAME, BASE, N)
UNIV_INTERN char * ut_memcpyq(char *dest, char q, const char *src, ulint len)
UNIV_INTERN void ut_print_timestamp(FILE *file)
UNIV_INTERN void os_fast_mutex_lock(os_fast_mutex_t *fast_mutex)
UNIV_INTERN ulint ut_strlcpy_rev(char *dst, const char *src, ulint size)