我是靠谱客的博主 听话纸飞机,最近开发中收集的这篇文章主要介绍简单的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实现函数的全部内容,希望文章能够帮你解决简单的malloc,free实现函数简单的malloc,free实现函数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部