isolationserver/messagequeue/src/msgqinternals.cpp
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /** 
       
     2  *  @file MsgQInternals.cpp
       
     3  *  Description: Source file for MsgQLib
       
     4  *  Copyright (c) 2007 Nokia Corporation.
       
     5 *  All rights reserved.
       
     6 *  Redistribution and use in source and binary forms, with or without modification, 
       
     7 *  are permitted provided that the following conditions are met:
       
     8 *  Redistributions of source code must retain the above copyright notice, this list 
       
     9 *  of conditions and the following disclaimer.Redistributions in binary form must 
       
    10 *  reproduce the above copyright notice, this list of conditions and the following 
       
    11 *  disclaimer in the documentation and/or other materials provided with the distribution.
       
    12 *  Neither the name of the Nokia Corporation nor the names of its contributors may be used 
       
    13 *  to endorse or promote products derived from this software without specific prior written 
       
    14 *  permission.
       
    15 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
       
    16 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
       
    17 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
       
    18 *  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
       
    19 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
       
    20 *  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
       
    21 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
       
    22 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
       
    23 *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    24 */
       
    25 #include <stdlib.h>
       
    26 #include <msgqinternal.h>
       
    27 #include <e32property.h>
       
    28 
       
    29 /* Declaration and definition of Internal Functions and data structures */
       
    30 #ifdef __WINSCW__
       
    31 
       
    32 #include <e32std.h>
       
    33 typedef void TAny;
       
    34 #include <pls.h> // For emulator WSD API
       
    35 
       
    36 const TUid KMsgQLibUid3 = {0xA0001319};  
       
    37 
       
    38 
       
    39 typedef struct {
       
    40 	MSGQ_INFO_PTR        _MsgQInfo[MSGQ_TBL_SZ];
       
    41 	MSGQ_INFO_LIST_PTR   _MsgQListHead;
       
    42 }MsgQGlobalData;
       
    43 
       
    44 int InitWSDVar(MsgQGlobalData* p) {
       
    45 	p->_MsgQListHead = NULL;
       
    46 	return 0;
       
    47 }
       
    48 MsgQGlobalData* GetGlobal() {
       
    49 	 // Access the PLS of this process 
       
    50 	MsgQGlobalData* p = Pls<MsgQGlobalData>(KMsgQLibUid3, &InitWSDVar);
       
    51 	return p;
       
    52 }
       
    53 
       
    54 
       
    55 MSGQ_INFO_PTR* _MsgQInfo() {
       
    56 	//return &(GetGlobal()->_MsgQInfo[0]); // orig code	
       
    57 	MSGQ_INFO_PTR* p = (GetGlobal()->_MsgQInfo);
       
    58 	return p;
       
    59 	//return (GetGlobal()->_MsgQInfo);     // modified line
       
    60 };
       
    61 
       
    62 MSGQ_INFO_LIST_PTR* _MsgQListHead() {
       
    63 	return &(GetGlobal()->_MsgQListHead);	
       
    64 };
       
    65 
       
    66 #define MsgQInfo (_MsgQInfo())
       
    67 #define MsgQListHead (*_MsgQListHead())
       
    68 
       
    69 #else
       
    70 MSGQ_INFO_PTR        MsgQInfo[MSGQ_TBL_SZ];
       
    71 MSGQ_INFO_LIST_PTR   MsgQListHead = NULL;
       
    72 #endif
       
    73 
       
    74 inline unsigned int HashIndex(ULONG qName);
       
    75 
       
    76 /*************** INTERNAL FUNCTIONS ******************************/
       
    77 
       
    78 /***************************************************************************
       
    79 *  InstallMsqQTable (qName, qid, semId, sendState, numMsgs, err)
       
    80 *  Function: This function installs a queue into the hash table
       
    81 ****************************************************************************/
       
    82 
       
    83 int InstallMsqQTable(ULONG qName, int qId, int semId, int* err) {
       
    84 
       
    85 	MSGQ_INFO_PTR pMsgQInfo = NULL;
       
    86 	unsigned int  index;
       
    87 
       
    88 	if ((pMsgQInfo = MsgQTableLookup(qName)) == NULL) {
       
    89 		pMsgQInfo = (MSGQ_INFO_PTR)malloc(sizeof(*pMsgQInfo));
       
    90 
       
    91 		if(pMsgQInfo != NULL) {
       
    92 			index = HashIndex(qName);
       
    93 
       
    94 			pMsgQInfo->next  = MsgQInfo[index];
       
    95 			MsgQInfo[index]   = pMsgQInfo;
       
    96 			pMsgQInfo->qName = qName;
       
    97 			pMsgQInfo->qId   = qId;
       
    98 			pMsgQInfo->semId = semId;
       
    99 			pMsgQInfo->sendState = MSG_Q_READY;
       
   100 			pMsgQInfo->numMsgs   = 0;
       
   101 
       
   102 			*err = OK;
       
   103 			return (OK);
       
   104 		}
       
   105 		else
       
   106 			*err = KMsgQLibNoMemoryErr;
       
   107 	}
       
   108 	else
       
   109 		*err = KMsgQLibQIdErr;
       
   110 
       
   111 	return(ERROR);
       
   112 
       
   113 }
       
   114 
       
   115 
       
   116 /******************************************************************************
       
   117 *  HashIndex
       
   118 *  Function: This function returns the hash index
       
   119 *******************************************************************************/
       
   120 
       
   121 inline unsigned int HashIndex(ULONG qName) {
       
   122     unsigned int hash_index = (qName % MSGQ_TBL_SZ);
       
   123 	//return(qName % MSGQ_TBL_SZ);
       
   124 	return hash_index;
       
   125 }
       
   126 
       
   127 
       
   128 /************************************************************************
       
   129 *  MsgQTableLookup (qName)
       
   130 *  Function: This function finds the block pointer for each queue
       
   131 *************************************************************************/
       
   132 
       
   133 MSGQ_INFO* MsgQTableLookup(ULONG qName) {
       
   134 	MSGQ_INFO_PTR pMsgQInfo = NULL;
       
   135 
       
   136 	for (pMsgQInfo = MsgQInfo[HashIndex(qName)]; pMsgQInfo != NULL; pMsgQInfo = pMsgQInfo->next) {
       
   137 		if (qName == pMsgQInfo->qName)
       
   138 			return(pMsgQInfo);
       
   139 	}
       
   140 
       
   141 	return(NULL);
       
   142 }
       
   143 
       
   144 
       
   145 /*************************************************************************
       
   146 *  RemoveFromMsqQTable (qName, err)
       
   147 *  Function: This function removes a queue from the hash table
       
   148 **************************************************************************/
       
   149 
       
   150 
       
   151 int RemoveFromMsqQTable(ULONG qName, int* err) {
       
   152 	unsigned int  index = 0;
       
   153 	MSGQ_INFO_PTR prev = NULL;
       
   154 	MSGQ_INFO_PTR pMsgQInfo = NULL;
       
   155 
       
   156 	index = HashIndex(qName);
       
   157 	for (pMsgQInfo = MsgQInfo[index]; pMsgQInfo != NULL; pMsgQInfo = pMsgQInfo->next) {
       
   158 		if (qName == pMsgQInfo->qName)
       
   159 			break;
       
   160 		prev = pMsgQInfo;
       
   161 	}
       
   162 
       
   163 	if (pMsgQInfo != NULL) {
       
   164 		if (prev == NULL)
       
   165 			MsgQInfo[index] = pMsgQInfo->next;
       
   166 		else
       
   167 			prev->next = pMsgQInfo->next;
       
   168 
       
   169 		free((void*)pMsgQInfo);
       
   170 		*err = OK;
       
   171 		return (OK);
       
   172 	}
       
   173 	else
       
   174 		*err = KMsgQLibQIdErr;
       
   175 
       
   176 	return(ERROR);
       
   177 }
       
   178 
       
   179 
       
   180 /************************************************************************
       
   181 *  AddToMsgQTable (qName)
       
   182 *  Function: Adding a queue to list
       
   183 *************************************************************************/
       
   184 
       
   185 void AddToMsgQTable(ULONG qName) {
       
   186 	MSGQ_INFO_LIST_PTR tempNext;
       
   187 
       
   188 	if (MsgQListHead != NULL) {
       
   189 		/* subsequent entries */
       
   190 		tempNext = MsgQListHead->next;
       
   191 
       
   192 		if ((MsgQListHead->next = (MSGQ_INFO_LIST*)malloc(sizeof(MSGQ_INFO_LIST))) != NULL) 		{
       
   193 			MsgQListHead->next->next = tempNext;
       
   194 			MsgQListHead->next->qName = qName;
       
   195 		}
       
   196 		else 
       
   197 			MsgQListHead->next = tempNext;
       
   198 	}
       
   199 	else {
       
   200 		if ((MsgQListHead = (MSGQ_INFO_LIST*)malloc(sizeof(MSGQ_INFO_LIST))) != NULL) {
       
   201 			MsgQListHead->next = NULL;
       
   202 			MsgQListHead->qName = qName;
       
   203 		}
       
   204 	}
       
   205 }
       
   206 
       
   207 /************************************************************************
       
   208 *  DeleteFromMsgQTable (qName)
       
   209 *  Function:  removing a queu entry from  list
       
   210 *************************************************************************/
       
   211 
       
   212 void DeleteFromMsgQTable(ULONG qName) {
       
   213 	MSGQ_INFO_LIST_PTR prev = NULL;
       
   214 	MSGQ_INFO_LIST_PTR pMsgQInfo = NULL;
       
   215 
       
   216 	for (pMsgQInfo = MsgQListHead; pMsgQInfo != NULL; pMsgQInfo = pMsgQInfo->next) {
       
   217 		if (qName == pMsgQInfo->qName)
       
   218 			break;
       
   219 		prev = pMsgQInfo;
       
   220 	}
       
   221 
       
   222 	if (pMsgQInfo != NULL) {
       
   223 		/* Check whether prev pointer is null or not. If it is Null, update Head pointer */
       
   224 		if( prev == NULL )
       
   225 			MsgQListHead = MsgQListHead->next ;
       
   226 
       
   227 		/* Else update the linked list by removing present node and updating prev next pointer */
       
   228 		else
       
   229 			prev->next = pMsgQInfo->next;
       
   230 
       
   231 		/* Now free up the memory used by the present node */
       
   232 		free((void*) pMsgQInfo);
       
   233 	}
       
   234 }
       
   235 
       
   236 // end of fil