#include "ut0mem.h"#include "mem0mem.h"#include "os0sync.h"#include "os0thread.h"Include dependency graph for ut0mem.c:

Go to the source code of this file.
Classes | |
| struct | ut_mem_block_struct |
Defines | |
| #define | UT_MEM_MAGIC_N 1601650166 |
Typedefs | |
| typedef ut_mem_block_struct | ut_mem_block_t |
Functions | |
| UT_LIST_BASE_NODE_T (ut_mem_block_t) | |
| void * | ut_malloc_low (ulint n, ibool set_to_zero, ibool assert_on_error) |
| void * | ut_malloc (ulint n) |
| ibool | ut_test_malloc (ulint n) |
| void | ut_free (void *ptr) |
| void * | ut_realloc (void *ptr, ulint size) |
| void | ut_free_all_mem (void) |
| ulint | ut_strlcpy (char *dst, const char *src, ulint size) |
| ulint | ut_strlcpy_rev (char *dst, const char *src, ulint size) |
| char * | ut_strcpyq (char *dest, char q, const char *src) |
| char * | ut_memcpyq (char *dest, char q, const char *src, ulint len) |
| ulint | ut_strcount (const char *s1, const char *s2) |
| char * | ut_strreplace (const char *str, const char *s1, const char *s2) |
Variables | |
| ulint | ut_total_allocated_memory = 0 |
| #define UT_MEM_MAGIC_N 1601650166 |
Definition at line 32 of file ut0mem.c.
Referenced by ut_free(), ut_free_all_mem(), ut_malloc_low(), and ut_realloc().
| typedef struct ut_mem_block_struct ut_mem_block_t |
| void ut_free | ( | void * | ptr | ) |
Definition at line 221 of file ut0mem.c.
References yaSSL::block, free, os_fast_mutex_lock(), os_fast_mutex_unlock(), ut_a, UT_LIST_REMOVE, UT_MEM_MAGIC_N, and ut_total_allocated_memory.
Referenced by fil_create_new_single_table_tablespace(), fil_load_single_table_tablespace(), fil_node_open_file(), fil_open_single_table_tablespace(), fil_read_flushed_lsn_and_arch_log_no(), fil_reset_too_high_lsns(), hash_table_free(), mem_area_free(), os_event_free(), os_file_opendir(), os_file_readdir_next_file(), os_file_set_size(), os_mem_free_large(), os_mutex_free(), recv_sys_free(), sync_array_free(), trx_sys_doublewrite_init_or_restore_pages(), and ut_realloc().
00223 : memory block */ 00224 { 00225 ut_mem_block_t* block; 00226 00227 block = (ut_mem_block_t*)((byte*)ptr - sizeof(ut_mem_block_t)); 00228 00229 os_fast_mutex_lock(&ut_list_mutex); 00230 00231 ut_a(block->magic_n == UT_MEM_MAGIC_N); 00232 ut_a(ut_total_allocated_memory >= block->size); 00233 00234 ut_total_allocated_memory -= block->size; 00235 00236 UT_LIST_REMOVE(mem_block_list, ut_mem_block_list, block); 00237 free(block); 00238 00239 os_fast_mutex_unlock(&ut_list_mutex); 00240 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void ut_free_all_mem | ( | void | ) |
Definition at line 321 of file ut0mem.c.
References yaSSL::block, free, os_fast_mutex_free(), ut_a, UT_LIST_GET_FIRST, UT_LIST_REMOVE, UT_MEM_MAGIC_N, and ut_total_allocated_memory.
Referenced by innobase_shutdown_for_mysql().
00323 { 00324 ut_mem_block_t* block; 00325 00326 os_fast_mutex_free(&ut_list_mutex); 00327 00328 while ((block = UT_LIST_GET_FIRST(ut_mem_block_list))) { 00329 00330 ut_a(block->magic_n == UT_MEM_MAGIC_N); 00331 ut_a(ut_total_allocated_memory >= block->size); 00332 00333 ut_total_allocated_memory -= block->size; 00334 00335 UT_LIST_REMOVE(mem_block_list, ut_mem_block_list, block); 00336 free(block); 00337 } 00338 00339 if (ut_total_allocated_memory != 0) { 00340 fprintf(stderr, 00341 "InnoDB: Warning: after shutdown total allocated memory is %lu\n", 00342 (ulong) ut_total_allocated_memory); 00343 } 00344 }
Here is the call graph for this function:

Here is the caller graph for this function:

| UT_LIST_BASE_NODE_T | ( | ut_mem_block_t | ) |
Definition at line 36 of file ut0mem.c.
References os_fast_mutex_init(), TRUE, and UT_LIST_INIT.
00050 { 00051 os_fast_mutex_init(&ut_list_mutex); 00052 UT_LIST_INIT(ut_mem_block_list); 00053 ut_mem_block_list_inited = TRUE; 00054 }
Here is the call graph for this function:

| void* ut_malloc | ( | ulint | n | ) |
Definition at line 173 of file ut0mem.c.
References TRUE, and ut_malloc_low().
Referenced by buf_pool_init(), fil_create_new_single_table_tablespace(), fil_load_single_table_tablespace(), fil_node_open_file(), fil_open_single_table_tablespace(), fil_read_flushed_lsn_and_arch_log_no(), fil_reset_too_high_lsns(), hash_create(), log_init(), mem_area_alloc(), mem_pool_create(), os_aio_array_create(), os_aio_init(), os_awe_allocate_physical_mem(), os_awe_allocate_virtual_mem_window(), os_event_create(), os_file_opendir(), os_file_readdir_next_file(), os_file_set_size(), os_mem_alloc_nocache(), os_mutex_create(), recv_sys_init(), srv_add_path_separator_if_needed(), srv_parse_data_file_paths_and_sizes(), srv_parse_log_group_home_dirs(), sync_array_create(), sync_init(), sync_thread_add_level(), trx_doublewrite_init(), trx_sys_doublewrite_init_or_restore_pages(), and ut_realloc().
00175 : allocated memory */ 00176 ulint n) /* in: number of bytes to allocate */ 00177 { 00178 return(ut_malloc_low(n, TRUE, TRUE)); 00179 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void* ut_malloc_low | ( | ulint | n, | |
| ibool | set_to_zero, | |||
| ibool | assert_on_error | |||
| ) |
Definition at line 61 of file ut0mem.c.
References __WIN__, errno, malloc, memset, NULL, os_fast_mutex_lock(), os_fast_mutex_unlock(), os_thread_sleep(), ut_a, ut_ad, UT_LIST_ADD_FIRST, UT_MEM_MAGIC_N, ut_print_timestamp(), and ut_total_allocated_memory.
Referenced by mem_pool_create(), os_mem_alloc_large(), and ut_malloc().
00063 : allocated memory */ 00064 ulint n, /* in: number of bytes to allocate */ 00065 ibool set_to_zero, /* in: TRUE if allocated memory should be 00066 set to zero if UNIV_SET_MEM_TO_ZERO is 00067 defined */ 00068 ibool assert_on_error)/* in: if TRUE, we crash mysqld if the 00069 memory cannot be allocated */ 00070 { 00071 ulint retry_count = 0; 00072 void* ret; 00073 00074 ut_ad((sizeof(ut_mem_block_t) % 8) == 0); /* check alignment ok */ 00075 00076 if (!ut_mem_block_list_inited) { 00077 ut_mem_block_list_init(); 00078 } 00079 retry: 00080 os_fast_mutex_lock(&ut_list_mutex); 00081 00082 ret = malloc(n + sizeof(ut_mem_block_t)); 00083 00084 if (ret == NULL && retry_count < 60) { 00085 if (retry_count == 0) { 00086 ut_print_timestamp(stderr); 00087 00088 fprintf(stderr, 00089 " InnoDB: Error: cannot allocate %lu bytes of\n" 00090 "InnoDB: memory with malloc! Total allocated memory\n" 00091 "InnoDB: by InnoDB %lu bytes. Operating system errno: %lu\n" 00092 "InnoDB: Check if you should increase the swap file or\n" 00093 "InnoDB: ulimits of your operating system.\n" 00094 "InnoDB: On FreeBSD check you have compiled the OS with\n" 00095 "InnoDB: a big enough maximum process size.\n" 00096 "InnoDB: Note that in most 32-bit computers the process\n" 00097 "InnoDB: memory space is limited to 2 GB or 4 GB.\n" 00098 "InnoDB: We keep retrying the allocation for 60 seconds...\n", 00099 (ulong) n, (ulong) ut_total_allocated_memory, 00100 #ifdef __WIN__ 00101 (ulong) GetLastError() 00102 #else 00103 (ulong) errno 00104 #endif 00105 ); 00106 } 00107 00108 os_fast_mutex_unlock(&ut_list_mutex); 00109 00110 /* Sleep for a second and retry the allocation; maybe this is 00111 just a temporary shortage of memory */ 00112 00113 os_thread_sleep(1000000); 00114 00115 retry_count++; 00116 00117 goto retry; 00118 } 00119 00120 if (ret == NULL) { 00121 /* Flush stderr to make more probable that the error 00122 message gets in the error file before we generate a seg 00123 fault */ 00124 00125 fflush(stderr); 00126 00127 os_fast_mutex_unlock(&ut_list_mutex); 00128 00129 /* Make an intentional seg fault so that we get a stack 00130 trace */ 00131 /* Intentional segfault on NetWare causes an abend. Avoid this 00132 by graceful exit handling in ut_a(). */ 00133 #if (!defined __NETWARE__) 00134 if (assert_on_error) { 00135 ut_print_timestamp(stderr); 00136 00137 fprintf(stderr, 00138 " InnoDB: We now intentionally generate a seg fault so that\n" 00139 "InnoDB: on Linux we get a stack trace.\n"); 00140 00141 if (*ut_mem_null_ptr) ut_mem_null_ptr = 0; 00142 } else { 00143 return(NULL); 00144 } 00145 #else 00146 ut_a(0); 00147 #endif 00148 } 00149 00150 if (set_to_zero) { 00151 #ifdef UNIV_SET_MEM_TO_ZERO 00152 memset(ret, '\0', n + sizeof(ut_mem_block_t)); 00153 #endif 00154 } 00155 00156 ((ut_mem_block_t*)ret)->size = n + sizeof(ut_mem_block_t); 00157 ((ut_mem_block_t*)ret)->magic_n = UT_MEM_MAGIC_N; 00158 00159 ut_total_allocated_memory += n + sizeof(ut_mem_block_t); 00160 00161 UT_LIST_ADD_FIRST(mem_block_list, ut_mem_block_list, 00162 ((ut_mem_block_t*)ret)); 00163 os_fast_mutex_unlock(&ut_list_mutex); 00164 00165 return((void*)((byte*)ret + sizeof(ut_mem_block_t))); 00166 }
Here is the call graph for this function:

Here is the caller graph for this function:

| char* ut_memcpyq | ( | char * | dest, | |
| char | q, | |||
| const char * | src, | |||
| ulint | len | |||
| ) |
Definition at line 422 of file ut0mem.c.
00424 : pointer to end of dest */ 00425 char* dest, /* in: output buffer */ 00426 char q, /* in: the quote character */ 00427 const char* src, /* in: string to be quoted */ 00428 ulint len) /* in: length of src */ 00429 { 00430 const char* srcend = src + len; 00431 00432 while (src < srcend) { 00433 if ((*dest++ = *src++) == q) { 00434 *dest++ = q; 00435 } 00436 } 00437 00438 return(dest); 00439 }
| void* ut_realloc | ( | void * | ptr, | |
| ulint | size | |||
| ) |
Definition at line 268 of file ut0mem.c.
References yaSSL::block, NULL, ut_a, ut_free(), ut_malloc(), UT_MEM_MAGIC_N, and ut_memcpy().
00270 : pointer to new mem block or NULL */ 00271 void* ptr, /* in: pointer to old block or NULL */ 00272 ulint size) /* in: desired size */ 00273 { 00274 ut_mem_block_t* block; 00275 ulint old_size; 00276 ulint min_size; 00277 void* new_ptr; 00278 00279 if (ptr == NULL) { 00280 00281 return(ut_malloc(size)); 00282 } 00283 00284 if (size == 0) { 00285 ut_free(ptr); 00286 00287 return(NULL); 00288 } 00289 00290 block = (ut_mem_block_t*)((byte*)ptr - sizeof(ut_mem_block_t)); 00291 00292 ut_a(block->magic_n == UT_MEM_MAGIC_N); 00293 00294 old_size = block->size - sizeof(ut_mem_block_t); 00295 00296 if (size < old_size) { 00297 min_size = size; 00298 } else { 00299 min_size = old_size; 00300 } 00301 00302 new_ptr = ut_malloc(size); 00303 00304 if (new_ptr == NULL) { 00305 00306 return(NULL); 00307 } 00308 00309 /* Copy the old data from ptr */ 00310 ut_memcpy(new_ptr, ptr, min_size); 00311 00312 ut_free(ptr); 00313 00314 return(new_ptr); 00315 }
Here is the call graph for this function:

| ulint ut_strcount | ( | const char * | s1, | |
| const char * | s2 | |||
| ) |
Definition at line 446 of file ut0mem.c.
References count, strlen(), and strstr().
Referenced by ut_strreplace().
00448 : the number of times s2 occurs in s1 */ 00449 const char* s1, /* in: string to search in */ 00450 const char* s2) /* in: string to search for */ 00451 { 00452 ulint count = 0; 00453 ulint len = strlen(s2); 00454 00455 if (len == 0) { 00456 00457 return(0); 00458 } 00459 00460 for (;;) { 00461 s1 = strstr(s1, s2); 00462 00463 if (!s1) { 00464 00465 break; 00466 } 00467 00468 count++; 00469 s1 += len; 00470 } 00471 00472 return(count); 00473 }
Here is the call graph for this function:

Here is the caller graph for this function:

| char* ut_strcpyq | ( | char * | dest, | |
| char | q, | |||
| const char * | src | |||
| ) |
Definition at line 400 of file ut0mem.c.
00402 : pointer to end of dest */ 00403 char* dest, /* in: output buffer */ 00404 char q, /* in: the quote character */ 00405 const char* src) /* in: null-terminated string */ 00406 { 00407 while (*src) { 00408 if ((*dest++ = *src++) == q) { 00409 *dest++ = q; 00410 } 00411 } 00412 00413 return(dest); 00414 }
Definition at line 352 of file ut0mem.c.
References memcpy, n, strlen(), and ut_min().
Referenced by trx_set_detailed_error().
00354 : strlen(src) */ 00355 char* dst, /* in: destination buffer */ 00356 const char* src, /* in: source buffer */ 00357 ulint size) /* in: size of destination buffer */ 00358 { 00359 ulint src_size = strlen(src); 00360 00361 if (size != 0) { 00362 ulint n = ut_min(src_size, size - 1); 00363 00364 memcpy(dst, src, n); 00365 dst[n] = '\0'; 00366 } 00367 00368 return(src_size); 00369 }
Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 376 of file ut0mem.c.
References memcpy, n, strlen(), and ut_min().
Referenced by mem_heap_create_block().
00378 : strlen(src) */ 00379 char* dst, /* in: destination buffer */ 00380 const char* src, /* in: source buffer */ 00381 ulint size) /* in: size of destination buffer */ 00382 { 00383 ulint src_size = strlen(src); 00384 00385 if (size != 0) { 00386 ulint n = ut_min(src_size, size - 1); 00387 00388 memcpy(dst, src + src_size - n, n + 1); 00389 } 00390 00391 return(src_size); 00392 }
Here is the call graph for this function:

Here is the caller graph for this function:

| char* ut_strreplace | ( | const char * | str, | |
| const char * | s1, | |||
| const char * | s2 | |||
| ) |
Definition at line 480 of file ut0mem.c.
References count, int(), mem_alloc, memcpy, strlen(), strstr(), and ut_strcount().
00482 : modified string, must be 00483 freed with mem_free() */ 00484 const char* str, /* in: string to operate on */ 00485 const char* s1, /* in: string to replace */ 00486 const char* s2) /* in: string to replace s1 with */ 00487 { 00488 char* new_str; 00489 char* ptr; 00490 const char* str_end; 00491 ulint str_len = strlen(str); 00492 ulint s1_len = strlen(s1); 00493 ulint s2_len = strlen(s2); 00494 ulint count = 0; 00495 int len_delta = (int)s2_len - (int)s1_len; 00496 00497 str_end = str + str_len; 00498 00499 if (len_delta <= 0) { 00500 len_delta = 0; 00501 } else { 00502 count = ut_strcount(str, s1); 00503 } 00504 00505 new_str = mem_alloc(str_len + count * len_delta + 1); 00506 ptr = new_str; 00507 00508 while (str) { 00509 const char* next = strstr(str, s1); 00510 00511 if (!next) { 00512 next = str_end; 00513 } 00514 00515 memcpy(ptr, str, next - str); 00516 ptr += next - str; 00517 00518 if (next == str_end) { 00519 00520 break; 00521 } 00522 00523 memcpy(ptr, s2, s2_len); 00524 ptr += s2_len; 00525 00526 str = next + s1_len; 00527 } 00528 00529 *ptr = '\0'; 00530 00531 return(new_str); 00532 }
Here is the call graph for this function:

