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

📄 iomgr.cpp

📁 C写的小型操作系统源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

__SUCCESSFUL:

	ObjectManager.DestroyObject(&ObjectManager,
		(__COMMON_OBJECT*)lpDrcb);    //Destroy the DRCB object.

__TERMINAL:
	if(!bResult)    //This routine failed.
	{
		if(lpDrcb != NULL)    //Destroy the DRCB object.
		{
			ObjectManager.DestroyObject(&ObjectManager,
				(__COMMON_OBJECT*)lpDrcb);
		}
	}
	return bResult;
}

//
//The WriteFile's implementation.
//The routine does the following:
// 1. Create a DRCB object,and initialize it;
// 2. Commit the write transaction by calling DeviceWrite routine;
// 3. According to the result,set appropriate return value(s).
//

static BOOL WriteFile(__COMMON_OBJECT*  lpThis,
					  __COMMON_OBJECT*  lpFileObj,
					  DWORD             dwWriteSize,
					  LPVOID            lpBuffer,
					  DWORD*            lpWrittenSize)
{
	BOOL              bResult           = FALSE;
	__DRCB*           lpDrcb            = NULL;
	__DEVICE_OBJECT*  lpDevObject       = NULL;
	__DRIVER_OBJECT*  lpDrvObject       = NULL;
	DWORD             dwWriteBlockSize  = 0L;
	DWORD             dwTotalSize       = 0L;

	if((NULL == lpThis) ||
	  (NULL == lpFileObj) ||
	  (0 == dwWriteSize) ||
	  (NULL == lpBuffer))    //Parameters check.
	  return bResult;

	lpDevObject = (__DEVICE_OBJECT*)lpFileObj;
	lpDrvObject = lpDevObject->lpDriverObject;

	lpDrcb = (__DRCB*)ObjectManager.CreateObject(&ObjectManager,
		NULL,
		OBJECT_TYPE_DRCB);
	if(NULL == lpDrcb)  //Failed to create DRCB object.
		goto __TERMINAL;

	if(!lpDrcb->Initialize((__COMMON_OBJECT*)lpDrcb)) //Failed to initialize.
		goto __TERMINAL;

	//
	//The following code gets the write block size of this device object.
	//
	lpDrcb->dwRequestMode     = DRCB_REQUEST_MODE_IOCTRL;
	lpDrcb->dwCtrlCommand     = IOCONTROL_GET_WRITE_BLOCK_SIZE;
	lpDrcb->dwStatus          = DRCB_STATUS_INITIALIZED;
	lpDrcb->dwOutputLen       = sizeof(DWORD);
	lpDrcb->lpOutputBuffer    = (LPVOID)&dwWriteBlockSize;

	lpDrcb->WaitForCompletion = WaitForCompletion;
	lpDrcb->OnCancel          = OnCancel;
	lpDrcb->OnCompletion      = OnCompletion;

	lpDrvObject->DeviceCtrl((__COMMON_OBJECT*)lpDrvObject,
		(__COMMON_OBJECT*)lpDevObject,
		lpDrcb);    //Get the write block size.
	if(DRCB_STATUS_SUCCESS != lpDrcb->dwStatus)    //Failed to get the write block size.
		goto __TERMINAL;

	//
	//Now,the variable dwWriteBlockSize countains the write block size.
	//The following code,we commit the write transaction to device driver object
	//by calling DeviceWrite.
	//

	dwTotalSize           = dwWriteSize;
	lpDrcb->dwRequestMode = DRCB_REQUEST_MODE_WRITE;
	lpDrcb->dwStatus      = DRCB_STATUS_INITIALIZED;

	while(TRUE)
	{
		lpDrcb->dwInputLen     = 
			dwTotalSize > dwWriteBlockSize ? dwWriteBlockSize : dwTotalSize;
		lpDrcb->lpInputBuffer  = lpBuffer;
		lpDrvObject->DeviceWrite((__COMMON_OBJECT*)lpDrvObject,
			(__COMMON_OBJECT*)lpDevObject,
			lpDrcb);  //Commit the write transaction.

		if(DRCB_STATUS_SUCCESS != lpDrcb->dwStatus)  //Failed to write.
		{
			goto __TERMINAL;
		}
		if(dwTotalSize <= dwWriteBlockSize)    //This indicates the write transaction is
			                                   //over.
		{
			*lpWrittenSize = dwWriteSize;
			bResult = TRUE;
			goto __TERMINAL;
		}
		dwTotalSize -= dwWriteBlockSize;
		lpBuffer = (LPVOID)((DWORD)lpBuffer + dwWriteBlockSize);  //Adjust the buffer.
	}

__TERMINAL:
	if(lpDrcb != NULL)    //Destroy the DRCB object.
	{
		ObjectManager.DestroyObject(&ObjectManager,
			(__COMMON_OBJECT*)lpDrcb);
	}
	return bResult;
}

