void BuddyMemoryAllocator::MarkTree (const unsigned int &rootIndex, const NodeState &nodeState) { if (IsLeaf (rootIndex, m_TreeLength)) { m_NodeTree [rootIndex] = nodeState; return; } else { MarkTree (GetLeftNodeIndex (rootIndex), nodeState); MarkTree (GetRightNodeIndex (rootIndex), nodeState); m_NodeTree [rootIndex] = nodeState; } }
/* MarkTree: scan tree and give each node a unique id */ static void MarkTree(VQNode n, short *nid) { if (n != NULL){ n->nid = (*nid)++; n->lid = n->rid = 0; if (n->left != NULL){ MarkTree(n->left,nid); n->lid = n->left->nid; } if (n->right != NULL){ MarkTree(n->right,nid); n->rid = n->right->nid; } } }
/* EXPORT->PrintVQTab: Print the given VQTable */ void PrintVQTab(VQTable vqTab) { VQTable v=vqTab; VQNode n; short nid,s; printf("VQTable %s: ",v->tabFN); switch (v->type){ case linTree: printf("LinTree "); break; case binTree: printf("BinTree "); break; } switch (v->ckind){ case NULLC: printf("Euclidean "); break; case INVDIAGC: printf("Inv Diag "); break; case FULLC: printf("Inv Full "); break; } printf(" magic=%d nodes=%d\n",v->magic,v->numNodes); for (s=1; s<=v->swidth[0]; s++){ nid = 1; n = v->tree[s]; MarkTree(n,&nid); } for (s=1; s<=v->swidth[0]; s++){ printf("Stream %d\n",s); n = v->tree[s]; PrintTree(n,v->ckind); } }
/* EXPORT->StoreVQTab: store VQTable in tabFN */ void StoreVQTab(VQTable vqTab, char *tabFN) { FILE *f; char *fn; short nid,s,sum; VQTable v=vqTab; VQNode n; fn = (tabFN==NULL)?vqTab->tabFN:tabFN; if ((f = fopen(fn,"w")) == NULL) HError(6111,"StoreVQTab: cannot create file %s",fn); /* Stamp each node with a unique id */ sum = 0; for (s=1; s<=v->swidth[0]; s++){ nid = 1; n = v->tree[s]; MarkTree(n,&nid); sum += (nid-1); } v->numNodes = sum; /* Write the Header */ fprintf(f,"%d %d %d %d %d ", v->magic,v->type,v->ckind,v->numNodes,v->swidth[0]); for (s=1; s<=v->swidth[0]; s++) fprintf(f,"%d ",v->swidth[s]); fprintf(f,"\n"); for (s=1; s<=v->swidth[0]; s++){ n = v->tree[s]; StoreTree(f,n,v->ckind,s); } fclose(f); }
BuddyMemoryAllocator::BuddyMemoryAllocator () { m_MemAlloc = new Byte [GetPowof2 (AllocSizeExp)]; m_NodeTree = new NodeState [GetPowof2 (AllocSizeExp - GetExpof2 (AllocUnit) + 1)]; m_TreeLength = GetPowof2 (AllocSizeExp - GetExpof2 (AllocUnit) + 1) - 1; MarkTree (0, NodeState::eNODE_UNUSE); }
void MemCheckOutputView::OnUnmarkAllErrors(wxCommandEvent& event) { wxDataViewItemArray items; m_dataViewCtrlErrorsModel->GetChildren(wxDataViewItem(0), items); for(wxDataViewItemArray::iterator it = items.begin(); it != items.end(); ++it) { MarkTree(*it, false); } }
void MemCheckOutputView::MarkAllErrors(bool state) { wxDataViewItemArray items; m_dataViewCtrlErrorsModel->GetChildren(wxDataViewItem(0), items); for(wxDataViewItemArray::iterator it = items.begin(); it != items.end(); ++it) { MarkTree(*it, state); } }
void MemCheckOutputView::MarkTree(const wxDataViewItem& item, bool checked) { int col = GetColumnByName(_("Suppress")); if(col == wxNOT_FOUND) { return; } m_dataViewCtrlErrorsModel->ChangeValue(wxVariant(checked), item, col); if(m_dataViewCtrlErrorsModel->IsContainer(item)) { wxDataViewItemArray subItems; m_dataViewCtrlErrorsModel->GetChildren(item, subItems); for(size_t i = 0; i < subItems.GetCount(); ++i) MarkTree(subItems.Item(i), checked); } }
/** * @brief This callback is trigered when user click on checkbox - wants un/mark error. * @param event just wxDataViewEvent */ void MemCheckOutputView::OnValueChanged(wxDataViewEvent& event) { // CL_DEBUG1(PLUGIN_PREFIX("MemCheckOutputView::OnValueChanged()")); int col = GetColumnByName(_("Suppress")); if(col == wxNOT_FOUND) { return; } if(m_onValueChangedLocked || event.GetColumn() != col) return; m_onValueChangedLocked = true; wxVariant variant; m_dataViewCtrlErrorsModel->GetValue(variant, event.GetItem(), col); MarkTree(GetTopParent(event.GetItem()), variant.GetBool()); variant.GetBool() ? ++m_markedErrorsCount : --m_markedErrorsCount; m_onValueChangedLocked = false; }
void * BuddyMemoryAllocator::Malloc (unsigned int allocSize) { if (allocSize == 0) { assert (false); return nullptr; } if (GetPowof2 (AllocSizeExp) < allocSize) { // OVER LIMIT! assert (false); return nullptr; } unsigned int searchDepth = 0; if (allocSize <= AllocUnit) { searchDepth = AllocSizeExp; } else { unsigned int value = GetNextPowof2 (allocSize); searchDepth = AllocSizeExp - value; } // In this Depth, Search which node can be use unsigned int beginIndex = (1 << searchDepth) - 1; unsigned int endIndex = (1 << (searchDepth + 1)) - 2; unsigned int searchIndex = 0; for (searchIndex = beginIndex; searchIndex <= endIndex; ++searchIndex) { NodeState *nodeState = m_NodeTree + searchIndex; if (*nodeState == NodeState::eNODE_UNUSE) { break; } else { continue; } } if (searchIndex > endIndex) { // Can't Alloc. return nullptr; } else { // Make this node used, all children Occupied to parent, all parent occupied to children. // Children Part. MarkTree (searchIndex, NodeState::eNODE_OCCUPIED_TO_PARENT); // Root Part. m_NodeTree [searchIndex] = NodeState::eNODE_USED; // Parent Part. auto parentIndex = searchIndex; while (true) { if (parentIndex == 0) { *(m_NodeTree + 0) = NodeState::eNODE_OCCUPIED_TO_CHILD; break; } else { parentIndex = GetParentNodeIndex (parentIndex); *(m_NodeTree + parentIndex) = NodeState::eNODE_OCCUPIED_TO_CHILD; } } } unsigned int unitSizeofThisDepth = (unsigned int)(GetPowof2 (AllocSizeExp) / std::pow (2, searchDepth)); void *retAddr = m_MemAlloc; long retAddrValue = (long)retAddr; retAddrValue += (searchIndex - beginIndex) * unitSizeofThisDepth; retAddr = (void *)(retAddrValue); return retAddr; }
void BuddyMemoryAllocator::Free (void *ptr) { if (ptr == nullptr) { return; } unsigned long freeMemPtrValue = (unsigned long)(ptr); unsigned long beginAddrValue = (unsigned long)(m_MemAlloc); unsigned long endAddrValue = (unsigned long)(m_MemAlloc + AllocSize); // Test if this ptr is valid. if (freeMemPtrValue < beginAddrValue || freeMemPtrValue > endAddrValue) { // This Ptr is not belongs to this Memory Area. assert (false); } else { unsigned long offset = freeMemPtrValue - beginAddrValue; unsigned int beginSearchDepth = 0; unsigned int beginSearchIndex = 0; // The Following code used to find the search beginning level in the // tree by the offset // At first the offset must be divided exactly to the AllocUnit assert (offset % AllocUnit == 0); // Try to search up to find the highest Tree Level unsigned int exp = 0; unsigned int levelUnitSize = GetPowof2 (exp); for (exp = AllocUnitExp; exp <= AllocSizeExp; ++exp) { levelUnitSize = GetPowof2 (exp); if (offset % levelUnitSize == 0) { } else { --exp; break; } } levelUnitSize = GetPowof2 (exp); beginSearchDepth = TreeDepth - (exp - AllocUnitExp); auto levelBeginIndex = GetPowof2 (beginSearchDepth) - 1; beginSearchIndex = levelBeginIndex + offset / levelUnitSize; // auto tB = beginAddrValue; // auto tE = endAddrValue; // auto tP = freeMemPtrValue; // while (true) // { // if (tB == tP) // { // break; // } // else // { // if (tE - tB == AllocUnit) // { // if (tP != tB) // { // // This Ptr is not belongs to this Memory Area. // // It points to the Mid Part of AllocUnit. // assert (false); // } // else // { // break; // } // } // else // { // ++beginSearchDepth; // auto tMid = (tB + tE) / 2; // if (tP < tMid) // { // // Go To Left Tree To search // beginSearchIndex = GetLeftNodeIndex (beginSearchIndex); // tE = tMid; // } // else // { // beginSearchIndex = GetRightNodeIndex (beginSearchIndex); // tB = tMid; // } // } // } // } // Release if (IsLeaf (beginSearchIndex, m_TreeLength)) { assert (m_NodeTree [beginSearchIndex] == NodeState::eNODE_USED); m_NodeTree [beginSearchIndex] = NodeState::eNODE_UNUSE; } else { if (m_NodeTree [beginSearchIndex] == NodeState::eNODE_USED) { MarkTree (beginSearchIndex, NodeState::eNODE_UNUSE); } else if (m_NodeTree [beginSearchIndex] == NodeState::eNODE_OCCUPIED_TO_CHILD) { // Go left Tree to find NodeState::eNODE_USED while (true) { beginSearchIndex = GetLeftNodeIndex (beginSearchIndex); if (IsLeaf (beginSearchIndex, m_TreeLength)) { assert (m_NodeTree [beginSearchIndex] == NodeState::eNODE_USED); break; } else { if (m_NodeTree [beginSearchIndex] == NodeState::eNODE_USED) { break; } assert (m_NodeTree [beginSearchIndex] == NodeState::eNODE_OCCUPIED_TO_CHILD); } } MarkTree (beginSearchIndex, NodeState::eNODE_UNUSE); } else { // Data Structure Error. assert (false); } } // Try Combine. unsigned int nodeIndex = beginSearchIndex; while (true) { if (IsRootNode (nodeIndex)) { break; } else { unsigned int siblingNodeIndex = GetSiblingNode (nodeIndex); unsigned int parentIndex = GetParentNodeIndex (nodeIndex); if (m_NodeTree [siblingNodeIndex] == NodeState::eNODE_UNUSE && m_NodeTree [parentIndex] == NodeState::eNODE_OCCUPIED_TO_CHILD) { m_NodeTree [parentIndex] = NodeState::eNODE_UNUSE; nodeIndex = parentIndex; } else { break; } } } } }