函数ut_malloc_low

 

/**********************************************************************//**
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((sizeof(ut_mem_block_t) % 8) == 0); /* check alignment ok */
    ut_a(ut_mem_block_list_inited);

    retry_count = 0;
retry:
    os_fast_mutex_lock(&ut_list_mutex);

    ret = malloc(n + sizeof(ut_mem_block_t));
    //尝试60次
    if (ret == NULL && retry_count < 60) {
        if (retry_count == 0) {
        ...
        }

        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(1000000);

        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 */ }

 

你可能感兴趣的:(函数ut_malloc_low)