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

📄 fifo.ex.c

📁 OPNET下的FIFO程序,用以增强OPNET下的FIFO功能
💻 C
字号:
///////////////////////////////////////////////////////////////////////////
////////////////////// OPNET'S FIFO LIBRARY SOURCE FILE //////////////////////////////////////////////////////////////////////////////////////////////
/// 
/// Contains:	The FIFO (First In First Out) service
///
///	Company:	National Institute of Standards and Technology
/// Written by:	Xavier Pallot
///	Date: 		04/14/00
///
///////////////////////////////////////////////////////////////////////////
/// Description:	This library provides the FIFO service for Opnet.
///					The "FIFO" can receive data pointer of any types.
///					A FIFO multiplexing service is also provided by
///					specifying a FIFO number with the data pointer.
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
///////////////////////////////// INCLUDE /////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "fifo.h" 
///////////////////////////////////////////////////////////////////////////
////////////////////////// FUNCTIONS DEFINITION ///////////////////////////
///////////////////////////////////////////////////////////////////////////

/****************************** FIFO_INIT ********************************/
/* This function initializes a sFIFO structure
/* 		sFifo fifo: a pointer to the sFIFO structure to initialize
/*************************************************************************/

void fifo_init(sFifo *fifo)
{
// initialize the FIFOfifo->nbrObjects=0;
fifo->firstObject=OPC_NIL;
fifo->lastObject=OPC_NIL;
}

/****************************** FIFO_NEW *********************************/
/* This function creates and initializes a new sFIFO structure in the dynamic
/* memory.  
/* 		Return: a pointer to the new sFIFO structure
/*************************************************************************/

sFifo* fifo_new()
{
sFifo* fifo;
// allocate the memory for the new fifo
fifo=(sFifo*)op_prg_mem_alloc(sizeof(sFifo));
// initialize this new structure
fifo_init(fifo);
// and return a pointer to this new sFifo structurereturn fifo;
}

/****************************** FIFO_INSERT ******************************/
/* This function is used to add a data pointer at the end of a "FIFO"
/* Note that by default the data pointer is added with a FIFO number equal 
/* to 0 (be careful when you also use the multiplexing service) 
/* 		sFifo fifo: a pointer to the FIFO 
in which the data will be inserted/*	   	void* data: a pointer to the data (of any type) to add in the FIFO
/* 		Return: 1 if success/*				0 otherwise
/*************************************************************************/

int fifo_insert(sFifo* fifo, void* data)
{
sObject* object;
// allocates dynamic memory for the new fifo object
object=(sObject*)op_prg_mem_alloc(sizeof(sObject));
// if this memory allocation was successfullif (object!=OPC_NIL)
	{
	// adds the data in the fifo object
	object->data=data;
	// by default the fifoNumber of the object is 0
	object->fifoNumber=0;
	object->next=OPC_NIL;
	// adds the object in the fifo
	if (fifo->nbrObjects<=0)
		{
		// at the beginning if the fifo is empty
		fifo->firstObject=object;
		fifo->lastObject=object;
		}
	else
		{
		// at the end otherwise
		fifo->lastObject->next=object;
		fifo->lastObject=object;
		}
	// increment by one the number of Object contained in the Fifo	fifo->nbrObjects++;
	return 1;
	}
// if the memory allocation failedelse
	{
	// return 0 as error indicator	return 0;
	}
}
/**************************** FIFO_EXTRACT *******************************/
/* This function is used to get the data pointer which is at the beginning 
/* of a "FIFO"
./* 		sFifo fifo: a pointer to the FIFO 
from which the data will be extracted/*		Return: a pointer to the extracted data if success
/*	   			OPC_NIL otherwise
/*************************************************************************/

void* fifo_extract(sFifo *fifo)
{
sObject* object;
void *data;
// if there are some object in the fifo
if (fifo->nbrObjects!=0)
	{
	// extract the first object from the fifo
	object=fifo->firstObject;
	// extract the data pointer from this object
	data=object->data;
	// reorganize the fifo
	fifo->firstObject=object->next;
	// destroy the extracted object
	op_prg_mem_free(object);
	// decrement by one the number of Object in the Fifo	fifo->nbrObjects--;
	// return the pointer to the extracted data	return data;
	}// otherwise return OPC_NIL since there is nothing in the fifo
else
	{
	return OPC_NIL;
	}
}

/***************************** FIFO_READ ********************************/
/* This function is used to read the data pointer which is at the beginning 
/* of a "FIFO" whitout extracting the data from the queue
./* 		sFifo fifo: a pointer to the FIFO 
from which the data will be read/* 		Return: a pointer to the readed data if success
/*	   			OPC_NIL otherwise
/*************************************************************************/

