细说MySQL 之MEM_ROOT
细说MySQL 之MEM_ROOT
这篇文章会详细解说MySQL中使用非常广泛的MEM_ROOT的结构体,同时省去debug部分的信息,仅分析正常情况下,mysql中使用MEM_ROOT来做内存分配的部分。在具体分析之前我们先例举在该结构体使用过程中用到的一些宏:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>#define MALLOC_OVERHEAD 8 //分配过程中,需要保留一部分额外的空间<br /> </li><li>#define ALLOC_MAX_BLOCK_TO_DROP 4096 //后续会继续分析该宏的用途<br /></li><li>#define ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP 10 //后续会继续分析该宏的用途<br /></li><li><br /></li><li>#define ALIGN_SIZE(A) MY_ALIGN((A),sizeof(double))<br /></li><li>#define MY_ALIGN(A,L) (((A) + (L) - 1) & ~((L) - 1))<br /></li><li><br /></li><li>#define ALLOC_ROOT_MIN_BLOCK_SIZE (MALLOC_OVERHEAD + sizeof(USED_MEM) + 8)<br /></li><li>/* Define some useful general macros (should be done after all headers). */<br /></li><li>#define MY_MAX(a, b) ((a) > (b) ? (a) : (b)) //求两个数值之间的最大值<br /></li><li>#define MY_MIN(a, b) ((a) < (b) ? (a) : (b)) //求两个数值之间的最小值</li></ol>
下面再来看看MEM_ROOT结构体相关的信息:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>typedef struct st_mem_root<br /> </li><li>{<br /></li><li>USED_MEM *free; //free block link list的链表头指针<br /></li><li>USED_MEM *used;//used block link list的链表头指针<br /></li><li>USED_MEM *pre_alloc; //预先分配的block<br /></li><li>size_t min_malloc; //如果block剩下的可用空间小于该值,将会从free list移动到used list<br /></li><li>size_t block_size; //每次初始化的空间大小<br /></li><li>unsigned int block_num; //记录实际的block数量,初始化为4<br /></li><li>unsigned int first_block_usage; //free list中的第一个block 测试不满足分配空间大小的次数<br /></li><li>void (*error_handler)(void);//分配失败的错误处理函数<br /></li><li>} MEM_ROOT; </li></ol>
以下是分配具体的block信息.
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>typedef struct st_used_mem<br /> </li><li>{ <br /></li><li>struct st_used_mem *next; //指向下一个分配的block<br /></li><li>unsigned int left; //该block剩余的空间大小<br /></li><li>unsigned int size; //该block的总大小<br /></li><li>} USED_MEM; </li></ol>
MEM_ROOT的初始化过程如下:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,size_t pre_alloc_size __attribute__((unused)))<br /> </li><li>{<br /></li><li>mem_root->free= mem_root->used= mem_root->pre_alloc= 0;<br /></li><li>mem_root->min_malloc= 32;<br /></li><li>mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;<br /></li><li>mem_root->error_handler= 0;<br /></li><li>mem_root->block_num= 4; /* We shift this with >>2 */<br /></li><li>mem_root->first_block_usage= 0;<br /></li><li>} </li></ol>
初始化过程中,block_size空间为block_size-ALLOC_ROOT_MIN_BLOCK_SIZE。因为在内存不够,需要 扩容时,是通过mem_root->block_num >>2 *block_size 来扩容的,所以mem_root->block_num >>2 至少为1,因此在初始化的过程中mem_root->block_num=4(注:4>>2=1)。
下面来看看具体分配内存的步骤:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>void *alloc_root(MEM_ROOT *mem_root, size_t length)<br /> </li><li>{<br /></li><li>size_t get_size, block_size;<br /></li><li>uchar* point;<br /></li><li>reg1 USED_MEM *next= 0;<br /></li><li>reg2 USED_MEM **prev;<br /></li><li><br /></li><li>length= ALIGN_SIZE(length);<br /></li><li>if ((*(prev= &mem_root->free)) != NULL)<br /></li><li>{<br /></li><li>if ((*prev)->left < length &&<br /></li><li>mem_root->first_block_usage++ >= ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP &&<br /></li><li>(*prev)->left < ALLOC_MAX_BLOCK_TO_DROP)<br /></li><li>{<br /></li><li>next= *prev;<br /></li><li>*prev= next->next; /* Remove block from list */<br /></li><li>next->next= mem_root->used;<br /></li><li>mem_root->used= next;<br /></li><li>mem_root->first_block_usage= 0;<br /></li><li>}<br /></li><li>for (next= *prev ; next && next->left < length ; next= next->next)<br /></li><li>prev= &next->next;<br /></li><li>}<br /></li><li>if (! next)<br /></li><li>{ /* Time to alloc new block */<br /></li><li>block_size= mem_root->block_size * (mem_root->block_num >> 2);<br /></li><li>get_size= length+ALIGN_SIZE(sizeof(USED_MEM));<br /></li><li>get_size= MY_MAX(get_size, block_size);<br /></li><li><br /></li><li>if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME | ME_FATALERROR))))<br /></li><li>{<br /></li><li>if (mem_root->error_handler)<br /></li><li>(*mem_root->error_handler)();<br /></li><li>DBUG_RETURN((void*) 0); /* purecov: inspected */<br /></li><li>}<br /></li><li>mem_root->block_num++;<br /></li><li>next->next= *prev;<br /></li><li>next->size= get_size;<br /></li><li>next->left= get_size-ALIGN_SIZE(sizeof(USED_MEM)); //bug:如果该block是通过mem_root->block_size * (mem_root->block_num >> 2)计算出来的,则已经去掉了ALIGN_SIZE(sizeof(USED_MEM),这里重复了。<br /></li><li>*prev=next;<br /></li><li>}<br /></li><li><br /></li><li>point= (uchar*) ((char*) next+ (next->size-next->left));<br /></li><li>/*TODO: next part may be unneded due to mem_root->first_block_usage counter*/<br /></li><li>if ((next->left-= length) < mem_root->min_malloc)<br /></li><li>{ /* Full block */<br /></li><li>*prev= next->next; /* Remove block from list */<br /></li><li>next->next= mem_root->used;<br /></li><li>mem_root->used= next;<br /></li><li>mem_root->first_block_usage= 0;<br /></li><li>}<br /></li><li>} </li></ol>
上述代码的具体逻辑如下:1.查看free链表,寻找满足空间的block。如果找到了合适的block,则: 1.1 直接返回该block从size-left处的初始地址即可。当然,在free list遍历的过程中,会去判断freelist 中第一个block中left的空间不满足需要分配的空间,且该block中已经查找过了10次 (ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP)都不满足分配长度,且该block剩余空间小于 4k(ALLOC_MAX_BLOCK_TO_DROP),则将该block 移动到used链表中。2.如果free链表中,没有合适的block,则: 2.1 分配 mem_root->block_size * (mem_root->block_num >> 2)和length+ALIGN_SIZE(sizeof(USED_MEM)) 中比较大的作为新的block内存空间。 2.2根据该block的使用情况,将该block挂在used或者free链表上。
这里需要注意的是二级指针的使用:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>for (next= *prev ; next && next->left < length ; next= next->next)<br /> </li><li>prev= &next->next;<br /></li><li>} </li></ol>
所以将prev的地址替换为newblock的地址,即将该new block加到了free list的结尾:*prev=next;
总结:MEM_ROOT的内存分配采用的是启发式分配算法,随着后续block的数量越多,单个block的内存也会越大:block_size= mem_root->block_size * (mem_root->block_num >> 2).

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T
