Add short pointer class, storing 6 bytes.

This commit is contained in:
Bartosz Taudul 2019-11-02 15:32:40 +01:00
parent 45ff14d678
commit 654f54d877
3 changed files with 82 additions and 0 deletions

View File

@ -170,6 +170,7 @@
<ClInclude Include="..\..\..\server\TracyMemory.hpp" /> <ClInclude Include="..\..\..\server\TracyMemory.hpp" />
<ClInclude Include="..\..\..\server\TracyPopcnt.hpp" /> <ClInclude Include="..\..\..\server\TracyPopcnt.hpp" />
<ClInclude Include="..\..\..\server\TracyPrint.hpp" /> <ClInclude Include="..\..\..\server\TracyPrint.hpp" />
<ClInclude Include="..\..\..\server\TracyShortPtr.hpp" />
<ClInclude Include="..\..\..\server\TracySlab.hpp" /> <ClInclude Include="..\..\..\server\TracySlab.hpp" />
<ClInclude Include="..\..\..\server\TracyStorage.hpp" /> <ClInclude Include="..\..\..\server\TracyStorage.hpp" />
<ClInclude Include="..\..\..\server\TracyStringDiscovery.hpp" /> <ClInclude Include="..\..\..\server\TracyStringDiscovery.hpp" />

View File

@ -296,6 +296,9 @@
<ClInclude Include="..\..\..\server\TracyTaskDispatch.hpp"> <ClInclude Include="..\..\..\server\TracyTaskDispatch.hpp">
<Filter>server</Filter> <Filter>server</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\server\TracyShortPtr.hpp">
<Filter>server</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="DebugVis.natvis" /> <Natvis Include="DebugVis.natvis" />

78
server/TracyShortPtr.hpp Normal file
View File

@ -0,0 +1,78 @@
#ifndef __TRACYSHORTPTR_HPP__
#define __TRACYSHORTPTR_HPP__
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "../common/TracyForceInline.hpp"
namespace tracy
{
#if UINTPTR_MAX == 0xFFFFFFFFFFFFFFFF
template<typename T>
class short_ptr
{
public:
tracy_force_inline short_ptr() {}
tracy_force_inline short_ptr( T* ptr ) { Set( ptr ); }
tracy_force_inline operator T*() { return Get(); }
tracy_force_inline operator const T*() const { return Get(); }
tracy_force_inline T& operator*() { return *Get(); }
tracy_force_inline const T& operator*() const { return *Get(); }
tracy_force_inline T* operator->() { return Get(); }
tracy_force_inline const T* operator->() const { return Get(); }
private:
tracy_force_inline void Set( T* ptr )
{
assert( ( uint64_t( ptr ) & 0xFFFF000000000000 ) == 0 );
memcpy( m_ptr, &ptr, 4 );
memcpy( m_ptr+4, ((char*)&ptr)+4, 2 );
}
tracy_force_inline T* Get()
{
uint32_t lo;
uint16_t hi;
memcpy( &lo, m_ptr, 4 );
memcpy( &hi, m_ptr+4, 2 );
return (T*)( uint64_t( lo ) | ( ( uint64_t( hi ) << 32 ) ) );
}
tracy_force_inline const T* Get() const
{
uint32_t lo;
uint16_t hi;
memcpy( &lo, m_ptr, 4 );
memcpy( &hi, m_ptr+4, 2 );
return (T*)( uint64_t( lo ) | ( ( uint64_t( hi ) << 32 ) ) );
}
uint8_t m_ptr[6];
};
#else
template<typename T>
class short_ptr
{
public:
tracy_force_inline short_ptr() {}
tracy_force_inline short_ptr( T* ptr ) : m_ptr( ptr ) {}
tracy_force_inline operator T*() { return m_ptr; }
tracy_force_inline operator const T*() const { return m_ptr; }
tracy_force_inline T& operator*() { return *m_ptr; }
tracy_force_inline const T& operator*() const { return *m_ptr; }
tracy_force_inline T* operator->() { return m_ptr; }
tracy_force_inline const T* operator->() const { return m_ptr; }
private:
T* m_ptr;
};
#endif
}
#endif