MySQL内存池结构源码 my_alloc.h

MySQL使用内存池结构,理解该结构挺简单的,但是对于my_alloc.c中的分配过程感觉上很难理解。
/*
Data structures for mysys/my_alloc.c (root memory allocator)
*/
#ifndef _my_alloc_h
#define _my_alloc_h
#define ALLOC_MAX_BLOCK_TO_DROP                      4096
#define ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP    10
#ifdef __cplusplus
extern "C" {
#endif
//内存链表
typedef struct st_used_mem
{                                      /* struct for once_alloc (block) */
struct st_used_mem *next;      /* Next block in use */
unsigned int  left;              /* memory left in block  */
unsigned int  size;              /* size of block */
} USED_MEM;
//链表首地址,分别维护空闲、使用和预分配的内存
typedef struct st_mem_root
{
USED_MEM *free;                  /* blocks with free memory in it */
USED_MEM *used;                  /* blocks almost without free memory */
USED_MEM *pre_alloc;             /* preallocated block */
/* if block have less memory it will be put in 'used' list */
size_t min_malloc;
size_t block_size;               /* initial block size */
unsigned int block_num;          /* allocated blocks counter */
/*
first free block in queue test counter (if it exceed
MAX_BLOCK_USAGE_BEFORE_DROP block will be dropped in 'used' list)
*/
unsigned int first_block_usage;
void (*error_handler)(void);
} MEM_ROOT;
#ifdef  __cplusplus
}
#endif
#endif

相关推荐