m_r.c
Upload User: anhua0585
Upload Date: 2020-12-05
Package Size: 2k
Code Size: 6k
Category:

MacOS develop

Development Platform:

Visual C++

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. //const N=30;
  4. struct map{                       //coremap结构体定义
  5. unsigned size;
  6. unsigned addr;
  7. struct map *fore,*next;
  8. };
  9. struct map *coremap,*tmp,*temp;
  10. struct map *lmalloc(struct map *mp,unsigned size);    //申请空间
  11. struct map *lfree(unsigned size,unsigned addr);       //释放空间
  12. int main(void)
  13. {
  14. unsigned size;
  15. char *a,*addr;
  16. char c;
  17. if((coremap=(struct map *)malloc(sizeof(struct map)))==NULL)
  18. printf("链表空间申请失败!n");
  19. if((a=(char *)malloc(1000))==NULL)             //分配1000 bytes的内存
  20. printf("内存分配失败!n");
  21. coremap->addr=a;                 //链表初始化
  22. coremap->size=1000;
  23. coremap->fore=NULL;
  24. coremap->next=NULL;
  25. // printf("%sn",a);                                 //用此显示测试是否成功
  26. // printf("%sn",coremap->addr);
  27. //程序应用的说明:
  28. printf("Statement:nThis is a program to malloc or free the RAM.nThe total size you malloc should be smaller than 1000 bytesn");
  29. printf("If you want to malloc 10 bytes,Please input as below:m10.n");
  30. printf("If you want to free 20 bytes at 3680400,Please input as below:f 20 3680400.n");
  31. printf("Press 'q' to get out of the system.n");
  32. while(1){
  33. agn:
  34. printf("Please input the command:n");
  35. do{                                 //跳过换行和空格
  36. c=getchar();
  37. }while(c=='n'||c=='t'||c==' ');
  38. switch(c){
  39. case 'm':                                //输入'm'时跳到lmalloc() 申请内存
  40. scanf("%u",&size);
  41. if(size>1000){
  42. printf("The size should be smaller than 1000!n");
  43. goto agn;
  44. }
  45. // printf("The size you command is:%un",size);      //用此显示语句测试是否成功
  46. if(coremap->next!=NULL)
  47. tmp=coremap->next;
  48. else
  49. tmp=coremap;
  50. coremap=lmalloc(tmp,size);
  51. break;
  52.  
  53. case 'f':                                //输入'f'时跳到lfree()   释放内存
  54. scanf("%u%u",&size,&addr);
  55. // printf("%u %un",size,addr);                     //用此显示语句测试是否成功
  56. coremap=lfree(size,addr);
  57. break;
  58. case 'q':                                //输入'q' 退出
  59. exit(0);
  60. break;
  61. default:                                 //输入其他字符时,显示错误,提示重新输入
  62. printf("Sorry,your command is illegal!n");
  63. goto agn;
  64. }
  65.     tmp=coremap;
  66. if(tmp->next!=NULL){                     //输出指针队列信息
  67. temp=tmp;
  68. printf("n%sn%s%un","The results:","The address is:",tmp->addr);
  69. printf("%s%un","The size left is:",tmp->size);
  70. do{
  71. tmp=tmp->next;
  72. printf("%s%un","The address is:",tmp->addr);
  73. printf("%s%un","The size left is:",tmp->size);
  74. }while(tmp->next!=temp);
  75. }
  76. else{
  77. printf("n%sn%s%un","The results","The address is:",tmp->addr);
  78. printf("%s%unn","The size left is:",tmp->size);
  79. }
  80. printf("%s%un","当前指针指向:",coremap->addr);        //输出当前指针所指的位置
  81. }
  82. return 0;
  83. }
  84. struct map *lmalloc(struct map *mp,unsigned size)                //申请空间
  85. {
  86.     register struct map *a,*b;
  87. b=mp;
  88. do{
  89. if(mp->size>=size){
  90. mp->addr+=size;
  91. a=mp;
  92. if((mp->size-=size)==0){
  93. a=mp->next;
  94. if(mp==mp->next->next){                        //删除此节点
  95. mp->fore->next=NULL;
  96. mp->next->fore=NULL;
  97. free(mp);
  98. }
  99. else{
  100. mp->fore->next=mp->next;    
  101. mp->next->fore=mp->fore;
  102. free(mp);
  103. }
  104. }
  105. return a;
  106. }
  107. else{                                     //所申请空间比所有剩余空间均大时,退出
  108. if(mp->next!=NULL)
  109. mp=mp->next;
  110. else
  111. ;
  112. if(b==mp){
  113. printf("No enough RAM to malloc!!!n");
  114. exit(0);
  115. }
  116. }
  117. }while(mp->size);
  118. return NULL;
  119. }
  120. struct map *lfree(unsigned size,unsigned addr)
  121. {
  122. struct map *bp;
  123. if(coremap->fore!=NULL){                     //指针指向队首
  124. do{
  125. coremap=coremap->fore;
  126. }while(coremap->addr > (coremap->fore)->addr);
  127. }
  128. else
  129. ;
  130. bp=coremap;
  131. for(;coremap->addr<=addr&&coremap->size!=0;coremap=coremap->next);   //使指针指向地址比addr大的最小地址指针
  132. if(coremap->fore!=NULL){                      //coremap链表不止一个节点时
  133. if((coremap->fore)->addr+(coremap->fore)->size==addr){
  134. (coremap->fore)->size+=size;              //情况1
  135. if(addr+size==coremap->addr){             //情况2
  136. if(bp==coremap)
  137. bp=coremap->fore;
  138. coremap->fore->size+=coremap->size;
  139. coremap->fore->next=coremap->next;
  140. coremap->next->fore=coremap->fore;
  141. free(coremap);                    //删除此节点
  142. }
  143. }
  144. else{
  145. if(addr+size==coremap->addr&&coremap->size){   //情况3
  146. if(bp==coremap){
  147. coremap->size+=size;
  148. coremap->addr-=size;
  149. bp=coremap;
  150. }
  151. else{                                          
  152. coremap->size+=size;
  153. coremap->addr-=size;
  154. }
  155. }
  156. else                                            //情况4
  157. if(size){
  158. struct map *newm=(struct map *)malloc(sizeof(struct map));
  159. newm->fore=coremap->fore;
  160. newm->next=coremap;
  161. coremap->fore->next=newm;
  162. coremap->fore=newm;
  163. newm->addr=addr;
  164. newm->size=size;
  165. }
  166. }
  167. }
  168. else{                                          //else    coremap只有一个节点时,即只有一个空闲区
  169. if(addr+size==coremap->addr&&coremap->size){
  170. coremap->size+=size;
  171. coremap->addr-=size;
  172. bp=coremap;
  173. }
  174. else
  175. if(size){
  176. struct map *newm=(struct map *)malloc(sizeof(struct map));   //申请一个新的指针指向此新的空间
  177. newm->fore=coremap;
  178. newm->next=coremap;
  179. newm->addr=addr;
  180. newm->size=size;
  181. coremap->next=newm;
  182. coremap->fore=newm;
  183. }
  184. }
  185. return bp;               //返回coremap,且其所指向的位置根据内存释放的情况进行了调整
  186. }