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

📄 cqueue.c

📁 wince host 和 target PCI驱动程序
💻 C
字号:
/*
	cqueue

	#define	TR	Tilakraj	Roy

	960807	TR	Created	from scratch for vtmman

	Circular fixed length queue manipulation routines.
	FIFO queuesof fixed length
*/


/*----------------------------------------------------------------------------
          SYSTEM INCLUDE FILES
----------------------------------------------------------------------------*/
#define WANTVXDWRAPS
#include <basedef.h>
#include <vmm.h>
#include <vxdwraps.h>
#include "vxstd.h"
#include "vxwin.h"
#include "vxdbg.h"



/*----------------------------------------------------------------------------
          FOLLOWING SECTION  REMAINS UNCHANGED
----------------------------------------------------------------------------*/

#include "tmwincom.h"
#include "cqueue.h"

#pragma VxD_LOCKED_CODE_SEG
#pragma	VxD_LOCKED_DATA_SEG

PVOID	memMalloc ( DWORD Size );
VOID	memFree ( PVOID pMemory );
VOID	memCopy ( PVOID pSource, PVOID pDestination, DWORD Size );
DWORD	csectCreate ( VOID );
DWORD	csectEnter ( DWORD CriticalSection );
DWORD	csectLeave ( DWORD CriticalSection );
DWORD	csectDestroy ( DWORD CriticalSection );

/*
	cqueueCreate
	ItemCount		Number of elements preallocated in the queue
	ItemSize		Size of each item in the queue. This size in bytes
					is used to copy queue Items at the Insert, Delete and
					Retrieve calls.
	ItemBuffer		Pointer to the preallocated item buffer which this 
					function can use. Specifying this paramter as NULL will 
					cause Create function to allocate is own item buffer
					computed from ItemSize * ItemCount.
	Object			Address of teh location where the pointer to the 
					newly allocated object will be stored.
*/
BOOL	cqueueCreate ( IN DWORD ItemCount, IN DWORD ItemSize, 
	IN PVOID ItemBuffer, OUT PVOID *Object )
{
	PCQUEUE_OBJECT	this;

	if ( *Object == NULL )
	{
		if ( ( this = memMalloc ( sizeof ( CQUEUE_OBJECT ) ) ) == NULL )
		{
			goto Create_fail1;
		}
		else //success full allocation
		{
			this->Flags = 0;
			FlagSet ( this->Flags, CQUEUE_FLAGS_DYNAMICALLOC );
		}
	}
	else
	{
		this = *((PCQUEUE_OBJECT *)Object);
		this->Flags = 0;
	}


	// Object has alread been allocated
	this->Size = sizeof ( CQUEUE_OBJECT );
	FlagSet ( this->Flags, CQUEUE_FLAGS_ACTIVE );

	if ( ItemBuffer == NULL  )
	{
		if ( ( this->ItemBuffer = 
			memMalloc ( ItemCount * ItemSize ) ) == NULL )
		{
			goto Create_fail2;
		}
		FlagSet ( this->Flags, CQUEUE_FLAGS_DYNAMICITEMBUFFER );
	}
	else
	{
		this->ItemBuffer = ItemBuffer;
	}
	this->ItemCount = ItemCount;
	this->ItemSize = ItemSize;
	this->ReadIndex = 0;
	this->WriteIndex = 0;

	*((PCQUEUE_OBJECT *)Object) = this;
	return TRUE;

Create_fail2:
	if ( FlagGet ( this->Flags, CQUEUE_FLAGS_DYNAMICALLOC ) )
	{
		this->Size = 0;
		this->Flags = 0;
		memFree ( this );
	}

Create_fail1:
	return FALSE;
}

/*
	cqueueDestroy
	Destroys the queue object, after this all references to Object are invalid.
	TRUE	Successful destruction of queue object
	FALSE	Invalid object pointer
*/
BOOL	cqueueDestroy ( PVOID Object )
{
	PCQUEUE_OBJECT	this = (PCQUEUE_OBJECT) Object;

	if ( FlagGet ( this->Flags, CQUEUE_FLAGS_DYNAMICITEMBUFFER ) )
	{
		memFree ( this->ItemBuffer );
	}

	if ( FlagGet ( this->Flags, CQUEUE_FLAGS_DYNAMICALLOC ) )
	{
		this->Size = 0;
		this->Flags = 0;
		memFree ( this );
	}
	return TRUE;
}


/*
	cqueueIsEmpty
	TRUE	Queue is empty
	FALSE	Queue is not empty
			Invalid object pointer
*/
BOOL	cqueueIsEmpty ( PVOID Object )
{
	PCQUEUE_OBJECT	this = (PCQUEUE_OBJECT) Object;
	BOOL	Result;
	DWORD	CriticalSection;

	if ( this == NULL )
		return 	TRUE;

	csectEnter ( (DWORD)&CriticalSection );

	if ( this->ReadIndex == this->WriteIndex )
	{
		Result =  TRUE;
	}
	else
	{
		Result = FALSE;
	}	
	csectLeave ( CriticalSection ); 
	return Result;

}