| ibool ut_test_malloc | ( | ulint | n | ) |
Definition at line 187 of file ut0mem.c.
References errno, FALSE, free, malloc, NULL, TRUE, ut_print_timestamp(), and ut_total_allocated_memory.
00189 : TRUE if succeeded */ 00190 ulint n) /* in: try to allocate this many bytes */ 00191 { 00192 void* ret; 00193 00194 ret = malloc(n); 00195 00196 if (ret == NULL) { 00197 ut_print_timestamp(stderr); 00198 fprintf(stderr, 00199 " InnoDB: Error: cannot allocate %lu bytes of memory for\n" 00200 "InnoDB: a BLOB with malloc! Total allocated memory\n" 00201 "InnoDB: by InnoDB %lu bytes. Operating system errno: %d\n" 00202 "InnoDB: Check if you should increase the swap file or\n" 00203 "InnoDB: ulimits of your operating system.\n" 00204 "InnoDB: On FreeBSD check you have compiled the OS with\n" 00205 "InnoDB: a big enough maximum process size.\n", 00206 (ulong) n, 00207 (ulong) ut_total_allocated_memory, 00208 (int) errno); 00209 return(FALSE); 00210 } 00211 00212 free(ret); 00213 00214 return(TRUE); 00215 }
Here is the call graph for this function:

Definition at line 23 of file ut0mem.c.
Referenced by os_awe_allocate_physical_mem(), os_awe_allocate_virtual_mem_window(), srv_printf_innodb_monitor(), ut_free(), ut_free_all_mem(), ut_malloc_low(), and ut_test_malloc().
1.4.7

