📄 intlinux.c
字号:
U8 last; /*store address of last byte of heap + 1*//* prototypes------------------------------------------------------*/void heapInit();U8 alloc(U8 nbytes);int currentNodeAlloc(U8 i,U8 nbytes);void deAlloc(U8 address);void printMemory();/* definitions-----------------------------------------------------*/void handleAllocCall(){ switch((U1)R[$R1]) { case 0: { R[$R3] = alloc(R[$R2]); }break; case 1: { deAlloc(R[$R2]); }break; case 2: { printMemory(); }break; case 3: { heapInit(); }break; default: { ERROR1_LVL2("INT 7 %lu function not handled",(U1)R[$R1]); } } return;}/*end handleAllocCall*/void heapInit(){ /* set up scale parameters*/ memory=RAM; first=R[$HS]; last=R[$HE]+1; /*initialize the entire heap*/ prev(first)=0; next(first)=0; status(first)=FREE; return;}/*end heapInit*/U8 alloc(U8 nbytes){ int ret; U8 i; i=first; if(status(i)==FREE) { ret = currentNodeAlloc(i,nbytes); if(ret==TRUE) { return(i+MEM_HEADER); } } while(next(i)!=0) { i=next(i); if(status(i)==FREE) { ret = currentNodeAlloc(i,nbytes); if(ret==TRUE) { return(i+MEM_HEADER); } } } return(0);}/*end alloc*/int currentNodeAlloc(U8 i,U8 nbytes){ U8 size; /*handle case of current block being the last*/ if(next(i)==0){ size = last-i-MEM_HEADER; } else{ size = size(i); } /*either split current block, use entire current block, or fail*/ if(size >= nbytes + MEM_HEADER + MIN_FREE_BYTES) { U8 old_next; U8 old_block; U8 new_block; old_next = next(i); old_block = i; /*fix up original block*/ next(i)=i+MEM_HEADER+nbytes; new_block = next(i); status(i)=RESERVED; /*set up new free block*/ i = next(i); next(i)=old_next; prev(i)=old_block; status(i)=FREE; /*right nieghbor must point to new free block*/ if(next(i)!=0) { i = next(i); prev(i)=new_block; } return(TRUE); } else if(size >= nbytes) { status(i)=RESERVED; return(TRUE); } return(FALSE);}/*end currentNodeAlloc*//*Note: disaster will strike if fed wrong address*/void deAlloc(U8 address){ U8 block; U8 lblock; U8 rblock; block = address-MEM_HEADER; lblock = prev(block); rblock = next(block); if((address>R[$HE])||(address<R[$HS])) { ERROR1_LVL2("deAlloc(): %I64u address out of bounds!\n",address); return; } /* 4 cases: FFF->F, FFR->FR, RFF->RF, RFR always want to merge free blocks */ if((lblock!=0)&&(rblock!=0)&&(status(lblock)==FREE)&&(status(rblock)==FREE)) { next(lblock)=next(rblock); status(lblock)=FREE; if(next(rblock)!=0){ prev(next(rblock))=lblock; } } else if((lblock!=0)&&(status(lblock)==FREE)) { next(lblock)=next(block); status(lblock)=FREE; if(next(block)!=0){ prev(next(block))=lblock; } } else if((rblock!=0)&&(status(rblock)==FREE)) { next(block)=next(rblock); status(block)=FREE; if(next(rblock)!=0){ prev(next(rblock))=block; } } else{ status(block)=FREE; } return;}/*end deAlloc*/void printMemory(){ U8 i; i=first; printf("--HEAP--\n"); printf("[%llu,%llu,%llu,%s]\n",prev(i),i,next(i),statStr[status(i)]); while(next(i)!=0) { i=next(i); printf("[%llu,%llu,%llu,%s]\n",prev(i),i,next(i),statStr[status(i)]); } return;}/*end printMemory*//*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ handleMathCall ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/#include<math.h>void handleMathCall(){ switch((U1)R[$R1]) { case 0: { /* no call to handle 64-bit values so we handle what we can ( 32-bit ) */ printf("Warning: (INT 8, $R1=0) only handles 32-bit values on Linux\n"); R[$R3]=atol(&RAM[R[$R2]]); }break; case 1: { Rd[$D1]=atof(&RAM[R[$R2]]); }break; case 2: { sprintf(&RAM[R[$R3]],"%lld",R[$R2]); }break; case 3: { sprintf(&RAM[R[$R3]],"%e",Rd[$D1]); }break; case 4: { printf("Warning: (INT 8, $R1=4) not handled on Linux\n"); }break; case 5: { R[$R3] = labs((long)R[$R2]); }break; case 6: { Rd[$D2] = fabs(Rd[$D1]); }break; case 7: { Rd[$D2] = ceil(Rd[$D1]); }break; case 8: { Rd[$D2] = floor(Rd[$D1]); }break; case 9: { Rd[$D2] = exp(Rd[$D1]); }break; case 10: { Rd[$D2] = log(Rd[$D1]); }break; case 11: { Rd[$D2] = log10(Rd[$D1]); }break; case 12: { Rd[$D3] = pow(Rd[$D1],Rd[$D2]); }break; case 13: { Rd[$D2] = sqrt(Rd[$D1]); }break; case 14: { Rd[$D2] = cos(Rd[$D1]); }break; case 15: { Rd[$D2] = sin(Rd[$D1]); }break; case 16: { Rd[$D2] = tan(Rd[$D1]); }break; case 17: { Rd[$D2] = acos(Rd[$D1]); }break; case 18: { Rd[$D2] = asin(Rd[$D1]); }break; case 19: { Rd[$D2] = atan(Rd[$D1]); }break; case 20: { Rd[$D2] = cosh(Rd[$D1]); }break; case 21: { Rd[$D2] = sinh(Rd[$D1]); }break; case 22: { Rd[$D2] = tanh(Rd[$D1]); }break; default: { ERROR1_LVL2("INT 8 %lu function not handled",(U1)R[$R1]); } } return;}/*end handleMathCall*//*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ handleNativeCall ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/void handleNativeCall(){ /* Linux shared libraries must be specified on the command line when an application is compiled and linked. You can't just specify a file name dynamically like you do on windows */ switch((U1)R[$R1]) { case 0: { printf("Warning: (INT 9, $R1=0) not handled on Linux\n"); }break; case 1: { printf("Warning: (INT 9, $R1=1) not handled on Linux\n"); }break; case 2: { printf("Warning: (INT 9, $R1=2) not handled on Linux\n"); }break; default: { ERROR1_LVL2("INT 9 %lu function not handled",(U1)R[$R1]); } } return;}/*end handleNativeCall*//*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ handleIPC ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/#include<sys/socket.h>#include<netdb.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>#include<errno.h>#include<netinet/in.h>char fname[_POSIX_PATH_MAX];void handleIPC(){ switch((U1)R[$R1]) { /*use file locking instead of semaphores (RS p.463)*/ case 0: { int fd; fd = open(&RAM[R[$R2]],O_WRONLY|O_CREAT|O_EXCL,0644); while(fd<0 && errno==EEXIST) { fd = open(&RAM[R[$R2]],O_WRONLY|O_CREAT|O_EXCL,0644); } if(fd<0){ R[$R3]=0; R[$R4]=1; } else { R[$R3]=fd; R[$R4]=1; strcpy(fname,&RAM[R[$R2]]); } }break; case 1: { int retval; retval = close((int)R[$R2]); if(retval==-1) { R[$R3]=1; } else { retval = unlink(fname); if(retval==-1){ R[$R3]=1; } else{ R[$R3]=0; } } }break; case 2: { struct sockaddr_in address; int client; int err; address.sin_family=AF_INET; address.sin_port = htons((unsigned short)R[$R3]); address.sin_addr.s_addr = inet_addr(&RAM[R[$R2]]); client = socket(AF_INET,SOCK_STREAM,0); if(client<0) { R[$R5]=1; } else { err = connect(client, &address, sizeof(struct sockaddr_in)); if(err==-1) { /*printf("connection to %s failed\n",&RAM[R[$R2]]);*/ R[$R5]=1; } else { /*printf("connection to %s suceeded\n",&RAM[R[$R2]]);*/ R[$R5]=0; R[$R4]=(U8)client; } } }break; case 3: { if(close((int)R[$R2])==-1) { R[$R3]=1; } else { R[$R3]=0; } }break; case 4: { int nLeft; int index; char *buffer; nLeft=(int)R[$R4]; index=0; R[$R5]=0; buffer = &RAM[R[$R3]]; while(nLeft>0) { int ret; ret = send((int)R[$R2], &(buffer[index]), nLeft, 0); if(ret==-1) { R[$R5]=1; break; } nLeft = nLeft - ret; index = index + ret; } }break; case 5: { int nLeft; int index; char *buffer; nLeft=(int)R[$R4]; index=0; R[$R5]=0; buffer = &RAM[R[$R3]]; while(nLeft>0) { int ret; ret = recv((int)R[$R2], &(buffer[index]), nLeft, 0); if(ret==-1) { R[$R5]=1; break; } nLeft = nLeft - ret; index = index + ret; } }break; case 6: { if(gethostname(&RAM[R[$R2]],(int)R[$R3])) { R[$R4]=1; } else { R[$R4]=0; } }break; case 7: { struct hostent *hp; int i; hp = gethostbyname(&RAM[R[$R2]]); if(hp==NULL) { R[$R4]=1; } else { if((*hp).h_addr_list[0]!=0) { struct in_addr addr; memcpy(&addr, (*hp).h_addr_list[0], sizeof(struct in_addr)); strcpy(&RAM[R[$R3]],inet_ntoa(addr)); } for (i =1;(*hp).h_addr_list[i]!=0;++i) { struct in_addr addr; memcpy(&addr, (*hp).h_addr_list[i], sizeof(struct in_addr)); strcat(&RAM[R[$R3]],":"); strcat(&RAM[R[$R3]],inet_ntoa(addr)); } R[$R4]=0; } }break; case 8: { struct hostent *hp; struct in_addr hostaddr; hostaddr.s_addr = inet_addr(&RAM[R[$R2]]); hp = gethostbyaddr((char *)&hostaddr, sizeof(struct in_addr), AF_INET); if(hp==NULL) { R[$R4]=1; } else { strcpy(&RAM[R[$R3]],(*hp).h_name); R[$R4]=0; } }break; case 9: { struct sockaddr_in address; int server; int err; address.sin_family=AF_INET; address.sin_port = htons((unsigned short)R[$R3]); address.sin_addr.s_addr = inet_addr(&RAM[R[$R2]]); server = socket(AF_INET,SOCK_STREAM,0); if(server==-1) { /*printf("socket for %s failed\n",&RAM[R[$R2]]);*/ R[$R5]=1; } else { err = bind(server, &address, sizeof(address)); if(err==-1) { /*printf("bind to %s failed\n",&RAM[R[$R2]]);*/ R[$R5]=1; } else { /*printf("bound to %s \n",&RAM[R[$R2]]);*/ err = listen(server,5); if(err==-1) { /*printf("listen to %s failed\n",&RAM[R[$R2]]);*/ R[$R5]=1; } else { /*printf("listen to %s succeeded\n",&RAM[R[$R2]]);*/ R[$R5]=0; R[$R4]=(U8)server; } } } }break; case 10: { int connection; struct sockaddr_in client; int length; connection = accept((int)R[$R2], &client, &length); if(connection==-1) { /*printf("accept failed\n");*/ R[$R5]=1; } else { R[$R5]=0; strcpy(&RAM[R[$R3]],inet_ntoa(client.sin_addr)); R[$R4]=(U8)connection; /*printf("accept of %s was a sucess\n",&RAM[R[$R3]]);*/ } }break; case 11: { if(close((int)R[$R2])) { R[$R3]=1; } else { R[$R3]=0; } }break; case 12: { if(close((int)R[$R2])) { R[$R3]=1; } else { R[$R3]=0; } }break; default: { ERROR1_LVL2("INT 10 %lu function not handled",(U1)R[$R1]); } } return;}/*end handleIPC*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -