void InvertPathColors(int color1, int color2, Path path, FreeList *free_list, EdgeList *edge_list, Table *table){ Edge edge; int i, color; if (path.size == 0){ printf("\nCaminho vazio"); return; } /* Garante que as cores possam ser enviadas por parametro em qualquer ordem */ edge = edge_list->edge[path.iEdges[0]]; if (color1 != edge.color){ color = color1; color1 = edge.color; color2 = color; } if (path.size > 1){ edge = edge_list->edge[path.iEdges[1]]; color2 = edge.color; } for (i = 0; i < path.size; i++){ edge = edge_list->edge[path.iEdges[i]]; if (i%2 == 0){ /* Atualiza no EdgeList */ edge_list->edge[path.iEdges[i]].color = color2; /* Atualiza no FreeList */ RemoveFreeByColor(free_list, edge.v, color2, table); RemoveFreeByColor(free_list, edge.w, color2, table); InsertFree(edge.v, color1, free_list, table); InsertFree(edge.w, color1, free_list, table); /* Atualiza no Table */ UpdateInTable(edge.v, edge.w, color2, path.iEdges[i], table); } else{ edge_list->edge[path.iEdges[i]].color = color1; /* Atualiza no FreeList */ RemoveFreeByColor(free_list, edge.v, color1, table); RemoveFreeByColor(free_list, edge.w, color1, table); InsertFree(edge.v, color2, free_list, table); InsertFree(edge.w, color2, free_list, table); /* Atualiza no Table */ UpdateInTable(edge.v, edge.w, color1, path.iEdges[i], table); } } }
void InsertFree(unsigned n,void *pData) //向2^n*seg区插入一段空闲空间 { if(n > MAX_FREE_PART) { //free(pData); return; } LinkNode* t = &freeList.partner[n]; LinkNode* parent = t; t = t->next; while(t && t->pData < pData) // 寻找相邻块并合并 { if(t->pData + (SEGMENT_SIZE << n) == pData) { parent->next = t->next; //printf("combine"); InsertFree(n+1,t->pData); //Combine free(t); return; } parent = t; t = t->next; } if(t && t->pData == pData + (SEGMENT_SIZE << n)) { //printf("combine"); InsertFree(n+1,pData); //合并并插入上个链表 } else { if(!t) { LinkNode* p = malloc(sizeof(LinkNode)); p->next = NULL; p->pData = pData; parent->next = p; } else { LinkNode* p = malloc(sizeof(LinkNode)); p->next = t->next; p->pData = pData; t->next = p; } } return; }
void* Splice(unsigned int n) //分裂节点并返回分裂出的一个节点的指向内存,另一个节点插入链表相应位置 { if(n > MAX_FREE_PART) //请求大于最大支持的内存块 { LinkNode* p = &freeList.partner[MAX_FREE_PART - 1]; //是否是最大的块内存不足 if(p->next) //最大的块还有剩余~说明请求的内存太大了,不支持 { fprintf(stderr,"Max support memory part is 2 ^ %d!\n",MAX_FREE_PART); exit(-1); } //分裂超出最大块出现在最大块已经不足的情况下,那么返回新申请的最大块给他即可 return malloc(SEGMENT_SIZE << MAX_FREE_PART); } else { void* pBig = GetFree(n); //索取n的空间并分裂 void* pHalf = pBig + (SEGMENT_SIZE << (n-1)); InsertFree(n-1,pHalf); return pBig; } }
void Free(void* pData) { unsigned int n = *((char*)pData); //读出第一个字节表示的该段内存大小 InsertFree(n,pData); //回收内存 }