#define MALLOC_OVERHEAD 8 //分配过程中,需要保留一部分额外的空间 #define ALLOC_MAX_BLOCK_TO_DROP 4096 //后续会继续分析该宏的用途 #define ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP 10 //后续会继续分析该宏的用途 #define ALIGN_SIZE(A) MY_ALIGN((A),sizeof(double)) #define MY_ALIGN(A,L) (((A) + (L) - 1) & ~((L) - 1)) #define ALLOC_ROOT_MIN_BLOCK_SIZE (MALLOC_OVERHEAD + sizeof(USED_MEM) + 8) /* Define some useful general macros (should be done after all headers). */ /*作者:www.manongjc.com */ #define MY_MAX(a, b) ((a) > (b) ? (a) : (b)) //求两个数值之间的最大值 #define MY_MIN(a, b) ((a) < (b) ? (a) : (b)) //求两个数值之间的最小值
typedef struct st_mem_root
{
USED_MEM *free; /* free block link list的链表头指针 */
USED_MEM *used; /* used block link list的链表头指针 */
USED_MEM *pre_alloc; /* 预先分配的block */
size_t min_malloc; /* 如果block剩下的可用空间小于该值,将会从free list移动到used list */
size_t block_size; /* 每次初始化的空间大小 */
unsigned int block_num; /* 记录实际的block数量,初始化为4 */
unsigned int first_block_usage; /* free list中的第一个block 测试不满足分配空间大小的次数 */
void (*error_handler)( void ); /* 分配失败的错误处理函数 */
} MEM_ROOT;
typedef struct st_used_mem
{
struct st_used_mem *next; //指向下一个分配的block
unsigned int left; //该block剩余的空间大小
unsigned int size; //该block的总大小
} USED_MEM;
void init_alloc_root( MEM_ROOT *mem_root, size_t block_size, size_t pre_alloc_size __attribute__( (unused) ) )
{
mem_root->free = mem_root->used = mem_root->pre_alloc = 0;
mem_root->min_malloc = 32;
mem_root->block_size = block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
mem_root->error_handler = 0;
mem_root->block_num = 4; /* We shift this with >>2 */
mem_root->first_block_usage = 0;
}
void *alloc_root( MEM_ROOT *mem_root, size_t length )
{
size_t get_size, block_size;
uchar * point;
reg1 USED_MEM *next = 0;
reg2 USED_MEM **prev;
length = ALIGN_SIZE( length );
if ( (*(prev = &mem_root->free) ) != NULL )
{
if ( (*prev)->left < length &&
mem_root->first_block_usage++ >= ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP &&
(*prev)->left < ALLOC_MAX_BLOCK_TO_DROP )
{
next = *prev;
*prev = next->next; /* Remove block from list */
next->next = mem_root->used;
mem_root->used = next;
mem_root->first_block_usage = 0;
}
for ( next = *prev; next && next->left < length; next = next->next )
prev = &next->next;
}
if ( !next )
{ /* Time to alloc new block */
block_size = mem_root->block_size * (mem_root->block_num >> 2);
get_size = length + ALIGN_SIZE( sizeof(USED_MEM) );
get_size = MY_MAX( get_size, block_size );
if ( !(next = (USED_MEM *) my_malloc( get_size, MYF( MY_WME | ME_FATALERROR ) ) ) )
{
if ( mem_root->error_handler )
(*mem_root->error_handler)();
DBUG_RETURN( (void *) 0 ); /* purecov: inspected */
}
mem_root->block_num++;
next->next = *prev;
next->size = get_size;
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),这里重复了。 */
*prev = next;
}
point = (uchar *) ( (char *) next + (next->size - next->left) );
/*TODO: next part may be unneded due to mem_root->first_block_usage counter*/
/* 作者:www.manongjc.com */
if ( (next->left -= length) < mem_root->min_malloc )
{ /* Full block */
*prev = next->next; /* Remove block from list */
next->next = mem_root->used;
mem_root->used = next;
mem_root->first_block_usage = 0;
}
}
for (next= *prev ; next && next->left < length ; next= next->next) prev= &next->next; }
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有