linux资源分配算法,nullnulllinux动态异长存储资源分配算法的实现

发一下牢骚和主题无关:

每日一道理

坚持的昨天叫立足,坚持的今天叫进取,坚持的明天叫成功。

#ifdef HAVA_CONFIG_H

#include

#endif

#include

#include

#define MAPSIZE 100

struct map//store resoure table'struct

{

int m_addr;

int m_size;

}

;

struct map map[MAPSIZE];

//best fit

int BF_malloc(struct map *mp,int size)

{

register int a,s;

/*these var is recommend to be saved in register,so it can be vary fast

*but only if its size is not bigger thar 'int' can be declared as 'register type'

*more over,this var may not be in memory,so you can not use a '&' to get its address

*/

register struct map *bp,*bpp;

for(bp=mp;bp->m_size;bp++)

{

if(bp->m_size >=size)

{

a=bp->m_addr;

s=bp->m_size;

for(bpp=bp;bpp->m_size;bpp++)

{

if(bpp->m_size >=size && bpp->m_size

{

a=bpp->m_addr;

s=bpp->m_size;

bp=bpp;

}

}

bp->m_addr +=size;

if((bp->m_size -= size) ==0)

do{

bp++;

(bp-1)->m_addr=bp->m_addr;

}while( (bp-1)->m_size=bp->m_size ); //while not null

return(a);

}

}

return(-1);

}

//worst fit

int WF_malloc(struct map *mp,int size)

{

register int a,s;

register struct map *bp,*bpp;

for(bp=mp;bp->m_size;bp++)

{

if(bp->m_size >= size)

{

a=bp->m_addr;

s=bp->m_size;

for(bpp=bp;bpp->m_size;bpp++)

{

if(bpp->m_size>s)

{

a=bpp->m_addr;

s=bpp->m_size;

bp=bpp;

}

}

bp->m_addr += size;

if((bp->m_size -= size) ==0)

do{

bp++;

(bp-1)->m_addr=bp->m_addr;

}while( (bp-1)->m_size=bp->m_size);

return(a);

}

}

return(-1);

}

void mfree(struct map *mp,int aa,int size)

{

register struct map *bp;

register int t;

register int a;

a=aa;

for(bp=mp; bp->m_addr <= a && bp->m_size!=0;bp++);

if(bp>mp && (bp-1)->m_addr+(bp-1)->m_size==a)

{

//if the point has moved and can combine with forword one

//combine forword

(bp-1)->m_size+=size;

if(a+size==bp->m_addr)

{ //combine backword

(bp-1)->m_size+=bp->m_size;

while(bp->m_size)

{

bp++;

(bp-1)->m_addr=bp->m_addr;

(bp-1)->m_size=bp->m_size;

}

}

}else{

if(a+size==bp->m_addr && bp->m_size)

{

//combine backword

bp->m_addr-=size;

bp->m_size+=size;

}else if(size)

do

{

//no combination

//move each one towards the tail

t=bp->m_addr;

bp->m_addr=a;

a=t;

t=bp->m_size;

bp->m_size=size;

bp++;

}while(size=t);

}

}

void init()

{

struct map *bp; //not register

int addr,size;

int i=0;

bp=map;

printf("Please input starting addr and total size:");

scanf("%d,%d",&addr,&size);

bp->m_addr=addr;

bp->m_size=size;

(++bp)->m_size=0;//table's tail

}

void show_map()

{

int i=0;

struct map *bp;

bp=map;

printf("\nCurrent memory map...\n");

printf("Address\t\tSize\n");

while(bp->m_size!=0)

{

printf("\n",bp->m_addr,bp->m_size);

bp++;

}

printf("\n");

}

main()

{

int a,s;

char c;

int i;

init();

printf("Please input, b for BF, w for WF:");

scanf("\n%c",&c);

do

{

show_map();

printf("Please input,1 for request,2for release,0 for exit:");

scanf("%d",&i);

switch(i)

{

case 1:

printf("Please input size:");

scanf("%d",&s);

if(c=='b')//BF

a=BF_malloc(map,s);

else

a=WF_malloc(map,s);

if(a==-1)

printf("request can't be satisfied\n");

else

printf("alloc memory at address:%d,siz:%d\n",a,s);

break;

case 2:

printf("Please input addr and size:");

scanf("%d,%d",&a,&s);

mfree(map,a,s);

break;

case 0:

exit(0);

}

}while(1);

}

文章结束给大家分享下程序员的一些笑话语录: 一个程序员对自己的未来很迷茫,于是去问上帝。

"万能的上帝呀,请你告诉我,我的未来会怎样?"

上帝说"我的孩子,你去问Lippman,他现在领导的程序员的队伍可能是地球上最大的"

于是他去问Lippman。

Lippman说"程序员的未来就是驾驭程序员"

这个程序员对这个未来不满意,于是他又去问上帝。

"万能的上帝呀,请你告诉我,我的未来会怎样?"

上帝说"我的孩子,你去问Gates,他现在所拥有的财产可能是地球上最多的"

于是他去问Gates。

Gates说"程序员的未来就是榨取程序员"

这个程序员对这个未来不满意,于是他又去问上帝。

"万能的上帝呀,请你告诉我,我的未来会怎样?"

上帝说"我的孩子,你去问侯捷,他写的计算机书的读者可能是地球上最多的"

于是他去问侯捷。

侯捷说"程序员的未来就是诱惑程序员"

这个程序员对这个未来不满意,于是他又去问上帝。

"万能的上帝呀,请你告诉我,我的未来会怎样?"

上帝摇摇头"唉,我的孩子,你还是别当程序员了")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值