Changeset 1467 for trunk/src/win32k/ldr/ldr.cpp
- Timestamp:
- Oct 27, 1999, 4:03:01 AM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/win32k/ldr/ldr.cpp
r1273 r1467 1 /* $Id: ldr.cpp,v 1. 2 1999-10-14 01:25:38 bird Exp $1 /* $Id: ldr.cpp,v 1.3 1999-10-27 02:02:58 bird Exp $ 2 2 * 3 * ldr.cpp - Loader helper functions a structures.3 * ldr.cpp - Loader helpers. 4 4 * 5 5 * Copyright (c) 1999 knut St. osmundsen … … 23 23 #include <memory.h> 24 24 #include <stdlib.h> 25 #include <stddef.h> 25 26 26 27 #include "log.h" … … 29 30 #include "OS2Krnl.h" 30 31 #include "pe2lx.h" 32 #include "avl.h" 31 33 #include "ldr.h" 34 32 35 33 36 … … 35 38 * Global Variables * 36 39 *******************************************************************************/ 37 PPENODE pPE; 40 PAVLNODECORE pSFNRoot = NULL; 41 PAVLNODECORE pMTERoot = NULL; 42 38 43 unsigned char achHandleStates[MAX_FILE_HANDLES/8]; 39 44 40 45 41 /******************************************************************************* 42 * Internal Functions * 43 *******************************************************************************/ 44 static PPENODE findNodePtr2(PPENODE pRoot, const char *pszFilename); 45 static ULONG depth(PPENODE pNode); 46 47 48 /** 49 * Inserts a PENode into the pPE tree. 50 * @returns NO_ERROR on success. !0 on error. 51 * @param pNode Pointer to node to insert. 52 */ 53 ULONG insertNode(PPENODE pNode) 54 { 55 int level; 56 PPENODE pPrev; 57 PPENODE pTmp; 58 59 if (pPE == NULL) 60 { 61 pPE = pNode; 62 pNode->left = pNode->right = NULL; 46 /** 47 * Gets a module by the give hFile. 48 * @returns Pointer to module node. If not found NULL. 49 * @param hFile File handle of the module to be found. 50 * @sketch return a AVLGet on the pSFNRoot-tree. 51 * @status completely implemented. 52 * @author knut st. osmundsen 53 */ 54 PMODULE getModuleBySFN(SFN hFile) 55 { 56 return (PMODULE)AVLGet(&pSFNRoot, (AVLKEY)hFile); 57 } 58 59 60 /** 61 * Gets a module by the MTE. 62 * @returns Pointer to module node. If not found NULL. 63 * @param pMTE Pointer an Module Table Entry. 64 * @sketch Try find it in the MTE tree. 65 * IF not found THEN 66 * BEGIN 67 * DEBUG: validate pMTE pointer. 68 * Get the SFN from the MTE. 69 * IF found in the SFN-tree THEN 70 * BEGIN 71 * Update the pMTE in the node. 72 * Add the node to the MTE-tree. 73 * END 74 * ELSE return NULL 75 * END 76 * return pointer to module node. 77 * @status completely implemented. 78 * @author knut st. osmundsen 79 */ 80 PMODULE getModuleByMTE(PMTE pMTE) 81 { 82 PMODULE pMod = (PMODULE)AVLGet(&pMTERoot, (AVLKEY)pMTE); 83 if (pMod == NULL) 84 { 85 #ifdef DEBUG 86 if (pMTE <= (PMTE)0x10000) 87 { 88 kprintf(("getModuleByMTE: invalid pMTE pointer - %#8x\n", pMTE)); 89 return NULL; 90 } 91 #endif 92 pMod = (PMODULE)AVLGet(&pSFNRoot, (AVLKEY)pMTE->mte_sfn); 93 if (pMod != NULL) 94 { 95 pMod->coreMTE.Key = (AVLKEY)pMTE; 96 pMod->fFlags |= MOD_FLAGS_IN_MTETREE; 97 AVLInsert(&pMTERoot, (PAVLNODECORE)((unsigned)pMod + offsetof(MODULE, coreMTE))); 98 } 63 99 } 64 100 else 65 { 66 level = 0; 67 pPrev = NULL; 68 pTmp = pPE; 69 while (pTmp != NULL) 70 { 71 level = 0; 72 pPrev = pTmp; 73 pTmp = AdjustKey(pNode->hFile) < AdjustKey(pTmp->hFile) ? pTmp->left : pTmp->right; 74 } 75 76 if (pNode->hFile != pPrev->hFile) 77 { 78 if (AdjustKey(pNode->hFile) < AdjustKey(pPrev->hFile)) 79 pPrev->left = pNode; 80 else 81 pPrev->right = pNode; 82 pNode->left = pNode->right = NULL; 83 } 84 else 85 return -1; 101 pMod = (PMODULE)((unsigned)pMod - offsetof(MODULE, coreMTE)); 102 return pMod; 103 } 104 105 106 /** 107 * Get a module by filename. 108 * @returns Pointer to module node. If not found NULL. 109 * @param pszFilename Pointer to the filename which we are search by. 110 * @sketch Not implemented. 111 * @status Stub. 112 * @author knut st. osmundsen 113 */ 114 PMODULE getModuleByFilename(PCSZ pszFilename) 115 { 116 pszFilename = pszFilename; 117 return NULL; 118 } 119 120 121 /** 122 * Adds a module to the SFN-tree, if pMTE is not NULL it is added to the MTE-tree too. 123 * @returns NO_ERROR on success. Appropriate errorcode on failiure. 124 * @param hFile System file number for the module. 125 * @param pMTE Pointer to MTE. NULL is valid. 126 * @param fFlags Type flags for the fFlags field in the node. 127 * @param pvData Pointer to data. 128 * @sketch DEBUG: check that the module doesn't exists and parameter check. 129 * (try) Allocate a new node. (return errorcode on failure) 130 * Fill in the node. 131 * Add the node to the SFN-tree. 132 * IF valid MTE pointer THEN add it to the MTE tree and set the in MTE-tree flag. 133 * return successfully. 134 * @status completely implemented. 135 * @author knut st. osmundsen 136 */ 137 ULONG addModule(SFN hFile, PMTE pMTE, ULONG fFlags, void *pvData) 138 { 139 PMODULE pMod; 140 #ifdef DEBUG 141 if (AVLGet(&pSFNRoot, (AVLKEY)hFile) != NULL) 142 kprintf(("addModule: Module allready present in the SFN-tree!\n")); 143 if (hFile == 0) 144 { 145 kprintf(("addModule: invalid parameter: hFile = 0\n")); 146 return ERROR_INVALID_PARAMETER; 147 } 148 if ((fFlags & MOD_TYPE_MASK) == 0 || (fFlags & ~MOD_TYPE_MASK) != 0) 149 { 150 kprintf(("addModule: invalid parameter: fFlags = 0x%#8x\n", fFlags)); 151 return ERROR_INVALID_PARAMETER; 152 } 153 #endif 154 155 /* try allocate memory for the node. */ 156 pMod = (PMODULE)malloc(sizeof(MODULE)); 157 if (pMod == NULL) 158 { 159 kprintf(("addModule: out of memory!\n")); 160 return ERROR_NOT_ENOUGH_MEMORY; 161 } 162 163 /* fill in the module node. */ 164 pMod->coreKey.Key = (AVLKEY)hFile; 165 pMod->hFile = hFile; 166 pMod->pMTE = pMTE; 167 pMod->fFlags = fFlags; 168 pMod->Data.pv = pvData; 169 170 /* insert the module node into the tree(s) */ 171 AVLInsert(&pSFNRoot, (PAVLNODECORE)pMod); 172 if (pMTE != NULL) 173 { 174 pMod->coreMTE.Key = (AVLKEY)pMTE; 175 pMod->fFlags |= MOD_FLAGS_IN_MTETREE; 176 AVLInsert(&pMTERoot, (PAVLNODECORE)((unsigned)pMod + offsetof(MODULE, coreMTE))); 86 177 } 87 178 … … 91 182 92 183 /** 93 * Deletes a node from the pPE tree. 94 * @returns NO_ERROR on success. !0 on error. 95 * @param key Filehandle, which is the key. 96 */ 97 ULONG deleteNode(SFN key) 98 { 99 int level,level2; 100 ULONG rc; 101 PPENODE pTmp,pTmp2; 102 PPENODE pPrev,pPrev2; 103 int left; 104 105 if (pPE != NULL) 106 { 107 /* find node to delete */ 108 level = 1; 109 pPrev = NULL; 110 pTmp = pPE; 111 while (pTmp != NULL && key != pTmp->hFile) 112 { 113 pPrev = pTmp; 114 pTmp = (left = (AdjustKey(key) < AdjustKey(pTmp->hFile))) == TRUE ? pTmp->left : pTmp->right; 115 level ++; 116 } 117 118 if (pTmp != NULL) 119 { 120 /*found it: pTmp -> node to delete - pPrev -> parent node*/ 121 level--; 122 rc = -1; 123 if (pTmp->left != NULL && pTmp->right != NULL) 124 { 125 /* hard case - fetch the leftmost node in the right subtree */ 126 level2 = level; 127 pPrev2 = pTmp; 128 pTmp2 = pTmp->right; 129 while (pTmp2->left != NULL) 130 { 131 pPrev2 = pTmp2; 132 pTmp2 = pTmp2->left; 133 level2++; 134 } 135 /* pTmp2 -> new root - pPrev2 -> parent node */ 136 137 /* left child of pTmp2 */ 138 pTmp2->left = pTmp->left; 139 140 /* parent of pTmp2 and pTmp2->right */ 141 if (pPrev2 != pTmp) 142 { 143 pPrev2->left = pTmp2->right; 144 pTmp2->right = pTmp->right; 145 } 146 //else pTmp2->right = pTmp2->right; 147 148 /* link in pTmp2 */ 149 if (pTmp != pPE) 150 { 151 if (left) 152 pPrev->left = pTmp2; 153 else 154 pPrev->right = pTmp2; 155 } 156 else 157 pPE = pTmp2; 158 rc = NO_ERROR; 159 } 160 161 /* leaf */ 162 if (rc!=0 && pTmp->left == NULL && pTmp->right == NULL) 163 { 164 if (pTmp != pPE) 165 { 166 if (left) 167 pPrev->left = NULL; 168 else 169 pPrev->right = NULL; 170 } 171 else 172 pPE = NULL; 173 rc = NO_ERROR; 174 } 175 176 /* left is NULL */ 177 if (rc!=0 && pTmp->left == NULL && pTmp->right != NULL) 178 { 179 /* move up right node */ 180 if (pTmp != pPE) 181 { 182 if (left) 183 pPrev->left = pTmp->right; 184 else 185 pPrev->right = pTmp->right; 186 } 187 else 188 pPE = pTmp->right; 189 rc = NO_ERROR; 190 } 191 192 /* right is NULL */ 193 if (rc!=0 && pTmp->left != NULL && pTmp->right == NULL) 194 { 195 /* move up left node */ 196 if (pTmp != pPE) 197 { 198 if (left) 199 pPrev->left = pTmp->left; 200 else 201 pPrev->right = pTmp->left; 202 } 203 else 204 pPE = pTmp->left; 205 rc = NO_ERROR; 206 } 207 208 /* free node */ 209 if (rc == NO_ERROR) 210 rc = freeNode( pTmp ); 211 } 212 else 213 rc = 1; //not found 214 } 215 else 216 rc = 1; //not found 217 return rc; 218 } 219 220 221 /** 222 * Get the pointer to a node in the pPE tree. 223 * @returns Pointer to node on success. NULL if not found or error occured. 224 * @param key Filehandle, which is the key for the pPE tree. 225 */ 226 PPENODE getNodePtr(SFN key) 227 { 228 PPENODE pTmp = pPE; 229 int level = 1; 230 while (pTmp != NULL && pTmp->hFile != key) 231 { 232 pTmp = AdjustKey(key) < AdjustKey(pTmp->hFile) ? pTmp->left : pTmp->right; 233 level++; 234 } 235 return pTmp; 236 } 237 238 239 /** 240 * Find a PENode by filename in the node tree. 241 * @returns Pointer to PENode if found, NULL if not found. 242 * @param pszFilename Pointer to filename. 243 */ 244 PPENODE findNodePtr(const char *pszFilename) 245 { 246 /*depth first search thru the whole tree */ 247 return findNodePtr2(pPE, pszFilename); 248 } 249 250 251 /** 252 * Find a PENode by filename in the given tree. 253 * Depth first search thru the whole tree. 254 * @returns Pointer to matching PENode. 255 * @param pRoot Tree root. 256 * @param pszFilename Pointer to filename. 257 * @remark sub-function of findNodePtr. 258 */ 259 static PPENODE findNodePtr2(PPENODE pRoot, const char *pszFilename) 260 { 261 PPENODE pNode = NULL; 262 263 /*depth first search thru the whole tree */ 264 if (pRoot == NULL || pRoot->pPe2Lx->queryIsModuleName(pszFilename)) 265 return pRoot; 266 267 //search subtrees 268 if (pRoot->left != NULL) 269 pNode = findNodePtr2(pRoot->left,pszFilename); 270 if (pNode == NULL && pRoot->right != NULL) 271 pNode = findNodePtr2(pRoot->right,pszFilename); 272 return pNode; 273 } 274 275 276 /** 277 * Allocate memory for a PENode. 278 * @returns Pointer to new PENode on success. NULL pointer on error. 279 */ 280 PPENODE allocateNode(void) 281 { 282 PPENODE pNode; 283 284 pNode = new PENODE; 285 if (pNode == NULL) 286 kprintf(("allocateNode: new returned a NULL-pointer\n")); 287 288 return pNode; 289 } 290 291 292 /** 293 * Frees node. 294 * @returns NO_ERROR on success. 295 * @param pNode Pointer to node which is to be freed. 296 */ 297 ULONG freeNode(PPENODE pNode) 298 { 299 if (pNode != NULL) 300 delete pNode; 184 * Removes and frees a module node (including the data pointer). 185 * @returns NO_ERROR on success. Appropriate error code on failure. 186 * @param hFile System filehandle of the module. 187 * @sketch Remove the node from the SFN-tree. 188 * IF present in the MTE-tree THEN remove it from the tree. 189 * Delete the datapointer. 190 * Free the module node. 191 * return successfully. 192 * @status completely implemented. 193 * @author knut st. osmundsen 194 */ 195 ULONG removeModule(SFN hFile) 196 { 197 PMODULE pMod = (PMODULE)AVLRemove(&pSFNRoot, (AVLKEY)hFile); 198 if (pMod == NULL) 199 { 200 kprintf(("removeModule: Module not found! hFile=%#4x\n", hFile)); 201 return ERROR_INVALID_PARAMETER; 202 } 203 204 /* In MTE-tree too? */ 205 if (pMod->fFlags & MOD_FLAGS_IN_MTETREE) 206 { 207 if (AVLRemove(&pMTERoot, (AVLKEY)pMod->pMTE) == NULL) 208 { 209 kprintf(("removeModule: MOD_FLAGS_IN_MTETREE set but AVLRemove returns NULL\n")); 210 } 211 } 212 213 /* Delete the datapointer. */ 214 switch (pMod->fFlags & MOD_TYPE_MASK) 215 { 216 case MOD_TYPE_PE2LX: 217 delete pMod->Data.pPe2Lx; 218 break; 219 220 case MOD_TYPE_ELF2LX: 221 case MOD_TYPE_SCRIPT: 222 case MOD_TYPE_PE: 223 default: 224 kprintf(("removeModule: Unknown type, %#x\n", pMod->fFlags & MOD_TYPE_MASK)); 225 } 226 227 /* Free the module node. */ 228 free(pMod); 301 229 302 230 return NO_ERROR; 303 }304 305 306 /**307 * Gets the depth of the pPE tree.308 * @returns Number of levels in the the pPE tree.309 */310 ULONG depthPE(void)311 {312 return depth(pPE);313 }314 315 316 317 /**318 * Gets the depth of the pPE tree.319 * @returns Number of levels in the the pPE tree.320 * @param pNode Node to start at. (root node...)321 */322 static ULONG depth(PPENODE pNode)323 {324 if (pNode != NULL)325 {326 int l, r;327 l = depth(pNode->left);328 r = depth(pNode->right);329 return 1 + (l > r ? l : r);330 }331 else332 return 0;333 231 } 334 232 … … 346 244 memset(&achHandleStates[0], 0, sizeof(achHandleStates)); 347 245 348 /* init pPEFiles* */ 349 pPE = NULL; 246 /* init the tree roots */ 247 pSFNRoot = NULL; 248 pMTERoot = NULL; 350 249 351 250 return rc;
Note:
See TracChangeset
for help on using the changeset viewer.