Replace list with vector.

Maybe next time let's not forget that there's already a custom
allocating vector available.
This commit is contained in:
Bartosz Taudul 2020-02-24 21:54:19 +01:00
parent d60641cac4
commit 9c9e854005

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "TracyCallstack.hpp" #include "TracyCallstack.hpp"
#include "TracyFastVector.hpp"
#ifdef TRACY_HAS_CALLSTACK #ifdef TRACY_HAS_CALLSTACK
@ -62,10 +63,9 @@ struct ModuleCache
uint64_t end; uint64_t end;
char* name; char* name;
uint32_t nameLen; uint32_t nameLen;
ModuleCache* next;
}; };
static ModuleCache* s_modCache = nullptr; static FastVector<ModuleCache>* s_modCache;
void InitCallstack() void InitCallstack()
{ {
@ -82,6 +82,8 @@ void InitCallstack()
HANDLE proc = GetCurrentProcess(); HANDLE proc = GetCurrentProcess();
#ifndef __CYGWIN__ #ifndef __CYGWIN__
s_modCache = new FastVector<ModuleCache>( 512 );
if( EnumProcessModules( proc, mod, sizeof( mod ), &needed ) != 0 ) if( EnumProcessModules( proc, mod, sizeof( mod ), &needed ) != 0 )
{ {
const auto sz = needed / sizeof( HMODULE ); const auto sz = needed / sizeof( HMODULE );
@ -99,7 +101,7 @@ void InitCallstack()
while( ptr > name && *ptr != '\\' && *ptr != '/' ) ptr--; while( ptr > name && *ptr != '\\' && *ptr != '/' ) ptr--;
if( ptr > name ) ptr++; if( ptr > name ) ptr++;
const auto namelen = name + res - ptr; const auto namelen = name + res - ptr;
auto cache = (ModuleCache*)tracy_malloc( sizeof( ModuleCache ) ); auto cache = s_modCache->push_next();
cache->start = base; cache->start = base;
cache->end = base + info.SizeOfImage; cache->end = base + info.SizeOfImage;
cache->name = (char*)tracy_malloc( namelen+3 ); cache->name = (char*)tracy_malloc( namelen+3 );
@ -108,8 +110,6 @@ void InitCallstack()
cache->name[namelen+1] = ']'; cache->name[namelen+1] = ']';
cache->name[namelen+2] = '\0'; cache->name[namelen+2] = '\0';
cache->nameLen = namelen+2; cache->nameLen = namelen+2;
cache->next = s_modCache;
s_modCache = cache;
} }
} }
} }
@ -150,16 +150,14 @@ const char* DecodeCallstackPtrFast( uint64_t ptr )
static void GetModuleName( uint64_t addr, char* buf, ULONG& len ) static void GetModuleName( uint64_t addr, char* buf, ULONG& len )
{ {
#ifndef __CYGWIN__ #ifndef __CYGWIN__
auto ptr = s_modCache; for( auto& v : *s_modCache )
while( ptr )
{ {
if( addr >= ptr->start && addr < ptr->end ) if( addr >= v.start && addr < v.end )
{ {
memcpy( buf, ptr->name, ptr->nameLen+1 ); memcpy( buf, v.name, v.nameLen+1 );
len = ptr->nameLen; len = v.nameLen;
return; return;
} }
ptr = ptr->next;
} }
HMODULE mod[1024]; HMODULE mod[1024];
@ -191,14 +189,12 @@ static void GetModuleName( uint64_t addr, char* buf, ULONG& len )
buf[namelen+2] = '\0'; buf[namelen+2] = '\0';
len = namelen+2; len = namelen+2;
auto cache = (ModuleCache*)tracy_malloc( sizeof( ModuleCache ) ); auto cache = s_modCache->push_next();
cache->start = base; cache->start = base;
cache->end = base + info.SizeOfImage; cache->end = base + info.SizeOfImage;
cache->name = (char*)tracy_malloc( namelen+3 ); cache->name = (char*)tracy_malloc( namelen+3 );
memcpy( cache->name, buf, namelen+3 ); memcpy( cache->name, buf, namelen+3 );
cache->nameLen = namelen+2; cache->nameLen = namelen+2;
cache->next = s_modCache;
s_modCache = cache;
return; return;
} }