/**********************************************************************//** Allocates memory. @return own: allocated memory */ UNIV_INTERN void* ut_malloc_low( /*==========*/ ulint n, /*!< in: number of bytes to allocate */ ibool assert_on_error)/*!< in: if TRUE, we crash mysqld if the memory cannot be allocated */ { #ifndef UNIV_HOTBACKUP ulint retry_count; void* ret; //使用OS自带的malloc分配内存 if (UNIV_LIKELY(srv_use_sys_malloc)) { ret = malloc(n); ut_a(ret || !assert_on_error); return(ret); } ut_ad(() == ); /* check alignment ok */ ut_a(ut_mem_block_list_inited); retry_count = ; retry: os_fast_mutex_lock(&ut_list_mutex); ret = malloc(n + sizeof(ut_mem_block_t)); //尝试60次 ) { ) { ... } os_fast_mutex_unlock(&ut_list_mutex); /* Sleep for a second and retry the allocation; maybe this is just a temporary shortage of memory */ os_thread_sleep(); retry_count++; goto retry; } if (ret == NULL) { ... } /** *设置ut_mem_block_t结构体的属性 */ ((ut_mem_block_t*)ret)->size = n + sizeof(ut_mem_block_t); ((ut_mem_block_t*)ret)->magic_n = UT_MEM_MAGIC_N; ut_total_allocated_memory += n + sizeof(ut_mem_block_t); /** *将ret放到ut_mem_block_list头部 */ UT_LIST_ADD_FIRST(mem_block_list, ut_mem_block_list,((ut_mem_block_t*)ret)); os_fast_mutex_unlock(&ut_list_mutex); return((void*)((byte*)ret + sizeof(ut_mem_block_t))); #else /* !UNIV_HOTBACKUP */ void* ret = malloc(n); ut_a(ret || !assert_on_error); return(ret); #endif /* !UNIV_HOTBACKUP */ }