void* fifo_read(sFifo *fifo)
{
sObject* object;
void *data;
// if there are some object in the fifo
if (fifo->nbrObjects!=0)
	{
	// extract the first object from the fifo
	object=fifo->firstObject;
	// extract the data pointer from this object
	data=object->data;
	// return the pointer to the read data	return data;
	}
// otherwise return OPC_NIL since there is nothing in the fifo
else
	{
	return OPC_NIL;
	}
}

 /*********************** FIFO_MULTIPLEX_INSERT ***************************/
/* This function is used to add a data pointer in a FIFO using the 
/* multeplexing service
/* 		sFifo fifo: a pointer to the FIFO 
in which the data will be inserted/*	   	void* data: a pointer to the data (of any type) to add in the FIFO
/*	    int fifo_number: the number of the FIFO for multiplexing
. that is the /* number of the subqueue in which the data will be inserted/* 		Return: 1 if success				0 otherwise
/*************************************************************************/

int fifo_multiplex_insert(sFifo* fifo,void* data,int fifo_number)
{
int result;
// add the data pointer at the end of the fifo
 by calling the fifo_insert functionresult=fifo_insert(fifo,data);
// if this operation was successfullif (result!=0)
	{
	// specify the fifoNumber for multiplexing
	fifo->lastObject->fifoNumber=fifo_number;
	}
return result;
}


/********************** FIFO_MULTIPLEX_EXTRACT **************************/
/* This function is used to get (extract) a data pointer from a FIFO using the 
/* multiplexing service
/* 		sFifo fifo: a pointer to the FIFO 
from which the data will be extracted/*	    int fifo_number: the number of the FIFO for multiplexing
, that is the /* number of the subqueue from which the data will be extracted/* 		Return: a pointer to the extracted data if success
/*	   			OPC_NIL otherwise
/*************************************************************************/

void* fifo_multiplex_extract(sFifo* fifo,int fifo_number)
{
sObject* object;
sObject* previousObject;
void *data;
previousObject=OPC_NIL;
object=fifo->firstObject;
// scan all the object contained in the fifo
while (object!=OPC_NIL)
	{
	// if the object has the wanted fifo number
 	if (object->fifoNumber==fifo_number)
		{
		// extract the data pointer
		data=object->data;
		// reorganize the fifo without the object
		if (previousObject==OPC_NIL)
			{
			fifo->firstObject=object->next;
			}
		else
			{
			previousObject->next=object->next;
			if (object==fifo->lastObject)
				{
				fifo->lastObject=previousObject;
				}
			}
		// destroy the object
		op_prg_mem_free(object);



		// decrement by one the number of Object in the Fifo		fifo->nbrObjects--;
		// and return the data pointer
 to the extracted data
		return data;
		}
	// otherwise the object doesn't belong to the right subqueue, thus read the next object in the fifo
	else
		{
		previousObject=object;
		object=object->next;
		}
	}
// return OPC_NIL if no data were foundreturn OPC_NIL;
}

/*********************** FIFO_MULTIPLEX_READ  ***************************/
/* This function is used to read a data pointer in a FIFO using the 
multeplexing/* service whitout extracting it
/*  	sFifo fifo: a pointer to the FIFO 
in which the data will be read/*	    int fifo_number: the number of the FIFO for multiplexing
, that is the/* number of the subqueue in which the data will be read/* 		Return: a pointer to the read data if success
/*	   			OPC_NIL otherwise
/*************************************************************************/

void* fifo_multiplex_read(sFifo* fifo,int fifo_number)
{
sObject* object;
//sObject* previousObject;
void *data;
//previousObject=OPC_NIL;
object=fifo->firstObject;
// scan all the object contained in the fifo
while (object!=OPC_NIL)
	{
	// if the object has the wanted fifo number
 	if (object->fifoNumber==fifo_number)
		{
		// read the data pointer
		data=object->data;
		// and return it		return data;
		}
	// otherwise the object doesn't belong to the right subqueue, thus read the next object in the fifo
	else
		{
		//previousObject=object;
		object=object->next;
		}
	}
// return NULL if no data were foundreturn OPC_NIL;
}

/******************* FIFO_MULTIPLEX_EXTRACT_FIRST ************************/
/* This function is used to get the data pointer which is at the beginning 
/* of a "FIFO" and to return its fifo number
. Thus it extracts the oldest data/* from queue regardless the subqueue in which it is stored./* 		 sFifo fifo: a pointer to the FIFO
 from which the data will be extracted/* 		 int* fifo_number: return the fifo number (subqueue) of the extracted data
/*		 Return: a pointer to the extracted data if success
/*	   			 OPC_NIL otherwise
/*************************************************************************/