/*
	cqueueIsFull
 	TRUE	Queue is full
	FALSE	Queue is not Full
			Invalid Object Pointer
*/
BOOL	cqueueIsFull ( PVOID Object )
{
	PCQUEUE_OBJECT	this = (PCQUEUE_OBJECT) Object;
	DWORD	WriteIndex;
	BOOL	Result;
	DWORD	CriticalSection;
	csectEnter ( (DWORD)&CriticalSection );

	WriteIndex = ( this->WriteIndex + 1 ) % this->ItemCount;	
	if ( this->ReadIndex == WriteIndex )
	{
		Result =  TRUE;
	}
	else
	{
		Result = FALSE;
	}	
	csectLeave ( CriticalSection ); 
	return Result;
}

/*
	cqueueRetrieve
	Retrieves the first item from the queue without deleting it
*/
BOOL	cqueueRetrieve ( PVOID Object, PVOID Item )
{
	PCQUEUE_OBJECT	this = (PCQUEUE_OBJECT) Object;
	DWORD	ReadIndex;
	PBYTE	ItemQueue;

	BOOL	Result;
	DWORD	CriticalSection;
	csectEnter ( (DWORD)&CriticalSection );
	
	if ( this->ReadIndex == this->WriteIndex )
	{
		Result = FALSE;
		goto cqueueRetrieve_exit;
	}
		

	ReadIndex = this->ReadIndex;

	ItemQueue = ( this->ItemBuffer + ( ReadIndex * this->ItemSize ) );

	memCopy  ( Item, ItemQueue, this->ItemSize );

	Result = TRUE;

cqueueRetrieve_exit :
	csectLeave ( CriticalSection ); 
	return Result;
}

DWORD	cqueueLength ( PVOID Object )
{
	PCQUEUE_OBJECT	this = (PCQUEUE_OBJECT) Object;

	DWORD	ReadIndex;
	PBYTE	ItemQueue;

	BOOL	Result;
	DWORD	CriticalSection, Idx;
	
	csectEnter ( (DWORD)&CriticalSection );
	
	ReadIndex = this->ReadIndex;
	
	for ( Idx = 0 ; ; Idx++ )
	{
		if ( ReadIndex == this->WriteIndex )
			break;

		ReadIndex = ( ReadIndex + 1 ) % this->ItemCount;	
	}
		
	csectLeave ( CriticalSection ); 

	return Idx;
}


/*
	cqueueInsert
	Inserts Item at the end of the  queue
	TRUE	Successful Insertion
	FALSE	No room in the queue
			Invalid Object Pointer
*/
BOOL	cqueueInsert ( PVOID Object, PVOID Item )
{
	PCQUEUE_OBJECT	this = (PCQUEUE_OBJECT) Object;
	DWORD	WriteIndex;
	PBYTE	ItemQueue;

	BOOL	Result;
	DWORD	CriticalSection;
	csectEnter ( (DWORD)&CriticalSection );


	WriteIndex = ( this->WriteIndex + 1 ) % this->ItemCount;	

	if ( this->ReadIndex == WriteIndex )
	{
		Result = FALSE;
		goto cqueueInsert_exit;
	}

	ItemQueue = ( this->ItemBuffer + ( this->WriteIndex * this->ItemSize ) );
	memCopy  ( ItemQueue, Item, this->ItemSize );

	this->WriteIndex = WriteIndex;

	Result = TRUE;

cqueueInsert_exit :
	csectLeave ( CriticalSection ); 
	return Result;

}


/*
	cqueueDelete
	deletes the first item in the queue, and retrieves it in Item
*/
BOOL	cqueueDelete ( PVOID Object, PVOID Item )
{
	PCQUEUE_OBJECT	this = (PCQUEUE_OBJECT) Object;
	DWORD	ReadIndex;
	PBYTE	ItemQueue;
	BOOL	Result;
	DWORD	CriticalSection;
	csectEnter ( (DWORD)&CriticalSection );

	if ( this->ReadIndex == this->WriteIndex )
	{
		Result  =  FALSE;
		goto cqueueDelete_exit;
	}
	ReadIndex = this->ReadIndex;

	ItemQueue = ( this->ItemBuffer + ( ReadIndex * this->ItemSize ) );
	memCopy  ( Item, ItemQueue, this->ItemSize );

	this->ReadIndex = ( ReadIndex + 1 ) % this->ItemCount;	

	Result = TRUE;

cqueueDelete_exit :
	csectLeave ( CriticalSection ); 
	return Result;

}



/*----------------------------------------------------------------------------
          IMPLEMENTATION SPECIFIC CHANGES 
----------------------------------------------------------------------------*/

PVOID	memMalloc ( DWORD Size )
{
	return vxdMalloc ( Size );
}

VOID	memFree ( PVOID pMemory )
{
	vxdFree ( pMemory );
}

VOID	memCopy ( PVOID pDestination, PVOID pSource, DWORD Size )
{
	vxdMemCopy  ( pSource, pDestination, Size );
}

DWORD	csectCreate ( VOID );
DWORD	csectEnter ( DWORD CriticalSection )
{
	return vxdEnterCritical ( (PDWORD)CriticalSection );
}

DWORD	csectLeave ( DWORD CriticalSection )
{
	return vxdLeaveCritical ( CriticalSection );
}

DWORD	csectDestroy ( DWORD CriticalSection );

⌨️ 快捷键说明

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