⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memory.h

📁 操作系统的内存管理
💻 H
字号:
//内存管理申明文件
#ifndef _MEMORY_H_
#define _MEMORY_H_
#include <stdio.h>
#include <stdlib.h>
//全局变量定义
#define TOTALMEMORY 60000//数组长度
char memoryarray[TOTALMEMORY];
//链,记录空闲区或使用区
struct list
{
  //char * firstaddress;//空闲区或使用区首地址
  struct list * pre;//上一块空闲区或使用区指针,
  //NULL表示此空闲区或使用区为第一块空闲区或使用区
  unsigned int size;//空闲区或使用区大小
  struct list * next;//下一块空闲区或使用区指针,
  //NULL表示此空闲区或使用区为最后一块空闲区或使用区

};

struct memory
{
   char *memorybase; //内存区首地址 
   long memorysize;//内存区大小
   struct list * myalloclist;//使用链
   struct list * myfreelist;//自由链
};
struct memory mymemory;
char *myalloc(unsigned int size);
void  myfree(char*p);
char * myalloc(unsigned int size)
{
 struct list *tmplist; 
 struct list *tmplist2;
 struct list *tmplist3;
 tmplist2 = mymemory.myfreelist;//初始化tmplist2
 tmplist3 = mymemory.myfreelist;//初始化tmplist3
 for(tmplist = mymemory.myfreelist;tmplist!=NULL;tmplist = tmplist->next)
 {
          if (size<=(tmplist->size)) //最先适应法,找到就给它
          {   if(size<=(tmplist->size-sizeof(struct list)))//一分为二
              { 
                 tmplist2 = tmplist;
			     tmplist =(struct list*) (tmplist+size+sizeof(struct list));
				 tmplist->size = tmplist2->size-size-sizeof(struct list);
				 tmplist->next = tmplist2->next;
                 tmplist->pre = tmplist2->pre;
				 if(tmplist2->pre!=NULL)(tmplist2->pre)->next = tmplist;
				 else mymemory.myfreelist = tmplist;
				 tmplist2->size = size;
				 printf("分到%d块内存,余下的可以划分为一块!\n",size);
              }
	      else//不能分了
		  {     if(tmplist->size==size) printf("刚好分到%d块内存!\n",size);
		        else printf("分到%d块内存,余下的不能划分为一块!\n",size);
                tmplist2 = tmplist;
                tmplist3 = tmplist->pre;
				tmplist = tmplist->next;
				if(tmplist!=NULL) tmplist->pre = tmplist3;
                if(tmplist3!=NULL) tmplist3->next = tmplist;
				else mymemory.myfreelist = tmplist;
              }
              
              // 加入 mymemory.myalloclist,直接放到头就可以
			  tmplist3 = mymemory.myalloclist;
              mymemory.myalloclist = tmplist2;
			  mymemory.myalloclist->pre = NULL;
			  mymemory.myalloclist->next = tmplist3;
			  if(tmplist3!=NULL) tmplist3->pre = mymemory.myalloclist;
              return (char*) (tmplist2 + sizeof(struct list));
          }
 }
 printf("内存不够,没有分到%d块内存!\n",size);
 return NULL;
}

void myfree(char*p)
{   int mark1 = 0;
    int mark2 = 0,size;
	struct list *tmplist; 
	struct list *tmplist2;
    struct list *tmplist3;
    tmplist2 = mymemory.myfreelist;//初始化tmplist2
    tmplist3 = mymemory.myfreelist;//初始化tmplist3 
    // 首先在 mymemory.myalloclist 中去除
	if(p==NULL) printf("此变量内存已经释放了,不要再释放!\n");
	else{
	for(tmplist = mymemory.myalloclist;tmplist!=NULL;tmplist = tmplist->next)
	{
	  if(p==(char *)(tmplist+sizeof(struct list)))//在 mymemory.myalloclist找到了
      {
		  
	    tmplist2 = tmplist;
		size = tmplist->size;
		if(tmplist->pre==NULL&&tmplist->next==NULL){mark1=1;mymemory.myalloclist=NULL;break;}
		else if(tmplist->pre==NULL&&tmplist->next!=NULL){mymemory.myalloclist = (mymemory.myalloclist)->next;(mymemory.myalloclist)->pre =NULL;mark1 = 1;break;}
		else if(tmplist->pre!=NULL&&tmplist->next==NULL){(tmplist->pre)->next=NULL;mark1 = 1;break;}
		else
		{
		  tmplist3 = tmplist->pre;
		  tmplist = tmplist->next; 
          if(tmplist!=NULL) tmplist->pre = tmplist3;
		  if(tmplist3!=NULL)tmplist3->next = tmplist;
		  mark1 = 1;break;
        } 
	  }
	}
	if(mark1!=1) {printf("严重错误:不是我们给的内存不能释放!\n");}
	else{
	mark1 = 0;
      // 然后放入 mymemory.myfreelist
	if(mymemory.myfreelist==NULL) mymemory.myfreelist = tmplist2;
	else{
    for(tmplist = mymemory.myfreelist;tmplist!=NULL;tmplist = tmplist->next)
	{
	   if(tmplist2<tmplist)//插在tmplist的前面
       {
	     tmplist3 = tmplist->pre;
		 if(tmplist3!=NULL)tmplist3->next = tmplist2;
		 else mymemory.myfreelist = tmplist2;
		 tmplist2->pre = tmplist3;
		 tmplist2->next = tmplist;
		 tmplist->pre = tmplist2;
		 break;
	   }
	}
	}
      // 再检查相临的是否需要合并
	//先检查和前面的是否能合并
    if(tmplist3!=NULL)
	{
		if((tmplist3+tmplist3->size+sizeof(struct list))==tmplist2)
		{
		  //合并
			tmplist3->size = tmplist3->size+tmplist2->size+sizeof(struct list);
			tmplist3->next = tmplist;
			tmplist->pre = tmplist3;
			tmplist2 = tmplist3;
			mark1 = 1;
		}
	} 
	//再检查跟后面的是否能合并
	if(tmplist2->next!=NULL)
	{
		if((tmplist2+tmplist2->size+sizeof(struct list))==tmplist)
		{
            //合并
			tmplist2->size = tmplist2->size+sizeof(struct list)+tmplist->size;
			tmplist2->next = tmplist->next;
			if(tmplist->next!=NULL)(tmplist->next)->pre = tmplist2;
			mark2 = 1;
		}
	}
	if(mark1==0&&mark2==0) printf("释放%d块内存成功,没有合并\n",size);
    else if(mark1==0&&mark2==1) printf("释放%d块内存成功,释放块和他后面块合并\n",size);

	else if(mark1==1&&mark2==0) printf("释放%d块内存成功,释放块和他前面块合并\n",size);
	else printf("释放%d块内存成功,释放块和他前,后面块合并\n",size);
	}
	}
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -