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

📄 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
----------------------------------------------------------------------------*/
#include "windows.h"


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

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


PVOID	memMalloc ( DWORD Size );
PVOID	memFree ( PVOID pMemory );
VOID	memCopy ( PVOID pDestination, PVOID pSource, 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;
	if ( this->ReadIndex == this->WriteIndex )
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}	
	
}

/*
	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;

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

/*
	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;

	if ( this->ReadIndex == this->WriteIndex )
		return FALSE;

	ReadIndex = this->ReadIndex;

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

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

	return TRUE;
}


/*
	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;

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

	if ( this->ReadIndex == WriteIndex )
		return FALSE;

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

	this->WriteIndex = WriteIndex;

	return TRUE;
}


/*
	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;

	if ( this->ReadIndex == this->WriteIndex )
		return FALSE;

	ReadIndex = this->ReadIndex;

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

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

	return TRUE;
}



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

PVOID	memMalloc ( DWORD Size )
{
	return HeapAlloc( GetProcessHeap (), HEAP_ZERO_MEMORY,
		Size );
}

PVOID	memFree ( PVOID pMemory )
{
	HeapFree ( GetProcessHeap(), 0, pMemory );
}

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

DWORD	csectCreate ( VOID );
DWORD	csectEnter ( DWORD CriticalSection );
DWORD	csectLeave ( DWORD CriticalSection );
DWORD	csectDestroy ( DWORD CriticalSection );

⌨️ 快捷键说明

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