//
//The implementation of CloseFile.
//This routine does the following:
// 1. Check the validation of the parameters;
// 2. Check the device type of the target device object;
// 3. If the device to closed is a normal file,then destroy the object;
// 4. If the target deivce is not a normal file,then reduce the
//    reference counter,and return.
//

static VOID CloseFile(__COMMON_OBJECT*  lpThis,
					  __COMMON_OBJECT*  lpFileObj)
{
	__IO_MANAGER*              lpIoManager        = NULL;
	__DEVICE_OBJECT*           lpDevObject        = NULL;

	if((NULL == lpThis) || (NULL == lpFileObj)) //Parameter check.
		return;

	lpIoManager = (__IO_MANAGER*)lpThis;
	lpDevObject = (__DEVICE_OBJECT*)lpFileObj;

	if(lpDevObject->dwDevType != DEVICE_TYPE_FILE)  //Not a normal file.
	{
		//ENTER_CRITICAL_SECTION();
		//lpDevObject->dwRefCounter --;
		//LEAVE_CRITICAL_SECTION();
		return;
	}

	//
	//If control flow reaches here,it indicates that the device to be closed is
	//a normal file,then,we will destroy this file object by calling DestroyDevice
	//routine of IOManager object.
	//
	lpIoManager->DestroyDevice(lpThis,lpFileObj);  //Destroy the file object.
}

//
//The implementation of IOControl.
//This routine does the following:
// 1. 
//

static BOOL IOControl(__COMMON_OBJECT* lpThis,
					  __COMMON_OBJECT* lpFileObj,
					  DWORD            dwCommand,
					  LPVOID           lpParameter,
					  DWORD            dwOutputBufLen,
					  LPVOID           lpOutputBuffer,
					  DWORD*           lpdwOutputFilled)
{
	BOOL              bResult          = FALSE;

	if((NULL == lpThis) ||
	   (NULL == lpFileObj))  //Parameters check.
	   return bResult;

	return bResult;
}

//
//The implementation of SetFilePointer.
//This routine does the following:
// 1. 
//

static BOOL SetFilePointer(__COMMON_OBJECT* lpThis,
						   __COMMON_OBJECT* lpFileObj,
						   DWORD            dwWhereBegin,
						   INT              nOffset)
{
	BOOL                   bResult          = FALSE;

	if((NULL == lpThis) || (NULL == lpFileObj)) //Parameters check.
		return bResult;

	return bResult;
}

//
//The implementation of FlushFile.
//This routine does the following:
// 1. 
//

static BOOL FlushFile(__COMMON_OBJECT*  lpThis,
					  __COMMON_OBJECT*  lpFileObj)
{
	BOOL              bResult           = FALSE;

	if((NULL == lpThis) || (NULL == lpFileObj))  //Parameters check.
		return bResult;

	return bResult;
}

//
//The implementation of CreateDevice,this routine is called by device
//driver(s) to create device object.Generally,this routine is called in
//DriverEntry of device driver(s).
//This routine does the following:
// 1. Creates a device object by calling ObjectManager's interface;
// 2. Initializes the device object;
// 3. Allocates a block of memory as device object's extension;
// 4. Inserts the device object into device object's list.
//

static __DEVICE_OBJECT* CreateDevice(__COMMON_OBJECT*  lpThis,
									 LPSTR             lpszDevName,
									 __COMMON_OBJECT*  lpDrv,
									 DWORD             dwExtensionSize,
									 __RESOURCE_DESCRIPTOR*
									                   lpResDesc)
{
	__DEVICE_OBJECT*                 lpDevObj          = NULL;
	LPVOID                           lpDevExtension    = NULL;
	__IO_MANAGER*                    lpIoManager       = NULL;
	DWORD                            dwFlags           = 0L;

	if((NULL == lpThis)           ||
	   (NULL == lpszDevName)      ||
	   (NULL == lpDrv))           //Parameters check.
	   return lpDevObj;

	if(StrLen(lpszDevName) > MAX_DEV_NAME_LEN)  //The device's name is too long.
		return lpDevObj;

	lpIoManager = (__IO_MANAGER*)lpThis;
	lpDevObj    = (__DEVICE_OBJECT*)ObjectManager.CreateObject(
		&ObjectManager,
		NULL,
		OBJECT_TYPE_DEVICE);
	if(NULL == lpDevObj)      //Failed to create device object.
		return lpDevObj;

	if(!lpDevObj->Initialize((__COMMON_OBJECT*)lpDevObj)) //Failed to initialize the object.
	{
		ObjectManager.DestroyObject(&ObjectManager,
			(__COMMON_OBJECT*)lpDevObj);
		return NULL;
	}

	lpDevExtension = KMemAlloc(dwExtensionSize,KMEM_SIZE_TYPE_ANY);
	if(NULL == lpDevExtension)    //Failed to allocate device extension.
	{
		ObjectManager.DestroyObject(&ObjectManager,
			(__COMMON_OBJECT*)lpDevObj);
		return NULL;
	}

	lpDevObj->lpDevExtension = lpDevExtension;
	lpDevObj->lpDriverObject = (__DRIVER_OBJECT*)lpDrv;

	StrCpy(lpszDevName,(LPSTR)&lpDevObj->DevName[0]);

	//
	//The following code add the device object into device object's list.
	//

	//ENTER_CRITICAL_SECTION();
	__ENTER_CRITICAL_SECTION(NULL,dwFlags);

	if(NULL == lpIoManager->lpDeviceRoot)  //This is the first object.
	{
		lpIoManager->lpDeviceRoot = lpDevObj;
	}
	else    //This is not the first object.
	{
		lpDevObj->lpNext                  = lpIoManager->lpDeviceRoot;
		lpDevObj->lpPrev                  = NULL;
		lpIoManager->lpDeviceRoot->lpPrev = lpDevObj;
		lpIoManager->lpDeviceRoot         = lpDevObj;
	}
	//LEAVE_CRITICAL_SECTION();
	__LEAVE_CRITICAL_SECTION(NULL,dwFlags);

	return lpDevObj;
}

