examples/PIPS/opencmessagequeuelibraryex/engine/src/msgqinternals.c

00001 
00010 #include <stdlib.h>
00011 #include "MsgQInternal.h"
00012 
00013 
00014 /* Declaration and definition of Internal Functions and data structures */
00015 MSGQ_INFO_PTR        MsgQInfo[MSGQ_TBL_SZ];
00016 MSGQ_INFO_LIST_PTR   MsgQListHead = NULL;
00017 
00018 inline unsigned int HashIndex(ULONG qName);
00019 
00020 /*************** INTERNAL FUNCTIONS ******************************/
00021 
00022 /***************************************************************************
00023 *  InstallMsqQTable (qName, qid, semId, sendState, numMsgs, err)
00024 *  Function: This function installs a queue into the hash table
00025 ****************************************************************************/
00026 
00027 int InstallMsqQTable(ULONG qName, int qId, int semId, int* err) {
00028 
00029         MSGQ_INFO_PTR pMsgQInfo = NULL;
00030         unsigned int  index;
00031 
00032         if ((pMsgQInfo = MsgQTableLookup(qName)) == NULL) {
00033                 pMsgQInfo = (MSGQ_INFO_PTR)malloc(sizeof(*pMsgQInfo));
00034 
00035                 if(pMsgQInfo != NULL) {
00036                         index = HashIndex(qName);
00037 
00038                         pMsgQInfo->next  = MsgQInfo[index];
00039                         MsgQInfo[index]   = pMsgQInfo;
00040                         pMsgQInfo->qName = qName;
00041                         pMsgQInfo->qId   = qId;
00042                         pMsgQInfo->semId = semId;
00043                         pMsgQInfo->sendState = MSG_Q_READY;
00044                         pMsgQInfo->numMsgs   = 0;
00045 
00046                         *err = OK;
00047                         return (OK);
00048                 }
00049                 else
00050                         *err = KMsgQLibNoMemoryErr;
00051         }
00052         else
00053                 *err = KMsgQLibQIdErr;
00054 
00055         return(ERROR);
00056 
00057 }
00058 
00059 
00060 /******************************************************************************
00061 *  HashIndex
00062 *  Function: This function returns the hash index
00063 *******************************************************************************/
00064 
00065 inline unsigned int HashIndex(ULONG qName) {
00066         return(qName % MSGQ_TBL_SZ);
00067 }
00068 
00069 
00070 /************************************************************************
00071 *  MsgQTableLookup (qName)
00072 *  Function: This function finds the block pointer for each queue
00073 *************************************************************************/
00074 
00075 MSGQ_INFO* MsgQTableLookup(ULONG qName) {
00076         MSGQ_INFO_PTR pMsgQInfo = NULL;
00077 
00078         for (pMsgQInfo = MsgQInfo[HashIndex(qName)]; pMsgQInfo != NULL; pMsgQInfo = pMsgQInfo->next) {
00079                 if (qName == pMsgQInfo->qName)
00080                         return(pMsgQInfo);
00081         }
00082 
00083         return(NULL);
00084 }
00085 
00086 
00087 /*************************************************************************
00088 *  RemoveFromMsqQTable (qName, err)
00089 *  Function: This function removes a queue from the hash table
00090 **************************************************************************/
00091 
00092 
00093 int RemoveFromMsqQTable(ULONG qName, int* err) {
00094         unsigned int  index = 0;
00095         MSGQ_INFO_PTR prev = NULL;
00096         MSGQ_INFO_PTR pMsgQInfo = NULL;
00097 
00098         index = HashIndex(qName);
00099         for (pMsgQInfo = MsgQInfo[index]; pMsgQInfo != NULL; pMsgQInfo = pMsgQInfo->next) {
00100                 if (qName == pMsgQInfo->qName)
00101                         break;
00102                 prev = pMsgQInfo;
00103         }
00104 
00105         if (pMsgQInfo != NULL) {
00106                 if (prev == NULL)
00107                         MsgQInfo[index] = pMsgQInfo->next;
00108                 else
00109                         prev->next = pMsgQInfo->next;
00110 
00111                 free((void*)pMsgQInfo);
00112                 *err = OK;
00113                 return (OK);
00114         }
00115         else
00116                 *err = KMsgQLibQIdErr;
00117 
00118         return(ERROR);
00119 }
00120 
00121 
00122 /************************************************************************
00123 *  AddToMsgQTable (qName)
00124 *  Function: Adding a queue to list
00125 *************************************************************************/
00126 
00127 void AddToMsgQTable(ULONG qName) {
00128         MSGQ_INFO_LIST_PTR tempNext;
00129 
00130         if (MsgQListHead != NULL) {
00131                 /* subsequent entries */
00132                 tempNext = MsgQListHead->next;
00133 
00134                 if ((MsgQListHead->next = (MSGQ_INFO_LIST*)malloc(sizeof(MSGQ_INFO_LIST))) != NULL)             {
00135                         MsgQListHead->next->next = tempNext;
00136                         MsgQListHead->next->qName = qName;
00137                 }
00138                 else 
00139                         MsgQListHead->next = tempNext;
00140         }
00141         else {
00142                 if ((MsgQListHead = (MSGQ_INFO_LIST*)malloc(sizeof(MSGQ_INFO_LIST))) != NULL) {
00143                         MsgQListHead->next = NULL;
00144                         MsgQListHead->qName = qName;
00145                 }
00146         }
00147 }
00148 
00149 /************************************************************************
00150 *  DeleteFromMsgQTable (qName)
00151 *  Function:  removing a queu entry from  list
00152 *************************************************************************/
00153 
00154 void DeleteFromMsgQTable(ULONG qName) {
00155         MSGQ_INFO_LIST_PTR prev = NULL;
00156         MSGQ_INFO_LIST_PTR pMsgQInfo = NULL;
00157 
00158         for (pMsgQInfo = MsgQListHead; pMsgQInfo != NULL; pMsgQInfo = pMsgQInfo->next) {
00159                 if (qName == pMsgQInfo->qName)
00160                         break;
00161                 prev = pMsgQInfo;
00162         }
00163 
00164         if (pMsgQInfo != NULL) {
00165                 /* Check whether prev pointer is null or not. If it is Null, update Head pointer */
00166                 if( prev == NULL )
00167                         MsgQListHead = MsgQListHead->next ;
00168 
00169                 /* Else update the linked list by removing present node and updating prev next pointer */
00170                 else
00171                         prev->next = pMsgQInfo->next;
00172 
00173                 /* Now free up the memory used by the present node */
00174                 free((void*) pMsgQInfo);
00175         }
00176 }

Generated by  doxygen 1.6.2