From ec8d72a567b8c8bddd3d31a96d3b992946c40b4d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 23 Aug 2020 14:40:39 -0400 Subject: microprofile: Don't memset through std::atomic types Two of the members of the MicroProfileThreadLog contains two std::atomic instances. Given these aren't trivially-copyable types, we shouldn't be memsetting the structure, given implementation details can contain other members within it. To avoid potential undefined behavior on platforms, we can use aggregate initialization to zero out the members while still having well-defined behavior. While we're at it we can also silence some sign conversion warnings. --- externals/microprofile/microprofile.h | 57 +++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'externals/microprofile/microprofile.h') diff --git a/externals/microprofile/microprofile.h b/externals/microprofile/microprofile.h index 0c0d0a4d3..6dae65a66 100644 --- a/externals/microprofile/microprofile.h +++ b/externals/microprofile/microprofile.h @@ -152,9 +152,11 @@ typedef uint16_t MicroProfileGroupId; #include #include -#include -#include +#include +#include #include +#include +#include #ifndef MICROPROFILE_API #define MICROPROFILE_API @@ -605,28 +607,45 @@ struct MicroProfileFrameState struct MicroProfileThreadLog { - MicroProfileLogEntry Log[MICROPROFILE_BUFFER_SIZE]; + std::array Log{}; - std::atomic nPut; - std::atomic nGet; - uint32_t nActive; - uint32_t nGpu; - ThreadIdType nThreadId; + std::atomic nPut{0}; + std::atomic nGet{0}; + uint32_t nActive = 0; + uint32_t nGpu = 0; + ThreadIdType nThreadId{}; - uint32_t nStack[MICROPROFILE_STACK_MAX]; - int64_t nChildTickStack[MICROPROFILE_STACK_MAX]; - uint32_t nStackPos; + std::array nStack{}; + std::array nChildTickStack{}; + uint32_t nStackPos = 0; - uint8_t nGroupStackPos[MICROPROFILE_MAX_GROUPS]; - int64_t nGroupTicks[MICROPROFILE_MAX_GROUPS]; - int64_t nAggregateGroupTicks[MICROPROFILE_MAX_GROUPS]; + std::array nGroupStackPos{}; + std::array nGroupTicks{}; + std::array nAggregateGroupTicks{}; enum { THREAD_MAX_LEN = 64, }; - char ThreadName[64]; - int nFreeListNext; + char ThreadName[64]{}; + int nFreeListNext = 0; + + void Reset() { + Log.fill({}); + nPut = 0; + nGet = 0; + nActive = 0; + nGpu = 0; + nThreadId = {}; + nStack.fill(0); + nChildTickStack.fill(0); + nStackPos = 0; + nGroupStackPos.fill(0); + nGroupTicks.fill(0); + nAggregateGroupTicks.fill(0); + std::fill(std::begin(ThreadName), std::end(ThreadName), '\0'); + nFreeListNext = 0; + } }; #if MICROPROFILE_GPU_TIMERS_D3D11 @@ -1151,6 +1170,7 @@ MicroProfileThreadLog* MicroProfileCreateThreadLog(const char* pName) MP_ASSERT(pLog->nPut.load() == 0); MP_ASSERT(pLog->nGet.load() == 0); S.nFreeListHead = S.Pool[S.nFreeListHead]->nFreeListNext; + pLog->Reset(); } else { @@ -1158,7 +1178,6 @@ MicroProfileThreadLog* MicroProfileCreateThreadLog(const char* pName) S.nMemUsage += sizeof(MicroProfileThreadLog); S.Pool[S.nNumLogs++] = pLog; } - memset(pLog, 0, sizeof(*pLog)); int len = (int)strlen(pName); int maxlen = sizeof(pLog->ThreadName)-1; len = len < maxlen ? len : maxlen; @@ -1206,8 +1225,8 @@ void MicroProfileOnThreadExit() { S.Frames[i].nLogStart[nLogIndex] = 0; } - memset(pLog->nGroupStackPos, 0, sizeof(pLog->nGroupStackPos)); - memset(pLog->nGroupTicks, 0, sizeof(pLog->nGroupTicks)); + pLog->nGroupStackPos.fill(0); + pLog->nGroupTicks.fill(0); } } -- cgit v1.2.3