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

00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include <stdlib.h>
00032 #include "MsgQInternal.h"
00033 
00034 
00035 /* Declaration and definition of Internal Functions and data structures */
00036 MSGQ_INFO_PTR        MsgQInfo[MSGQ_TBL_SZ];
00037 MSGQ_INFO_LIST_PTR   MsgQListHead = NULL;
00038 
00039 inline unsigned int HashIndex(ULONG qName);
00040 
00041 /*************** INTERNAL FUNCTIONS ******************************/
00042 
00043 /***************************************************************************
00044 *  InstallMsqQTable (qName, qid, semId, sendState, numMsgs, err)
00045 *  Function: This function installs a queue into the hash table
00046 ****************************************************************************/
00047 
00048 int InstallMsqQTable(ULONG qName, int qId, int semId, int* err) {
00049 
00050         MSGQ_INFO_PTR pMsgQInfo = NULL;
00051         unsigned int  index;
00052 
00053         if ((pMsgQInfo = MsgQTableLookup(qName)) == NULL) {
00054                 pMsgQInfo = (MSGQ_INFO_PTR)malloc(sizeof(*pMsgQInfo));
00055 
00056                 if(pMsgQInfo != NULL) {
00057                         index = HashIndex(qName);
00058 
00059                         pMsgQInfo->next  = MsgQInfo[index];
00060                         MsgQInfo[index]   = pMsgQInfo;
00061                         pMsgQInfo->qName = qName;
00062                         pMsgQInfo->qId   = qId;
00063                         pMsgQInfo->semId = semId;
00064                         pMsgQInfo->sendState = MSG_Q_READY;
00065                         pMsgQInfo->numMsgs   = 0;
00066 
00067                         *err = OK;
00068                         return (OK);
00069                 }
00070                 else
00071                         *err = KMsgQLibNoMemoryErr;
00072         }
00073         else
00074                 *err = KMsgQLibQIdErr;
00075 
00076         return(ERROR);
00077 
00078 }
00079 
00080 
00081 /******************************************************************************
00082 *  HashIndex
00083 *  Function: This function returns the hash index
00084 *******************************************************************************/
00085 
00086 inline unsigned int HashIndex(ULONG qName) {
00087         return(qName % MSGQ_TBL_SZ);
00088 }
00089 
00090 
00091 /************************************************************************
00092 *  MsgQTableLookup (qName)
00093 *  Function: This function finds the block pointer for each queue
00094 *************************************************************************/
00095 
00096 MSGQ_INFO* MsgQTableLookup(ULONG qName) {
00097         MSGQ_INFO_PTR pMsgQInfo = NULL;
00098 
00099         for (pMsgQInfo = MsgQInfo[HashIndex(qName)]; pMsgQInfo != NULL; pMsgQInfo = pMsgQInfo->next) {
00100                 if (qName == pMsgQInfo->qName)
00101                         return(pMsgQInfo);
00102         }
00103 
00104         return(NULL);
00105 }
00106 
00107 
00108 /*************************************************************************
00109 *  RemoveFromMsqQTable (qName, err)
00110 *  Function: This function removes a queue from the hash table
00111 **************************************************************************/
00112 
00113 
00114 int RemoveFromMsqQTable(ULONG qName, int* err) {
00115         unsigned int  index = 0;
00116         MSGQ_INFO_PTR prev = NULL;
00117         MSGQ_INFO_PTR pMsgQInfo = NULL;
00118 
00119         index = HashIndex(qName);
00120         for (pMsgQInfo = MsgQInfo[index]; pMsgQInfo != NULL; pMsgQInfo = pMsgQInfo->next) {
00121                 if (qName == pMsgQInfo->qName)
00122                         break;
00123                 prev = pMsgQInfo;
00124         }
00125 
00126         if (pMsgQInfo != NULL) {
00127                 if (prev == NULL)
00128                         MsgQInfo[index] = pMsgQInfo->next;
00129                 else
00130                         prev->next = pMsgQInfo->next;
00131 
00132                 free((void*)pMsgQInfo);
00133                 *err = OK;
00134                 return (OK);
00135         }
00136         else
00137                 *err = KMsgQLibQIdErr;
00138 
00139         return(ERROR);
00140 }
00141 
00142 
00143 /************************************************************************
00144 *  AddToMsgQTable (qName)
00145 *  Function: Adding a queue to list
00146 *************************************************************************/
00147 
00148 void AddToMsgQTable(ULONG qName) {
00149         MSGQ_INFO_LIST_PTR tempNext;
00150 
00151         if (MsgQListHead != NULL) {
00152                 /* subsequent entries */
00153                 tempNext = MsgQListHead->next;
00154 
00155                 if ((MsgQListHead->next = (MSGQ_INFO_LIST*)malloc(sizeof(MSGQ_INFO_LIST))) != NULL)             {
00156                         MsgQListHead->next->next = tempNext;
00157                         MsgQListHead->next->qName = qName;
00158                 }
00159                 else 
00160                         MsgQListHead->next = tempNext;
00161         }
00162         else {
00163                 if ((MsgQListHead = (MSGQ_INFO_LIST*)malloc(sizeof(MSGQ_INFO_LIST))) != NULL) {
00164                         MsgQListHead->next = NULL;
00165                         MsgQListHead->qName = qName;
00166                 }
00167         }
00168 }
00169 
00170 /************************************************************************
00171 *  DeleteFromMsgQTable (qName)
00172 *  Function:  removing a queu entry from  list
00173 *************************************************************************/
00174 
00175 void DeleteFromMsgQTable(ULONG qName) {
00176         MSGQ_INFO_LIST_PTR prev = NULL;
00177         MSGQ_INFO_LIST_PTR pMsgQInfo = NULL;
00178 
00179         for (pMsgQInfo = MsgQListHead; pMsgQInfo != NULL; pMsgQInfo = pMsgQInfo->next) {
00180                 if (qName == pMsgQInfo->qName)
00181                         break;
00182                 prev = pMsgQInfo;
00183         }
00184 
00185         if (pMsgQInfo != NULL) {
00186                 /* Check whether prev pointer is null or not. If it is Null, update Head pointer */
00187                 if( prev == NULL )
00188                         MsgQListHead = MsgQListHead->next ;
00189 
00190                 /* Else update the linked list by removing present node and updating prev next pointer */
00191                 else
00192                         prev->next = pMsgQInfo->next;
00193 
00194                 /* Now free up the memory used by the present node */
00195                 free((void*) pMsgQInfo);
00196         }
00197 }

Generated by  doxygen 1.6.2