//----------------------------------------------------------------------------- // Call this when the element moves //----------------------------------------------------------------------------- void CBSPTreeData::ElementMoved( BSPTreeDataHandle_t handle, Vector const& mins, Vector const& maxs ) { if (handle != TREEDATA_INVALID_HANDLE) { RemoveFromTree( handle ); InsertIntoTree( handle, mins, maxs ); } }
//----------------------------------------------------------------------------- // This is what happens before rendering a particular view //----------------------------------------------------------------------------- void CClientLeafSystem::PreRender() { VPROF( "CClientLeafSystem::PreRender" ); // Iterate through all renderables and tell them to compute their FX blend for ( int i = m_DirtyRenderables.Count(); --i >= 0; ) { ClientRenderHandle_t handle = m_DirtyRenderables[i]; RenderableInfo_t& renderable = m_Renderables[ handle ]; Assert( renderable.m_Flags & RENDER_FLAGS_HASCHANGED ); // Update position in leaf system RemoveFromTree( handle ); InsertIntoTree( handle ); renderable.m_Flags &= ~RENDER_FLAGS_HASCHANGED; } m_DirtyRenderables.RemoveAll(); }
int main() { TreeNode *root = NULL; InsertIntoTree( &root, 1 ); InsertIntoTree( &root->left, 2 ); InsertIntoTree( &root->right, 3 ); InsertIntoTree( &root->left->left, 4 ); // InsertIntoTree( &root->left->right, 5 ); InsertIntoTree( &root->right->left, 6 ); InsertIntoTree( &root->right->right, 7 ); printf("\n Size of Binary Tree is: %d. \n", SizeOfBinaryTree( root ) ); return 0; }
// //DoIoMap routine.When VirtualAlloc is called with VIRTUAL_AREA_ALLOCATE_IO, //then is routine is called by VirtualAlloc. // static LPVOID DoIoMap(__COMMON_OBJECT* lpThis, LPVOID lpDesiredAddr, DWORD dwSize, DWORD dwAllocFlags, DWORD dwAccessFlags, UCHAR* lpVaName, LPVOID lpReserved) { __VIRTUAL_AREA_DESCRIPTOR* lpVad = NULL; __VIRTUAL_MEMORY_MANAGER* lpMemMgr = (__VIRTUAL_MEMORY_MANAGER*)lpThis; LPVOID lpStartAddr = lpDesiredAddr; LPVOID lpEndAddr = NULL; DWORD dwFlags = 0; BOOL bResult = FALSE; LPVOID lpPhysical = NULL; __PAGE_INDEX_MANAGER* lpIndexMgr = NULL; DWORD dwPteFlags = 0; if((NULL == lpThis) || (0 == dwSize)) //Parameter check. return NULL; if(VIRTUAL_AREA_ALLOCATE_IO != dwAllocFlags) //Invalidate flags. return NULL; lpIndexMgr = lpMemMgr->lpPageIndexMgr; if(NULL == lpIndexMgr) //Validate. return NULL; lpStartAddr = (LPVOID)((DWORD)lpStartAddr & ~(PAGE_FRAME_SIZE - 1)); //Round up to page. lpEndAddr = (LPVOID)((DWORD)lpDesiredAddr + dwSize ); lpEndAddr = (LPVOID)(((DWORD)lpEndAddr & (PAGE_FRAME_SIZE - 1)) ? (((DWORD)lpEndAddr & ~(PAGE_FRAME_SIZE - 1)) + PAGE_FRAME_SIZE - 1) : ((DWORD)lpEndAddr - 1)); //Round down to page. dwSize = (DWORD)lpEndAddr - (DWORD)lpStartAddr + 1; //Get the actually size. lpVad = (__VIRTUAL_AREA_DESCRIPTOR*)KMemAlloc(sizeof(__VIRTUAL_AREA_DESCRIPTOR), KMEM_SIZE_TYPE_ANY); //In order to avoid calling KMemAlloc routine in the //critical section,we first call it here. if(NULL == lpVad) //Can not allocate memory. goto __TERMINAL; lpVad->lpManager = lpMemMgr; lpVad->lpStartAddr = NULL; lpVad->lpEndAddr = NULL; lpVad->lpNext = NULL; lpVad->dwAccessFlags = dwAccessFlags; lpVad->dwAllocFlags = dwAllocFlags; __INIT_ATOMIC(lpVad->Reference); lpVad->lpLeft = NULL; lpVad->lpRight = NULL; if(lpVaName) { if(StrLen((LPSTR)lpVaName) > MAX_VA_NAME_LEN) lpVaName[MAX_VA_NAME_LEN - 1] = 0; StrCpy((LPSTR)lpVad->strName[0],(LPSTR)lpVaName); //Set the virtual area's name. } else lpVad->strName[0] = 0; lpVad->dwCacheFlags = VIRTUAL_AREA_CACHE_IO; // //The following code searchs virtual area list or AVL tree,to check if the lpDesiredAddr //is occupied,if so,then find a new one. // __ENTER_CRITICAL_SECTION(NULL,dwFlags); if(lpMemMgr->dwVirtualAreaNum < SWITCH_VA_NUM) //Should search in the list. lpStartAddr = SearchVirtualArea_l((__COMMON_OBJECT*)lpMemMgr,lpStartAddr,dwSize); else //Should search in the AVL tree. lpStartAddr = SearchVirtualArea_t((__COMMON_OBJECT*)lpMemMgr,lpStartAddr,dwSize); if(NULL == lpStartAddr) //Can not find proper virtual area. { __LEAVE_CRITICAL_SECTION(NULL,dwFlags); goto __TERMINAL; } lpVad->lpStartAddr = lpStartAddr; lpVad->lpEndAddr = (LPVOID)((DWORD)lpStartAddr + dwSize -1); lpDesiredAddr = lpStartAddr; if(lpMemMgr->dwVirtualAreaNum < SWITCH_VA_NUM) InsertIntoList((__COMMON_OBJECT*)lpMemMgr,lpVad); //Insert into list or tree. else InsertIntoTree((__COMMON_OBJECT*)lpMemMgr,lpVad); // //The following code reserves page table entries for the committed memory. // dwPteFlags = PTE_FLAGS_FOR_IOMAP; //IO map flags,that is,this memory range will not //use hardware cache. lpPhysical = lpStartAddr; while(dwSize) { if(!lpIndexMgr->ReservePage((__COMMON_OBJECT*)lpIndexMgr, lpStartAddr,lpPhysical,dwPteFlags)) { PrintLine("Fatal Error : Internal data structure is not consist."); __LEAVE_CRITICAL_SECTION(NULL,dwFlags); goto __TERMINAL; } dwSize -= PAGE_FRAME_SIZE; lpStartAddr = (LPVOID)((DWORD)lpStartAddr + PAGE_FRAME_SIZE); lpPhysical = (LPVOID)((DWORD)lpPhysical + PAGE_FRAME_SIZE); } __LEAVE_CRITICAL_SECTION(NULL,dwFlags); bResult = TRUE; //Indicate that the whole operation is successfully. __TERMINAL: if(!bResult) //Process failed. { if(lpVad) KMemFree((LPVOID)lpVad,KMEM_SIZE_TYPE_ANY,0); if(lpPhysical) PageFrameManager.FrameFree((__COMMON_OBJECT*)&PageFrameManager, lpPhysical, dwSize); return NULL; } return lpDesiredAddr; }
// //DoReserve routine.When VirtualAlloc is called with VIRTUAL_AREA_ALLOCATE_RESERVE, //then is routine is called by VirtualAlloc. // static LPVOID DoReserve(__COMMON_OBJECT* lpThis, LPVOID lpDesiredAddr, DWORD dwSize, DWORD dwAllocFlags, DWORD dwAccessFlags, UCHAR* lpVaName, LPVOID lpReserved) { __VIRTUAL_AREA_DESCRIPTOR* lpVad = NULL; __VIRTUAL_MEMORY_MANAGER* lpMemMgr = (__VIRTUAL_MEMORY_MANAGER*)lpThis; LPVOID lpStartAddr = lpDesiredAddr; LPVOID lpEndAddr = NULL; DWORD dwFlags = 0; BOOL bResult = FALSE; LPVOID lpPhysical = NULL; __PAGE_INDEX_MANAGER* lpIndexMgr = NULL; DWORD dwPteFlags = 0; if((NULL == lpThis) || (0 == dwSize)) //Parameter check. return NULL; if(VIRTUAL_AREA_ALLOCATE_RESERVE != dwAllocFlags) //Invalidate flags. return NULL; lpIndexMgr = lpMemMgr->lpPageIndexMgr; if(NULL == lpIndexMgr) //Validate. return NULL; lpStartAddr = (LPVOID)((DWORD)lpStartAddr & ~(PAGE_FRAME_SIZE - 1)); //Round up to page. lpEndAddr = (LPVOID)((DWORD)lpDesiredAddr + dwSize ); lpEndAddr = (LPVOID)(((DWORD)lpEndAddr & (PAGE_FRAME_SIZE - 1)) ? (((DWORD)lpEndAddr & ~(PAGE_FRAME_SIZE - 1)) + PAGE_FRAME_SIZE - 1) : ((DWORD)lpEndAddr - 1)); //Round down to page. dwSize = (DWORD)lpEndAddr - (DWORD)lpStartAddr + 1; //Get the actually size. lpVad = (__VIRTUAL_AREA_DESCRIPTOR*)KMemAlloc(sizeof(__VIRTUAL_AREA_DESCRIPTOR), KMEM_SIZE_TYPE_ANY); //In order to avoid calling KMemAlloc routine in the //critical section,we first call it here. if(NULL == lpVad) //Can not allocate memory. goto __TERMINAL; lpVad->lpManager = lpMemMgr; lpVad->lpStartAddr = NULL; lpVad->lpEndAddr = NULL; lpVad->lpNext = NULL; lpVad->dwAccessFlags = dwAccessFlags; lpVad->dwAllocFlags = dwAllocFlags; __INIT_ATOMIC(lpVad->Reference); lpVad->lpLeft = NULL; lpVad->lpRight = NULL; if(lpVaName) { if(StrLen((LPSTR)lpVaName) > MAX_VA_NAME_LEN) lpVaName[MAX_VA_NAME_LEN - 1] = 0; StrCpy((LPSTR)lpVad->strName[0],(LPSTR)lpVaName); //Set the virtual area's name. } else lpVad->strName[0] = 0; lpVad->dwCacheFlags = VIRTUAL_AREA_CACHE_NORMAL; // //The following code searchs virtual area list or AVL tree,to check if the lpDesiredAddr //is occupied,if so,then find a new one. // __ENTER_CRITICAL_SECTION(NULL,dwFlags); if(lpMemMgr->dwVirtualAreaNum < SWITCH_VA_NUM) //Should search in the list. lpStartAddr = SearchVirtualArea_l((__COMMON_OBJECT*)lpMemMgr,lpStartAddr,dwSize); else //Should search in the AVL tree. lpStartAddr = SearchVirtualArea_t((__COMMON_OBJECT*)lpMemMgr,lpStartAddr,dwSize); if(NULL == lpStartAddr) //Can not find proper virtual area. { __LEAVE_CRITICAL_SECTION(NULL,dwFlags); goto __TERMINAL; } lpVad->lpStartAddr = lpStartAddr; lpVad->lpEndAddr = (LPVOID)((DWORD)lpStartAddr + dwSize -1); lpDesiredAddr = lpStartAddr; if(lpMemMgr->dwVirtualAreaNum < SWITCH_VA_NUM) InsertIntoList((__COMMON_OBJECT*)lpMemMgr,lpVad); //Insert into list or tree. else InsertIntoTree((__COMMON_OBJECT*)lpMemMgr,lpVad); __LEAVE_CRITICAL_SECTION(NULL,dwFlags); bResult = TRUE; //Indicate that the whole operation is successfully. //In this operation(only reserve),we do not commit page table entries, //so,if access the memory space(read or write) of this range,will //cause a page fault exception. //The caller must call VirtualAlloc routine again to commit this //block of area before access it. __TERMINAL: if(!bResult) //Process failed. { if(lpVad) KMemFree((LPVOID)lpVad,KMEM_SIZE_TYPE_ANY,0); if(lpPhysical) PageFrameManager.FrameFree((__COMMON_OBJECT*)&PageFrameManager, lpPhysical, dwSize); return NULL; } return lpDesiredAddr; }
BSPTreeDataHandle_t CBSPTreeData::Insert( int userId, Vector const& mins, Vector const& maxs ) { BSPTreeDataHandle_t handle = NewHandle( userId ); InsertIntoTree( handle, mins, maxs ); return handle; }
int main() { TreeNode *root = NULL; InsertIntoTree( &root, 1 ); InsertIntoTree( &root->left, 2 ); InsertIntoTree( &root->right, 3 ); InsertIntoTree( &root->left->left, 4 ); InsertIntoTree( &root->left->right, 5 ); InsertIntoTree( &root->right->left, 6 ); InsertIntoTree( &root->right->right, 7 ); // printf("\n LevelOrder: "); // LevelOrderIterative( root ); // printf("\n"); RecursiveSiblings( root ); int i = 0; if( root->next ) { printf("\n %d's next is %d. \n", root->data, root->next->data); } else { printf("\n %d's next is NULL. \n", root->data); } if( root->left->next ) { printf("\n %d's next is %d. \n", root->left->data, root->left->next->data); } else { printf("\n %d's next is NULL. \n", root->left->data); } if( root->right->next ) { printf("\n %d's next is %d. \n", root->right->data, root->right->next->data); } else { printf("\n %d's next is NULL. \n", root->right->data); } if( root->left->left->next ) { printf("\n %d's next is %d. \n", root->left->left->data, root->left->left->next->data); } else { printf("\n %d's next is NULL. \n", root->left->left->data); } if( root->left->right->next ) { printf("\n %d's next is %d. \n", root->left->right->data, root->left->right->next->data); } else { printf("\n %d's next is NULL. \n", root->left->right->data); } if( root->right->left->next ) { printf("\n %d's next is %d. \n", root->right->left->data, root->right->left->next->data); } else { printf("\n %d's next is NULL. \n", root->right->left->data); } if( root->right->right->next ) { printf("\n %d's next is %d. \n", root->right->right->data, root->right->right->next->data); } else { printf("\n %d's next is NULL. \n", root->right->right->data); } return 0; }