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
/*
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
相关推荐
yongzhang 2020-05-25
paopao00 2020-02-16
gzx0 2020-01-31
Annihilation 2011-08-15
fatansitic 2011-07-27
追寻水中桥 2019-10-19
lglovejava 2016-04-19
帅性而为号 2019-07-11
二十不悔三十而立 2016-07-10
了不起的厂长 2019-06-28
Lincain 2019-06-26
mfcaiblog 2011-08-16
ljlxyf 2016-07-13