Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
m_r.c
Package: m_r.rar [view]
Upload User: anhua0585
Upload Date: 2020-12-05
Package Size: 2k
Code Size: 6k
Category:
MacOS develop
Development Platform:
Visual C++
- #include<stdio.h>
- #include<stdlib.h>
- //const N=30;
- struct map{ //coremap结构体定义
- unsigned size;
- unsigned addr;
- struct map *fore,*next;
- };
- struct map *coremap,*tmp,*temp;
- struct map *lmalloc(struct map *mp,unsigned size); //申请空间
- struct map *lfree(unsigned size,unsigned addr); //释放空间
- int main(void)
- {
- unsigned size;
- char *a,*addr;
- char c;
- if((coremap=(struct map *)malloc(sizeof(struct map)))==NULL)
- printf("链表空间申请失败!n");
- if((a=(char *)malloc(1000))==NULL) //分配1000 bytes的内存
- printf("内存分配失败!n");
- coremap->addr=a; //链表初始化
- coremap->size=1000;
- coremap->fore=NULL;
- coremap->next=NULL;
- // printf("%sn",a); //用此显示测试是否成功
- // printf("%sn",coremap->addr);
- //程序应用的说明:
- printf("Statement:nThis is a program to malloc or free the RAM.nThe total size you malloc should be smaller than 1000 bytesn");
- printf("If you want to malloc 10 bytes,Please input as below:m10.n");
- printf("If you want to free 20 bytes at 3680400,Please input as below:f 20 3680400.n");
- printf("Press 'q' to get out of the system.n");
- while(1){
- agn:
- printf("Please input the command:n");
- do{ //跳过换行和空格
- c=getchar();
- }while(c=='n'||c=='t'||c==' ');
- switch(c){
- case 'm': //输入'm'时跳到lmalloc() 申请内存
- scanf("%u",&size);
- if(size>1000){
- printf("The size should be smaller than 1000!n");
- goto agn;
- }
- // printf("The size you command is:%un",size); //用此显示语句测试是否成功
- if(coremap->next!=NULL)
- tmp=coremap->next;
- else
- tmp=coremap;
- coremap=lmalloc(tmp,size);
- break;
- case 'f': //输入'f'时跳到lfree() 释放内存
- scanf("%u%u",&size,&addr);
- // printf("%u %un",size,addr); //用此显示语句测试是否成功
- coremap=lfree(size,addr);
- break;
- case 'q': //输入'q' 退出
- exit(0);
- break;
- default: //输入其他字符时,显示错误,提示重新输入
- printf("Sorry,your command is illegal!n");
- goto agn;
- }
- tmp=coremap;
- if(tmp->next!=NULL){ //输出指针队列信息
- temp=tmp;
- printf("n%sn%s%un","The results:","The address is:",tmp->addr);
- printf("%s%un","The size left is:",tmp->size);
- do{
- tmp=tmp->next;
- printf("%s%un","The address is:",tmp->addr);
- printf("%s%un","The size left is:",tmp->size);
- }while(tmp->next!=temp);
- }
- else{
- printf("n%sn%s%un","The results","The address is:",tmp->addr);
- printf("%s%unn","The size left is:",tmp->size);
- }
- printf("%s%un","当前指针指向:",coremap->addr); //输出当前指针所指的位置
- }
- return 0;
- }
- struct map *lmalloc(struct map *mp,unsigned size) //申请空间
- {
- register struct map *a,*b;
- b=mp;
- do{
- if(mp->size>=size){
- mp->addr+=size;
- a=mp;
- if((mp->size-=size)==0){
- a=mp->next;
- if(mp==mp->next->next){ //删除此节点
- mp->fore->next=NULL;
- mp->next->fore=NULL;
- free(mp);
- }
- else{
- mp->fore->next=mp->next;
- mp->next->fore=mp->fore;
- free(mp);
- }
- }
- return a;
- }
- else{ //所申请空间比所有剩余空间均大时,退出
- if(mp->next!=NULL)
- mp=mp->next;
- else
- ;
- if(b==mp){
- printf("No enough RAM to malloc!!!n");
- exit(0);
- }
- }
- }while(mp->size);
- return NULL;
- }
- struct map *lfree(unsigned size,unsigned addr)
- {
- struct map *bp;
- if(coremap->fore!=NULL){ //指针指向队首
- do{
- coremap=coremap->fore;
- }while(coremap->addr > (coremap->fore)->addr);
- }
- else
- ;
- bp=coremap;
- for(;coremap->addr<=addr&&coremap->size!=0;coremap=coremap->next); //使指针指向地址比addr大的最小地址指针
- if(coremap->fore!=NULL){ //coremap链表不止一个节点时
- if((coremap->fore)->addr+(coremap->fore)->size==addr){
- (coremap->fore)->size+=size; //情况1
- if(addr+size==coremap->addr){ //情况2
- if(bp==coremap)
- bp=coremap->fore;
- coremap->fore->size+=coremap->size;
- coremap->fore->next=coremap->next;
- coremap->next->fore=coremap->fore;
- free(coremap); //删除此节点
- }
- }
- else{
- if(addr+size==coremap->addr&&coremap->size){ //情况3
- if(bp==coremap){
- coremap->size+=size;
- coremap->addr-=size;
- bp=coremap;
- }
- else{
- coremap->size+=size;
- coremap->addr-=size;
- }
- }
- else //情况4
- if(size){
- struct map *newm=(struct map *)malloc(sizeof(struct map));
- newm->fore=coremap->fore;
- newm->next=coremap;
- coremap->fore->next=newm;
- coremap->fore=newm;
- newm->addr=addr;
- newm->size=size;
- }
- }
- }
- else{ //else coremap只有一个节点时,即只有一个空闲区
- if(addr+size==coremap->addr&&coremap->size){
- coremap->size+=size;
- coremap->addr-=size;
- bp=coremap;
- }
- else
- if(size){
- struct map *newm=(struct map *)malloc(sizeof(struct map)); //申请一个新的指针指向此新的空间
- newm->fore=coremap;
- newm->next=coremap;
- newm->addr=addr;
- newm->size=size;
- coremap->next=newm;
- coremap->fore=newm;
- }
- }
- return bp; //返回coremap,且其所指向的位置根据内存释放的情况进行了调整
- }