tracy/server/TracyVarArray.hpp

98 lines
2.3 KiB
C++
Raw Normal View History

2018-06-19 18:54:46 +00:00
#ifndef __TRACYVARARRAY_HPP__
#define __TRACYVARARRAY_HPP__
#include <stdint.h>
2019-02-12 17:45:51 +00:00
#include <string.h>
2018-06-19 18:54:46 +00:00
2021-12-01 20:52:31 +00:00
#define XXH_INLINE_ALL
#include "tracy_xxhash.h"
2020-02-29 14:31:05 +00:00
#include "../public/common/TracyForceInline.hpp"
2018-06-19 18:54:46 +00:00
#include "TracyCharUtil.hpp"
#include "TracyEvent.hpp"
2019-11-03 15:29:05 +00:00
#include "TracyMemory.hpp"
#include "TracyShortPtr.hpp"
2018-06-19 18:54:46 +00:00
namespace tracy
{
#pragma pack( 1 )
template<typename T>
class VarArray
{
public:
VarArray( uint16_t size, const T* data )
2018-06-19 18:54:46 +00:00
: m_size( size )
, m_ptr( data )
{
CalcHash();
2018-06-19 18:54:46 +00:00
}
VarArray( const VarArray& ) = delete;
VarArray( VarArray&& ) = delete;
VarArray& operator=( const VarArray& ) = delete;
VarArray& operator=( VarArray&& ) = delete;
tracy_force_inline uint32_t get_hash() const { return m_hash; }
tracy_force_inline bool empty() const { return m_size == 0; }
tracy_force_inline uint16_t size() const { return m_size; }
2018-06-19 18:54:46 +00:00
tracy_force_inline const T* data() const { return m_ptr; };
tracy_force_inline const T* begin() const { return m_ptr; }
tracy_force_inline const T* end() const { return m_ptr + m_size; }
tracy_force_inline const T& front() const { assert( m_size > 0 ); return m_ptr[0]; }
tracy_force_inline const T& back() const { assert( m_size > 0 ); return m_ptr[m_size - 1]; }
tracy_force_inline const T& operator[]( size_t idx ) const { return m_ptr[idx]; }
private:
tracy_force_inline void CalcHash();
uint16_t m_size;
2018-06-19 18:54:46 +00:00
uint32_t m_hash;
2019-11-03 15:29:05 +00:00
const short_ptr<T> m_ptr;
2018-06-19 18:54:46 +00:00
};
#pragma pack()
2019-11-03 15:29:05 +00:00
enum { VarArraySize = sizeof( VarArray<int> ) };
template<typename T>
2019-03-03 21:09:20 +00:00
inline void VarArray<T>::CalcHash()
{
2020-02-29 14:31:05 +00:00
m_hash = uint32_t( XXH3_64bits( m_ptr.get(), m_size * sizeof( T ) ) );
}
2018-06-19 18:54:46 +00:00
template<typename T>
2019-03-03 21:09:20 +00:00
static inline bool Compare( const VarArray<T>& lhs, const VarArray<T>& rhs )
2018-06-19 18:54:46 +00:00
{
if( lhs.size() != rhs.size() || lhs.get_hash() != rhs.get_hash() ) return false;
2019-02-12 17:45:51 +00:00
return memcmp( lhs.data(), rhs.data(), lhs.size() * sizeof( T ) ) == 0;
2018-06-19 18:54:46 +00:00
}
template<typename T>
struct VarArrayHasher
{
size_t operator()( const VarArray<T>* arr ) const
{
return arr->get_hash();
}
};
template<typename T>
struct VarArrayComparator
{
bool operator()( const VarArray<T>* lhs, const VarArray<T>* rhs ) const
{
return Compare( *lhs, *rhs );
}
};
}
#endif