//
//The implementation of DestroyDevice.
//

static VOID DestroyDevice(__COMMON_OBJECT* lpThis,
						  __COMMON_OBJECT* lpDevObj)
{
	__DEVICE_OBJECT*      lpDeviceObject = NULL;
	__IO_MANAGER*         lpIoManager    = NULL;
	DWORD                 dwFlags        = 0L;

	if((NULL == lpThis) || (NULL == lpDevObj)) //Parameters check.
		return;

	lpIoManager         = (__IO_MANAGER*)lpThis;
	lpDeviceObject      = (__DEVICE_OBJECT*)lpDevObj;

	//
	//The following code deletes the device object from system list.
	//
	//ENTER_CRITICAL_SECTION();
	__ENTER_CRITICAL_SECTION(NULL,dwFlags);
	if(NULL == lpDeviceObject->lpPrev)    //This is the first object.
	{
		if(NULL == lpDeviceObject->lpNext)  //This is the last object.
			lpIoManager->lpDeviceRoot = NULL;
		else    //This is not the last object.
		{
			lpDeviceObject->lpNext->lpPrev = NULL;
			lpIoManager->lpDeviceRoot      = lpDeviceObject->lpNext;
		}
	}
	else    //This is not the first object.
	{
		if(NULL == lpDeviceObject->lpNext)  //This is the last object.
		{
			lpDeviceObject->lpPrev->lpNext = NULL;
		}
		else    //This is not the last object.
		{
			lpDeviceObject->lpPrev->lpNext = lpDeviceObject->lpNext;
			lpDeviceObject->lpNext->lpPrev = lpDeviceObject->lpPrev;
		}
	}
	//LEAVE_CRITICAL_SECTION();
	__LEAVE_CRITICAL_SECTION(NULL,dwFlags);

	//
	//The following code destroy the device object.
	//
	ObjectManager.DestroyObject(&ObjectManager,
		(__COMMON_OBJECT*)lpDeviceObject);

	return;
}

//
//The implementation of ReserveResource.
//This routine does the following:
// 1. 
//

static BOOL ReserveResource(__COMMON_OBJECT*    lpThis,
							__RESOURCE_DESCRIPTOR*
							                    lpResDesc)
{
	BOOL                    bResult             = FALSE;

	if((NULL == lpThis) || (NULL == lpResDesc)) //Parameters check.
		return bResult;

	return bResult;
}

/*************************************************************************
**************************************************************************
**************************************************************************
**************************************************************************
*************************************************************************/

//
//The following code defines one of the global objects in Hello China - IOManager.
//This object is a global object,and only one in the whole system life-cycle.
//

__IO_MANAGER IOManager = {
	NULL,                                   //lpDeviceRoot.
	NULL,                                   //lpDriverRoot.
	NULL,                                   //lpResDescriptor.
	IOManagerInitialize,                    //Initialize.
	CreateFile,                             //CreateFile.
	ReadFile,                               //ReadFile.
	WriteFile,                              //WriteFile.
	CloseFile,                              //CloseFile.
	IOControl,                              //IOControl.
	SetFilePointer,                         //SetFilePointer.
	FlushFile,                              //FileFlush.

	CreateDevice,                           //CreateDevice.
	DestroyDevice,                          //DestroyDevice.
	ReserveResource                         //ReserveResource.
};

⌨️ 快捷键说明

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