void tFree(TNodePtr *proot) { if (*proot == NULL) return; tFree(&((*proot)->left)); tFree(&((*proot)->right)); free(*proot); *proot = NULL; return; }
bool TCPSocket::SendPacket( const Message& packet ) { bool result = false; unsigned int dataSize = sizeof( unsigned int ) + packet.GetSerializationSize(); Byte* serializedPacket = static_cast<Byte*>( tMalloc( dataSize ) ); Byte* walker = serializedPacket; SerializationUtility::CopyAndIncrementDestination( walker, &dataSize, sizeof( unsigned int ) ); packet.Serialize( walker ); #if NETWORK_DEBUG if ( walker != serializedPacket + dataSize ) { int differentiatingSize = static_cast<int>(walker - serializedPacket - dataSize); assert(false); } MemoryAllocator::VerifyPointer( serializedPacket ); #endif if ( serializedPacket != nullptr ) { result = SendRawData( serializedPacket, dataSize ); tFree( serializedPacket ); } return result; }
static void PostDominators(void) { int w, i; domCount = 0; WalkFlowgraph(blockArray[exitBlock], domNumber, FALSE); vectorData = tAlloc(sizeof(struct _tarjan *) * (domCount + 1)); for (i=0; i <= domCount; i++) { vectorData[i] = tAlloc(sizeof(struct _tarjan)); vectorData[i]->bucket = briggsAlloct(domCount+1); } WalkFlowgraph(blockArray[exitBlock], domInit, FALSE); for (w = domCount; w>=2; w--) { int p = vectorData[w]->parent; int j; struct _tarjan *v = vectorData[w]; BLOCKLIST *bl = v->preds; while (bl) { int u = domEval(bl->block->dfstOrder); if (vectorData[w]->semi > vectorData[u]->semi) vectorData[w]->semi = vectorData[u]->semi; bl = bl->next; } briggsSet(vectorData[vectorData[w]->semi]->bucket, w); domLink(p, w); for (j = vectorData[p]->bucket->top -1 ; j >= 0; j--) { int v = vectorData[p]->bucket->data[j]; int u = domEval(v); if (vectorData[u]->semi >= p) vectorData[v]->idom = p; else vectorData[v]->idom = u; } briggsClear(vectorData[p]->bucket); } for (w=2; w <=domCount; w++) { if (vectorData[w]->idom != vectorData[w]->semi) { vectorData[w]->idom = vectorData[vectorData[w]->idom]->idom; } /* transfer idom to our block struct */ blockArray[vectorData[w]->blocknum]->pdom = vectorData[vectorData[w]->idom]->blocknum; } tFree(); }
void genfunc(SYMBOL *funcsp) /* * generate a function body and dump the icode */ { IMODE *allocaAP = NULL; SYMBOL *oldCurrentFunc; EXPRESSION *funcexp = varNode(en_global, funcsp); if (total_errors) return; // //printf("%s\n", funcsp->name); contlab = breaklab = - 1; structret_imode = 0 ; tempCount = 0; blockCount = 0; blockMax = 0; exitBlock = 0; oldCurrentFunc = theCurrentFunc; theCurrentFunc = funcsp; iexpr_func_init(); /* firstlabel = nextLabel;*/ cseg(); gen_line(funcsp->linedata); gen_func(funcexp, 1); /* in C99 inlines can clash if declared 'extern' in multiple modules */ /* in C++ we introduce virtual functions that get coalesced at link time */ if (cparams.prm_cplusplus && funcsp->linkage == lk_inline) gen_virtual(funcsp, FALSE); else { if (funcsp->storage_class == sc_global) globaldef(funcsp); else localdef(funcsp); gen_strlab(funcsp); /* name of function */ } addblock( - 1); if (funcsp->linkage == lk_interrupt || funcsp->linkage == lk_fault) { gen_icode(i_pushcontext, 0,0,0); /* if (funcsp->loadds) */ /* gen_icode(i_loadcontext, 0,0,0); */ } gen_icode(i_prologue,0,0,0); gen_label(startlab); /* if (funcsp->loadds && funcsp->farproc) */ /* gen_icode(i_loadcontext, 0,0,0); */ AllocateLocalContext(NULL, funcsp); if (funcsp->allocaUsed) { EXPRESSION *allocaExp = varNode(en_auto, anonymousVar(sc_auto, &stdpointer)); allocaAP = gen_expr(funcsp, allocaExp, 0, ISZ_ADDR); gen_icode(i_savestack, 0, allocaAP, 0); } /* Generate the icode */ /* LCSE is done while code is generated */ genstmt(funcsp->inlineFunc.stmt->lower, funcsp); if (funcsp->inlineFunc.stmt->blockTail) { gen_icode(i_functailstart, 0, 0, 0); genstmt(funcsp->inlineFunc.stmt->blockTail, funcsp); gen_icode(i_functailend, 0, 0, 0); } genreturn(0, funcsp, 1, 0, allocaAP); gen_func(funcexp, 0); tFree(); InsertParameterThunks(funcsp, blockArray[1]); optimize(funcsp); FreeLocalContext(NULL, funcsp); AllocateStackSpace(funcsp); FillInPrologue(intermed_head, funcsp); /* Code gen from icode */ rewrite_icode(); /* Translate to machine code & dump */ if (chosenAssembler->gen->post_function_gen) chosenAssembler->gen->post_function_gen(funcsp, intermed_head); if (cparams.prm_cplusplus && funcsp->linkage == lk_inline) gen_endvirtual(funcsp); intermed_head = NULL; dag_rundown(); oFree(); theCurrentFunc = oldCurrentFunc; if (blockCount > maxBlocks) maxBlocks = blockCount; if (tempCount > maxTemps) maxTemps = tempCount; }
TCPSocket::~TCPSocket() { // Delete any half received messages if ( m_PayloadData != nullptr ) tFree( m_PayloadData ); }
Message* TCPSocket::Receive() { if ( m_Socket == INVALID_SOCKET ) { Logger::Log( "Attempted to receive from invalid socket", "TCPSocket", LogSeverity::WARNING_MSG ); return nullptr; } int nrOfBytesReceived; if ( m_ExpectedHeaderBytes > 0 ) // If we are waiting for header data { nrOfBytesReceived = recv( m_Socket, m_Pos, m_ExpectedHeaderBytes, 0 ); // Try to receive header if ( nrOfBytesReceived == 0 ) // Check if the socket was gracefully disconnected { m_Connected = false; Logger::Log( "Connection to " + m_Destination.GetPrintableAddressAndPort() + " terminated gracefully", "TCPSocket", LogSeverity::INFO_MSG ); } else if ( nrOfBytesReceived == -1 ) { int error = GET_NETWORK_ERROR; if ( error != ROBOEWOULDBLOCK ) { if ( error == ROBOECONNECTIONABORTED || error == ROBOECONNRESET ) { m_Connected = false; Logger::Log( "Connection to " + m_Destination.GetPrintableAddressAndPort( ) + " was aborted", "TCPSocket", LogSeverity::INFO_MSG ); } else LogErrorMessage( "An unhandled error occured while receiving header data", "TCPSocket" ); } return nullptr; // No data was ready to be received or there was an error } if ( nrOfBytesReceived == m_ExpectedHeaderBytes ) { m_ExpectedHeaderBytes = 0; // We received the full header // Get the size of the packet (Embedded as first part) and create a buffer of that size m_PayloadData = static_cast<Byte*>( tMalloc( m_ExpectedPayloadBytes ) ); m_ExpectedPayloadBytes -= sizeof( unsigned int ); // We have already received the size value m_Pos = m_PayloadData; // Pos now points to the new buffer since that is where we will want to write on the next recv } else // Only a part of the header was received. Account for this and handle it on next function call { m_ExpectedHeaderBytes -= nrOfBytesReceived; m_Pos += nrOfBytesReceived; return nullptr; } } nrOfBytesReceived = recv( m_Socket, m_Pos, m_ExpectedPayloadBytes, 0 ); // Try to receive payload if ( nrOfBytesReceived == -1 ) { int error = GET_NETWORK_ERROR; if ( error != ROBOEWOULDBLOCK ) { if ( error == ROBOECONNECTIONABORTED ) { m_Connected = false; Logger::Log( "Connection to " + m_Destination.GetPrintableAddressAndPort( ) + " was aborted", "TCPSocket", LogSeverity::INFO_MSG ); } else LogErrorMessage( "An unhandled error occured while receiving payload data", "TCPSocket" ); } return nullptr; // No data was ready to be received or there was an error } // If all data was received. Clean up, prepare for next call and return the buffer as a packet (Will need to be cast to the correct type on the outside using the Type field) if ( nrOfBytesReceived == m_ExpectedPayloadBytes ) { MessageTypes::MessageType messageType; Byte* walker = m_PayloadData; memcpy( &messageType, walker, sizeof( MESSAGE_TYPE_ENUM_UNDELYING_TYPE ) ); Message* packet; if ( messageType == 0 ) { NetworkMessageTypes::NetworkMessageType networkedMessageType; memcpy( &networkedMessageType, walker + sizeof( MESSAGE_TYPE_ENUM_UNDELYING_TYPE ) + sizeof( bool ), sizeof( NetworkMessageTypes::NetworkMessageType ) ); packet = NetworkMessages::GetDefaultMessage( networkedMessageType ); } else { packet = Messages::GetDefaultMessage( messageType ); } packet->Deserialize( ( const char*& )walker ); #if NETWORK_DEBUG if ( walker != m_PayloadData + packet->GetSerializationSize( ) ) { int differentiatingSize = walker - m_PayloadData - packet->GetSerializationSize( ); assert( false ); } MemoryAllocator::VerifyPointer( packet ); #endif tFree( m_PayloadData ); m_PayloadData = nullptr; m_Pos = reinterpret_cast<Byte*>( &m_ExpectedPayloadBytes ); m_ExpectedPayloadBytes = -1; m_ExpectedHeaderBytes = sizeof( unsigned int ); return packet; } else // Only part of the payload was received. Account for this and try to receive the rest in an upcoming function call { m_ExpectedPayloadBytes -= nrOfBytesReceived; m_Pos += nrOfBytesReceived; return nullptr; } }
static void Dominators(void) { int w, i; domCount = 0; WalkFlowgraph(blockArray[0], domNumber, TRUE); vectorData = tAlloc(sizeof(struct _tarjan *) * (domCount + 1)); for (i=0; i <= domCount; i++) { vectorData[i] = tAlloc(sizeof(struct _tarjan)); vectorData[i]->bucket = briggsAlloct(domCount+1); } WalkFlowgraph(blockArray[0], domInit, TRUE); for (w = domCount; w>=2; w--) { int p = vectorData[w]->parent; int j; struct _tarjan *v = vectorData[w]; BLOCKLIST *bl = v->preds; while (bl) { int u = domEval(bl->block->dfstOrder); if (vectorData[w]->semi > vectorData[u]->semi) vectorData[w]->semi = vectorData[u]->semi; bl = bl->next; } briggsSet(vectorData[vectorData[w]->semi]->bucket, w); domLink(p, w); for (j = vectorData[p]->bucket->top -1 ; j >= 0; j--) { int v = vectorData[p]->bucket->data[j]; int u = domEval(v); if (vectorData[u]->semi >= p) vectorData[v]->idom = p; else vectorData[v]->idom = u; } briggsClear(vectorData[p]->bucket); } for (w=2; w <=domCount; w++) { if (vectorData[w]->idom != vectorData[w]->semi) { vectorData[w]->idom = vectorData[vectorData[w]->idom]->idom; } /* transfer idom to our block struct */ blockArray[vectorData[w]->blocknum]->idom = vectorData[vectorData[w]->idom]->blocknum; } /* now make the forward tree */ for (i=blockCount-1; i >=1; i--) { /* make dominates lists by walking the idom tree backwards from each node*/ if (blockArray[i] && blockArray[i]->pred) { int w = blockArray[i]->idom; BLOCK *ub = blockArray[w]; BLOCKLIST *bl = oAlloc(sizeof(BLOCKLIST)); bl->block = blockArray[i]; bl->next = ub->dominates; ub->dominates = bl; } } tFree(); }