码迷,mamicode.com
首页 > 其他好文 > 详细

函数ut_malloc_low

时间:2015-12-02 00:45:15      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

 

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

标签:

原文地址:http://www.cnblogs.com/taek/p/5011746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!