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.
This commit is contained in:
Lioncash 2020-08-23 14:40:39 -04:00
parent ab350b8975
commit ec8d72a567
2 changed files with 144 additions and 125 deletions

View file

@ -152,9 +152,11 @@ typedef uint16_t MicroProfileGroupId;
#include <stdint.h>
#include <string.h>
#include <thread>
#include <mutex>
#include <algorithm>
#include <array>
#include <atomic>
#include <mutex>
#include <thread>
#ifndef MICROPROFILE_API
#define MICROPROFILE_API
@ -605,28 +607,45 @@ struct MicroProfileFrameState
struct MicroProfileThreadLog
{
MicroProfileLogEntry Log[MICROPROFILE_BUFFER_SIZE];
std::array<MicroProfileLogEntry, MICROPROFILE_BUFFER_SIZE> Log{};
std::atomic<uint32_t> nPut;
std::atomic<uint32_t> nGet;
uint32_t nActive;
uint32_t nGpu;
ThreadIdType nThreadId;
std::atomic<uint32_t> nPut{0};
std::atomic<uint32_t> 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<uint32_t, MICROPROFILE_STACK_MAX> nStack{};
std::array<int64_t, MICROPROFILE_STACK_MAX> 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<uint8_t, MICROPROFILE_MAX_GROUPS> nGroupStackPos{};
std::array<int64_t, MICROPROFILE_MAX_GROUPS> nGroupTicks{};
std::array<int64_t, MICROPROFILE_MAX_GROUPS> 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);
}
}