void* MemDefaultAllocator::Realloc(void* pOldPtr, size_t size, const char* tag, const char* pFile, unsigned int Line) { BEHAVIAC_UNUSED_VAR(tag); BEHAVIAC_UNUSED_VAR(pFile); BEHAVIAC_UNUSED_VAR(Line); #if BEHAVIAC_DEBUG_MEMORY_STATS if (pOldPtr && m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { GetPtrSizeRegister().UnRegisterPtr(pOldPtr); } } #endif void* p = realloc(pOldPtr, size); #if BEHAVIAC_DEBUG_MEMORY_STATS if (m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { GetPtrSizeRegister().RegisterPtrSize(p, size); } } #endif return p; }
void MemDefaultAllocator::FreeAligned(void* pData, size_t alignment, const char* tag, const char* pFile, unsigned int Line) { BEHAVIAC_UNUSED_VAR(tag); BEHAVIAC_UNUSED_VAR(pFile); BEHAVIAC_UNUSED_VAR(Line); BEHAVIAC_UNUSED_VAR(alignment); if (pData) { #if BEHAVIAC_DEBUG_MEMORY_STATS if (m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { GetPtrSizeRegister().UnRegisterPtr(pData); } } #endif #if BEHAVIAC_COMPILER_MSVC return _aligned_free(pData); #else return free(pData); #endif//#if BEHAVIAC_COMPILER_MSVC } }
void* MemDefaultAllocator::ReallocAligned(void* pOldPtr, size_t size, size_t alignment, const char* tag, const char* pFile, unsigned int Line) { BEHAVIAC_UNUSED_VAR(alignment); BEHAVIAC_UNUSED_VAR(tag); BEHAVIAC_UNUSED_VAR(pFile); BEHAVIAC_UNUSED_VAR(Line); if (pOldPtr) { #if BEHAVIAC_DEBUG_MEMORY_STATS if (m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { GetPtrSizeRegister().UnRegisterPtr(pOldPtr); } } #endif } #if BEHAVIAC_COMPILER_MSVC void* p = _aligned_realloc(pOldPtr, size, alignment); #else void* p = realloc(pOldPtr, size); #endif//BEHAVIAC_COMPILER_MSVC #if BEHAVIAC_DEBUG_MEMORY_STATS if (m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { GetPtrSizeRegister().RegisterPtrSize(p, size); } } #endif return p; }
MemDefaultAllocator::~MemDefaultAllocator() { #if BEHAVIAC_DEBUG_MEMORY_STATS BEHAVIAC_ASSERT(!m_bEnablePtrSizeRegister || m_PtrSizeRegister); { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { PtrSizeRegister::Destroy(m_PtrSizeRegister); } } m_PtrSizeRegister = 0; #endif }
void ConnectorImpl::OnConnection() { Log("behaviac: sending initial settings.\n"); this->SendInitialSettings(); Socket::SendWorkspaceSettings(); this->SendInitialProperties(); { ScopedInt_t scopedInt(&gs_threadFlag); Log("behaviac: sending packets before connecting.\n"); this->SendExistingPackets(); } behaviac::Socket::SendText("[connected]precached message done"); //when '[connected]' is handled in the designer, it will send back all the breakpoints if any and '[breakcpp]' and '[start]' //here we block until all those messages have been received, otherwise, if we don't block here to wait for all those messages //the breakpoints checking might be wrong. bool bLoop = true; while (bLoop && !m_isDisconnected) { //sending packets if any if (m_packetsCount > 0) { SendAllPackets(); } const char* kStartMsg = "[start]"; bool bFound = this->ReceivePackets(kStartMsg); if (bFound) { bLoop = false; } else { behaviac::Thread::Sleep(1); } } Log("behaviac: OnConnection done.\n"); //this->m_bHandleMessage = false; }
void* MemDefaultAllocator::AllocAligned(size_t size, size_t alignment, const char* tag, const char* pFile, unsigned int Line) { BEHAVIAC_UNUSED_VAR(size); BEHAVIAC_UNUSED_VAR(alignment); BEHAVIAC_UNUSED_VAR(tag); BEHAVIAC_UNUSED_VAR(pFile); BEHAVIAC_UNUSED_VAR(Line); #if BEHAVIAC_COMPILER_MSVC void* p = _aligned_malloc(size, alignment); #elif BEHAVIAC_COMPILER_APPLE void* p = 0; if (alignment < sizeof(void*)) { alignment = sizeof(void*); } int errCode = posix_memalign(&p, alignment, size); if (errCode) { BEHAVIAC_ASSERT(false, "The alignment argument was not a power of two, or was not a multiple of sizeof(void*)" "or there was insufficient memory to fulfill the allocate request\n"); } #else void* p = memalign(alignment, size); #endif//BEHAVIAC_COMPILER_MSVC #if BEHAVIAC_DEBUG_MEMORY_STATS if (m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { GetPtrSizeRegister().RegisterPtrSize(p, size); } } #endif return p; }
MemDefaultAllocator::MemDefaultAllocator() #if BEHAVIAC_DEBUG_MEMORY_STATS : m_PtrSizeRegister(0), m_bEnablePtrSizeRegister(true) #endif { #if BEHAVIAC_DEBUG_MEMORY_STATS if (m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { m_PtrSizeRegister = PtrSizeRegister::Create(); } } #endif }
void MemDefaultAllocator::Free(void* pData, const char* tag, const char* pFile, unsigned int Line) { BEHAVIAC_UNUSED_VAR(tag); BEHAVIAC_UNUSED_VAR(pFile); BEHAVIAC_UNUSED_VAR(Line); if (pData) { #if BEHAVIAC_DEBUG_MEMORY_STATS if (m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { GetPtrSizeRegister().UnRegisterPtr(pData); } } #endif free(pData); } }
void ConnectorInterface::Log(const char* msg) { ScopedInt_t scopedInt(&gs_threadFlag); BEHAVIAC_LOGMSG(msg); }
void ConnectorInterface::ThreadFunc() { #if BEHAVIAC_COMPILER_MSVC //printf("ThreadFunc gs_threadFlag = %d\n", (int)gs_threadFlag.value()); BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); #endif { ScopedInt_t scopedInt(&gs_threadFlag); Log("behaviac: Socket Thread Starting\n"); #if BEHAVIAC_COMPILER_MSVC BEHAVIAC_ASSERT(t_packetBufferIndex != TLS_OUT_OF_INDEXES); #else //printf("ThreadFunc t_packetBufferIndex = %d\n", t_packetBufferIndex); //BEHAVIAC_ASSERT(t_packetBufferIndex != (unsigned int)-1); #endif// } namespace Socket = behaviac::Socket; const bool blockingSocket = true; behaviac::Socket::Handle serverSocket = 0; { ScopedInt_t scopedInt(&gs_threadFlag); serverSocket = Socket::Create(blockingSocket); if (!serverSocket) { Log("behaviac: Couldn't create server socket.\n"); return; } char bufferTemp[64]; string_sprintf(bufferTemp, "behaviac: Listening at port %d...\n", m_port); Log(bufferTemp); // max connections: 1, don't allow multiple clients? if (!Socket::Listen(serverSocket, m_port, 1)) { Log("behaviac: Couldn't configure server socket.\n"); Socket::Close(serverSocket); return; } } #if BEHAVIAC_COMPILER_MSVC BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); #endif this->ReserveThreadPacketBuffer(); while (!m_terminating) { #if BEHAVIAC_COMPILER_MSVC //wait for connecting while (!m_terminating) { //Log("Socket::TestConnection.\n"); if (Socket::TestConnection(serverSocket)) { break; } behaviac::Thread::Sleep(100); } #endif if (!m_terminating) { BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); Log("behaviac: accepting...\n"); { ScopedInt_t scopedInt(&gs_threadFlag); m_writeSocket = Socket::Accept(serverSocket, kSocketBufferSize); if (!m_writeSocket) { Log("behaviac: Couldn't create write socket.\n"); Socket::Close(serverSocket); return; } Log("behaviac: connection accepted\n"); } BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); { ScopedInt_t scopedInt(&gs_threadFlag); AtomicInc(m_isConnected); behaviac::Thread::Sleep(1); OnConnection(); AtomicInc(m_isConnectedFinished); behaviac::Thread::Sleep(1); //this->OnConnectionFinished(); Log("behaviac: after Connected.\n"); } BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); while (!m_terminating && this->m_writeSocket) { behaviac::Thread::Sleep(1); SendAllPackets(); ReceivePackets(); } BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); // One last time, to send any outstanding packets out there. if (this->m_writeSocket) { SendAllPackets(); Socket::Close(m_writeSocket); } this->Clear(); Log("behaviac: disconnected. \n"); } }//while (!m_terminating) Socket::Close(serverSocket); this->Clear(); BEHAVIAC_ASSERT(gs_threadFlag.value() == 0); Log("behaviac: ThreadFunc exited. \n"); }
int ConnectorInterface::ReserveThreadPacketBuffer() { #if BEHAVIAC_COMPILER_MSVC uint32_t bufferIndex = (uint32_t)(uint64_t)TlsGetValue(t_packetBufferIndex); #else uint32_t bufferIndex = t_packetBufferIndex; #endif //THREAD_ID_TYPE id = behaviac::GetTID(); //BEHAVIAC_LOGINFO("ReserveThreadPacketBuffer:%d thread %d\n", bufferIndex, id); //bufferIndex initially is 0 if (bufferIndex <= 0) { int retIndex(-2); ScopedLock lock(m_packetBuffersLock); // NOTE: This is quite naive attempt to make sure that main thread queue // is the last one (rely on the fact that it's most likely to be the first // one trying to send message). This means EndFrame event should be sent after // memory operations from that frame. // (doesn't matter in SEQUENTIAL mode). for (int i = m_maxTracedThreads - 1; i >= 0; --i) { if (!m_packetBuffers[i]) { ScopedInt_t scopedInt(&gs_threadFlag); m_packetBuffers[i] = BEHAVIAC_NEW PacketBuffer(this); } if (m_packetBuffers[i]) { if (m_packetBuffers[i]->m_free) { m_packetBuffers[i]->m_free = false; retIndex = i; break; } } } if (retIndex > 0) { #if BEHAVIAC_COMPILER_MSVC TlsSetValue(t_packetBufferIndex, (PVOID)(uint64_t)retIndex); #else t_packetBufferIndex = retIndex; #endif } else { Log("behaviac: Couldn't reserve packet buffer, too many active threads.\n"); BEHAVIAC_ASSERT(false); } bufferIndex = retIndex; //BEHAVIAC_LOGINFO("ReserveThreadPacketBuffer:%d thread %d\n", bufferIndex, id); } return bufferIndex; }