void* fifo_multiplex_extract_first(sFifo *fifo, int *fifo_number)
{
sObject* object;
void* data;
// if there are some object in the fifo
if (fifo->nbrObjects!=0)
	{
	// extract the first object from the fifo
	object=fifo->firstObject;
	// extract the data pointer from this object
	data=object->data;
	// get the fifoNumber corresponding to the data pointer
	*fifo_number=object->fifoNumber;
	// reorganize the fifo
	fifo->firstObject=object->next;
	// destroy the extracted object
	op_prg_mem_free(object);
	// decrenment by one the number of Object contained in the Fifo	fifo->nbrObjects--;
	// and return the pointer to the extracted data	return data;
	}
// otherwise return OPC_NIL since there is nothing in the fifo
else
	{
	return OPC_NIL;
	}
}

/********************* FIFO_MULTIPLEX_READ_FIRST ************************/
/* This function is used to read the data pointer which is at the beginning 
/* of a "FIFO" and to return its fifo number, whithout extracting the data
./* Thus it reads the oldest data in all the subqueue./* 		sFifo fifo: a pointer to the FIFO
 in which the data will be read/* 		int* fifo_number: return the fifo number (subqueue) of the read data
/* 		Return: a pointer to the read data if success
/*	   			OPC_NIL otherwise
/*************************************************************************/

void* fifo_multiplex_read_first(sFifo *fifo, int *fifo_number)
{
sObject* object;
void* data;
// if there are some object in the fifo
if (fifo->nbrObjects!=0)
	{
	// extract the first object from the fifo
	object=fifo->firstObject;
	// extract the data pointer from this object
	data=object->data;
	// read the fifoNumber corresponding to the data pointer
	*fifo_number=object->fifoNumber;
	// and return the pointer to the read data	return data;
	}
// otherwise return OPC_NIL since there is nothing in the fifo
else
	{
	
return OPC_NIL;
	}
}

/***************************** FIFO_SIZE *********************************/
/* This function returns the size of a fifo, that is the number of data /* objects contained in it/* 		sFifo: a pointer to the sFIFO that must report its size/*		Return: the size of the fifo/*************************************************************************/
int fifo_size(sFifo* fifo)
{
return (fifo->nbrObjects)
;}
/***************************** FIFO_SIZE *********************************/
/* This function returns the size of a fifo using the multiplex service, that /* is returns the number of data objects contained in one of its subqueue/* 		sFifo: a pointer to the sFIFO that must report the size of one of * its subqueue/*		fifo_number: the fifo numer (subqueue) that must report its size/*		Return: the size of the specified fifo_number subqueue/*************************************************************************/
int fifo_multiplex_size(sFifo* fifo, int fifo_number)
{
int fifo_number_size;sObject* object;
fifo_number_size=0;object=fifo->firstObject;
// scan all the object contained in the fifo
while (object!=OPC_NIL)
	{
	// if the object has the wanted fifo number
 	if (object->fifoNumber==fifo_number)
		{
		// increment by 1 the number of objects contained in the subqueue		fifo_number_size++;		}
	// read the next object in the fifo
	object=object->next;
	}
// return the number of objects found in the wanted subqueuereturn fifo_number_size;}
 /**************************** FIFO_DESTROY *******************************/
/* This function destroy a dynamic FIFO structure which was created by the 
/* fifo_new function. Thus this function also destroys all the objects and 
/* data which are contained in the FIFO.
/* 		sFifo: a pointer to the sFIFO structure to destroy
/*************************************************************************/

void fifo_destroy(sFifo* fifo)
{
void *data;
// extract all the data pointer from the fifo
do 
	{
	data=fifo_extract(fifo);
	// free the memory used by these data
	op_prg_mem_free(data);
	}
while (data!=OPC_NIL);
// free the memory used by the sFifo
 structureop_prg_mem_free(fifo);
}


/***************************** FIFO_PRINT ********************************/
/* This function display a FIFO on the computer screen
/* 		sFifo fifo: a pointer to the FIFO to display
/*************************************************************************/

void fifo_print(sFifo fifo)
{
int* i;
int nbr;
sObject* object;
object=fifo.firstObject;
nbr=0;
// for each Object contained in the Fifowhile(object!=OPC_NIL)
	{
	// print the pointer value of the user's data	i=(int*)(object->data);
	printf("%d...",*i);
	object=object->next;
	nbr++;
	}
printf("end of the %d objects\n",fifo.nbrObjects);
}

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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