/********************************************************************//**
Creates a memory pool.
@return memory pool */
UNIV_INTERN
mem_pool_t*
mem_pool_create(
/*============*/
ulint size) /*!< in: pool size in bytes */
{
mem_pool_t* pool; 结构体见
mem_area_t* area;
ulint i;
ulint used;
pool = ut_malloc(sizeof(mem_pool_t));
pool->buf = ut_malloc_low(size, TRUE);
pool->size = size;
mutex_create(mem_pool_mutex_key, &pool->mutex, SYNC_MEM_POOL);
/* Initialize the free lists */
/** *pool中的free_list可理解为64个元素的数组,数组元素的类型为mem_area_t *每个元素同时又是个双循环链表,大小都一样,都是2的N次方 */
; i < ; i++) {
UT_LIST_INIT(pool->free_list[i]);
}
used = ;
/** *假设size为36 *1) 36-0 > * i=ut_2_log(36-0)=5 计算出不超过36的最大的2的N次方 * 令pool->buf+0为area,其area->size 为 ut_2_exp(5)=32 * 再将些area置为pool->free_list[5]中的数组中头结点 * 此时used=0+32 * *2) 36-32 > * i=ut_2_log(36-32)=2 * 令pool->buf+32为area,其area->size为ut_2_exp(2)=4 * 再将此area置为pool->free_list[2]中数组的头结点 * 此时used=32+ut_2_exp(2)=32+4=36 * *3) 36-36 ? 退出循环 */
while (size - used >= MEM_AREA_MIN_SIZE) {
i = ut_2_log(size - used);
if (ut_2_exp(i) > size - used) {
/* ut_2_log rounds upward */
i--;
}
area = (mem_area_t*)(pool->buf + used);
mem_area_set_size(area, ut_2_exp(i));
mem_area_set_free(area, TRUE);
UNIV_MEM_FREE(MEM_AREA_EXTRA_SIZE + (byte*) area,ut_2_exp(i) - MEM_AREA_EXTRA_SIZE);
UT_LIST_ADD_FIRST(free_list, pool->free_list[i], area);
used = used + ut_2_exp(i);
}
ut_ad(size >= used);
pool->reserved = ;
return(pool);
}