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

📄 intwin32.c

📁 《虚拟机的设计与实现c/cpp》(win32平台)源码 好东西大家熟知不多说
💻 C
📖 第 1 页 / 共 2 页
字号:
		}
	}
	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("[%I64u,%I64u,%I64u,%s]\n",prev(i),i,next(i),statStr[status(i)]);
	while(next(i)!=0)
	{
		i=next(i);
		printf("[%I64u,%I64u,%I64u,%s]\n",prev(i),i,next(i),statStr[status(i)]);
	}
	return;

}/*end printMemory*/

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  handleMathCall                                                   +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

#include<float.h>
#include<math.h>

void handleMathCall()
{
	switch((U1)R[$R1])
	{
		case 0:
		{
			R[$R3]=_atoi64(&RAM[R[$R2]]);

		}break;
		case 1:
		{
			Rd[$D1]=atof(&RAM[R[$R2]]);

		}break;
		case 2:
		{
			sprintf(&RAM[R[$R3]],"%I64d",R[$R2]);

		}break;
		case 3:
		{
			sprintf(&RAM[R[$R3]],"%e",Rd[$D1]);

		}break;
		case 4:
		{
			switch(_fpclass(Rd[$D1]))
			{
				case _FPCLASS_NINF:{ R[$R2]= 0; }break;
				case _FPCLASS_NN:{ R[$R2]= 1; }break;
				case _FPCLASS_ND:{ R[$R2]= 2; }break;
				case _FPCLASS_NZ:{ R[$R2]= 3; }break;
				case _FPCLASS_PZ:{ R[$R2]= 4; }break;
				case _FPCLASS_PD:{ R[$R2]= 5; }break;
				case _FPCLASS_PN:{ R[$R2]= 6; }break;
				case _FPCLASS_PINF:{ R[$R2]= 7; }break;
				case _FPCLASS_SNAN:{ R[$R2]= 8; }break;
				case _FPCLASS_QNAN:{ R[$R2]= 8; }break;
				default:{ R[$R2]= 8; }break;
			}

		}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()
{
	typedef void (*FunctionPtr)(char*, char*);
	FunctionPtr address;

	switch((U1)R[$R1])
	{
		case 0:
		{
			HINSTANCE file_handle;
			file_handle = LoadLibrary(&RAM[R[$R2]]);
			R[$R3]= (U8)file_handle;
			if(file_handle==NULL){ R[$R4]=1; }
			else{ R[$R4]=0; }

		}break;
		case 1:
		{
			address = 
			(FunctionPtr)GetProcAddress((HINSTANCE)R[$R2],"gateway");			
			if(address==NULL)
			{
				R[$R5]=1;
			}
			else
			{
				R[$R5]=0;
				(address)(&RAM[R[$R3]],&RAM[R[$R4]]);
			}

		}break;
		case 2:
		{
			int unload;
			unload = (int)FreeLibrary((HINSTANCE)R[$R2]);
			if(unload==0)
			{
				R[$R3]=1;
			}
			else
			{
				R[$R3]=0;
			}

		}break;
		default:
		{ 
			ERROR1_LVL2("INT 9 %lu function not handled",(U1)R[$R1]);
		}
	}
	return;

}/*end handleNativeCall*/

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  handleIPC                                                        +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

int initSockets()
{
	WORD wVersionRequested;
	WSADATA wsaData;

	wVersionRequested = MAKEWORD( 2, 2 );
 
	if(WSAStartup( wVersionRequested, &wsaData ))
	{ 
		return(1); 
	}

	if(LOBYTE(wsaData.wVersion)!=2 || 
	   HIBYTE(wsaData.wVersion)!=2)
	{
		return(1);
	}

	return(0);

}/*end initSockets*/

void handleIPC()
{
	switch((U1)R[$R1])
	{
		case 0:
		{
			HANDLE hmutex;  // void*

			hmutex = CreateMutex(NULL,TRUE,&RAM[R[$R2]]);
			if(hmutex==NULL)
			{
				if(GetLastError()==ERROR_ALREADY_EXISTS)
				{
					hmutex = 
					OpenMutex(MUTEX_ALL_ACCESS,FALSE,&RAM[R[$R2]]);
					if(hmutex==FALSE)
					{
						R[$R4]=1;
					}
					else
					{
						WaitForSingleObject(hmutex,INFINITE);
						R[$R3]=(U8)hmutex;
						R[$R4]=0;
					}
				}
				else
				{
					R[$R4]=1;
				}
			}
			else
			{	
				WaitForSingleObject(hmutex,INFINITE);
				R[$R3]=(U8)hmutex;
				R[$R4]=0;
			}
		
		}break;
		case 1:
		{
			BOOL retval;
			retval = ReleaseMutex((HANDLE)R[$R2]);
			if(retval==FALSE)
			{
				R[$R3]=1;
			}
			else
			{
				retval = CloseHandle((HANDLE)R[$R2]);
				if(retval==FALSE)
				{
					R[$R3]=1;
				}
				else
				{
					R[$R3]=0;
				}
			}

		}break;
		case 2:
		{
			if(initSockets())
			{
				R[$R5]=1;
			}
			else
			{
				struct sockaddr_in address;
				SOCKET 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==INVALID_SOCKET)
				{
					R[$R5]=1;
				}
				else
				{
					err = connect(client,
						         (SOCKADDR*)&address,
								  sizeof(address));
					if(err==SOCKET_ERROR)
					{
						R[$R5]=1;
					}
					else
					{
						R[$R5]=0;
						R[$R4]=(U8)client;
					}
				}
			}

		}break;
		case 3:
		{
			if(shutdown((SOCKET)R[$R2],0x02)==SOCKET_ERROR)
			{
				R[$R3]=1;
			}
			else
			{
				if(closesocket((SOCKET)R[$R2])==SOCKET_ERROR)
				{
					R[$R3]=1;
				}
				else
				{
					R[$R3]=0;
					WSACleanup();
				}
			}

		}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((SOCKET)R[$R2],
					       &(buffer[index]),
						   nLeft,
						   0);
				if(ret==SOCKET_ERROR)
				{ 
					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((SOCKET)R[$R2],
					       &(buffer[index]),
						   nLeft,
						   0);
				if(ret==SOCKET_ERROR)
				{ 
					R[$R5]=1;
					break;
				}
				nLeft = nLeft - ret;
				index = index + ret;
			}

		}break;
		case 6:
		{
			if(initSockets())
			{
				R[$R4]=1;
			}
			else
			{
				if(gethostname(&RAM[R[$R2]],(int)R[$R3]))
				{ 
					R[$R4]=1; 
					WSACleanup();
				}
				else
				{
					R[$R4]=0;
					WSACleanup();
				}
			}

		}break;
		case 7:
		{
			HOSTENT *hp;
			int i;

			if(initSockets())
			{
				R[$R4]=1;
			}
			else
			{
				hp = gethostbyname(&RAM[R[$R2]]);
				if(hp==NULL)
				{
					R[$R4]=1;
					WSACleanup();
				}
				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;
				}
				WSACleanup();
			}

		}break;
		case 8:
		{
			HOSTENT *hp;
			struct in_addr hostaddr;

			if(initSockets())
			{
				R[$R4]=1;
			}
			else
			{
				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;
				}

				WSACleanup();
			}

		}break;
		case 9:
		{
			if(initSockets())
			{
				R[$R4]=1;
			}
			else
			{
				struct sockaddr_in address;
				SOCKET 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==INVALID_SOCKET)
				{
					R[$R5]=1;
				}
				else
				{
					err = bind(server,
						       (SOCKADDR*)&address,
							   sizeof(address));
					if(err==SOCKET_ERROR)
					{
						R[$R5]=1;
					}
					else
					{
						err = listen(server,SOMAXCONN);
						if(err==SOCKET_ERROR)
						{
							R[$R5]=1;
						}
						else
						{
							R[$R5]=0;
							R[$R4]=(U8)server;
						}
					}
				}
			}

		}break;
		case 10:
		{
			SOCKET connection;
			struct sockaddr_in client;
			int length;
			connection = accept((SOCKET)R[$R2],
				                (SOCKADDR*)&client,
								&length);
			if(connection==INVALID_SOCKET)
			{
				R[$R5]=1;
			}
			else
			{
				R[$R5]=0;
				strcpy(&RAM[R[$R3]],inet_ntoa(client.sin_addr));
				R[$R4]=(U8)connection;
			}

		}break;
		case 11:
		{
			if(shutdown((SOCKET)R[$R2],0x02)==SOCKET_ERROR)
			{
				R[$R3]=1;
			}
			else
			{
				if(closesocket((SOCKET)R[$R2])==SOCKET_ERROR)
				{
					R[$R3]=1;
				}
				else
				{
					R[$R3]=0;
				}
			}

		}break;
		case 12:
		{
			if(closesocket((SOCKET)R[$R2])==SOCKET_ERROR)
			{
				R[$R3]=1;
			}
			else
			{
				R[$R3]=0;
				WSACleanup();
			}

		}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 + -