__section("SectionForFlashOperations") Native_Profiler::~Native_Profiler()
{
    if(s_native_profiler.isOn)
    {
        s_native_profiler.isOn = FALSE;
        s_native_profiler.writtenData = TRUE;
        UINT64 returnTime = Native_Profiler_TimeInMicroseconds() - s_native_profiler.initTime;

        if(s_native_profiler.useBuffer)
        {
            *s_native_profiler.position++ = ((unsigned int)functionAddress) + 1;
            *s_native_profiler.position++ = (UINT32)(returnTime);
            if((s_native_profiler.position + 2) >= &ProfilerBufferEnd)
            {
                *s_native_profiler.position++ = NATIVE_PROFILER_END_TAG;
                Native_Profiler_Dump();
            }
        }
        else
        {
            UINT32 tempBuffer[2];
            tempBuffer[0] = ((unsigned int)functionAddress) + 1;
            tempBuffer[1] = (UINT32)(returnTime);
            Native_Profiler_WriteToCOM(tempBuffer, 8);
        }
        s_native_profiler.isOn = TRUE;
    }
}
__section(SectionForFlashOperations) Native_Profiler::Native_Profiler()
{
    if(s_native_profiler.isOn)
    {
        s_native_profiler.isOn = FALSE;
        s_native_profiler.writtenData = TRUE;
        UINT64 entryTime = Native_Profiler_TimeInMicroseconds() - s_native_profiler.initTime;
        functionAddress = __return_address();

        if(s_native_profiler.useBuffer)
        {
            *s_native_profiler.position++ = functionAddress;
            *s_native_profiler.position++ = (UINT32)(entryTime);
            if((s_native_profiler.position + 2) >= &ProfilerBufferEnd)
            {
                *s_native_profiler.position++ = NATIVE_PROFILER_END_TAG;
                Native_Profiler_Dump();
            }
        }
        else
        {
            UINT32 tempBuffer[2];
            tempBuffer[0] = functionAddress;
            tempBuffer[1] = (UINT32)(entryTime);
            Native_Profiler_WriteToCOM(tempBuffer, 8);
        }
        s_native_profiler.isOn = TRUE;
    }
}
void __section("SectionForFlashOperations") Native_Profiler_Stop()
{
    if(s_native_profiler.isOn && s_native_profiler.writtenData && s_native_profiler.lock == FALSE)
    {
        s_native_profiler.isOn = FALSE;
        if(s_native_profiler.useBuffer)
        {
            // Send all data that is still in the buffer.
            *s_native_profiler.position++ = NATIVE_PROFILER_END_TAG;
            UINT32 size = (char *)s_native_profiler.position - (char *)&ProfilerBufferBegin;
            Native_Profiler_WriteToCOM(&ProfilerBufferBegin, size);
            s_native_profiler.position = &ProfilerBufferBegin;
            *s_native_profiler.position++ = NATIVE_PROFILER_START_TAG;
            *s_native_profiler.position++ = s_native_profiler.engineTimeOffset;
            s_native_profiler.writtenData = FALSE;
        }
    }
}