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


    发一下牢骚和主题无关:

        每日一道理
    坚持的昨天叫立足,坚持的今天叫进取,坚持的明天叫成功。
    #ifdef HAVA_CONFIG_H
    #include <config.h>
    #endif
    
    #include<stdio.h>
    #include<stdlib.h>
    #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 <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 ); //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("<%d\t\t%d>\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说"程序员的未来就是榨取程序员"
    这个程序员对这个未来不满意,于是他又去问上帝。
    "万能的上帝呀,请你告诉我,我的未来会怎样?"
    上帝说"我的孩子,你去问侯捷,他写的计算机书的读者可能是地球上最多的"
    于是他去问侯捷。
    侯捷说"程序员的未来就是诱惑程序员"
    这个程序员对这个未来不满意,于是他又去问上帝。
    "万能的上帝呀,请你告诉我,我的未来会怎样?"
    上帝摇摇头"唉,我的孩子,你还是别当程序员了")

  • 相关阅读:
    openstack-9块存储服务(此服务可选)
    openstack-7dashboard(仪表盘)
    openstack-6neutron(网络服务)
    openstack-5computer(计算服务)
    openstack-4glance(镜像服务)
    openstack-12补充
    openstack-10实现VPC自定义网络
    openstack-8实现内外网结构
    openstack-3keystone(认证服务)
    js实现填写身份证号、手机号、准考证号等信息自动空格的效果
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3067695.html
Copyright © 2020-2023  润新知