/** * Free all the allocated memory */ void terminateLibrary() { Block* blockToClose; TStruct toTerminate; while (!hashQueue.empty()) { toTerminate = hashQueue.front(); hashQueue.pop_front(); free(toTerminate.block->getData()); toTerminate.block->addData(NULL); free(toTerminate.data); theChain->updateAvailableBlockNum(toTerminate.block->getBlockNum()); toTerminate.data = NULL; delete toTerminate.block; } //dealing with all the blocks that attached to the chain: map<int, Block*>::iterator it; for (it = theChain->usedBlock.begin(); it != theChain->usedBlock.end();) { blockToClose = it->second; free(blockToClose->getData()); theChain->updateAvailableBlockNum(blockToClose->getBlockNum()); blockToClose->addData(NULL); delete blockToClose; ++it; } delete theChain; theChain = NULL; }
/** * The function of the closing thread */ void *closingThraedFunc(void* args) { mutexLocker(); TStruct toAttach; int nonce; char* hashedData; Block* blockToClose; isDeamonRun = false; mutexUnlocker(); // make the deamon thread to stop pthread_join(hashingThread, NULL); mutexLocker(); if (theChain == NULL) { mutexUnlocker(); return NULL; } // dealing with all the pending threads. while (!hashQueue.empty()) { toAttach = hashQueue.front(); hashQueue.pop_front(); nonce = generate_nonce(toAttach.block->getBlockNum(), toAttach.block->getFather()->getBlockNum()); free(toAttach.block->getData()); toAttach.block->addData(NULL); hashedData = generate_hash(toAttach.data, toAttach.length, nonce); cout << hashedData << endl; free(hashedData); hashedData = NULL; free(toAttach.data); theChain->updateAvailableBlockNum(toAttach.block->getBlockNum()); theChain->usedBlock.erase(toAttach.block->getBlockNum()); toAttach.data = NULL; //cout << "Pending block : " << toAttach.block->getBlockNum() << endl; delete toAttach.block; } //dealing with all the blocks that attached to the chain: map<int, Block*>::iterator it; for (it = theChain->usedBlock.begin(); it != theChain->usedBlock.end();) { blockToClose = it->second; //cout << "Closing block : " << blockToClose->getBlockNum() << endl; free(blockToClose->getData()); theChain->updateAvailableBlockNum(blockToClose->getBlockNum()); blockToClose->addData(NULL); delete blockToClose; ++it; } delete theChain; theChain = NULL; close_hash_generator(); chainStatus = CLOSED; mutexUnlocker(); pthread_exit(NULL); }