Table of Contents
Details of MySQL's MEM_ROOT
Home Backend Development PHP Tutorial Describe the MEM_ROOT_PHP tutorial of MySQL in detail

Describe the MEM_ROOT_PHP tutorial of MySQL in detail

Jul 12, 2016 am 09:03 AM
android

Details of MySQL's MEM_ROOT

This article will explain in detail the MEM_ROOT structure that is widely used in MySQL. At the same time, it will omit the debug part of the information and only analyze the normal conditions in mysql. Use MEM_ROOT to do the memory allocation part.

Before the specific analysis, let’s first enumerate some macros used in the use of this structure:
<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>
Copy after login

Let’s take a look at the information related to the MEM_ROOT structure:
<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>
Copy after login

The following is the specific block information allocated.
<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>
Copy after login
In fact, during the allocation process, MEM_ROOT manages used and free blocks through a doubly linked list:


The initialization process of MEM_ROOT is as follows:
<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>
Copy after login

During the initialization process, the block_size space is block_size-ALLOC_ROOT_MIN_BLOCK_SIZE. Because when the memory is not enough and needs to be expanded, the capacity is expanded through mem_root->block_num >>2 *block_size, so mem_root->block_num >>2 is at least 1, so during the initialization process mem_root->block_num=4 (Note :4>>2=1).

Let’s take a look at the specific steps of allocating memory:
<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>
Copy after login

The specific logic of the above code is as follows: 1. Check the free linked list to find a block that meets the space. If a suitable block is found, then: 1.1 Directly return the initial address of the block from size-left. Of course, during the free list traversal process, it will be judged that the left space in the first block in the freelist does not meet the space that needs to be allocated, and the block has been searched 10 times (ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP) and does not meet the allocation length, and If the remaining space of the block is less than 4k (ALLOC_MAX_BLOCK_TO_DROP), the block will be moved to the used linked list. 2. If there is no suitable block in the free linked list, then: 2.1 Allocate the larger of mem_root->block_size * (mem_root->block_num >> 2) and length ALIGN_SIZE(sizeof(USED_MEM)) as the new block memory space. 2.2 Depending on the usage of the block, hang the block on the used or free linked list.
What needs to be noted here is the use of secondary pointers:
<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>
Copy after login
prev points to the address pointed to by next of the last block:

So replace the address of prev is the address of the new block, that is, the new block is added to the end of the free list: *prev=next;


Summary: MEM_ROOT uses a heuristic allocation algorithm for memory allocation. The greater the number of blocks, the larger the memory of a single block will be: block_size= mem_root->block_size * (mem_root->block_num >> 2).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1083375.htmlTechArticleExplain in detail the MEM_ROOT of MySQL. This article will explain in detail the MEM_ROOT structure that is widely used in MySQL, and at the same time save Go to the debug part of the information and only analyze the use of M in mysql under normal circumstances...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

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

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

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

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

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

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

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 Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

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

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

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

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

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

iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship Sep 10, 2024 am 06:45 AM

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

See all articles