我是靠谱客的博主 听话纸飞机,这篇文章主要介绍简单的malloc,free实现函数简单的malloc,free实现函数,现在分享给大家,希望可以做个参考。

简单的malloc,free实现函数

struct mem_control_block
{
int is_avaliable;
int size;
};
int has_initialized = 0;
void *managed_mem_start;
void *last_valid_address;
void mem_init()
{
last_valid_address = sbrk(0);
managed_mem_start = last_valid_address;
has_initalized = 1;
}
void free(*firstbyte)
{
struct mem_control_block *mcb;
mcb = firstbyte - sizeof(struct mem_control_block);
mcb->is_avaliable = 1;
return;
}
void malloc(int numbyte)
{
struct mem_control_block *current_mcb;
void *mem_location,current_location;
if(!has_initialized)
mem_init();
numbyte += sizeof(struct mem_control_block);
mem_location = 0;
current_location = managed_mem_start;
while(current_location != last_valid_address)
{
current_mcb = (struct mem_control_block *)current_location;
if(current_mcb->is_avaliable)
if(current_mcb->size >= numbyte)
{
current_mcb->is_avaliable = 0;
mem_location = current_location;
break;
}
//current_location += sizeof(struct mem_control_block);
current_location += current_mcb->size;
}
if(!memlocation)
{
sbrk(numbyte);
mem_location = last_valid_address;
last_valid_address += numbyte;
current_mcb = mem_location;
current_mcb->is_avaliable = 0;
current_mcb->size = numbyte;
}
mem_location += sizeof(struct mem_control_block);
return mem_location;
}


 

最后

以上就是听话纸飞机最近收集整理的关于简单的malloc,free实现函数简单的malloc,free实现函数的全部内容,更多相关简单内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部