tracy/server/TracyVector.hpp

316 lines
8.9 KiB
C++
Raw Normal View History

#ifndef __TRACYVECTOR_HPP__
#define __TRACYVECTOR_HPP__
2019-03-26 22:02:32 +00:00
#include <algorithm>
#include <assert.h>
#include <limits>
#include <stdint.h>
#include <type_traits>
#include "../common/TracyForceInline.hpp"
2017-09-16 23:22:46 +00:00
#include "TracyMemory.hpp"
#include "TracyPopcnt.hpp"
#include "TracySlab.hpp"
2017-09-16 23:22:46 +00:00
namespace tracy
{
#pragma pack( 1 )
template<typename T>
class Vector
{
public:
using iterator = T*;
using const_iterator = const T*;
2019-10-29 00:32:09 +00:00
tracy_force_inline Vector()
2017-11-15 20:26:58 +00:00
: m_ptr( nullptr )
, m_size( 0 )
, m_capacity( 0 )
{
}
2017-09-22 23:10:48 +00:00
Vector( const Vector& ) = delete;
2019-10-29 00:32:09 +00:00
tracy_force_inline Vector( Vector&& src ) noexcept
2017-09-22 23:10:48 +00:00
{
memcpy( this, &src, sizeof( Vector<T> ) );
memset( &src, 0, sizeof( Vector<T> ) );
2017-09-22 23:10:48 +00:00
}
2019-10-29 00:32:09 +00:00
tracy_force_inline Vector( const T& value )
2018-07-22 14:01:58 +00:00
: m_ptr( new T[1] )
, m_size( 1 )
, m_capacity( 0 )
{
memUsage.fetch_add( sizeof( T ), std::memory_order_relaxed );
m_ptr[0] = value;
}
2019-10-29 00:32:09 +00:00
tracy_force_inline ~Vector()
{
if( m_capacity != std::numeric_limits<uint8_t>::max() )
{
memUsage.fetch_sub( Capacity() * sizeof( T ), std::memory_order_relaxed );
delete[] m_ptr;
}
}
2017-09-22 23:10:48 +00:00
Vector& operator=( const Vector& ) = delete;
2019-10-29 00:32:09 +00:00
tracy_force_inline Vector& operator=( Vector&& src ) noexcept
2017-09-22 23:10:48 +00:00
{
delete[] m_ptr;
memcpy( this, &src, sizeof( Vector<T> ) );
memset( &src, 0, sizeof( Vector<T> ) );
2017-09-22 23:10:48 +00:00
return *this;
}
2019-10-29 00:32:09 +00:00
tracy_force_inline void swap( Vector& other )
2019-03-26 22:02:32 +00:00
{
std::swap( m_ptr, other.m_ptr );
std::swap( m_size, other.m_size );
std::swap( m_capacity, other.m_capacity );
}
2018-05-02 16:27:37 +00:00
tracy_force_inline bool empty() const { return m_size == 0; }
tracy_force_inline size_t size() const { return m_size; }
tracy_force_inline void set_size( size_t sz ) { assert( m_capacity != std::numeric_limits<uint8_t>::max() ); m_size = sz; }
2018-04-30 11:56:58 +00:00
2018-05-02 16:27:37 +00:00
tracy_force_inline T* data() { return m_ptr; }
tracy_force_inline const T* data() const { return m_ptr; };
2018-05-02 16:27:37 +00:00
tracy_force_inline T* begin() { return m_ptr; }
tracy_force_inline const T* begin() const { return m_ptr; }
tracy_force_inline T* end() { return m_ptr + m_size; }
tracy_force_inline const T* end() const { return m_ptr + m_size; }
2018-05-02 16:27:37 +00:00
tracy_force_inline T& front() { assert( m_size > 0 ); return m_ptr[0]; }
tracy_force_inline const T& front() const { assert( m_size > 0 ); return m_ptr[0]; }
2017-10-19 18:34:07 +00:00
2018-05-02 16:27:37 +00:00
tracy_force_inline T& back() { assert( m_size > 0 ); return m_ptr[m_size - 1]; }
tracy_force_inline const T& back() const { assert( m_size > 0 ); return m_ptr[m_size - 1]; }
2018-05-02 16:27:37 +00:00
tracy_force_inline T& operator[]( size_t idx ) { return m_ptr[idx]; }
tracy_force_inline const T& operator[]( size_t idx ) const { return m_ptr[idx]; }
2017-09-16 23:05:47 +00:00
2018-05-02 16:27:37 +00:00
tracy_force_inline void push_back( const T& v )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
if( m_size == Capacity() ) AllocMore();
m_ptr[m_size++] = v;
}
2018-05-02 16:27:37 +00:00
tracy_force_inline void push_back_non_empty( const T& v )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
if( m_size == CapacityNoNullptrCheck() ) AllocMore();
m_ptr[m_size++] = v;
}
2018-05-02 16:27:37 +00:00
tracy_force_inline void push_back_no_space_check( const T& v )
2018-03-15 20:32:06 +00:00
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( m_size < Capacity() );
2018-03-15 20:32:06 +00:00
m_ptr[m_size++] = v;
}
2018-05-02 16:27:37 +00:00
tracy_force_inline void push_back( T&& v )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
if( m_size == Capacity() ) AllocMore();
m_ptr[m_size++] = std::move( v );
}
2018-05-02 16:27:37 +00:00
tracy_force_inline T& push_next()
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
if( m_size == Capacity() ) AllocMore();
return m_ptr[m_size++];
}
2018-05-02 16:27:37 +00:00
tracy_force_inline T& push_next_no_space_check()
2018-04-29 11:39:06 +00:00
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
2018-04-29 11:39:06 +00:00
assert( m_size < Capacity() );
return m_ptr[m_size++];
}
2017-09-25 21:01:22 +00:00
T* insert( T* it, const T& v )
2017-09-16 23:05:47 +00:00
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
2017-09-16 23:05:47 +00:00
assert( it >= m_ptr && it <= m_ptr + m_size );
2017-09-25 21:01:22 +00:00
const auto dist = it - m_ptr;
if( m_size == Capacity() ) AllocMore();
2017-09-25 21:01:22 +00:00
if( dist != m_size ) memmove( m_ptr + dist + 1, m_ptr + dist, ( m_size - dist ) * sizeof( T ) );
2017-09-16 23:05:47 +00:00
m_size++;
2017-09-25 21:01:22 +00:00
m_ptr[dist] = v;
return m_ptr + dist;
2017-09-16 23:05:47 +00:00
}
2017-09-25 21:01:22 +00:00
T* insert( T* it, T&& v )
2017-09-16 23:05:47 +00:00
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
2017-09-16 23:05:47 +00:00
assert( it >= m_ptr && it <= m_ptr + m_size );
2017-09-25 21:01:22 +00:00
const auto dist = it - m_ptr;
if( m_size == Capacity() ) AllocMore();
2017-09-25 21:01:22 +00:00
if( dist != m_size ) memmove( m_ptr + dist + 1, m_ptr + dist, ( m_size - dist ) * sizeof( T ) );
2017-09-16 23:05:47 +00:00
m_size++;
2017-09-25 21:01:22 +00:00
m_ptr[dist] = std::move( v );
return m_ptr + dist;
2017-09-16 23:05:47 +00:00
}
void insert( T* it, T* begin, T* end )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( it >= m_ptr && it <= m_ptr + m_size );
const auto sz = end - begin;
const auto dist = it - m_ptr;
while( m_size + sz > Capacity() ) AllocMore();
if( dist != m_size ) memmove( m_ptr + dist + sz, m_ptr + dist, ( m_size - dist ) * sizeof( T ) );
m_size += sz;
memcpy( m_ptr + dist, begin, sz * sizeof( T ) );
}
T* erase( T* it )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( it >= m_ptr && it <= m_ptr + m_size );
m_size--;
2019-07-11 18:20:50 +00:00
memmove( it, it+1, ( m_size - ( it - m_ptr ) ) * sizeof( T ) );
return it;
}
2017-09-25 21:11:56 +00:00
T* erase( T* begin, T* end )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
2017-09-25 21:11:56 +00:00
assert( begin >= m_ptr && begin <= m_ptr + m_size );
assert( end >= m_ptr && end <= m_ptr + m_size );
assert( begin <= end );
const auto dist = end - begin;
if( dist > 0 )
{
memmove( begin, end, ( m_size - ( end - m_ptr ) ) * sizeof( T ) );
m_size -= dist;
}
return begin;
2017-11-11 19:51:03 +00:00
}
2018-05-02 16:27:37 +00:00
tracy_force_inline void pop_back()
2017-11-11 19:51:03 +00:00
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
2017-11-11 19:51:03 +00:00
assert( m_size > 0 );
m_size--;
2017-09-25 21:11:56 +00:00
}
2018-05-02 16:27:37 +00:00
tracy_force_inline T& back_and_pop()
2017-11-19 18:17:54 +00:00
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
2017-11-19 18:17:54 +00:00
assert( m_size > 0 );
m_size--;
return m_ptr[m_size];
}
2018-05-02 16:27:37 +00:00
tracy_force_inline void reserve( size_t cap )
2017-09-30 23:51:29 +00:00
{
2017-11-15 20:26:58 +00:00
if( cap == 0 || cap <= Capacity() ) return;
reserve_non_zero( cap );
}
void reserve_non_zero( size_t cap )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
cap--;
cap |= cap >> 1;
cap |= cap >> 2;
cap |= cap >> 4;
cap |= cap >> 8;
cap |= cap >> 16;
cap = TracyCountBits( cap );
memUsage.fetch_add( ( ( 1 << cap ) - Capacity() ) * sizeof( T ), std::memory_order_relaxed );
2017-09-30 23:51:29 +00:00
m_capacity = cap;
Realloc();
}
2018-05-02 16:27:37 +00:00
tracy_force_inline void reserve_and_use( size_t sz )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
reserve( sz );
m_size = sz;
}
template<size_t U>
tracy_force_inline void reserve_exact( uint32_t sz, Slab<U>& slab )
{
assert( !m_ptr );
m_capacity = std::numeric_limits<uint8_t>::max();
m_size = sz;
m_ptr = (T*)slab.AllocBig( sizeof( T ) * sz );
}
2018-05-02 16:27:37 +00:00
tracy_force_inline void clear()
2017-10-19 21:25:49 +00:00
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
2017-10-19 21:25:49 +00:00
m_size = 0;
}
private:
tracy_no_inline void AllocMore()
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
2017-11-15 20:26:58 +00:00
if( m_ptr == nullptr )
{
memUsage.fetch_add( sizeof( T ), std::memory_order_relaxed );
m_ptr = new T[1];
m_capacity = 0;
2017-11-15 20:26:58 +00:00
}
else
{
memUsage.fetch_add( Capacity() * sizeof( T ), std::memory_order_relaxed );
m_capacity++;
Realloc();
}
2017-09-30 23:51:29 +00:00
}
void Realloc()
{
T* ptr = new T[CapacityNoNullptrCheck()];
2017-09-28 18:47:57 +00:00
if( m_size != 0 )
{
if( std::is_trivially_copyable<T>() )
{
memcpy( ptr, m_ptr, m_size * sizeof( T ) );
}
else
{
for( uint32_t i=0; i<m_size; i++ )
{
ptr[i] = std::move( m_ptr[i] );
}
}
2017-09-28 18:47:57 +00:00
delete[] m_ptr;
}
m_ptr = ptr;
}
2018-05-02 16:27:37 +00:00
tracy_force_inline uint32_t Capacity() const
2017-11-15 20:26:58 +00:00
{
return m_ptr == nullptr ? 0 : 1 << m_capacity;
}
2018-05-02 16:27:37 +00:00
tracy_force_inline uint32_t CapacityNoNullptrCheck() const
{
return 1 << m_capacity;
}
T* m_ptr;
uint32_t m_size;
uint8_t m_capacity;
};
#pragma pack()
}
#endif